From 233834d6c82866b5530ab4408599b5ca609f4ab7 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期一, 03 八月 2026 17:00:13 +0800
Subject: [PATCH] feat: 发货库存返回源单号

---
 src/main/resources/mapper/stock/StockInventoryMapper.xml |  134 +++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 117 insertions(+), 17 deletions(-)

diff --git a/src/main/resources/mapper/stock/StockInventoryMapper.xml b/src/main/resources/mapper/stock/StockInventoryMapper.xml
index 8d20ee3..f79b59e 100644
--- a/src/main/resources/mapper/stock/StockInventoryMapper.xml
+++ b/src/main/resources/mapper/stock/StockInventoryMapper.xml
@@ -367,6 +367,7 @@
                 and sir.create_time &lt;= #{ew.endMonth}
             </if>
         </where>
+        order by sir.create_time desc
     </select>
 
     <select id="stockInAndOutRecord" resultType="com.ruoyi.stock.dto.StockInventoryDto">
@@ -513,28 +514,127 @@
         order by si.product_model_id, si.batch_no
     </select>
     <select id="getByModelId" resultType="com.ruoyi.stock.dto.StockInventoryDto">
-        select si.id, si.batch_no, si.locked_quantity, (si.qualitity - IFNULL(sd.qualitity, 0)) as qualitity,
-               p.product_name, pm.model, pm.unit
+        select si.id,
+               si.batch_no,
+               si.locked_quantity,
+               (si.qualitity - IFNULL(sd.qualitity, 0)) as qualitity,
+               p.product_name,
+               pm.model,
+               pm.unit,
+               order_source.salesContractNo,
+               order_source.sourceOrderNo
         from stock_inventory si
