소스 검색

feature: route reports/get now can be used with only company_id, returning every order.

EduLascala 4 달 전
부모
커밋
dc3a025f81
2개의 변경된 파일30개의 추가작업 그리고 16개의 파일을 삭제
  1. 4 4
      controllers/ReportsGetController.php
  2. 26 12
      models/ReportsModel.php

+ 4 - 4
controllers/ReportsGetController.php

@@ -23,15 +23,15 @@ class ReportsGetController
 
         try {
             v::key('company_id', v::intType()->positive())->assert($body);
-            v::optional(v::key('page', v::intType()->positive()))->assert($body);
-            v::optional(v::key('limit', v::intType()->positive()))->assert($body);
+            v::key('page', v::intType()->positive(), false)->assert($body);
+            v::key('limit', v::intType()->positive(), false)->assert($body);
         } catch (ValidationException $e) {
             return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = (int) $body['company_id'];
-        $page      = isset($body['page']) ? (int) $body['page'] : 1;
-        $limit     = isset($body['limit']) ? (int) $body['limit'] : 10;
+        $page  = isset($body['page']) ? (int) $body['page'] : null;
+        $limit = isset($body['limit']) ? (int) $body['limit'] : null;
 
         $reports = $this->model->getReports($companyId, $page, $limit);
 

+ 26 - 12
models/ReportsModel.php

@@ -16,10 +16,9 @@ class ReportsModel
         $this->pdo->exec('PRAGMA busy_timeout = 5000;');
     }
 
-    public function getReports(int $companyId, int $page = 1, int $limit = 10): array
+    public function getReports(int $companyId, ?int $page = null, ?int $limit = null): array
     {
-        $offset = ($page - 1) * $limit;
-    
+        // Total de vendas
         $sqlTotalValue = "
             SELECT SUM(CAST(product_price AS REAL)) AS total_sales
             FROM `order`
@@ -33,6 +32,7 @@ class ReportsModel
         $stmtTotal->execute(['company_id' => $companyId]);
         $totalSales = $stmtTotal->fetchColumn() ?: 0;
 
+        // Top 3 itens mais vendidos
         $sqlTopItems = "
             SELECT product_name, COUNT(*) AS sold_quantity
             FROM `order`
@@ -49,6 +49,7 @@ class ReportsModel
         $stmtTopItems->execute(['company_id' => $companyId]);
         $topItems = $stmtTopItems->fetchAll(\PDO::FETCH_ASSOC);
 
+        // Lista de pedidos
         $sqlOrders = "
             SELECT order_id,
                    order_finished_at,
@@ -60,33 +61,45 @@ class ReportsModel
             WHERE company_id = :company_id
               AND status_status = 'Finalizada'
             ORDER BY order_finished_at DESC
-            LIMIT :limit OFFSET :offset;
         ";
+
+        $offset = 0;
+        if ($page !== null && $limit !== null) {
+            $offset = ($page - 1) * $limit;
+            $sqlOrders .= " LIMIT :limit OFFSET :offset";
+        }
+
         $stmtOrders = $this->pdo->prepare($sqlOrders);
         $stmtOrders->bindValue(':company_id', $companyId, \PDO::PARAM_INT);
-        $stmtOrders->bindValue(':limit', $limit, \PDO::PARAM_INT);
-        $stmtOrders->bindValue(':offset', $offset, \PDO::PARAM_INT);
+
+        if ($page !== null && $limit !== null) {
+            $stmtOrders->bindValue(':limit', $limit, \PDO::PARAM_INT);
+            $stmtOrders->bindValue(':offset', $offset, \PDO::PARAM_INT);
+        }
+
         $stmtOrders->execute();
         $ordersData = $stmtOrders->fetchAll(\PDO::FETCH_ASSOC);
 
         if (empty($ordersData)) {
             return [
                 'total_sales' => (float) $totalSales,
-                'top_items' => $topItems,
-                'orders' => []
+                'top_items'   => $topItems,
+                'orders'      => []
             ];
         }
 
+        // Pega os IDs dos pedidos encontrados
         $orderIds = array_column($ordersData, 'order_id');
         $inQuery = implode(',', array_fill(0, count($orderIds), '?'));
 
+        // Busca itens dos pedidos
         $sqlItems = "
             SELECT order_id,
                    product_name,
                    product_price
             FROM order_item
             NATURAL JOIN product
-            WHERE order_id IN ($inQuery);
+            WHERE order_id IN ($inQuery)
         ";
         $stmtItems = $this->pdo->prepare($sqlItems);
         foreach ($orderIds as $k => $id) {
@@ -95,6 +108,7 @@ class ReportsModel
         $stmtItems->execute();
         $itemsData = $stmtItems->fetchAll(\PDO::FETCH_ASSOC);
 
+        // Monta estrutura final de pedidos + itens
         $orders = [];
         foreach ($ordersData as $order) {
             $orders[$order['order_id']] = $order + ['items' => []];
@@ -108,8 +122,8 @@ class ReportsModel
 
         return [
             'total_sales' => (float) $totalSales,
-            'top_items' => $topItems,
-            'orders' => array_values($orders),
+            'top_items'   => $topItems,
+            'orders'      => array_values($orders),
         ];
     }
-}
+}