From bd02d873d7bbcae36a2a1262d921f2fc6b0cce09 Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期三, 06 五月 2026 14:33:41 +0800
Subject: [PATCH] feat(sales): 新增发货台账详情功能并优化库存管理

---
 src/main/resources/mapper/sales/SalesLedgerProductMapper.xml |   64 +++++++++++++++++++++++---------
 1 files changed, 46 insertions(+), 18 deletions(-)

diff --git a/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml b/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
index 2a1b94a..49689a1 100644
--- a/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
+++ b/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
@@ -8,31 +8,53 @@
         SELECT
         T1.*,
         CASE
-        WHEN t2.qualitity > T1.quantity THEN 1
+        WHEN (IFNULL(t2.qualitity, 0) - IFNULL(t2.locked_quantity, 0)) >0 THEN 1
         ELSE 0
-        END as has_sufficient_stock
+        END as has_sufficient_stock,
+        (IFNULL(T1.quantity, 0) - IFNULL(t3.shipped_quantity, 0)) as no_quantity,
+        CASE
+        WHEN (IFNULL(T1.quantity, 0) - IFNULL(t3.shipped_quantity, 0)) > 0 THEN '寰呭彂璐�'
+        ELSE '宸插彂璐�'
+        END as shippingStatus
         FROM
         sales_ledger_product T1
-        LEFT JOIN stock_inventory t2 ON T1.product_model_id = t2.product_model_id
+        LEFT JOIN (
+        SELECT product_model_id, SUM(qualitity) as qualitity, SUM(locked_quantity) as locked_quantity
+        FROM stock_inventory
+        GROUP BY product_model_id
+        ) t2 ON T1.product_model_id = t2.product_model_id
+        LEFT JOIN (
+        SELECT sales_ledger_product_id, IFNULL(SUM(spd.quantity), 0) as shipped_quantity
+        FROM shipping_info si
+        LEFT JOIN shipping_product_detail spd ON si.id = spd.shipping_info_id
+        GROUP BY sales_ledger_product_id
+        ) t3 ON t3.sales_ledger_product_id = T1.id
         <where>
-            1=1
-            <if test="salesLedgerProduct.salesLedgerId != null and salesLedgerProduct.salesLedgerId != '' ">
+            <if test="salesLedgerProduct.salesLedgerId != null">
                 AND T1.sales_ledger_id = #{salesLedgerProduct.salesLedgerId}
             </if>
-            <if test="salesLedgerProduct.type != null and salesLedgerProduct.type != '' ">
+            <if test="salesLedgerProduct.type != null">
                 AND T1.type = #{salesLedgerProduct.type}
             </if>
         </where>
         ORDER BY T1.register_date DESC
     </select>
+
     <select id="selectSalesLedgerProductByMainId" resultType="com.ruoyi.sales.pojo.SalesLedgerProduct">
         select slp.*
         from quality_inspect qi
                  left join production_product_main ppm on qi.product_main_id = ppm.id
-                 left join product_work_order pwo on ppm.work_order_id = pwo.id
-                 left join product_order po on pwo.product_order_id = po.id
-                 left join sales_ledger_product slp on po.sale_ledger_product_id = slp.id
+                 left join production_operation_task pot on ppm.production_operation_task_id = pot.id
+                 left join production_order po on pot.production_order_id = po.id
+                 left join production_plan pp_main
+                           on find_in_set(pp_main.id, replace(replace(replace(po.production_plan_ids, '[', ''), ']', ''), ' ', '')) > 0
+                 left join sales_ledger_product slp on slp.sales_ledger_id = pp_main.sales_ledger_id
+            and slp.product_model_id = po.product_model_id
+            and slp.type = 1
         where qi.product_main_id = #{productMainId}
+          and slp.id is not null
+        order by slp.id desc
+        limit 1
 
 
     </select>
