ImportCatalogService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Services;
  3. use App\Helpers\DateHelper;
  4. use App\Models\Import;
  5. use App\Models\Product;
  6. class ImportCatalogService extends ImportBaseService
  7. {
  8. const HEADERS = [
  9. 'Фото' => 'photo',
  10. 'Артикул образца' => 'article',
  11. 'Наименование по ТЗ' => 'name_tz',
  12. 'Тип по ТЗ' => 'type_tz',
  13. '№ по номенкл.' => 'nomenclature_number',
  14. 'Габаритные размеры' => 'sizes',
  15. 'Производитель' => 'manufacturer',
  16. 'ед. изм.' => 'unit',
  17. 'Тип оборудования' => 'type',
  18. 'Цена поставки' => 'product_price',
  19. 'Цена установки' => 'installation_price',
  20. 'Итого цена' => 'total_price',
  21. 'Наименование производителя' => 'manufacturer_name',
  22. 'Примечание' => 'note',
  23. 'Наименование по паспорту' => 'passport_name',
  24. 'Наименование в ведомости' => 'statement_name',
  25. 'Срок службы' => 'service_life',
  26. 'Номер сертификата' => 'certificate_number',
  27. 'Дата сертификата' => 'certificate_date',
  28. 'Орган сертификации' => 'certificate_issuer',
  29. 'Вид сертификата' => 'certificate_type',
  30. 'Вес' => 'weight',
  31. 'Объем' => 'volume',
  32. 'Мест' => 'places',
  33. ];
  34. private int $year;
  35. public function __construct(Import $import, int $year)
  36. {
  37. parent::__construct($import);
  38. $this->headers = self::HEADERS;
  39. $this->year = $year;
  40. }
  41. public function handle(): bool
  42. {
  43. if (!$this->prepare()) {
  44. return false;
  45. }
  46. $strNumber = 0;
  47. $result = [
  48. 'productsCreated' => 0,
  49. 'productsUpdated' => 0,
  50. ];
  51. foreach ($this->rowIterator as $row) {
  52. $strNumber++;
  53. $r = $this->rowToArray($row);
  54. if ($strNumber === 1) {
  55. echo $this->import->log('Skip headers Row: ' . $strNumber);
  56. continue;
  57. }
  58. // Пропускаем пустые строки
  59. if (empty($r['nomenclature_number'])) {
  60. continue;
  61. }
  62. try {
  63. $logMessage = "Row $strNumber: " . $r['nomenclature_number'] . ' - ' . $r['name_tz'] . '. ';
  64. $certDate = (int) $r['certificate_date'];
  65. $existing = Product::query()
  66. ->withoutGlobalScopes()
  67. ->where('year', $this->year)
  68. ->where('nomenclature_number', $r['nomenclature_number'])
  69. ->first();
  70. $data = [
  71. 'article' => (string) $r['article'],
  72. 'name_tz' => (string) $r['name_tz'],
  73. 'type_tz' => (string) $r['type_tz'],
  74. 'sizes' => (string) $r['sizes'],
  75. 'manufacturer' => (string) $r['manufacturer'],
  76. 'unit' => (string) $r['unit'],
  77. 'type' => (string) $r['type'],
  78. 'product_price' => (float) $r['product_price'],
  79. 'installation_price' => (float) $r['installation_price'],
  80. 'total_price' => (float) $r['total_price'],
  81. 'manufacturer_name' => (string) $r['manufacturer_name'],
  82. 'note' => (string) $r['note'],
  83. 'passport_name' => (string) $r['passport_name'],
  84. 'statement_name' => (string) $r['statement_name'],
  85. 'service_life' => (int) $r['service_life'],
  86. 'certificate_number' => (string) $r['certificate_number'],
  87. 'certificate_date' => ($certDate > 0) ? DateHelper::excelDateToISODate($certDate) : null,
  88. 'certificate_issuer' => (string) $r['certificate_issuer'],
  89. 'certificate_type' => (string) $r['certificate_type'],
  90. 'weight' => (float) $r['weight'],
  91. 'volume' => (float) $r['volume'],
  92. 'places' => (int) $r['places'],
  93. ];
  94. if ($existing) {
  95. $existing->update($data);
  96. $logMessage .= 'Updated product ID: ' . $existing->id;
  97. $result['productsUpdated']++;
  98. } else {
  99. $data['year'] = $this->year;
  100. $data['nomenclature_number'] = $r['nomenclature_number'];
  101. $product = Product::query()->create($data);
  102. $logMessage .= 'Created product ID: ' . $product->id;
  103. $result['productsCreated']++;
  104. }
  105. echo $this->import->log($logMessage);
  106. } catch (\Exception $e) {
  107. echo $this->import->log("Row $strNumber: " . $e->getMessage(), 'WARNING');
  108. }
  109. }
  110. echo $this->import->log(print_r($result, true));
  111. $this->import->status = 'DONE';
  112. $this->import->save();
  113. return true;
  114. }
  115. }