|
|
@@ -0,0 +1,264 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services;
|
|
|
+
|
|
|
+use App\Helpers\DateHelper;
|
|
|
+use App\Models\Import;
|
|
|
+use App\Models\MafOrder;
|
|
|
+use App\Models\Order;
|
|
|
+use App\Models\Product;
|
|
|
+use App\Models\ProductSKU;
|
|
|
+use Exception;
|
|
|
+
|
|
|
+class ImportOrdersService extends ImportBaseService
|
|
|
+{
|
|
|
+ const HEADERS = [
|
|
|
+ 'Округ' => 'districts.name',
|
|
|
+ 'Район' => 'areas.name',
|
|
|
+ 'Название площадки' => 'orders.name',
|
|
|
+ 'Адрес объекта' => 'orders.object_address',
|
|
|
+ 'Тип объекта' => 'object_types.name',
|
|
|
+ 'Артикул' => 'products.article',
|
|
|
+ 'Номер номенклатуры' => 'products.nomenclature_number',
|
|
|
+ 'Габаритные размеры' => 'products.sizes',
|
|
|
+ 'RFID' => 'products_sku.rfid',
|
|
|
+ 'Наименование производителя' => 'products.manufacturer_name',
|
|
|
+ 'Наименование по ТЗ' => 'products.name_tz',
|
|
|
+ 'Тип по тз' => 'products.type_tz',
|
|
|
+ 'ТИП' => 'products.type',
|
|
|
+ 'Цена товара' => 'products.product_price',
|
|
|
+ 'Цена установки' => 'products.installation_price',
|
|
|
+ 'Итоговая цена' => 'products.total_price',
|
|
|
+ 'Примечание' => 'products.note',
|
|
|
+ 'Год установки' => 'orders.year',
|
|
|
+ 'Номер заказа МАФ' => 'maf_orders.order_number',
|
|
|
+ '№ Ведомости' => 'products_sku.statement_number',
|
|
|
+ 'Дата ведомости' => 'products_sku.statement_date',
|
|
|
+ '№УПД' => 'products_sku.upd_number',
|
|
|
+ 'Статус' => 'order_statuses.name',
|
|
|
+ 'Комментарий' => 'orders.comment',
|
|
|
+ 'Менеджер' => 'users.name',
|
|
|
+ 'Номер фабрики' => 'products_sku.factory_number',
|
|
|
+ 'Дата пр-ва' => 'products_sku.manufacture_date',
|
|
|
+ 'Срок эксплуатации (месяцев)' => 'products.service_life',
|
|
|
+ '№ сертификата' => 'products.certificate_number',
|
|
|
+ 'Дата выдачи' => 'products.certificate_date',
|
|
|
+ 'Орган сертификации' => 'products.certificate_issuer',
|
|
|
+ 'ТИП (Декларация/Сертификат/Отказное)' => 'products.certificate_type',
|
|
|
+
|
|
|
+ // заголовки из файла для проверки и маппинга
|
|
|
+ ];
|
|
|
+
|
|
|
+
|
|
|
+ public function __construct(Import $import)
|
|
|
+ {
|
|
|
+ parent::__construct($import);
|
|
|
+ $this->headers = self::HEADERS;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function handle(): bool
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $this->import->log('Reading file...');
|
|
|
+ $this->readFile();
|
|
|
+ } catch (Exception $exception){
|
|
|
+ $this->import->log($exception->getMessage(), 'ERROR');
|
|
|
+ $this->import->status = 'ERROR';
|
|
|
+ $this->import->save();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ $this->import->log('Checking headers...');
|
|
|
+ $headers = $this->rowToArray($this->rowIterator->current());
|
|
|
+ if (!$this->checkHeaders(array_values($headers))) {
|
|
|
+ throw new Exception("Invalid headers");
|
|
|
+ }
|
|
|
+ } catch (Exception $exception){
|
|
|
+ $this->import->log($exception->getMessage(), 'ERROR');
|
|
|
+ $this->import->status = 'ERROR';
|
|
|
+ $this->import->save();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $this->rowIterator->next();
|
|
|
+ $strNumber = 0;
|
|
|
+ $result = [
|
|
|
+ 'productsCreated' => 0,
|
|
|
+ 'ordersCreated' => 0,
|
|
|
+ 'mafOrdersCreated' => 0,
|
|
|
+ 'productsSkuCreated' => 0,
|
|
|
+ ];
|
|
|
+ foreach($this->rowIterator as $row){
|
|
|
+ $strNumber++;
|
|
|
+ $r = $this->rowToArray($row);
|
|
|
+ if($strNumber === 1) {
|
|
|
+ echo $this->import->log('Skip headers Row: ' . $strNumber);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ echo $this->import->log("Row $strNumber: " . $r['orders.object_address']);
|
|
|
+ $year = (int) $r['orders.year'];
|
|
|
+
|
|
|
+ // округ
|
|
|
+ if(!($districtId = $this->findId('districts.shortname', $r['districts.name']))) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // район
|
|
|
+ if(!($areaId = $this->findId('areas.name', $r['areas.name']))) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // manager
|
|
|
+ if(!($userId = $this->findId('users.name', $r['users.name']))) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // object_type
|
|
|
+ if(!($objectTypeId = $this->findId('object_types.name', $r['object_types.name']))) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // order_statuses.name
|
|
|
+ if(!($orderStatusId = $this->findId('order_statuses.name', $r['order_statuses.name']))) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // product
|
|
|
+ $product = Product::query()
|
|
|
+ ->where('year', $year)
|
|
|
+ ->where('nomenclature_number', $r['products.nomenclature_number'])
|
|
|
+ ->first();
|
|
|
+ if(!$product) {
|
|
|
+ $product = Product::query()
|
|
|
+ ->create([
|
|
|
+ 'year' => $year,
|
|
|
+ 'article' => $r['products.article'],
|
|
|
+ 'nomenclature_number' => $r['products.nomenclature_number'],
|
|
|
+ 'manufacturer_name' => $r['products.manufacturer_name'],
|
|
|
+ 'name_tz' => $r['products.name_tz'],
|
|
|
+ 'type' => $r['products.type'],
|
|
|
+ 'type_tz' => $r['products.type_tz'],
|
|
|
+ 'product_price' => $r['products.product_price'],
|
|
|
+ 'installation_price' => $r['products.installation_price'],
|
|
|
+ 'total_price' => $r['products.total_price'],
|
|
|
+ 'note' => $r['products.note'],
|
|
|
+ 'service_life' => $r['products.service_life'],
|
|
|
+ 'certificate_number' => $r['products.certificate_number'],
|
|
|
+ 'certificate_date' => is_int($r['products.certificate_date']) ? DateHelper::excelDateToISODate($r['products.certificate_date']) : null,
|
|
|
+ 'certificate_issuer' => $r['products.certificate_issuer'],
|
|
|
+ 'certificate_type' => $r['products.certificate_type'],
|
|
|
+ 'sizes' => $r['products.sizes'],
|
|
|
+ 'unit' => 'шт.',
|
|
|
+ 'passport_name' => $r['products.manufacturer_name'],
|
|
|
+ 'statement_name' => $r['products.name_tz'],
|
|
|
+ 'manufacturer' => 'Наш двор',
|
|
|
+ ]);
|
|
|
+ echo $this->import->log('Created product: ' . $product->name_tz);
|
|
|
+ $result['productsCreated'] += 1;
|
|
|
+ } else {
|
|
|
+ echo $this->import->log('Found product: ' . $product->name_tz);
|
|
|
+ }
|
|
|
+
|
|
|
+ // order
|
|
|
+ $order = Order::query()
|
|
|
+ ->where('year', $year)
|
|
|
+ ->where('object_address', $r['orders.object_address'])
|
|
|
+ ->first();
|
|
|
+ if(!$order) {
|
|
|
+ $order = Order::query()
|
|
|
+ ->create([
|
|
|
+ 'year' => $year,
|
|
|
+ 'name' => $r['orders.object_address'],
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'district_id' => $districtId,
|
|
|
+ 'area_id' => $areaId,
|
|
|
+ 'object_address' => $r['orders.object_address'],
|
|
|
+ 'object_type_id' => $objectTypeId,
|
|
|
+ 'comment' => $r['orders.comment'],
|
|
|
+ 'order_status_id' => $orderStatusId,
|
|
|
+ 'tg_group_name' => '/' . $r['districts.name'] . ' ' . $r['orders.object_address'] . ' (' . $r['areas.name'] . ')' . ' - ' . $r['users.name'],
|
|
|
+ 'tg_group_link' => '',
|
|
|
+ ]);
|
|
|
+ echo $this->import->log('Created order: ' . $order->object_address);
|
|
|
+ $result['ordersCreated'] += 1;
|
|
|
+ } else {
|
|
|
+ echo $this->import->log('Found order: ' . $order->object_address);
|
|
|
+ }
|
|
|
+
|
|
|
+ // maf order
|
|
|
+ if ($r['maf_orders.order_number'] != '') {
|
|
|
+ $mafOrder = MafOrder::query()
|
|
|
+ ->where('year', $year)
|
|
|
+ ->where('product_id', $product->id)
|
|
|
+ ->where('order_number', $r['maf_orders.order_number'])
|
|
|
+ ->first();
|
|
|
+ if(!$mafOrder) {
|
|
|
+ $mafOrder = MafOrder::query()
|
|
|
+ ->create([
|
|
|
+ 'year' => $year,
|
|
|
+ 'order_number' => $r['maf_orders.order_number'],
|
|
|
+ 'user_id' => config('app.default_maf_order_user_id'),
|
|
|
+ 'status' => 'на складе',
|
|
|
+ 'product_id' => $product->id,
|
|
|
+ 'quantity' => 1,
|
|
|
+ 'in_stock' => 0,
|
|
|
+ ]);
|
|
|
+ echo $this->import->log('Created maf order: ' . $mafOrder->order_number);
|
|
|
+ $result['mafOrdersCreated'] += 1;
|
|
|
+ } else {
|
|
|
+ echo $this->import->log('Found maf order: ' . $mafOrder->order_number);
|
|
|
+ $mafOrder->quantity += 1;
|
|
|
+ $mafOrder->save();
|
|
|
+ echo $this->import->log('Incremented maf order: ' . $mafOrder->quantity);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $mafOrder = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // search rfid in products_sku
|
|
|
+ $manufacture_date = is_int($r['products_sku.manufacture_date']) ? DateHelper::excelDateToISODate($r['products_sku.manufacture_date']) : null;
|
|
|
+ $statement_date = is_int($r['products_sku.statement_date']) ? DateHelper::excelDateToISODate($r['products_sku.statement_date']) : null;
|
|
|
+
|
|
|
+ $productSKU = ProductSKU::query()
|
|
|
+ ->where('year', $year)
|
|
|
+ ->where('rfid', $r['products_sku.rfid'])
|
|
|
+ ->where('product_id', $product->id)
|
|
|
+ ->where('order_id', $order->id)
|
|
|
+ ->where('maf_order_id', $mafOrder?->id)
|
|
|
+ ->where('factory_number', $r['products_sku.factory_number'])
|
|
|
+ ->where('statement_number', $r['products_sku.statement_number'])
|
|
|
+ ->where('upd_number', $r['products_sku.upd_number'])
|
|
|
+ ->where('statement_date', $statement_date)
|
|
|
+ ->where('manufacture_date', $manufacture_date)
|
|
|
+ ->first();
|
|
|
+// dd($productSKU->toRawSql());
|
|
|
+ if($productSKU) {
|
|
|
+ echo $this->import->log('Found product with rfid: ' . $productSKU->rfid . ' Skip.');
|
|
|
+ continue;
|
|
|
+ } else {
|
|
|
+ $productSKU = ProductSKU::query()->create([
|
|
|
+ 'year' => $year,
|
|
|
+ 'product_id' => $product->id,
|
|
|
+ 'order_id' => $order->id,
|
|
|
+ 'maf_order_id' => $mafOrder?->id,
|
|
|
+ 'status' => ($mafOrder?->id) ? 'отгружен' : 'требуется',
|
|
|
+ 'rfid' => $r['products_sku.rfid'],
|
|
|
+ 'factory_number' => $r['products_sku.factory_number'],
|
|
|
+ 'manufacture_date' => $manufacture_date,
|
|
|
+ 'statement_number' => $r['products_sku.statement_number'],
|
|
|
+ 'statement_date' => $statement_date,
|
|
|
+ 'upd_number' => $r['products_sku.upd_number'],
|
|
|
+ ]);
|
|
|
+ $result['productsSkuCreated'] += 1;
|
|
|
+ echo $this->import->log('Created product sku: ' . $productSKU->id);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ echo $this->import->log(print_r($result, true));
|
|
|
+ $this->import->status = 'DONE';
|
|
|
+ $this->import->save();
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|