Parcourir la source

order status triggers

Alexander Musikhin il y a 7 mois
Parent
commit
deb01563e0
2 fichiers modifiés avec 33 ajouts et 3 suppressions
  1. 4 1
      app/Http/Controllers/OrderController.php
  2. 29 2
      app/Models/Order.php

+ 4 - 1
app/Http/Controllers/OrderController.php

@@ -104,7 +104,6 @@ class OrderController extends Controller
         $data = $request->validated();
 
         $products = $request->validated('products');
-        $products_sku = $request->validated('products_sku');
         $quantities = $request->validated('quantity');
 
         unset($data['products']);
@@ -133,6 +132,9 @@ class OrderController extends Controller
             }
         }
 
+        $order->refresh();
+        $order->autoChangeStatus();
+
         return redirect()->route('order.show', $order);
     }
 
@@ -176,6 +178,7 @@ class OrderController extends Controller
             $mafOrder->decrement('in_stock');
             unset($mafOrder, $product_sku);
         }
+        $order->autoChangeStatus();
         return redirect()->route('order.show', $order);
     }
 

+ 29 - 2
app/Models/Order.php

@@ -34,8 +34,8 @@ class Order extends Model
     const STATUS_NOT_HANDED_OVER_WITH_NOTES = 8;
     const STATUS_HANDED_OVER_WITH_NOTES = 9;
     const STATUS_HANDED_OVER = 10;
-
-    const STATUS_PROBLEM = 11;
+    const STATUS_NO_MAF = 11;
+    const STATUS_PROBLEM = 12;
 
     //ПЛ не готова; ПЛ готова нет МАФ; Готов к монтажу; В монтаже; Долг;Готов к сдаче;Не сдан, замечания;Сдан с замечаниями;Сдан;Проблема
 
@@ -50,6 +50,7 @@ class Order extends Model
         self::STATUS_NOT_HANDED_OVER_WITH_NOTES => 'Не сдана, замечания',
         self::STATUS_HANDED_OVER_WITH_NOTES => 'Сдана с замечаниями',
         self::STATUS_HANDED_OVER => 'Сдана',
+        self::STATUS_NO_MAF => 'Отсутствуют МАФ',
         self::STATUS_PROBLEM => 'Проблема',
     ];
 
@@ -164,6 +165,32 @@ class Order extends Model
        $this->update(['ready_to_mount' => ($result) ? 'Да' : 'Нет']);
     }
 
+    public function autoChangeStatus(): void
+    {
+        if(($this->products_sku->count() < 1)
+            && ($this->order_status_id !== self::STATUS_NEW)
+        ) {
+            $this->update(['order_status_id' => self::STATUS_NO_MAF]);
+            return;
+        }
+
+        if(($this->order_status_id === self::STATUS_READY_TO_MOUNT)
+            && ($this->brigadier_id !== null)
+            && ($this->installation_date !== null)
+        ) {
+            $allMafConnected = true;
+            foreach ($this->products_sku as $sku) {
+                if($sku->maf_order) continue;
+                $allMafConnected = false;
+            }
+
+            if($allMafConnected) {
+                $this->update(['order_status_id' => self::STATUS_IN_MOUNT]);
+            }
+        }
+
+    }
+
     public function commonName(): Attribute
     {
         return Attribute::make(