ExportService.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Services;
  3. use App\Helpers\DateHelper;
  4. use App\Models\Product;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Facades\Storage;
  7. use Illuminate\Support\Str;
  8. use PhpOffice\PhpSpreadsheet\IOFactory;
  9. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  10. class ExportService
  11. {
  12. public function handle(array $filters = []): string
  13. {
  14. $q = Product::query();
  15. foreach ($filters as $filterName => $filterValue) {
  16. if(!$filterValue) continue;
  17. if($filterName === 's') {
  18. // accept search
  19. $searchFields = ['nomenclature_number', 'article', 'name_tz', 'manufacturer_name', 'note'];
  20. $q->where(function ($query) use ($searchFields, $filterValue) {
  21. foreach ($searchFields as $searchField) {
  22. $query->orWhere($searchField, 'LIKE', '%' . $filterValue . '%');
  23. }
  24. });
  25. continue;
  26. }
  27. if(Str::contains($filterName, 'price')) {
  28. $filterValue = $filterValue * 100;
  29. }
  30. if(Str::endsWith($filterName, '_from')) {
  31. if(is_string($filterValue) && DateHelper::isDate($filterValue)) {
  32. $filterValue .= ' 00:00:00';
  33. }
  34. $q->where(Str::replace('_from', '', $filterName), '>=', $filterValue);
  35. } elseif(Str::endsWith($filterName, '_to')) {
  36. if(is_string($filterValue) && DateHelper::isDate($filterValue)) {
  37. $filterValue .= ' 23:59:59';
  38. }
  39. $q->where(Str::replace('_to', '', $filterName), '<=', $filterValue);
  40. } else {
  41. $q->where($filterName, '=', $filterValue);
  42. }
  43. }
  44. $products = $q->cursor();
  45. $inputFileType = 'Xlsx'; // Xlsx - Xml - Ods - Slk - Gnumeric - Csv
  46. $inputFileName = './templates/ExportCatalogTemplate.xlsx';
  47. $reader = IOFactory::createReader($inputFileType);
  48. $spreadsheet = $reader->load($inputFileName);
  49. $sheet = $spreadsheet->getActiveSheet();
  50. $i = 2;
  51. $sum_format = '#,##0.00\ "₽";[Red]\-#,##0.00\ "₽"';
  52. foreach ($products as $product) {
  53. $sheet->setCellValue('B' . $i, $product->article);
  54. $sheet->setCellValue('C' . $i, $product->name_tz);
  55. $sheet->setCellValue('D' . $i, $product->type_tz);
  56. $sheet->setCellValue('E' . $i, $product->nomenclature_number);
  57. $sheet->setCellValue('F' . $i, $product->sizes);
  58. $sheet->setCellValue('G' . $i, $product->manufacturer);
  59. $sheet->setCellValue('H' . $i, $product->unit);
  60. $sheet->setCellValue('I' . $i, $product->type);
  61. $sheet->setCellValue('J' . $i, $product->product_price);
  62. $sheet->setCellValue('K' . $i, $product->installation_price);
  63. $sheet->setCellValue('L' . $i, $product->total_price);
  64. $sheet->setCellValue('M' . $i, $product->manufacturer_name);
  65. $sheet->setCellValue('N' . $i, $product->note);
  66. $sheet->getStyle("J{$i}:L{$i}")->getNumberFormat()->setFormatCode($sum_format);
  67. $i++;
  68. }
  69. $fileName = 'product_export_' . date('Y-m-d_H-i-s') . '.xlsx';
  70. $writer = new Xlsx($spreadsheet);
  71. Storage::disk('public')->makeDirectory('export');
  72. $writer->save(storage_path('app/public/export') . '/' . $fileName);
  73. return $fileName;
  74. }
  75. }