| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace App\Services;
- use App\Helpers\DateHelper;
- use App\Models\Product;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Str;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
- class ExportService
- {
- public function handle(array $filters = []): string
- {
- $q = Product::query();
- foreach ($filters as $filterName => $filterValue) {
- if(!$filterValue) continue;
- if($filterName === 's') {
- // accept search
- $searchFields = ['nomenclature_number', 'article', 'name_tz', 'manufacturer_name', 'note'];
- $q->where(function ($query) use ($searchFields, $filterValue) {
- foreach ($searchFields as $searchField) {
- $query->orWhere($searchField, 'LIKE', '%' . $filterValue . '%');
- }
- });
- continue;
- }
- if(Str::contains($filterName, 'price')) {
- $filterValue = $filterValue * 100;
- }
- if(Str::endsWith($filterName, '_from')) {
- if(is_string($filterValue) && DateHelper::isDate($filterValue)) {
- $filterValue .= ' 00:00:00';
- }
- $q->where(Str::replace('_from', '', $filterName), '>=', $filterValue);
- } elseif(Str::endsWith($filterName, '_to')) {
- if(is_string($filterValue) && DateHelper::isDate($filterValue)) {
- $filterValue .= ' 23:59:59';
- }
- $q->where(Str::replace('_to', '', $filterName), '<=', $filterValue);
- } else {
- $q->where($filterName, '=', $filterValue);
- }
- }
- $products = $q->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);
- Storage::disk('public')->makeDirectory('export');
- $writer->save(storage_path('app/public/export') . '/' . $fileName);
- return $fileName;
- }
- }
|