ImportService.php 3.5 KB

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