yuan
3 天以前 ccc9d4533bc7b696f6d539720a6052abc44ff7a5
src/main/resources/mapper/purchase/PaymentRegistrationMapper.xml
@@ -181,32 +181,45 @@
                AND t4.purchase_contract_number LIKE CONCAT('%',#{params.purchaseContractNumber},'%')
            </if>
        </where>
        ORDER BY T1.payment_date,T1.create_time DESC
        <!-- 2026-10-13 需求:付款流水按付款日期倒序(原来只写了 payment_date 没写方向,默认升序,最早的在最上面) -->
        ORDER BY T1.payment_date DESC, T1.id DESC
    </select>
    <select id="supplierNameListPage" 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
        IFNULL(SUM(T1.contract_amount), 0) AS invoiceAmount,
        IFNULL(SUM(T2.paid), 0)            AS paymentAmount,
        IFNULL(SUM(T1.contract_amount), 0) - IFNULL(SUM(T2.paid), 0) AS payableAmount
        FROM purchase_ledger T1
        LEFT JOIN payment_registration T2 ON T1.id = T2.purchase_ledger_id
        <!-- 2026-10-13 修复:原来直接 LEFT JOIN payment_registration 再 SUM(contract_amount),
             一笔采购合同有几笔付款,合同额就被重复累加几次(供应商往来页「合同金额/应付金额」因此虚高)。
             改成先按台账汇总付款再 JOIN。 -->
        LEFT JOIN (
            SELECT purchase_ledger_id, SUM(current_payment_amount) AS paid
            FROM payment_registration WHERE purchase_ledger_id IS NOT NULL GROUP BY purchase_ledger_id
        ) T2 ON T2.purchase_ledger_id = T1.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,
        T1.contract_amount AS invoiceAmount,
        IFNULL(SUM(T2.current_payment_amount), 0) AS paymentAmount,
        <!-- 2026-10-13 修复:原来写的是「合同额 − 本日期这一笔付款」,再叠加 GROUP BY payment_date,
             一笔合同有几笔付款就拆成几行、每行一个假的"应付"(例如 CG20260523002 已全额付款,
             却显示应付 18524.07)。应付金额应当是**该合同当前真正的未付款额 = 合同额 − 全部付款**。 -->
        GREATEST(T1.contract_amount - IFNULL((
            SELECT SUM(T3.current_payment_amount) FROM payment_registration T3
            WHERE T3.purchase_ledger_id = T1.id
        ), 0), 0) AS payableAmount,
        T1.purchase_contract_number,
        T2.payment_date
        FROM purchase_ledger T1
@@ -217,7 +230,8 @@
                T1.supplier_name LIKE CONCAT ('%',#{req.supplierName},'%')
            </if>
        </where>
        GROUP BY  T1.purchase_contract_number,T2.payment_date
        GROUP BY T1.id, T1.supplier_id, T1.supplier_name, T1.contract_amount, T1.purchase_contract_number, T2.payment_date
        ORDER BY T1.entry_date ASC, T2.payment_date ASC
    </select>
</mapper>