Browse Source

optimize log output

Alexander Musikhin 3 weeks ago
parent
commit
841c030821
2 changed files with 244 additions and 231 deletions
  1. 145 138
      app/Services/ImportOrdersService.php
  2. 99 93
      app/Services/ImportReclamationsService.php

+ 145 - 138
app/Services/ImportOrdersService.php

@@ -75,163 +75,170 @@ class ImportOrdersService extends ImportBaseService
                 echo $this->import->log('Skip headers Row: ' . $strNumber);
                 continue;
             }
-            echo $this->import->log("Row $strNumber: " . $r['orders.object_address']);
-            $year = (int) $r['orders.year'];
+            try {
 
-            // округ
-            if(!($districtId = $this->findId('districts.shortname', $r['districts.name']))) {
-                continue;
-            }
+                $logMessage = "Row $strNumber: " . $r['orders.object_address'] . '. ';
+                $year = (int)$r['orders.year'];
 
-            // район
-            if(!($areaId = $this->findId('areas.name', $r['areas.name']))) {
-                continue;
-            }
+                // округ
+                if (!($districtId = $this->findId('districts.shortname', $r['districts.name']))) {
+                    continue;
+                }
 
-            // manager
-            if(!($userId = $this->findId('users.name', $r['users.name']))) {
-                continue;
-            }
+                // район
+                if (!($areaId = $this->findId('areas.name', $r['areas.name']))) {
+                    continue;
+                }
 
-            // object_type
-            if(!($objectTypeId = $this->findId('object_types.name', $r['object_types.name']))) {
-                continue;
-            }
+                // manager
+                if (!($userId = $this->findId('users.name', $r['users.name']))) {
+                    continue;
+                }
 
-            // order_statuses.name
-            if(!($orderStatusId = $this->findId('order_statuses.name', $r['order_statuses.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 = 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);
-            }
+                    ->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' => 'Наш двор',
+                        ]);
+                    $logMessage .= 'Created product: ' . $product->name_tz . '. ';
+                    $result['productsCreated'] += 1;
+                } else {
+                    $logMessage .= 'Found product: ' . $product->name_tz . '. ';
+                }
 
-            // order
-            $order = Order::query()
-                ->where('year', $year)
-                ->where('object_address', $r['orders.object_address'])
-                ->first();
-            if(!$order) {
+                // 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'])
+                    ->where('object_address', $r['orders.object_address'])
                     ->first();
-                if(!$mafOrder) {
-                    $mafOrder = MafOrder::query()
+                if (!$order) {
+                    $order = Order::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,
+                            '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 maf order: ' . $mafOrder->order_number);
-                    $result['mafOrdersCreated'] += 1;
+                    echo $this->import->log('Created order: ' . $order->object_address);
+                    $result['ordersCreated'] += 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);
+                    $logMessage .= 'Found order: ' . $order->object_address . '. ';
                 }
-            } 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();
+                // 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,
+                            ]);
+                        $logMessage .= 'Created maf order: ' . $mafOrder->order_number . '. ';
+                        $result['mafOrdersCreated'] += 1;
+                    } else {
+                        $logMessage .= 'Found maf order: ' . $mafOrder->order_number . '. ';
+                        $mafOrder->quantity += 1;
+                        $mafOrder->save();
+                        $logMessage .= '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);
+                if ($productSKU) {
+                    echo $this->import->log($logMessage . '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;
+                    $logMessage .= 'Created product sku: ' . $productSKU->id . '. ';
+                }
+
+                echo $this->import->log($logMessage);
+            } catch (\Exception $e) {
+                echo $this->import->log($e->getMessage(), 'WARNING');
             }
 
         }

+ 99 - 93
app/Services/ImportReclamationsService.php

@@ -38,7 +38,7 @@ class ImportReclamationsService extends ImportBaseService
         $this->headers = self::HEADERS;
     }
 
