Browse Source

feat: order_item_flag and related features

Fernando 4 tháng trước cách đây
mục cha
commit
c14c0f9574

+ 6 - 1
controllers/OrderItemCreateController.php

@@ -36,7 +36,12 @@ class OrderItemCreateController
         $productId = (int) $body['product_id'];
         $kitchenNote = $body['kitchen_note'] ?? '';
 
-        $created = $this->model->createOrderItem($orderId, $productId, $companyId, $kitchenNote);
+        $created = $this->model->createOrderItem(
+            $orderId,
+            $productId,
+            $companyId,
+            $kitchenNote
+        );
 
         return $created
             ? ResponseLib::sendOk(['created' => true, 'order_item_id' => $created])

+ 45 - 0
controllers/OrderItemUpdateController.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Models\OrderItemModel;
+use Psr\Http\Message\ServerRequestInterface;
+use Respect\Validation\Validator as v;
+use Respect\Validation\Exceptions\ValidationException;
+
+class OrderItemUpdateController
+{
+    private OrderItemModel $model;
+
+    public function __construct()
+    {
+        $this->model = new OrderItemModel();
+    }
+
+    public function __invoke(ServerRequestInterface $request)
+    {
+        $body = json_decode((string)$request->getBody(), true) ?? [];
+
+        try {
+            v::key('company_id', v::intType()->positive())
+              ->key('order_item_id', v::intType()->positive())
+              ->assert($body);
+        } catch (ValidationException $e) {
+            return ResponseLib::sendFail(
+                "Validation failed: " . $e->getFullMessage(),
+                [],
+                "E_VALIDATE"
+            )->withStatus(400);
+        }
+
+        $companyId = (int) $body['company_id'];
+        $orderItemId = (int) $body['order_item_id'];
+
+        $updated = $this->model->updateOrderItem($orderItemId, $companyId);
+
+        return $updated
+            ? ResponseLib::sendOk(['updated' => true])
+            : ResponseLib::sendFail("Failed to Update Order Item Flag", [], "E_DATABASE")->withStatus(500);
+    }
+}

+ 1 - 0
migrations/migrations_v1.sql

@@ -82,6 +82,7 @@ CREATE TABLE "order_item" (
     "order_id" INTEGER NOT NULL,
     "product_id" INTEGER NOT NULL,
     "company_id" INTEGER NOT NULL,
+    "order_item_flag" TEXT NOT NULL,
     "order_item_kitchen_note" TEXT NOT NULL DEFAULT '',
     FOREIGN KEY ("order_id") REFERENCES "order" ("order_id"),
     FOREIGN KEY ("product_id") REFERENCES "product" ("product_id"),

+ 18 - 13
models/KitchenModel.php

@@ -17,10 +17,13 @@ class KitchenModel
     public function getKitchenOrders(int $companyId): array
     {
         $sql = "
-            SELECT order_id,
+            SELECT 
+                order_id,
                 table_id,
                 order_created_at,
                 order_flag,
+                order_item_id,
+                order_item_flag,
                 product_name,
                 order_item_kitchen_note
             FROM 'order' 
@@ -31,31 +34,33 @@ class KitchenModel
               AND product_is_kitchen = 1
             ORDER BY order_created_at ASC;   
         ";
-
+    
         $stmt = $this->pdo->prepare($sql);
         $stmt->execute(['company_id' => $companyId]);
         $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
-
+    
         $orders = [];
         foreach ($rows as $row) {
             $orderId = $row['order_id'];
-
+        
             if (!isset($orders[$orderId])) {
                 $orders[$orderId] = [
-                    'order_id'        => $row['order_id'],
-                    'table_id'        => $row['table_id'],
-                    'order_created_at'=> $row['order_created_at'],
-                    'order_flag'=> $row['order_flag'],
-                    'items'           => []
+                    'order_id'         => $row['order_id'],
+                    'table_id'         => $row['table_id'],
+                    'order_created_at' => $row['order_created_at'],
+                    'order_flag'       => $row['order_flag'],
+                    'items'            => []
                 ];
             }
-
+        
             $orders[$orderId]['items'][] = [
-                'product_name' => $row['product_name'],
-                'kitchen_note' => $row['order_item_kitchen_note'] ?? ''
+                'order_item_id'    => $row['order_item_id'],
+                'order_item_flag'  => $row['order_item_flag'],
+                'product_name'     => $row['product_name'],
+                'kitchen_note'     => $row['order_item_kitchen_note'] ?? ''
             ];
         }
-
+    
         return array_values($orders);
     }
 }

