From: fomichev Date: Fri, 6 Jun 2025 06:29:08 +0000 (+0300) Subject: Метод сохранения excel X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=e57c259a3b246605ebf3373911560bad27992e74;p=erp24_rep%2Fyii-erp24%2F.git Метод сохранения excel --- diff --git a/erp24/controllers/AutoPlannogrammaController.php b/erp24/controllers/AutoPlannogrammaController.php index 95389b6c..23e7ddaa 100644 --- a/erp24/controllers/AutoPlannogrammaController.php +++ b/erp24/controllers/AutoPlannogrammaController.php @@ -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; diff --git a/erp24/views/auto-plannogramma/week-sales-species-excel.php b/erp24/views/auto-plannogramma/week-sales-species-excel.php index 2166caef..c6b19097 100644 --- a/erp24/views/auto-plannogramma/week-sales-species-excel.php +++ b/erp24/views/auto-plannogramma/week-sales-species-excel.php @@ -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); ?>