-    public function handle()
+    public function handle(): bool
     {
         if(!$this->prepare()) {
             return false;
@@ -57,113 +57,119 @@ class ImportReclamationsService extends ImportBaseService
                 echo $this->import->log('Skip headers Row: ' . $strNumber);
                 continue;
             }
+            try {
 
-            echo $this->import->log("Row $strNumber: " . $r['orders.object_address']);
-            $year = (int) $r['orders.year'];
+                $logMessage = "Row $strNumber: " . $r['orders.object_address'] . ". ";
+                $year = (int)$r['orders.year'];
 
-            // округ
-            if(!($districtId = $this->findId('districts.shortname', $r['districts.name'] ?? ''))) {
-                continue;
-            }
+                // округ
+                if (!($districtId = $this->findId('districts.shortname', $r['districts.name'] ?? ''))) {
+                    continue;
+                }
 
-            // район
-            if(!($areaId = $this->findId('areas.name', $r['areas.name']))) {
-                continue;
-            }
+                // район
+                if (!($areaId = $this->findId('areas.name', $r['areas.name']))) {
+                    continue;
+                }
 
-            // manager
-            $userId = config('app.default_maf_order_user_id');
+                // manager
+                $userId = config('app.default_maf_order_user_id');
 
-            // status
-            if(!($statusId = $this->findId('reclamation_statuses.name', $r['reclamation_statuses.name']))) {
-                continue;
-            }
+                // status
+                if (!($statusId = $this->findId('reclamation_statuses.name', $r['reclamation_statuses.name']))) {
+                    continue;
+                }
 
-            // order
-            $order = Order::query()
-                ->where('year', $year)
-                ->where('object_address', $r['orders.object_address'])
-                ->first();
-            if(!$order) {
-                echo $this->import->log('Order NOT FOUND: ' . $r['orders.object_address'], 'WARNING');
-                continue;
-            } else {
-                echo $this->import->log('Found order: ' . $order->object_address);
-            }
-
-            // product
-            $product = Product::query()
-                ->where('year', $year)
-                ->where('nomenclature_number', $r['products.nomenclature_number'])
-                ->first();
-            if(!$product) {
-                echo $this->import->log('Product not found: ' . $r['products.nomenclature_number'], 'WARNING');
-                continue;
-            }
+                // order
+                $order = Order::query()
+                    ->where('year', $year)
+                    ->where('object_address', $r['orders.object_address'])
+                    ->first();
+                if (!$order) {
+                    echo $this->import->log('Order NOT FOUND: ' . $r['orders.object_address'], 'WARNING');
+                    continue;
+                } else {
+                    $logMessage .= 'Found order: ' . $order->object_address . ". ";
+                }
+
+                // product
+                $product = Product::query()
+                    ->where('year', $year)
+                    ->where('nomenclature_number', $r['products.nomenclature_number'])
+                    ->first();
+                if (!$product) {
+                    echo $this->import->log('Product not found: ' . $r['products.nomenclature_number'], 'WARNING');
+                    continue;
+                }
 
-            $rfid = Str::replace(' ', '', $r['products_sku.rfid']);
+                $rfid = Str::replace(' ', '', $r['products_sku.rfid']);
 
-            // check maf with this nomenclature number in order
-            $productSKU = ProductSKU::query()
-                ->where('year', $year)
-                ->where('product_id', $product->id)
-                ->where('order_id', $order->id)
-                ->where('rfid', $rfid)
-                ->first();
-            if(!$productSKU) {
+                // check maf with this nomenclature number in order
                 $productSKU = ProductSKU::query()
                     ->where('year', $year)
                     ->where('product_id', $product->id)
                     ->where('order_id', $order->id)
+                    ->where('rfid', $rfid)
                     ->first();
-            }
-            if(!$productSKU) {
-                echo $this->import->log('SKU not found: ' . $r['products.nomenclature_number'], 'WARNING');
-                continue;
-            }
-
-            $createDate = ($r['reclamations.create_date']) ? DateHelper::excelDateToISODate($r['reclamations.create_date']) : null;
-            $finishDate = ($r['reclamations.finish_date']) ? DateHelper::excelDateToISODate($r['reclamations.finish_date']) : null;
-            $startWorkDate = ($r['reclamations.start_work_date']) ? DateHelper::excelDateToISODate($r['reclamations.start_work_date']) : null;
-
-            // reclamation
-            $reclamation = Reclamation::query()
-                ->where('order_id', $order->id)
-                ->where('status_id', $statusId)
-                ->where('create_date', $createDate)
-                ->where('finish_date', $finishDate)
-                ->where('start_work_date', $startWorkDate)
-                ->where('reason', $r['reclamations.reason'])
-                ->where('whats_done', $r['reclamations.whats_done'])
-                ->where('guarantee', $r['reclamations.guarantee'])
-                ->first();
-
-            if(!$reclamation) {
+                if (!$productSKU) {
+                    $productSKU = ProductSKU::query()
+                        ->where('year', $year)
+                        ->where('product_id', $product->id)
+                        ->where('order_id', $order->id)
+                        ->first();
+                }
+                if (!$productSKU) {
+                    echo $this->import->log('SKU not found: ' . $r['products.nomenclature_number'], 'WARNING');
+                    continue;
+                }
+
+                $createDate = ($r['reclamations.create_date']) ? DateHelper::excelDateToISODate($r['reclamations.create_date']) : null;
+                $finishDate = ($r['reclamations.finish_date']) ? DateHelper::excelDateToISODate($r['reclamations.finish_date']) : null;
+                $startWorkDate = ($r['reclamations.start_work_date']) ? DateHelper::excelDateToISODate($r['reclamations.start_work_date']) : null;
+
+                // reclamation
                 $reclamation = Reclamation::query()
-                    ->create([
-                        'order_id' => $order->id,
-                        'user_id' => $userId,
-                        'status_id' => $statusId,
-                        'create_date' => $createDate,
-                        'finish_date' => $finishDate,
-                        'reason' => $r['reclamations.reason'],
-                        'guarantee' => $r['reclamations.guarantee'],
-                        'whats_done' => $r['reclamations.whats_done'],
-                        'start_work_date' => $startWorkDate,
-                        'comment' => $r['reclamations.comment'],
-                    ]);
-                echo $this->import->log('Reclamation created: ' . $r['orders.object_address']);
-                $result['reclamationsCreated']++;
-            } else {
-                echo $this->import->log('Reclamation found: ' . $r['orders.object_address']);
-            }
+                    ->where('order_id', $order->id)
+                    ->where('status_id', $statusId)
+                    ->where('create_date', $createDate)
+                    ->where('finish_date', $finishDate)
+                    ->where('start_work_date', $startWorkDate)
+                    ->where('reason', $r['reclamations.reason'])
+                    ->where('whats_done', $r['reclamations.whats_done'])
+                    ->where('guarantee', $r['reclamations.guarantee'])
+                    ->first();
 
-            if(!$reclamation->skus->contains($productSKU)) {
-                $reclamation->skus()->syncWithoutDetaching($productSKU->id);
-                echo $this->import->log('Attached MAF to reclamation, maf_id: ' . $productSKU->id);
-                $result['mafAttached']++;
-            } else {
-                echo $this->import->log('MAF already attached!');
+                if (!$reclamation) {
+                    $reclamation = Reclamation::query()
+                        ->create([
+                            'order_id' => $order->id,
+                            'user_id' => $userId,
+                            'status_id' => $statusId,
+                            'create_date' => $createDate,
+                            'finish_date' => $finishDate,
+                            'reason' => $r['reclamations.reason'],
+                            'guarantee' => $r['reclamations.guarantee'],
+                            'whats_done' => $r['reclamations.whats_done'],
+                            'start_work_date' => $startWorkDate,
+                            'comment' => $r['reclamations.comment'],
+                        ]);
+                    $logMessage .= 'Reclamation created: ' . $r['orders.object_address'] . ". ";
+                    $result['reclamationsCreated']++;
+                } else {
+                    $logMessage .= 'Reclamation found: ' . $r['orders.object_address']. ". ";
+                }
+
+                if (!$reclamation->skus->contains($productSKU)) {
+                    $reclamation->skus()->syncWithoutDetaching($productSKU->id);
+                    $logMessage .= 'Attached MAF to reclamation, maf_id: ' . $productSKU->id . ". ";
+                    $result['mafAttached']++;
+                } else {
+                    $logMessage .= 'MAF already attached!';
+                }
+
+                echo $this->import->log($logMessage);
+            } catch(\Exception $e) {
+                echo $this->import->log($e->getMessage(), 'WARNING');
             }
         }