fix: 优化客户往来查询逻辑,修复应收金额计算及批号显示问题
| | |
| | | if (TypeEnums.PURCHASE_APPROVAL.getCode().equals(vo.getBusinessType())) { |
| | | // 采购审批 - 查询采购单号 |
| | | PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(vo.getBusinessId()); |
| | | System.out.println("业务类型:" + purchaseLedger.getPurchaseContractNumber()); |
| | | if (purchaseLedger != null) { |
| | | vo.setPurchaseContractNumber(purchaseLedger.getPurchaseContractNumber()); |
| | | } |
| | |
| | | </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 |
| | |
| | | </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, |
| | |
| | | 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 |
| | |
| | | p.product_name, |
| | | sl.customer_name, |
| | | spd.totalQuantity, |
| | | spd.batchNos, |
| | | sor.outboundBatches |
| | | FROM shipping_info s |
| | | LEFT JOIN (select shipping_info_id,sum(quantity) totalQuantity from shipping_product_detail GROUP BY shipping_info_id) spd ON spd.shipping_info_id = s.id |
| | | LEFT JOIN (select shipping_info_id, sum(quantity) totalQuantity, |
| | | GROUP_CONCAT(DISTINCT batch_no ORDER BY batch_no SEPARATOR ',') batchNos |
| | | from shipping_product_detail GROUP BY shipping_info_id) spd ON spd.shipping_info_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 and slp.type = 1 |
| | | left join product_model pm on slp.product_model_id = pm.id |
| | | left join product p on pm.product_id = p.id |
| | | left join (select record_id,GROUP_CONCAT(outbound_batches SEPARATOR ',') AS outboundBatches |
| | | left join (select record_id, GROUP_CONCAT(DISTINCT outbound_batches SEPARATOR ',') AS outboundBatches |
| | | from stock_out_record |
| | | where record_type='13'and approval_status=1 |
| | | group by record_id)sor on sor.record_id= s.id |
| | |
| | | <result column="quantity" property="quantity" /> |
| | | <result column="shipping_info_id" property="shippingInfoId" /> |
| | | </resultMap> |
| | | <!-- |
| | | 发货明细(发货台账「详情」里的批号表): |
| | | 批号直接取 shipping_product_detail.batch_no(发货时选定的库存批号), |
| | | 产品名称/规格用 spd.product_model_id 关联。 |
| | | 注意:stock_inventory_id 在拆批发货时可能为空,不能再靠它取批号,否则批号、规格都会是空。 |
| | | --> |
| | | <select id="getDetail" resultType="com.ruoyi.sales.dto.ShippingProductDetailDto"> |
| | | select si.batch_no, pm.model as specification_model, p.product_name, spd.quantity as delivery_quantity |
| | | select spd.batch_no, |
| | | coalesce(pm.model, pm_inv.model) as specification_model, |
| | | coalesce(p.product_name, p_inv.product_name) as product_name, |
| | | spd.quantity as delivery_quantity |
| | | from shipping_product_detail spd |
| | | left join stock_inventory si on si.id = spd.stock_inventory_id |
| | | left join product_model pm on pm.id = si.product_model_id |
| | | left join product_model pm on pm.id = spd.product_model_id |
| | | left join product p on p.id = pm.product_id |
| | | left join product_model pm_inv on pm_inv.id = si.product_model_id |
| | | left join product p_inv on p_inv.id = pm_inv.product_id |
| | | where spd.shipping_info_id = #{id} |
| | | order by spd.id |
| | | </select> |
| | | <select id="getDateilByShippingNo" resultType="com.ruoyi.sales.dto.ShippingProductDetailDto"> |
| | | select si.batch_no, pm.model as specification_model, p.product_name, spd.quantity as delivery_quantity |
| | | from shipping_product_detail spd |
| | | left join shipping_info sp on sp.id = spd.shipping_info_id |
| | | left join stock_inventory si on si.id = spd.stock_inventory_id |
| | | left join product_model pm on pm.id = si.product_model_id |
| | | left join product p on p.id = pm.product_id |
| | | where sp.shipping_no = #{shippingNo} |
| | | select spd.batch_no, |
| | | coalesce(pm.model, pm_inv.model) as specification_model, |
| | | coalesce(p.product_name, p_inv.product_name) as product_name, |
| | | spd.quantity as delivery_quantity |
| | | from shipping_product_detail spd |
| | | left join shipping_info sp on sp.id = spd.shipping_info_id |
| | | left join stock_inventory si on si.id = spd.stock_inventory_id |
| | | left join product_model pm on pm.id = spd.product_model_id |
| | | left join product p on p.id = pm.product_id |
| | | left join product_model pm_inv on pm_inv.id = si.product_model_id |
| | | left join product p_inv on p_inv.id = pm_inv.product_id |
| | | where sp.shipping_no = #{shippingNo} |
| | | order by spd.id |
| | | </select> |
| | | |
| | | </mapper> |