ExportService.php 3.5 KB

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