ImportService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Product;
  4. use Exception;
  5. use Illuminate\Support\Facades\Storage;
  6. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  7. class ImportService
  8. {
  9. const HEADERS_TO_FIELDS = [
  10. "Фото" => '',
  11. "Наименование по ТЗ" => 'name_tz',
  12. "Тип по ТЗ" => 'type_tz',
  13. "№ по номенкл." => 'nomenclature_number',
  14. "Габаритные размеры" => 'sizes',
  15. "Производитель" => 'manufacturer',
  16. "ед. изм." => 'unit',
  17. "Тип оборудования" => 'type',
  18. "Статус цены" => 'price_status',
  19. "Цена поставки" => 'product_price',
  20. "Цена установки" => 'installation_price',
  21. "Цена обслуживания" => 'service_price',
  22. "Итого цена" => 'total_price',
  23. "Наименование производителя" => 'manufacturer_name',
  24. "Артикул образца" => 'article',
  25. "Примечание" => 'note',
  26. ];
  27. /**
  28. * @param string $path
  29. * @param int $year
  30. * @return void
  31. * @throws Exception
  32. */
  33. public function handle(string $path, int $year): void
  34. {
  35. $path = Storage::disk('upload')->path($path);
  36. $reader = new Xlsx();
  37. $spreadsheet = $reader->load($path);
  38. $sheet = $spreadsheet->getActiveSheet();
  39. $rowIterator = $sheet->getRowIterator();
  40. $headers = $this->rowToArray($rowIterator->current());
  41. if($this->checkHeaders($headers)) {
  42. foreach ($rowIterator as $row){
  43. $record = $this->rowToArray($row);
  44. if($record[0] === 'Фото') continue;
  45. Product::query()
  46. ->updateOrCreate(['year' => $year, 'nomenclature_number' => $record[3]],
  47. [
  48. 'name_tz' => $record[1],
  49. 'type_tz' => $record[2],
  50. 'sizes' => $record[4],
  51. 'manufacturer' => $record[5],
  52. 'unit' => $record[6],
  53. 'type' => $record[7],
  54. 'price_status' => $record[8],
  55. 'product_price' => $record[9],
  56. 'installation_price'=> $record[10],
  57. 'service_price' => $record[11],
  58. 'total_price' => $record[12],
  59. 'manufacturer_name' => $record[13],
  60. 'article' => $record[14],
  61. 'note' => $record[15],
  62. ]);
  63. }
  64. } else {
  65. throw new Exception('Ошибка заголовков файла!');
  66. }
  67. }
  68. protected function rowToArray($row): array
  69. {
  70. $cellIterator = $row->getCellIterator();
  71. $cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells, even if a cell value is not set.
  72. $row_content = [];
  73. foreach ($cellIterator as $cell) {
  74. $row_content[] = $cell->getValue();
  75. }
  76. return $row_content;
  77. }
  78. /**
  79. * @param array $headers
  80. * @return bool
  81. */
  82. protected function checkHeaders(array $headers): bool
  83. {
  84. return $this->getHeaders() == $headers;
  85. }
  86. /**
  87. * @return array
  88. */
  89. protected function getHeaders(): array
  90. {
  91. return array_keys(self::HEADERS_TO_FIELDS);
  92. }
  93. }