ExportService.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Product;
  4. use PhpOffice\PhpSpreadsheet\IOFactory;
  5. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  6. class ExportService
  7. {
  8. public function handle(array $filters = []): string
  9. {
  10. $products = Product::cursor();
  11. $inputFileType = 'Xlsx'; // Xlsx - Xml - Ods - Slk - Gnumeric - Csv
  12. $inputFileName = './templates/ExportCatalogTemplate.xlsx';
  13. $reader = IOFactory::createReader($inputFileType);
  14. $spreadsheet = $reader->load($inputFileName);
  15. $sheet = $spreadsheet->getActiveSheet();
  16. $i = 2;
  17. $sum_format = '#,##0.00\ "₽";[Red]\-#,##0.00\ "₽"';
  18. foreach ($products as $product) {
  19. $sheet->setCellValue('B' . $i, $product->name_tz);
  20. $sheet->setCellValue('C' . $i, $product->type_tz);
  21. $sheet->setCellValue('D' . $i, $product->nomenclature_number);
  22. $sheet->setCellValue('E' . $i, $product->sizes);
  23. $sheet->setCellValue('F' . $i, $product->manufacturer);
  24. $sheet->setCellValue('G' . $i, $product->unit);
  25. $sheet->setCellValue('H' . $i, $product->type);
  26. $sheet->setCellValue('I' . $i, $product->price_status);
  27. $sheet->setCellValue('J' . $i, $product->product_price);
  28. $sheet->setCellValue('K' . $i, $product->installation_price);
  29. $sheet->setCellValue('L' . $i, $product->service_price);
  30. $sheet->setCellValue('M' . $i, $product->total_price);
  31. $sheet->setCellValue('N' . $i, $product->manufacturer_name);
  32. $sheet->setCellValue('O' . $i, $product->article);
  33. $sheet->setCellValue('P' . $i, $product->note);
  34. $sheet->getStyle("J{$i}:M{$i}")->getNumberFormat()->setFormatCode($sum_format);
  35. $i++;
  36. }
  37. $fileName = 'product_export_' . date('Y-m-d_H-i-s') . '.xlsx';
  38. $writer = new Xlsx($spreadsheet);
  39. $writer->save(storage_path('app/public/export') . '/' . $fileName);
  40. return $fileName;
  41. }
  42. }