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/basic/CustomerMapper.xml |   26 ++++++++++++++++++++++----
 1 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/src/main/resources/mapper/basic/CustomerMapper.xml b/src/main/resources/mapper/basic/CustomerMapper.xml
index 8117c66..442173e 100644
--- a/src/main/resources/mapper/basic/CustomerMapper.xml
+++ b/src/main/resources/mapper/basic/CustomerMapper.xml
@@ -22,7 +22,14 @@
         from customer_user cu
         where cu.customer_id = c.id
         and cu.user_id != c.usage_user
-        ) as user_ids_str
+        ) as user_ids_str,
+        CASE
+            WHEN EXISTS (SELECT 1 FROM sales_ledger sl WHERE sl.customer_id = c.id)
+              OR EXISTS (SELECT 1 FROM sales_quotation sq WHERE sq.customer_id = c.id)
+              OR EXISTS (SELECT 1 FROM return_management rm WHERE rm.customer_id = c.id)
+            THEN 1
+            ELSE 0
+        END AS sales_reference_flag
         from customer c
         left join sys_user u on c.usage_user = u.user_id
         <where>
@@ -52,6 +59,7 @@
                 )
             </if>
         </where>
+        order by c.id desc
     </select>
 
     <select id="list" resultType="com.ruoyi.basic.vo.CustomerVo">
@@ -70,7 +78,14 @@
         from customer_user cu
         where cu.customer_id = c.id
         and cu.user_id != c.usage_user
-        ) as user_ids_str
+        ) as user_ids_str,
+        CASE
+            WHEN EXISTS (SELECT 1 FROM sales_ledger sl WHERE sl.customer_id = c.id)
+              OR EXISTS (SELECT 1 FROM sales_quotation sq WHERE sq.customer_id = c.id)
+              OR EXISTS (SELECT 1 FROM return_management rm WHERE rm.customer_id = c.id)
+            THEN 1
+            ELSE 0
+        END AS sales_reference_flag
         from customer c
         left join sys_user u on c.usage_user = u.user_id
         <where>
@@ -106,11 +121,12 @@
                 )
             </if>
         </where>
+        order by c.id desc
     </select>
     <select id="customewTransactions" resultType="com.ruoyi.sales.vo.CustomerTransactionsVo">
         select T1.customer_id,
                c.customer_name,
-               T1.contractAmounts,
+               T1.contractAmounts - IFNULL(T4.returnAmount, 0) AS contractAmounts,
                IFNULL(T2.receiptPaymentAmount, 0) AS receiptPaymentAmount,
                IFNULL(T3.outboundAmount, 0) - IFNULL(T4.returnAmount, 0) AS receiptableAmount
         from (select customer_id, sum(contract_amount) as contractAmounts from sales_ledger group by customer_id) T1
@@ -144,13 +160,14 @@
                 AND c.customer_name LIKE CONCAT('%', #{customerName}, '%')
             </if>
         </where>
+        order by T1.customer_id desc
     </select>
     <select id="customewTransactionsDetails"
             resultType="com.ruoyi.sales.vo.CustomerTransactionsDetailsVo">
         select sl.id salesLedgerId,
                sl.sales_contract_no,
                sl.execution_date,
-               sl.contract_amount,
+               sl.contract_amount - IFNULL(T3.returnAmount, 0) AS contract_amount,
                IFNULL(T1.receiptPaymentAmount, 0) AS receiptPaymentAmount,
                IFNULL(T2.outboundAmount, 0) - IFNULL(T3.returnAmount, 0) AS receiptableAmount
         from sales_ledger sl
@@ -189,5 +206,6 @@
             group by sl.id
         )T3 on T3.id = sl.id
         where sl.customer_id = #{customerId}
+        order by sl.id desc
     </select>
 </mapper>

--
Gitblit v1.9.3