| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- namespace App\Services;
- use App\Models\Product;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
- class ExportService
- {
- public function handle(array $filters = []): string
- {
- $products = Product::cursor();
- $inputFileType = 'Xlsx'; // Xlsx - Xml - Ods - Slk - Gnumeric - Csv
- $inputFileName = './templates/ExportCatalogTemplate.xlsx';
- $reader = IOFactory::createReader($inputFileType);
- $spreadsheet = $reader->load($inputFileName);
- $sheet = $spreadsheet->getActiveSheet();
- $i = 2;
- $sum_format = '#,##0.00\ "₽";[Red]\-#,##0.00\ "₽"';
- foreach ($products as $product) {
- $sheet->setCellValue('B' . $i, $product->name_tz);
- $sheet->setCellValue('C' . $i, $product->type_tz);
- $sheet->setCellValue('D' . $i, $product->nomenclature_number);
- $sheet->setCellValue('E' . $i, $product->sizes);
- $sheet->setCellValue('F' . $i, $product->manufacturer);
- $sheet->setCellValue('G' . $i, $product->unit);
- $sheet->setCellValue('H' . $i, $product->type);
- $sheet->setCellValue('I' . $i, $product->price_status);
- $sheet->setCellValue('J' . $i, $product->product_price);
- $sheet->setCellValue('K' . $i, $product->installation_price);
- $sheet->setCellValue('L' . $i, $product->service_price);
- $sheet->setCellValue('M' . $i, $product->total_price);
- $sheet->setCellValue('N' . $i, $product->manufacturer_name);
- $sheet->setCellValue('O' . $i, $product->article);
- $sheet->setCellValue('P' . $i, $product->note);
- $sheet->getStyle("J{$i}:M{$i}")->getNumberFormat()->setFormatCode($sum_format);
- $i++;
- }
- $fileName = 'product_export_' . date('Y-m-d_H-i-s') . '.xlsx';
- $writer = new Xlsx($spreadsheet);
- $writer->save(storage_path('app/public/export') . '/' . $fileName);
- return $fileName;
- }
- }
|