+                 left join (select spd.stock_inventory_id, sum(spd.quantity) as qualitity
+                            from shipping_product_detail spd
+                            where exists (select 1
+                                          from stock_out_record sor
+                                          where sor.record_id = spd.shipping_info_id
+                                            and sor.record_type = '13'
+                                            and sor.type = '0'
+                                            and sor.approval_status in (0, 3)
+                                            and sor.product_model_id = spd.product_model_id
+                                            and (sor.batch_no = spd.batch_no or
+                                                 (sor.batch_no is null and spd.batch_no is null)))
+                            group by spd.stock_inventory_id) as sd on sd.stock_inventory_id = si.id
                  left join (
-                    select spd.stock_inventory_id, sum(spd.quantity) as qualitity
-                    from shipping_product_detail spd
-                    where exists (
-                        select 1
-                        from stock_out_record sor
-                        where sor.record_id = spd.shipping_info_id
-                          and sor.record_type = '13'
-                          and sor.type = '0'
-                          and sor.approval_status in (0, 3)
-                          and sor.product_model_id = spd.product_model_id
-                          and (sor.batch_no = spd.batch_no or (sor.batch_no is null and spd.batch_no is null))
-                    )
-                    group by spd.stock_inventory_id
-                 ) as sd on sd.stock_inventory_id = si.id
+                    select source.product_model_id,
+                           source.batch_no,
+                           group_concat(distinct source.salesContractNo order by source.salesContractNo separator ',') as salesContractNo,
+                           group_concat(distinct source.sourceOrderNo order by source.sourceOrderNo separator ',') as sourceOrderNo
+                    from (
+                        select sir.product_model_id,
+                               sir.batch_no,
+                               case
+                                   when TRIM(sir.record_type) = '7' then COALESCE(pl_by_product.sales_contract_no, pl_direct.sales_contract_no)
+                                   when TRIM(sir.record_type) = '10' then pl_by_quality.sales_contract_no
+                                   when TRIM(sir.record_type) in ('14', '15') then sl_return.sales_contract_no
+                                   when TRIM(sir.record_type) in ('2', '5') then po_sales_main.sales_contract_no
+                                   when TRIM(sir.record_type) = '6' then po_sales_qi.sales_contract_no
+                                   when TRIM(sir.record_type) in ('4', '11') then po_sales_uq.sales_contract_no
+                                   when TRIM(sir.record_type) in ('20', '22') then po_sales_pick.sales_contract_no
+                                   else null
+                               end as salesContractNo,
+                               case
+                                   when TRIM(sir.record_type) = '7' then COALESCE(pl_by_product.sales_contract_no, pl_direct.sales_contract_no, pl_by_product.purchase_contract_number, pl_direct.purchase_contract_number)
+                                   when TRIM(sir.record_type) = '10' then COALESCE(pl_by_quality.sales_contract_no, pl_by_quality.purchase_contract_number)
+                                   when TRIM(sir.record_type) in ('14', '15') then sl_return.sales_contract_no
+                                   when TRIM(sir.record_type) in ('2', '5') then COALESCE(po_sales_main.sales_contract_no, po_main.nps_no)
+                                   when TRIM(sir.record_type) = '6' then COALESCE(po_sales_qi.sales_contract_no, po_qi.nps_no)
+                                   when TRIM(sir.record_type) in ('4', '11') then COALESCE(po_sales_uq.sales_contract_no, po_uq.nps_no)
+                                   when TRIM(sir.record_type) in ('20', '22') then COALESCE(po_sales_pick.sales_contract_no, po_pick.nps_no)
+                                   else null
+                               end as sourceOrderNo
+                        from stock_in_record sir
+                                 left join quality_inspect as qi_purchase on TRIM(sir.record_type) = '10' and sir.record_id = qi_purchase.id
+                                 left join sales_ledger_product as slp on TRIM(sir.record_type) = '7' and slp.id = sir.record_id and slp.type = 2
+                                 left join purchase_ledger as pl_by_product on TRIM(sir.record_type) = '7' and pl_by_product.id = slp.sales_ledger_id
+                                 left join purchase_ledger as pl_direct on TRIM(sir.record_type) = '7' and pl_direct.id = sir.record_id
+                                 left join purchase_ledger as pl_by_quality on TRIM(sir.record_type) = '10' and pl_by_quality.id = qi_purchase.purchase_ledger_id
+                                 left join production_product_main as ppm_main on TRIM(sir.record_type) in ('2', '5') and sir.record_id = ppm_main.id
+                                 left join production_operation_task as pot_main on ppm_main.production_operation_task_id = pot_main.id
+                                 left join production_order as po_main on pot_main.production_order_id = po_main.id
+                                 left join (
+                                     select po2.id as production_order_id,
+                                            group_concat(distinct sl2.sales_contract_no order by sl2.sales_contract_no separator ',') as sales_contract_no
+                                     from production_order po2
+                                              left join production_plan pp2
+                                                        on find_in_set(pp2.id, replace(replace(replace(po2.production_plan_ids, '[', ''), ']', ''), ' ', '')) > 0
+                                              left join sales_ledger sl2 on sl2.id = pp2.sales_ledger_id
+                                     group by po2.id
+                                 ) as po_sales_main on po_sales_main.production_order_id = po_main.id
+                                 left join quality_inspect as qi_prod on TRIM(sir.record_type) = '6' and sir.record_id = qi_prod.id
+                                 left join production_product_main as ppm_qi on qi_prod.product_main_id = ppm_qi.id
+                                 left join production_operation_task as pot_qi on ppm_qi.production_operation_task_id = pot_qi.id
+                                 left join production_order as po_qi on pot_qi.production_order_id = po_qi.id
+                                 left join (
+                                     select po2.id as production_order_id,
+                                            group_concat(distinct sl2.sales_contract_no order by sl2.sales_contract_no separator ',') as sales_contract_no
+                                     from production_order po2
+                                              left join production_plan pp2
+                                                        on find_in_set(pp2.id, replace(replace(replace(po2.production_plan_ids, '[', ''), ']', ''), ' ', '')) > 0
+                                              left join sales_ledger sl2 on sl2.id = pp2.sales_ledger_id
+                                     group by po2.id
+                                 ) as po_sales_qi on po_sales_qi.production_order_id = po_qi.id
+                                 left join quality_unqualified as qu on TRIM(sir.record_type) in ('4', '11') and sir.record_id = qu.id
+                                 left join quality_inspect as qi_uq on qu.inspect_id = qi_uq.id
+                                 left join production_product_main as ppm_uq on qi_uq.product_main_id = ppm_uq.id
+                                 left join production_operation_task as pot_uq on ppm_uq.production_operation_task_id = pot_uq.id
+                                 left join production_order as po_uq on pot_uq.production_order_id = po_uq.id
+                                 left join (
+                                     select po2.id as production_order_id,
+                                            group_concat(distinct sl2.sales_contract_no order by sl2.sales_contract_no separator ',') as sales_contract_no
+                                     from production_order po2
+                                              left join production_plan pp2
+                                                        on find_in_set(pp2.id, replace(replace(replace(po2.production_plan_ids, '[', ''), ']', ''), ' ', '')) > 0
+                                              left join sales_ledger sl2 on sl2.id = pp2.sales_ledger_id
+                                     group by po2.id
+                                 ) as po_sales_uq on po_sales_uq.production_order_id = po_uq.id
+                                 left join production_order_pick as pop on TRIM(sir.record_type) in ('20', '22') and sir.record_id = pop.id
+                                 left join production_order as po_pick on pop.production_order_id = po_pick.id
+                                 left join (
+                                     select po2.id as production_order_id,
+                                            group_concat(distinct sl2.sales_contract_no order by sl2.sales_contract_no separator ',') as sales_contract_no
+                                     from production_order po2
+                                              left join production_plan pp2
+                                                        on find_in_set(pp2.id, replace(replace(replace(po2.production_plan_ids, '[', ''), ']', ''), ' ', '')) > 0
+                                              left join sales_ledger sl2 on sl2.id = pp2.sales_ledger_id
+                                     group by po2.id
+                                 ) as po_sales_pick on po_sales_pick.production_order_id = po_pick.id
+                                 left join return_sale_product as rsp on TRIM(sir.record_type) in ('14', '15') and sir.record_id = rsp.id
+                                 left join return_management as rm on rsp.return_management_id = rm.id
+                                 left join shipping_info as si_return on rm.shipping_id = si_return.id
+                                 left join sales_ledger as sl_return on si_return.sales_ledger_id = sl_return.id
+                        where sir.product_model_id = #{productModelId}
+                          and sir.type = '0'
+                          and sir.approval_status = 1
+                    ) source
+                    group by source.product_model_id, source.batch_no
+                 ) as order_source on order_source.product_model_id = si.product_model_id
+                    and (order_source.batch_no = si.batch_no or (order_source.batch_no is null and si.batch_no is null))
                  left join product_model pm on si.product_model_id = pm.id
                  left join product p on pm.product_id = p.id
         where si.product_model_id = #{productModelId}
-        and si.qualitity > IFNULL(sd.qualitity, 0)
+          and si.qualitity > IFNULL(sd.qualitity, 0)
     </select>
 
     <select id="getBatchNoQty" resultType="com.ruoyi.stock.dto.StockInventoryDto">

--
Gitblit v1.9.3