ImportService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. if(!isset($record[4])) continue;
  55. $certDate = (int) $record[18];
  56. Product::query()
  57. ->updateOrCreate(['year' => $year, 'nomenclature_number' => $record[4]],
  58. [
  59. 'article' => (string) $record[1],
  60. 'name_tz' => (string) $record[2],
  61. 'type_tz' => (string) $record[3],
  62. 'sizes' => (string) $record[5],
  63. 'manufacturer' => (string) $record[6],
  64. 'unit' => (string) $record[7],
  65. 'type' => (string) $record[8],
  66. 'product_price' => (float) $record[9],
  67. 'installation_price' => (float) $record[10],
  68. 'total_price' => (float) $record[11],
  69. 'manufacturer_name' => (string) $record[12],
  70. 'note' => (string) $record[13],
  71. 'passport_name' => (string) $record[14],
  72. 'statement_name' => (string) $record[15],
  73. 'service_life' => (int) $record[16],
  74. 'certificate_number' => (string) $record[17],
  75. 'certificate_date' => ($certDate > 0) ? DateHelper::excelDateToISODate($certDate) : null,
  76. 'certificate_issuer' => (string) $record[19],
  77. 'certificate_type' => (string) $record[20],
  78. 'weight' => (float) $record[21],
  79. 'volume' => (float) $record[22],
  80. 'places' => (int) $record[23],
  81. ]);
  82. }
  83. } else {
  84. throw new Exception('Ошибка заголовков файла!');
  85. }
  86. }
  87. protected function rowToArray($row): array
  88. {
  89. $cellIterator = $row->getCellIterator();
  90. $cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells, even if a cell value is not set.
  91. $row_content = [];
  92. foreach ($cellIterator as $cell) {
  93. $row_content[] = $cell->getValue();
  94. }
  95. return $row_content;
  96. }
  97. /**
  98. * @param array $headers
  99. * @return bool
  100. */
  101. protected function checkHeaders(array $headers): bool
  102. {
  103. return $this->getHeaders() == $headers;
  104. }
  105. /**
  106. * @return array
  107. */
  108. protected function getHeaders(): array
  109. {
  110. return array_keys(self::HEADERS_TO_FIELDS);
  111. }
  112. }