ExportService.php 4.2 KB

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