From e731bf5f64d71ed93a570d505c52dc44d491175e Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期二, 22 九月 2026 11:21:33 +0800
Subject: [PATCH] fix(home): 修复首页采购品分布和出入库趋势数据统计问题
---
src/main/resources/mapper/sales/SalesLedgerProductMapper.xml | 289 ++++++++++++++++++++++++++++++++++++++++-----------------
1 files changed, 204 insertions(+), 85 deletions(-)
diff --git a/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml b/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
index 671fcf5..b23dfec 100644
--- a/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
+++ b/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
@@ -6,14 +6,190 @@
<select id="selectSalesLedgerProductList" resultType="com.ruoyi.sales.pojo.SalesLedgerProduct">
SELECT
- T1.*,
+ T1.id,
+ T1.sales_ledger_id,
+ T1.warn_num,
+ T1.quantity,
+ T1.min_stock,
+ T1.tax_rate,
+ T1.tax_inclusive_unit_price,
+ T1.tax_inclusive_total_price,
+ T1.tax_exclusive_total_price,
+ T1.invoice_type,
+ T1.type,
+ T1.product_id,
+ T1.product_model_id,
+ T1.register,
+ T1.register_date,
+ T1.approve_status,
+ T1.is_checked,
+ T1.is_production,
+ T1.create_user,
+ T1.dept_id,
+ p.product_name as product_category,
+ pm.model as specification_model,
+ pm.unit as unit,
CASE
- WHEN (IFNULL(t2.qualitity, 0) - IFNULL(t2.locked_quantity, 0)) >= IFNULL(T1.quantity, 0) 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) - IFNULL(t5.pending_approval_quantity, 0)) as no_quantity,
+ IFNULL(t3.shipped_quantity, 0) as shipped_quantity,
+ IFNULL(t6.return_quantity, 0) as return_quantity,
+ IFNULL(t4.approved_stock_in_num, 0) as approved_stock_in_num,
+ IFNULL(t5.pending_approval_quantity, 0) as pending_approval_quantity,
+ CASE
+ WHEN IFNULL(t3.shipped_quantity, 0) = 0 AND IFNULL(t5.pending_approval_quantity, 0) = 0 THEN '寰呭彂璐�'
+ WHEN IFNULL(t3.shipped_quantity, 0) = 0 AND IFNULL(t5.pending_approval_quantity, 0) > 0 THEN '瀹℃壒涓�'
+ WHEN (IFNULL(T1.quantity, 0) - IFNULL(t3.shipped_quantity, 0)) > 0 THEN '閮ㄥ垎鍙戣揣'
+ ELSE '宸插彂璐�'
+ END as shippingStatus,
+ CASE
+ WHEN T1.type != 2 THEN NULL
+ WHEN IFNULL(t4.approved_stock_in_num, 0) <= 0 THEN '寰呭叆搴�'
+ WHEN IFNULL(t4.approved_stock_in_num, 0) >= IFNULL(T1.quantity, 0) THEN '瀹屽叏鍏ュ簱'
+ ELSE '鍏ュ簱涓�'
+ END AS stock_in_approval_status
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
+ slp.id AS sales_ledger_product_id,
+ IFNULL(SUM(so.total_sale_out_num), 0) AS shipped_quantity
+ FROM sales_ledger_product slp
+ LEFT JOIN (
+ -- 閲囪喘鍙拌处鐨勩�屽凡閿�鍞暟閲忋�嶏細娌瑰搧鍑哄簱涓嶅啀浜� shipping_product_detail锛�
+ -- 鏀圭敱缁戝畾琛岀殑鍒嗘憡鏁伴噺鍙嶆帹 鈥斺�� 鍑哄簱鍗曠殑 瑙勬牸+鎵规 瀵逛笂閲囪喘鍏ュ簱鎵规锛屽嵆涓鸿閲囪喘鍙拌处鍗栨帀鐨勯噺
+ SELECT
+ pl.id AS purchase_ledger_id,
+ sir.product_model_id,
+ SUM(b.quantity) AS total_sale_out_num
+ FROM stock_out_record_sales_ledger b
+ INNER JOIN stock_out_record sor
+ ON sor.id = b.stock_out_record_id
+ AND sor.approval_status = 1
+ AND (sor.type IS NULL OR sor.type = '')
+ INNER JOIN stock_in_record sir
+ ON sir.product_model_id = sor.product_model_id
+ AND (
+ (sir.batch_no IS NOT NULL AND sir.batch_no = sor.batch_no)
+ OR (sir.batch_no IS NULL AND sor.batch_no IS NULL)
+ )
+ AND sir.approval_status = 1
+ AND sir.record_type IN ('7', '10')
+ LEFT JOIN quality_inspect qi
+ ON sir.record_type = '10'
+ AND sir.record_id = qi.id
+ LEFT JOIN purchase_ledger pl
+ ON pl.id = IF(sir.record_type = '7', sir.record_id, qi.purchase_ledger_id)
+ WHERE pl.id IS NOT NULL
+ GROUP BY pl.id, sir.product_model_id
+ ) so ON so.purchase_ledger_id = slp.sales_ledger_id
+ AND so.product_model_id = slp.product_model_id
+ WHERE slp.type = 2
+ GROUP BY slp.id
+
+ UNION ALL
+
+ -- 閿�鍞彴璐�(type=1): 宸插彂璐ч噺鐩存帴鎸夊埌璐ц褰曟眹鎬汇�傚埌璐ц褰曟湰韬氨鏄�屽疄闄呭埌浜嗗灏戙�嶇殑浜嬪疄锛�
+ -- 涓嶅啀缁� shipping_info + shipping_product_detail
+ SELECT
+ sla.sales_ledger_product_id AS sales_ledger_product_id,
+ IFNULL(SUM(sla.arrival_quantity), 0) AS shipped_quantity
+ FROM sales_ledger_arrival sla
+ WHERE sla.sales_ledger_product_id IS NOT NULL
+ GROUP BY sla.sales_ledger_product_id
+ ) t3 ON t3.sales_ledger_product_id = T1.id
+ LEFT JOIN (
+ SELECT rel.sales_ledger_product_id,
+ IFNULL(SUM(rel.stock_in_num), 0) AS approved_stock_in_num
+ FROM (
+ SELECT slp.id AS sales_ledger_product_id,
+ sir.stock_in_num
+ FROM stock_in_record sir
+ INNER JOIN sales_ledger_product slp
+ ON slp.type = 2
+ AND TRIM(sir.record_type) = '7'
+ AND sir.record_id = slp.sales_ledger_id
+ AND (
+ (sir.batch_no IS NOT NULL AND sir.batch_no LIKE CONCAT('%-', slp.id))
+ OR (sir.batch_no IS NULL AND sir.product_model_id = slp.product_model_id)
+ )
+ WHERE sir.approval_status = 1
+
+ UNION ALL
+
+ SELECT slp.id AS sales_ledger_product_id,
+ sir.stock_in_num
+ FROM stock_in_record sir
+ INNER JOIN quality_inspect qi
+ ON TRIM(sir.record_type) = '10'
+ AND sir.record_id = qi.id
+ INNER JOIN sales_ledger_product slp
+ ON slp.type = 2
+ AND slp.sales_ledger_id = qi.purchase_ledger_id
+ AND slp.product_model_id = qi.product_model_id
+ WHERE sir.approval_status = 1
+ ) rel
+ GROUP BY rel.sales_ledger_product_id
+ ) t4 ON t4.sales_ledger_product_id = T1.id
+ LEFT JOIN (
+ SELECT si.sales_ledger_product_id, IFNULL(SUM(spd.quantity), 0) as pending_approval_quantity
+ FROM shipping_info si
+ LEFT JOIN shipping_product_detail spd ON si.id = spd.shipping_info_id
+ WHERE si.status IN ('寰呭鏍�', '瀹℃牳涓�')
+ OR (si.status = '瀹℃牳閫氳繃' AND NOT EXISTS (
+ SELECT 1 FROM stock_out_record sor
+ WHERE sor.record_id = si.id
+ AND TRIM(sor.record_type) = '13'
+ AND sor.approval_status = 1
+ ))
+ GROUP BY si.sales_ledger_product_id
+
+ UNION ALL
+
+ -- 娌瑰搧鍑哄簱寰呭鎵� / 寰呯‘璁わ細杩樻病鍒拌揣锛岀畻銆屽鎵逛腑銆嶏紝涓� t3 鐨勫埌璐ч噺浜掕ˉ銆�
+ -- 鍙湁缁戝畾浜嗗崠娌瑰彴璐︾殑鍑哄簱鎵嶅崰杩欐潯鍒嗘敮锛堜唬鍌ㄣ�佸瀛樹笉浜х粦瀹氳锛�
+ SELECT b.sales_ledger_product_id AS sales_ledger_product_id,
+ IFNULL(SUM(b.quantity), 0) AS pending_approval_quantity
+ FROM stock_out_record_sales_ledger b
+ INNER JOIN stock_out_record sor
+ ON sor.id = b.stock_out_record_id
+ AND sor.approval_status IN (0, 3)
+ AND (sor.type IS NULL OR sor.type = '')
+ WHERE b.sales_ledger_product_id IS NOT NULL
+ GROUP BY b.sales_ledger_product_id
+ ) t5 ON t5.sales_ledger_product_id = T1.id
+ LEFT JOIN (
+ SELECT
+ prop.sales_ledger_product_id,
+ IFNULL(SUM(prop.return_quantity), 0) AS return_quantity
+ FROM purchase_return_order_products prop
+ LEFT JOIN purchase_return_orders pro
+ ON pro.id = prop.purchase_return_order_id
+ GROUP BY prop.sales_ledger_product_id
+
+ UNION ALL
+
+ SELECT
+ si.sales_ledger_product_id,
+ IFNULL(SUM(rsp.num), 0) AS return_quantity
+ FROM return_sale_product rsp
+ INNER JOIN return_management rm ON rm.id = rsp.return_management_id
+ INNER JOIN shipping_info si ON si.id = rm.shipping_id
+ INNER JOIN stock_in_record sir ON sir.record_id = rsp.id
+ AND TRIM(sir.record_type) IN ('14','15')
+ AND sir.approval_status = 1
+ WHERE si.sales_ledger_product_id IS NOT NULL
+ GROUP BY si.sales_ledger_product_id
+ ) t6 ON t6.sales_ledger_product_id = T1.id
+ left join product_model pm ON T1.product_model_id = pm.id
+ left join product p ON pm.product_id = p.id
<where>
<if test="salesLedgerProduct.salesLedgerId != null">
AND T1.sales_ledger_id = #{salesLedgerProduct.salesLedgerId}
@@ -29,78 +205,17 @@
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}
-
-
- </select>
- <select id="listPage" resultType="com.ruoyi.sales.dto.SalesLedgerProductDto">
- select slp.*,
- sl.project_name,
- sl.customer_name,
- sl.sales_contract_no,
- sl.customer_contract_no
- from sales_ledger_product slp
- left join sales_ledger sl on slp.sales_ledger_id = sl.id
- <where>
- slp.type = 1
- <if test="req.salesContractNo != null and req.salesContractNo != '' ">
- AND sl.sales_contract_no like concat('%',#{req.salesContractNo},'%')
- </if>
- <if test="req.customerContractNo != null and req.customerContractNo != '' ">
- AND sl.customer_contract_no like concat('%',#{req.customerContractNo},'%')
- </if>
- <if test="req.projectName != null and req.projectName != '' ">
- AND sl.project_name like concat('%',#{req.projectName},'%')
- </if>
- <if test="req.customerName != null and req.customerName != '' ">
- AND sl.customer_name like concat('%',#{req.customerName},'%')
- </if>
- <if test="req.productCategory != null and req.productCategory != '' ">
- AND slp.product_category like concat('%',#{req.productCategory},'%')
- </if>
- <if test="req.status != null and req.status ">
- AND slp.pending_invoice_total > 0
- </if>
- </where>
- order by slp.register_date desc
- </select>
- <select id="listPagePurchaseLedger" resultType="com.ruoyi.sales.dto.SalesLedgerProductDto">
- select slp.*,
- sl.project_name,
- sl.supplier_name,
- sl.supplier_id,
- sl.sales_contract_no,
- sl.purchase_contract_number
- from sales_ledger_product slp
- left join purchase_ledger sl on slp.sales_ledger_id = sl.id
- <where>
- slp.type = 2
- <if test="req.purchaseContractNumber != null and req.purchaseContractNumber != '' ">
- AND sl.purchase_contract_number like concat('%',#{req.purchaseContractNumber},'%')
- </if>
- <if test="req.approvalStatus != null and req.approvalStatus != ''">
- and sl.approval_status = #{req.approvalStatus}
- </if>
- <if test="req.customerContractNo != null and req.customerContractNo != '' ">
- AND sl.customer_contract_no like concat('%',#{req.customerContractNo},'%')
- </if>
- <if test="req.projectName != null and req.projectName != '' ">
- AND sl.project_name like concat('%',#{req.projectName},'%')
- </if>
- <if test="req.customerName != null and req.customerName != '' ">
- AND sl.customer_name like concat('%',#{req.customerName},'%')
- </if>
- <if test="req.productCategory != null and req.productCategory != '' ">
- AND slp.product_category like concat('%',#{req.productCategory},'%')
- </if>
- <if test="req.status != null and req.status ">
- AND slp.pending_tickets_total > 0
- </if>
- </where>
- order by slp.register_date desc
+ and slp.id is not null
+ order by slp.id desc
+ limit 1
</select>
<select id="procurementBusinessSummaryListPage"
resultType="com.ruoyi.purchase.dto.ProcurementBusinessSummaryDto">
@@ -114,10 +229,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 +246,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 +261,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
@@ -177,19 +294,21 @@
LIMIT 5
</select>
+ <!-- 閲囪喘鍝佸垎甯冿細涓庛�屽悇浜у搧閿�鍞噾棰濆垎鏋愩�嶅悓鍙e緞锛岀洿鎺ユ寜閲囪喘鏄庣粏涓婄殑鍝佺被鑱氬悎銆�
+ 涓嶈兘鍐嶆寜浜у搧鏍戙�屽師鏉愭枡銆嶅瓙鏍戣繃婊� 鈥斺�� 閲囪喘鍙拌处瀹為檯鐧昏鐨勫搧绫伙紙鏌存补銆佺伃鐏櫒锛夊垎鍒寕鍦�
+ 銆屾垚鍝�/鎴愬搧娌广�嶃�岃澶囪鏂�/鍏叡瀹夊叏璁惧銆嶄笅锛屾寜瀛愭爲杩囨护浼氭亽涓虹┖ -->
<select id="selectRawMaterialPurchaseAnalysis" resultType="java.util.Map">
SELECT
- pr.product_name AS name,
+ slp.product_category AS name,
SUM( slp.tax_inclusive_total_price ) AS value
FROM
sales_ledger_product slp
- JOIN product pr ON slp.product_id = pr.id
WHERE
slp.type = 2
- AND pr.parent_id = ( SELECT id FROM product WHERE product_name = '鍘熸潗鏂�' )
+ AND slp.product_category IS NOT NULL
+ AND slp.product_category != ''
GROUP BY
- pr.id,
- pr.product_name
+ slp.product_category
ORDER BY
value DESC
LIMIT 5
--
Gitblit v1.9.3