From c848c1bdbe9ed9268de3d181721a8b52905032cd Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 24 四月 2026 15:59:40 +0800
Subject: [PATCH] fix: 应付金额去重并扣减历史记录

---
 src/main/resources/mapper/purchase/PaymentRegistrationMapper.xml |   48 +++++++++++++++++++++++++++++++++---------------
 1 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/src/main/resources/mapper/purchase/PaymentRegistrationMapper.xml b/src/main/resources/mapper/purchase/PaymentRegistrationMapper.xml
index c5c007e..29acb67 100644
--- a/src/main/resources/mapper/purchase/PaymentRegistrationMapper.xml
+++ b/src/main/resources/mapper/purchase/PaymentRegistrationMapper.xml
@@ -185,39 +185,57 @@
     </select>
     <select id="supplierNameListPage" resultType="com.ruoyi.purchase.dto.PaymentRegistrationDto">
         SELECT
-        T1.supplier_id,
+        T1.supplier_id ,
         T1.supplier_name,
-        SUM(contract_amount) AS invoiceAmount,
-        IFNULL( SUM(T2.current_payment_amount) , 0 ) AS paymentAmount,
-        IFNULL((IFNULL(SUM(contract_amount),0)  - IFNULL(SUM(T2.current_payment_amount),0)),0) AS payableAmount
-        FROM purchase_ledger T1
-        LEFT JOIN payment_registration T2 ON T1.id = T2.purchase_ledger_id
+        SUM(T1.contract_amount) AS invoiceAmount,
+        IFNULL(SUM(T2.current_payment_amount), 0) AS paymentAmount,
+        IFNULL(SUM(T1.contract_amount) - IFNULL(SUM(T2.current_payment_amount), 0), 0) AS payableAmount
+        FROM (
+        SELECT id,supplier_id, supplier_name, SUM(contract_amount) contract_amount
+        FROM purchase_ledger
+        GROUP BY supplier_id, supplier_name,id
+        ) T1
+        LEFT JOIN (
+        SELECT purchase_ledger_id, SUM(current_payment_amount) current_payment_amount
+        FROM payment_registration
+        GROUP BY purchase_ledger_id
+        ) T2 ON T1.id = T2.purchase_ledger_id
         <where>
             <if test="req.supplierName != null and req.supplierName != '' ">
                 T1.supplier_name LIKE CONCAT ('%',#{req.supplierName},'%')
             </if>
         </where>
-        GROUP BY T1.supplier_name
+        GROUP BY T1.supplier_id, T1.supplier_name
     </select>
 
     <select id="supplierNameListPageDetails" resultType="com.ruoyi.purchase.dto.PaymentRegistrationDto">
         SELECT
-        T1.supplier_id,
-        T1.supplier_name,
-        SUM(contract_amount) AS invoiceAmount,
-        IFNULL( SUM(T2.current_payment_amount) , 0 ) AS paymentAmount,
-        IFNULL((IFNULL(SUM(contract_amount),0)  - IFNULL(SUM(T2.current_payment_amount),0)),0) AS payableAmount,
+        T2.id,
         T1.purchase_contract_number,
+        T1.contract_amount AS invoiceAmount,
+        IFNULL(T2.current_payment_amount, 0) AS paymentAmount,
+        IFNULL((
+        T1.contract_amount - IFNULL((
+        SELECT SUM(T3.current_payment_amount)
+        FROM payment_registration T3
+        WHERE T3.purchase_ledger_id = T1.id
+        AND (
+        T3.payment_date &lt; T2.payment_date
+        OR (T3.payment_date = T2.payment_date AND T3.id &lt;= T2.id)
+        )
+        ), 0)
+        ), 0) AS payableAmount,
         T2.payment_date
         FROM purchase_ledger T1
         INNER JOIN payment_registration T2 ON T1.id = T2.purchase_ledger_id
         <where>
             T1.supplier_id = #{req.supplierId}
             <if test="req.supplierName != null and req.supplierName != '' ">
-                T1.supplier_name LIKE CONCAT ('%',#{req.supplierName},'%')
+                AND T1.supplier_name LIKE CONCAT ('%', #{req.supplierName}, '%')
             </if>
         </where>
-        GROUP BY  T1.purchase_contract_number,T2.payment_date
+        ORDER BY T2.payment_date ASC, T2.id ASC
+
     </select>
 
-</mapper>
\ No newline at end of file
+</mapper>

--
Gitblit v1.9.3