yuan
2 天以前 1c8757f865bd5a0c80fa1d44ec4b62dfc6e24e39
src/main/resources/mapper/basic/CustomerMapper.xml
@@ -109,26 +109,40 @@
        </where>
        order by c.id desc
    </select>
    <!--
      客户往来(回款台账主表):
        应收金额 = 已出库金额 − 已退货款 − 已回款金额(不小于 0)
      即「回款金额」是累计回款,「应收金额」是还没收回来的部分。
      注意 T3 按 (shipping_info, 产品行) 先聚合再求和,避免同一发货单多批号出库时重复计数。
    -->
    <select id="customewTransactions" resultType="com.ruoyi.sales.vo.CustomerTransactionsVo">
        select T1.customer_id,
               c.customer_name,
               T1.contractAmounts,
               IFNULL(T2.receiptPaymentAmount, 0) AS receiptPaymentAmount,
               IFNULL(T3.outboundAmount, 0) - IFNULL(T4.returnAmount, 0) AS receiptableAmount
               GREATEST(
                   IFNULL(T3.outboundAmount, 0) - IFNULL(T4.returnAmount, 0)
                       - IFNULL(T2.receiptPaymentAmount, 0),
                   0) AS receiptableAmount
        from (select customer_id, sum(contract_amount) as contractAmounts from sales_ledger group by customer_id) T1
        left join (select customer_id, sum(collection_amount) as receiptPaymentAmount from account_sales_collection group by customer_id) T2 on T1.customer_id = T2.customer_id
        left join (
            SELECT
                sl.customer_id,
                sum(sor.stock_out_num * slp.tax_inclusive_unit_price) as outboundAmount
            FROM stock_out_record sor
            LEFT join shipping_info s on sor.record_id = s.id
            LEFT JOIN sales_ledger sl ON s.sales_ledger_id = sl.id
            LEFT JOIN sales_ledger_product slp ON s.sales_ledger_product_id = slp.id
            WHERE sor.record_type='13'
                and sor.approval_status=1
                and slp.type = 1
            group by sl.customer_id
            SELECT customer_id, sum(shipped_amount) as outboundAmount from (
                SELECT
                    sl.customer_id,
                    s.id AS shipping_id,
                    s.sales_ledger_product_id,
                    sum(sor.stock_out_num * slp.tax_inclusive_unit_price) as shipped_amount
                FROM stock_out_record sor
                LEFT join shipping_info s on sor.record_id = s.id
                LEFT JOIN sales_ledger sl ON s.sales_ledger_id = sl.id
                LEFT JOIN sales_ledger_product slp ON s.sales_ledger_product_id = slp.id
                WHERE sor.record_type='13'
                    and sor.approval_status=1
                    and slp.type = 1
                group by sl.customer_id, s.id, s.sales_ledger_product_id
            ) g
            group by customer_id
        ) T3 on T3.customer_id=T1.customer_id
        left join (
            select
@@ -148,6 +162,11 @@
        </where>
        order by T1.customer_id desc
    </select>
    <!--
      客户往来明细(按销售台账):应收金额 = 该台账已出库 − 已退货 − 已回款。
      单条回款只统计一次:同一回款可能同时命中该台账的多条出库记录,
      先用 GROUP BY ascc.id, sl.id 去重,再按台账求和。
    -->
    <select id="customewTransactionsDetails"
            resultType="com.ruoyi.sales.vo.CustomerTransactionsDetailsVo">
        select sl.id salesLedgerId,
@@ -155,7 +174,10 @@
               sl.execution_date,
               sl.contract_amount,
               IFNULL(T1.receiptPaymentAmount, 0) AS receiptPaymentAmount,
               IFNULL(T2.outboundAmount, 0) - IFNULL(T3.returnAmount, 0) AS receiptableAmount
               GREATEST(
                   IFNULL(T2.outboundAmount, 0) - IFNULL(T3.returnAmount, 0)
                       - IFNULL(T1.receiptPaymentAmount, 0),
                   0) AS receiptableAmount
        from sales_ledger sl
        left join (
            select