+ 126 - 10
models/OrderItemModel.php

@@ -19,26 +19,42 @@ class OrderItemModel
         $this->pdo->exec('PRAGMA busy_timeout = 5000;');
     }
 
-    public function createOrderItem(int $orderId, int $productId, int $companyId, string $kitchenNote = ''): int|false
+    public function createOrderItem(
+        int $orderId,
+        int $productId,
+        int $companyId,
+        string $kitchenNote = ''
+    ): int|false
     {
         $orderModel = new OrderModel();
         $productModel = new ProductModel();
-    
-        $order = $orderModel->getOrderById($orderId, $companyId);
+
+        $order = $orderModel->getOrderById($orderId, $companyId, false);
         if (!$order) {
             return false;
         }
-    
+
         $product = $productModel->getProductById($productId, $companyId);
         if (!$product) {
             return false;
         }
-    
+
         $stmt = $this->pdo->prepare("
-            INSERT INTO order_item (order_id, product_id, company_id, order_item_kitchen_note)
-            VALUES (:order_id, :product_id, :company_id, :order_item_kitchen_note)
+            INSERT INTO order_item (
+                order_id,
+                product_id,
+                company_id,
+                order_item_flag,
+                order_item_kitchen_note
+            ) VALUES (
+                :order_id,
+                :product_id,
+                :company_id,
+                'a',
+                :order_item_kitchen_note
+            )
         ");
-    
+
         try {
             $executed = $stmt->execute([
                 'order_id' => $orderId,
@@ -46,14 +62,29 @@ class OrderItemModel
                 'company_id' => $companyId,
                 'order_item_kitchen_note' => $kitchenNote
             ]);
-            return $executed ? (int)$this->pdo->lastInsertId() : false;
+
+            if ($executed) {
+                if (!empty($product['product_is_kitchen']) && $product['product_is_kitchen'] == 1) {
+                    $this->pdo->prepare("
+                        UPDATE `order`
+                        SET order_flag = 'a'
+                        WHERE order_id = :order_id
+                          AND company_id = :company_id
+                    ")->execute([
+                        'order_id' => $orderId,
+                        'company_id' => $companyId
+                    ]);
+                }
+                return (int)$this->pdo->lastInsertId();
+            }
+            return false;
+
         } catch (\PDOException $e) {
             error_log("PDO Exception during order item creation: " . $e->getMessage());
             return false;
         }
     }
 
