ImportService.php 4.9 KB

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