@@ -97,7 +119,7 @@
                 AND slp.product_category like concat('%',#{req.productCategory},'%')
             </if>
             <if test="req.status != null and req.status ">
-                AND slp.pending_invoice_total &gt; 0
+                AND slp.pending_tickets_total &gt; 0
             </if>
         </where>
         order by slp.register_date desc
@@ -114,10 +136,14 @@
         <!-- 骞冲潎鍗曚环 = 鎬婚噰璐噾棰�/鎬婚噰璐暟閲忥紝淇濈暀2浣嶅皬鏁帮紝閬垮厤闄�0 -->
         ROUND(IF(SUM(slp.quantity) = 0, 0, SUM(slp.tax_inclusive_total_price) / SUM(slp.quantity)), 2) AS averagePrice,
         <!-- 璇ヤ骇鍝佸ぇ绫讳笅鏈�鍚庝竴涓綍鍏ユ棩鏈燂紙鍙栧彴璐︿富琛ㄧ殑entry_date锛� -->
-        MAX(sl.entry_date) AS entryDate
+        MAX(sl.entry_date) AS entryDate,
+        COALESCE(NULLIF(SUM(t1.return_quantity), 0), 0) AS return_quantity,
+        COALESCE(SUM(t2.total_amount), 0) AS return_amount
         FROM sales_ledger_product slp
         <!-- 鍏宠仈鍙拌处涓昏〃锛氳幏鍙栧綍鍏ユ棩鏈焑ntry_date -->
         LEFT JOIN purchase_ledger sl ON slp.sales_ledger_id = sl.id
+        left join purchase_return_order_products as t1 on t1.sales_ledger_product_id = slp.id
+        left join purchase_return_orders as t2 on t2.id = t1.purchase_return_order_id
         WHERE slp.type = 2 <!-- 鍥哄畾绛涢�夛細閲囪喘鍙拌处锛坱ype=2锛� -->
         <!-- 閲囪喘鏃ユ湡绛涢�夛細鍙�夋潯浠� -->
         <if test="req.entryDateStart != null and req.entryDateEnd != null">
@@ -127,9 +153,7 @@
         <if test="req.productCategory != null and req.productCategory != ''">
             AND slp.product_category = #{req.productCategory}
         </if>
-        <!-- 鎸変骇鍝佸ぇ绫诲垎缁勮仛鍚� -->
-        GROUP BY slp.product_category
-        <!-- 鎸変骇鍝佸ぇ绫绘帓搴� -->
+        GROUP BY slp.product_category, slp.specification_model, sl.supplier_name
         ORDER BY slp.product_category
     </select>
     <select id="selectProductBomStructure" resultType="com.ruoyi.sales.dto.LossProductModelDto">
@@ -144,7 +168,7 @@
                      p.product_name,
                      ps.unit_quantity * slp.quantity AS single_quantity
               FROM sales_ledger sl
-                       LEFT JOIN sales_ledger_product slp ON slp.sales_ledger_id = sl.id
+                       LEFT JOIN sales_ledger_product slp ON slp.sales_ledger_id = sl.id and slp.type = 1
                        LEFT JOIN product_model pm ON pm.id = slp.product_model_id
                        LEFT JOIN product_bom pb ON pb.product_model_id = pm.id
                        LEFT JOIN product_structure ps ON pb.id = ps.bom_id
@@ -196,11 +220,15 @@
     </select>
 
     <select id="selectProductCountByTypeAndDate" resultType="int">
-        SELECT COUNT(*)
+        SELECT IFNULL(COUNT(*), 0)
         FROM sales_ledger_product
         WHERE type = #{type}
-        AND register_date &gt;= #{startDate}
-        AND register_date &lt;= #{endDate}
+        <if test="startDate != null">
+            AND register_date &gt;= #{startDate}
+        </if>
+        <if test="endDate != null">
+            AND register_date &lt;= #{endDate}
+        </if>
     </select>
 
     <select id="selectRawMaterialExpense" resultType="java.math.BigDecimal">

--
Gitblit v1.9.3