-
     public function deleteOrderItem(int $orderItemId, int $companyId): bool
     {
         $stmt = $this->pdo->prepare("
@@ -102,4 +133,89 @@ class OrderItemModel
     
         return $stmt->fetchAll(\PDO::FETCH_ASSOC);
     }
+
+    public function updateOrderItem(int $orderItemId, int $companyId): bool
+    {
+        $stmt = $this->pdo->prepare("
+            SELECT order_item_flag, order_id, product_id
+            FROM order_item
+            WHERE order_item_id = :order_item_id
+              AND company_id = :company_id
+        ");
+        $stmt->execute([
+            'order_item_id' => $orderItemId,
+            'company_id' => $companyId
+        ]);
+        $item = $stmt->fetch(\PDO::FETCH_ASSOC);
+
+        if (!$item) {
+            return false;
+        }
+
+        $newFlag = $item['order_item_flag'] === 'a' ? 'p' : 'a';
+
+        $update = $this->pdo->prepare("
+            UPDATE order_item
+            SET order_item_flag = :new_flag
+            WHERE order_item_id = :order_item_id
+              AND company_id = :company_id
+        ");
+        $executed = $update->execute([
+            'new_flag' => $newFlag,
+            'order_item_id' => $orderItemId,
+            'company_id' => $companyId
+        ]);
+
+        if ($executed && $update->rowCount() > 0) {
+            $stmtCheck = $this->pdo->prepare("
+                SELECT COUNT(*) 
+                FROM order_item
+                NATURAL JOIN product
+                WHERE order_id = :order_id
+                  AND company_id = :company_id
+                  AND product_is_kitchen = 1
+                  AND order_item_flag != 'p'
+            ");
+            $stmtCheck->execute([
+                'order_id' => $item['order_id'],
+                'company_id' => $companyId
+            ]);
+            $pendingKitchen = (int)$stmtCheck->fetchColumn();
+
+            if ($pendingKitchen === 0) {
+                $this->pdo->prepare("
+                    UPDATE `order`
+                    SET order_flag = 'p'
+                    WHERE order_id = :order_id
+                      AND company_id = :company_id
+                ")->execute([
+                    'order_id' => $item['order_id'],
+                    'company_id' => $companyId
+                ]);
+            } else {
+                $stmtOrderFlag = $this->pdo->prepare("
+                    SELECT order_flag FROM `order`
+                    WHERE order_id = :order_id AND company_id = :company_id
+                ");
+                $stmtOrderFlag->execute([
+                    'order_id' => $item['order_id'],
+                    'company_id' => $companyId
+                ]);
+                $currentOrderFlag = $stmtOrderFlag->fetchColumn();
+
+                if ($currentOrderFlag === 'p' && $newFlag === 'a') {
+                    $this->pdo->prepare("
+                        UPDATE `order`
+                        SET order_flag = 'a'
+                        WHERE order_id = :order_id AND company_id = :company_id
+                    ")->execute([
+                        'order_id' => $item['order_id'],
+                        'company_id' => $companyId
+                    ]);
+                }
+            }
+            return true;
+        }
+        return false;
+    }
 }

+ 6 - 2
models/OrderModel.php

@@ -193,9 +193,13 @@ class OrderModel
         return $stmt->fetchAll(\PDO::FETCH_ASSOC);
     }
 
-    public function getOrderById(int $orderId, int $companyId): ?array
+    public function getOrderById(int $orderId, int $companyId, bool $onlyActive = true): ?array
     {
-        $stmt = $this->pdo->prepare("SELECT * FROM `order` WHERE order_id = :order_id AND company_id = :company_id AND order_flag = 'a'");
+        $sql = "SELECT * FROM `order` WHERE order_id = :order_id AND company_id = :company_id";
+        if ($onlyActive) {
+            $sql .= " AND order_flag = 'a'";
+        }
+        $stmt = $this->pdo->prepare($sql);
         $stmt->execute(['order_id' => $orderId, 'company_id' => $companyId]);
         $result = $stmt->fetch(\PDO::FETCH_ASSOC);
         return $result ?: null;

+ 1 - 0
public/index.php

@@ -79,6 +79,7 @@ $app->post('/order/update', $cors, $authJwt, \Controllers\OrderUpdateController:
 $app->post('/order_item/create', $cors, $authJwt, \Controllers\OrderItemCreateController::class);
 $app->post('/order_item/delete', $cors, $authJwt, \Controllers\OrderItemDeleteController::class);
 $app->post('/order_item/get', $cors, $authJwt, \Controllers\OrderItemGetController::class);
+$app->post('/order_item/update', $cors, $authJwt, \Controllers\OrderItemUpdateController::class);
 
 // Description Routes
 $app->post('/description/create', $cors, $authJwt, \Controllers\DescriptionCreateController::class);