Parcourir la source

feat: order_flag update verification for status

Fernando il y a 4 mois
Parent
commit
ad1b8f79c8
2 fichiers modifiés avec 28 ajouts et 3 suppressions
  1. 4 0
      controllers/OrderUpdateController.php
  2. 24 3
      models/OrderModel.php

+ 4 - 0
controllers/OrderUpdateController.php

@@ -43,6 +43,10 @@ class OrderUpdateController
 
         $updated = $this->model->updateOrderStatus($orderId, $companyId, $statusId, $orderFlag);
 
+        if (is_string($updated)) {
+        return ResponseLib::sendFail($updated, [], "E_VALIDATE")->withStatus(400);
+        }
+
         return $updated
             ? ResponseLib::sendOk(['updated' => true])
             : ResponseLib::sendFail("Failed to Update Order Status or Order Not Found", [], "E_DATABASE")->withStatus(204);

+ 24 - 3
models/OrderModel.php

@@ -54,7 +54,7 @@ class OrderModel
         }
     }
 
-    public function updateOrderStatus(int $orderId, int $companyId, int $statusId, ?string $orderFlag = null): bool
+    public function updateOrderStatus(int $orderId, int $companyId, int $statusId, ?string $orderFlag = null): bool|string
     {
         $stmtStatus = $this->pdo->prepare("
             SELECT status_status 
@@ -64,6 +64,29 @@ class OrderModel
         $stmtStatus->execute(['status_id' => $statusId]);
         $statusName = $stmtStatus->fetchColumn();
 
+        if (strtolower($statusName) === 'finalizada') {
+            $stmtCheck = $this->pdo->prepare("
+                SELECT COUNT(*)
+                FROM order_item
+                NATURAL JOIN product
+                NATURAL JOIN 'order'
+                WHERE order_id = :order_id
+                  AND company_id = :company_id
+                  AND product_is_kitchen = 1
+                  AND order_flag = 'a';
+            ");
+            $stmtCheck->execute([
+                'order_id'   => $orderId,
+                'company_id' => $companyId
+            ]);
+
+            $pendingKitchen = (int)$stmtCheck->fetchColumn();
+
+            if ($pendingKitchen > 0) {
+                return "Comanda possui itens de cozinha não finalizados.";
+            }
+        }
+
         $sql = "
             UPDATE `order`
             SET status_id = :status_id
@@ -102,8 +125,6 @@ class OrderModel
         return $executed && $stmt->rowCount() > 0;
     }
 
-
-
         public function deleteOrder(int $orderId, int $companyId, bool $hardDelete = false): bool
     {
         if ($hardDelete) {