]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Метод сохранения excel
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Fri, 6 Jun 2025 06:29:08 +0000 (09:29 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Fri, 6 Jun 2025 06:29:08 +0000 (09:29 +0300)
erp24/controllers/AutoPlannogrammaController.php
erp24/views/auto-plannogramma/week-sales-species-excel.php

index 95389b6c941c8c8c81c5ebb0ed219c65c7f04274..23e7ddaa7ac376938b77fb7b390501107d84ff6a 100644 (file)
@@ -1617,6 +1617,116 @@ class AutoPlannogrammaController extends BaseController
     }
 
 
+    /**
+     * Получает JSON из тела запроса (массив строк) и экспортирует его в Excel.
+     * Если JSON не передан или невалиден, подставляет тестовые данные.
+     */
+    public function actionExportJsonToExcel()
+    {
+        $this->layout = false;
+        Yii::$app->response->format = Response::FORMAT_RAW;
+
+        while (ob_get_level() > 0) {
+            ob_end_clean();
+        }
+
+        $rawBody = Yii::$app->request->getRawBody();
+        $decoded = json_decode($rawBody, true);
+
+        if (is_array($decoded) && !empty($decoded)) {
+            $rows = $decoded;
+        } else {
+            $rows = [
+                [
+                    'week'         => '1',
+                    'type_pm'      => 'max',
+                    'shop'         => 'Ванеева',
+                    'category'     => 'Срезка',
+                    'subcategory'  => 'Розы',
+                    'species'      => 'Роза',
+                    'product_name' => 'Роза Эквадор',
+                    'quantity'     => '3',
+                    'value_type'   => 'offline',
+                    'group_name'   => 'Оффлайн',
+                ],
+                [
+                    'week'         => '2',
+                    'type_pm'      => 'min',
+                    'shop'         => 'Ленина',
+                    'category'     => 'Срезка',
+                    'subcategory'  => 'Гвоздики',
+                    'species'      => 'Гвоздика',
+                    'product_name' => 'Гвоздика Стандарт',
+                    'quantity'     => '5',
+                    'value_type'   => 'online',
+                    'group_name'   => 'Онлайн',
+                ],
+            ];
+        }
+
+        $spreadsheet = new Spreadsheet();
+        $sheet = $spreadsheet->getActiveSheet();
+        $sheet->setTitle('Отчёт из JSON');
+
+        $sheet->setCellValue('A1', 'Неделя');
+        $sheet->setCellValue('B1', 'Тип п-ма');
+        $sheet->setCellValue('C1', 'Магазин');
+        $sheet->setCellValue('D1', 'Категория');
+        $sheet->setCellValue('E1', 'Подкатегория');
+        $sheet->setCellValue('F1', 'Вид');
+        $sheet->setCellValue('G1', 'Товар');
+        $sheet->setCellValue('H1', 'Кол-во');
+        $sheet->setCellValue('I1', 'Тип значения');
+        $sheet->setCellValue('J1', 'Имя группы');
+
+        $rowNum = 2;
+        foreach ($rows as $row) {
+            $week         = $row['week'] ?? '';
+            $typePm       = $row['type_pm'] ?? '';
+            $shop         = $row['shop'] ?? '';
+            $category     = $row['category'] ?? '';
+            $subcategory  = $row['subcategory'] ?? '';
+            $species      = $row['species'] ?? '';
+            $productName  = $row['product_name'] ?? '';
+            $quantity     = $row['quantity'] ?? '';
+            $valueType    = $row['value_type'] ?? '';
+            $groupName    = $row['group_name'] ?? '';
+
+            $sheet->setCellValue("A{$rowNum}", $week);
+            $sheet->setCellValue("B{$rowNum}", $typePm);
+            $sheet->setCellValue("C{$rowNum}", $shop);
+            $sheet->setCellValue("D{$rowNum}", $category);
+            $sheet->setCellValue("E{$rowNum}", $subcategory);
+            $sheet->setCellValue("F{$rowNum}", $species);
+            $sheet->setCellValue("G{$rowNum}", $productName);
+            $sheet->setCellValue("H{$rowNum}", $quantity);
+            $sheet->setCellValue("I{$rowNum}", $valueType);
+            $sheet->setCellValue("J{$rowNum}", $groupName);
+
+            $rowNum++;
+        }
+
+        foreach (range('A', 'J') as $col) {
+            $sheet->getColumnDimension($col)->setAutoSize(true);
+        }
+
+        $tempFile = tempnam(sys_get_temp_dir(), 'xlsx');
+        $writer   = new Xlsx($spreadsheet);
+        $writer->save($tempFile);
+
+        return Yii::$app
+            ->response
+            ->sendStreamAsFile(
+                fopen($tempFile, 'rb'),
+                'report_' . date('Ymd_His') . '.xlsx',
+                [
+                    'mimeType'            => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
+                    'inline'              => false,
+                    'deleteFileAfterSend' => true,
+                ]
+            );
+    }
+
     public function actionTest()
     {
         $request = Yii::$app->request;
index 2166caef6dbca24a31a2cf37300a71e96ba75019..c6b19097735fa8da93d4d7ce75783f62a5087e98 100644 (file)
@@ -99,7 +99,7 @@
 
 $params = Yii::$app->request->get();
 
-$route = ['auto-plannogramma/export-excel'];
+$route = ['auto-plannogramma/export-json-to-excel'];
 
 $urlWithParams = array_merge($route, $params);
 ?>