| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace App\Services;
- use App\Helpers\DateHelper;
- use App\Models\Import;
- use App\Models\Product;
- class ImportCatalogService extends ImportBaseService
- {
- const HEADERS = [
- 'Фото' => 'photo',
- 'Артикул образца' => 'article',
- 'Наименование по ТЗ' => 'name_tz',
- 'Тип по ТЗ' => 'type_tz',
- '№ по номенкл.' => 'nomenclature_number',
- 'Габаритные размеры' => 'sizes',
- 'Производитель' => 'manufacturer',
- 'ед. изм.' => 'unit',
- 'Тип оборудования' => 'type',
- 'Цена поставки' => 'product_price',
- 'Цена установки' => 'installation_price',
- 'Итого цена' => 'total_price',
- 'Наименование производителя' => 'manufacturer_name',
- 'Примечание' => 'note',
- 'Наименование по паспорту' => 'passport_name',
- 'Наименование в ведомости' => 'statement_name',
- 'Срок службы' => 'service_life',
- 'Номер сертификата' => 'certificate_number',
- 'Дата сертификата' => 'certificate_date',
- 'Орган сертификации' => 'certificate_issuer',
- 'Вид сертификата' => 'certificate_type',
- 'Вес' => 'weight',
- 'Объем' => 'volume',
- 'Мест' => 'places',
- ];
- private int $year;
- public function __construct(Import $import, int $year)
- {
- parent::__construct($import);
- $this->headers = self::HEADERS;
- $this->year = $year;
- }
- public function handle(): bool
- {
- if (!$this->prepare()) {
- return false;
- }
- $strNumber = 0;
- $result = [
- 'productsCreated' => 0,
- 'productsUpdated' => 0,
- ];
- foreach ($this->rowIterator as $row) {
- $strNumber++;
- $r = $this->rowToArray($row);
- if ($strNumber === 1) {
- echo $this->import->log('Skip headers Row: ' . $strNumber);
- continue;
- }
- // Пропускаем пустые строки
- if (empty($r['nomenclature_number'])) {
- continue;
- }
- try {
- $logMessage = "Row $strNumber: " . $r['nomenclature_number'] . ' - ' . $r['name_tz'] . '. ';
- $certDate = (int) $r['certificate_date'];
- $existing = Product::query()
- ->withoutGlobalScopes()
- ->where('year', $this->year)
- ->where('nomenclature_number', $r['nomenclature_number'])
- ->first();
- $data = [
- 'article' => (string) $r['article'],
- 'name_tz' => (string) $r['name_tz'],
- 'type_tz' => (string) $r['type_tz'],
- 'sizes' => (string) $r['sizes'],
- 'manufacturer' => (string) $r['manufacturer'],
- 'unit' => (string) $r['unit'],
- 'type' => (string) $r['type'],
- 'product_price' => (float) $r['product_price'],
- 'installation_price' => (float) $r['installation_price'],
- 'total_price' => (float) $r['total_price'],
- 'manufacturer_name' => (string) $r['manufacturer_name'],
- 'note' => (string) $r['note'],
- 'passport_name' => (string) $r['passport_name'],
- 'statement_name' => (string) $r['statement_name'],
- 'service_life' => (int) $r['service_life'],
- 'certificate_number' => (string) $r['certificate_number'],
- 'certificate_date' => ($certDate > 0) ? DateHelper::excelDateToISODate($certDate) : null,
- 'certificate_issuer' => (string) $r['certificate_issuer'],
- 'certificate_type' => (string) $r['certificate_type'],
- 'weight' => (float) $r['weight'],
- 'volume' => (float) $r['volume'],
- 'places' => (int) $r['places'],
- ];
- if ($existing) {
- $existing->update($data);
- $logMessage .= 'Updated product ID: ' . $existing->id;
- $result['productsUpdated']++;
- } else {
- $data['year'] = $this->year;
- $data['nomenclature_number'] = $r['nomenclature_number'];
- $product = Product::query()->create($data);
- $logMessage .= 'Created product ID: ' . $product->id;
- $result['productsCreated']++;
- }
- echo $this->import->log($logMessage);
- } catch (\Exception $e) {
- echo $this->import->log("Row $strNumber: " . $e->getMessage(), 'WARNING');
- }
- }
- echo $this->import->log(print_r($result, true));
- $this->import->status = 'DONE';
- $this->import->save();
- return true;
- }
- }
|