From 31a5d6d89b7aa5abd5d8a83aca7fe2700cba029f Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期二, 09 六月 2026 17:36:41 +0800
Subject: [PATCH] 销售台账产品状态根据库存类型判断

---
 src/main/resources/mapper/sales/SalesLedgerProductMapper.xml |   52 ++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml b/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
index 48890f5..f9a5d1a 100644
--- a/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
+++ b/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
@@ -21,6 +21,7 @@
         T1.type,
         T1.product_id,
         T1.product_model_id,
+        T1.stock_type,
         T1.register,
         T1.register_date,
         T1.approve_status,
@@ -32,7 +33,12 @@
         pm.model as specification_model,
         pm.unit as unit,
         CASE
-        WHEN (IFNULL(t2.qualitity, 0) - IFNULL(t2.locked_quantity, 0)) >0 THEN 1
+        WHEN IFNULL(
+            CASE
+                WHEN T1.stock_type = 'waste' THEN t2.waste_available_quantity
+                ELSE t2.qualified_available_quantity
+            END, 0
+        ) >= (IFNULL(T1.quantity, 0) - IFNULL(t3.shipped_quantity, 0)) THEN 1
         ELSE 0
         END as has_sufficient_stock,
         (IFNULL(T1.quantity, 0) - IFNULL(t3.shipped_quantity, 0)) as no_quantity,
@@ -52,9 +58,47 @@
         FROM
         sales_ledger_product T1
         LEFT JOIN (
-        SELECT product_model_id, SUM(qualitity) as qualitity, SUM(locked_quantity) as locked_quantity
-        FROM stock_inventory
-        GROUP BY product_model_id
+        SELECT
+            t.product_model_id,
+            SUM(t.qualified_available_quantity) as qualified_available_quantity,
+            SUM(t.waste_available_quantity) as waste_available_quantity
+        FROM (
+            SELECT
+                si.product_model_id,
+                (IFNULL(si.qualitity, 0) - IFNULL(si.locked_quantity, 0) - IFNULL((
+                    SELECT SUM(sor.stock_out_num)
+                    FROM stock_out_record sor
+                    WHERE sor.product_model_id = si.product_model_id
+                      AND (
+                        (si.batch_no IS NULL AND sor.batch_no IS NULL)
+                        OR si.batch_no = sor.batch_no
+                      )
+                      AND sor.type = '0'
+                      AND sor.approval_status IN (0, 3)
+                ), 0)) as qualified_available_quantity,
+                0 as waste_available_quantity
+            FROM stock_inventory si
+
+            UNION ALL
+
+            SELECT
+                su.product_model_id,
+                0 as qualified_available_quantity,
+                (IFNULL(su.qualitity, 0) - IFNULL(su.locked_quantity, 0) - IFNULL((
+                    SELECT SUM(sor.stock_out_num)
+                    FROM stock_out_record sor
+                    WHERE sor.product_model_id = su.product_model_id
+                      AND (
+                        (su.batch_no IS NULL AND sor.batch_no IS NULL)
+                        OR su.batch_no = sor.batch_no
+                      )
+                      AND sor.type = '1'
+                      AND sor.approval_status IN (0, 3)
+                ), 0)) as waste_available_quantity
+            FROM stock_uninventory su
+            WHERE COALESCE(su.type, 'unqualified') = 'waste'
+        ) t
+        GROUP BY t.product_model_id
         ) t2 ON T1.product_model_id = t2.product_model_id
         LEFT JOIN (
         SELECT sales_ledger_product_id, IFNULL(SUM(spd.quantity), 0) as shipped_quantity

--
Gitblit v1.9.3