From 7b2c57746cf87ebc6079c3bc055fadf554642e84 Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期五, 26 六月 2026 09:59:08 +0800
Subject: [PATCH] 采购台账退货数量计算

---
 src/main/resources/mapper/sales/SalesLedgerProductMapper.xml |   33 ++++++++++++++++++++++++++-------
 1 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml b/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
index e2e6277..8787706 100644
--- a/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
+++ b/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
@@ -33,7 +33,8 @@
         WHEN (IFNULL(t2.qualitity, 0) - IFNULL(t2.locked_quantity, 0)) >0 THEN 1
         ELSE 0
         END as has_sufficient_stock,
-        (IFNULL(T1.quantity, 0) - IFNULL(t3.shipped_quantity, 0)) as no_quantity,
+        (IFNULL(T1.quantity, 0) - IFNULL(t3.shipped_quantity, 0) - IFNULL(t5.pending_approval_quantity, 0)) as no_quantity,
+        IFNULL(t6.return_quantity, 0) as return_quantity,
         IFNULL(t5.pending_approval_quantity, 0) as pending_approval_quantity,
         CASE
          WHEN IFNULL(t3.shipped_quantity, 0) = 0 AND IFNULL(t5.pending_approval_quantity, 0) = 0 THEN '寰呭彂璐�'
@@ -55,11 +56,17 @@
         GROUP BY 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
+        SELECT si.sales_ledger_product_id, IFNULL(SUM(spd.quantity), 0) as shipped_quantity
         FROM shipping_info si
-        LEFT JOIN shipping_product_detail spd ON si.id = spd.shipping_info_id
-        where si.status = '瀹℃牳閫氳繃' OR si.status = '宸插彂璐�'
-        GROUP BY sales_ledger_product_id
+        INNER JOIN shipping_product_detail spd ON si.id = spd.shipping_info_id
+        WHERE si.status IN ('瀹℃牳閫氳繃', '宸插彂璐�')
+          AND EXISTS (
+            SELECT 1 FROM stock_out_record sor
+            WHERE sor.record_id = si.id
+              AND TRIM(sor.record_type) = '13'
+              AND sor.approval_status = 1
+          )
+        GROUP BY si.sales_ledger_product_id
         ) t3 ON t3.sales_ledger_product_id = T1.id
         LEFT JOIN (
         SELECT rel.sales_ledger_product_id,
@@ -95,12 +102,24 @@
         GROUP BY rel.sales_ledger_product_id
         ) t4 ON t4.sales_ledger_product_id = T1.id
         LEFT JOIN (
-        SELECT sales_ledger_product_id, IFNULL(SUM(spd.quantity), 0) as pending_approval_quantity
+        SELECT si.sales_ledger_product_id, IFNULL(SUM(spd.quantity), 0) as pending_approval_quantity
         FROM shipping_info si
         LEFT JOIN shipping_product_detail spd ON si.id = spd.shipping_info_id
         WHERE si.status IN ('寰呭鏍�', '瀹℃牳涓�')
-        GROUP BY sales_ledger_product_id
+           OR (si.status = '瀹℃牳閫氳繃' AND NOT EXISTS (
+                SELECT 1 FROM stock_out_record sor
+                WHERE sor.record_id = si.id
+                  AND TRIM(sor.record_type) = '13'
+                  AND sor.approval_status = 1
+           ))
+        GROUP BY si.sales_ledger_product_id
         ) t5 ON t5.sales_ledger_product_id = T1.id
+        LEFT JOIN (
+        SELECT prop.sales_ledger_product_id, IFNULL(SUM(prop.return_quantity), 0) AS return_quantity
+        FROM purchase_return_order_products prop
+        INNER JOIN purchase_return_orders pro ON prop.purchase_return_order_id = pro.id
+        GROUP BY prop.sales_ledger_product_id
+        ) t6 ON t6.sales_ledger_product_id = T1.id
         left join product_model pm ON T1.product_model_id = pm.id
         left join product p ON pm.product_id = p.id
         <where>

--
Gitblit v1.9.3