From bb302afba7c479d313d513370ea7dd6ac4da3c21 Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期五, 03 七月 2026 15:39:46 +0800
Subject: [PATCH] Merge branch 'dev_New_pro' of http://114.132.189.42:9002/r/product-inventory-management-after into dev_New_pro

---
 src/main/java/com/ruoyi/procurementrecord/service/impl/ReturnManagementServiceImpl.java |   17 --------
 src/main/resources/mapper/procurementrecord/ReturnManagementMapper.xml                  |    4 +-
 src/main/resources/mapper/sales/SalesLedgerProductMapper.xml                            |   14 +++++++
 src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java                |   57 ++++++++++++++++++++++++++++
 4 files changed, 73 insertions(+), 19 deletions(-)

diff --git a/src/main/java/com/ruoyi/procurementrecord/service/impl/ReturnManagementServiceImpl.java b/src/main/java/com/ruoyi/procurementrecord/service/impl/ReturnManagementServiceImpl.java
index a6e3d6f..6420622 100644
--- a/src/main/java/com/ruoyi/procurementrecord/service/impl/ReturnManagementServiceImpl.java
+++ b/src/main/java/com/ruoyi/procurementrecord/service/impl/ReturnManagementServiceImpl.java
@@ -20,8 +20,6 @@
 import com.ruoyi.procurementrecord.service.ReturnManagementService;
 import com.ruoyi.procurementrecord.service.ReturnSaleProductService;
 import com.ruoyi.procurementrecord.utils.StockUtils;
-import com.ruoyi.sales.mapper.SalesLedgerMapper;
-import com.ruoyi.sales.pojo.SalesLedger;
 import com.ruoyi.sales.pojo.ShippingInfo;
 import com.ruoyi.sales.service.ShippingInfoService;
 import lombok.RequiredArgsConstructor;
@@ -47,7 +45,6 @@
     private final ReturnManagementMapper returnManagementMapper;
     private final ReturnSaleProductService returnSaleProductService;
     private final ShippingInfoService shippingInfoService;
-    private final SalesLedgerMapper salesLedgerMapper;
     private final SalesRefundAmountOrderService salesRefundAmountOrderService;
     private final StockUtils stockUtils;
 
@@ -132,20 +129,6 @@
         }).collect(Collectors.toList());
         returnSaleProductService.updateBatchById(updateList);
 
-        // 3. 鏇存柊閿�鍞彴璐︾殑瀹為檯鍚堝悓閲戦
-        if (byId.getShippingId() != null) {
-            ShippingInfo shippingInfo = shippingInfoService.getById(byId.getShippingId());
-            if (shippingInfo != null && shippingInfo.getSalesLedgerId() != null) {
-                SalesLedger salesLedger = salesLedgerMapper.selectById(shippingInfo.getSalesLedgerId());
-                if (salesLedger != null && salesLedger.getContractAmount() != null) {
-                    BigDecimal baseAmount = salesLedger.getNetContractAmount() != null
-                            ? salesLedger.getNetContractAmount()
-                            : salesLedger.getContractAmount();
-                    salesLedger.setNetContractAmount(baseAmount.subtract(totalRefund));
-                    salesLedgerMapper.updateById(salesLedger);
-                }
-            }
-        }
         return true;
     }
 
diff --git a/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java b/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
index 9ac5410..00161b5 100644
--- a/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
+++ b/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
@@ -21,6 +21,14 @@
 import com.ruoyi.stock.execl.StockInRecordExportData;
 import com.ruoyi.production.mapper.ProductionOrderPickMapper;
 import com.ruoyi.production.pojo.ProductionOrderPick;
+import com.ruoyi.procurementrecord.mapper.ReturnManagementMapper;
+import com.ruoyi.procurementrecord.mapper.ReturnSaleProductMapper;
+import com.ruoyi.procurementrecord.pojo.ReturnManagement;
+import com.ruoyi.procurementrecord.pojo.ReturnSaleProduct;
+import com.ruoyi.sales.mapper.SalesLedgerMapper;
+import com.ruoyi.sales.mapper.ShippingInfoMapper;
+import com.ruoyi.sales.pojo.SalesLedger;
+import com.ruoyi.sales.pojo.ShippingInfo;
 import com.ruoyi.stock.mapper.StockInRecordMapper;
 import com.ruoyi.stock.mapper.StockInventoryMapper;
 import com.ruoyi.stock.mapper.StockUninventoryMapper;
@@ -45,6 +53,10 @@
     private StockInventoryMapper stockInventoryMapper;
     private StockUninventoryMapper stockUninventoryMapper;
     private ProductionOrderPickMapper productionOrderPickMapper;
+    private ReturnSaleProductMapper returnSaleProductMapper;
+    private ReturnManagementMapper returnManagementMapper;
+    private ShippingInfoMapper shippingInfoMapper;
+    private SalesLedgerMapper salesLedgerMapper;
 
     @Override
     public IPage<StockInRecordDto> listPage(Page page, StockInRecordDto stockInRecordDto) {
@@ -285,6 +297,8 @@
                         stockUninventoryMapper.updateAddStockUnInventory(stockUninventoryDto);
                     }
                 }
+                // 閿�鍞��璐у叆搴�:鎵e噺閿�鍞彴璐﹀疄闄呭悎鍚岄噾棰�(閫�璐х敓鏁�)
+                handleSalesReturnRefund(stockInRecord);
             }
         }
         return ids.size();
@@ -335,4 +349,47 @@
 
         return ids.size();
     }
+
+    /**
+     * 閿�鍞��璐у叆搴撳鎵归�氳繃鏃�,鎵e噺瀵瑰簲閿�鍞彴璐︾殑瀹為檯鍚堝悓閲戦銆�
+     * 鍏宠仈閾�:stock_in_record.record_id = return_sale_product.id
+     *        鈫� return_management.shipping_id = shipping_info.id
+     *        鈫� shipping_info.sales_ledger_id = sales_ledger.id
+     * 鍙鐞� record_type 涓� 14(鍚堟牸)鎴� 15(涓嶅悎鏍�)鐨勯攢鍞��璐у叆搴撱��
+     */
+    private void handleSalesReturnRefund(StockInRecord stockInRecord) {
+        String recordType = stockInRecord.getRecordType();
+        if (!StockInQualifiedRecordTypeEnum.RETURN_HE_IN.getCode().equals(recordType)
+                && !StockInQualifiedRecordTypeEnum.RETURN_UNSTOCK_IN.getCode().equals(recordType)) {
+            return;
+        }
+        if (stockInRecord.getRecordId() == null) {
+            return;
+        }
+        ReturnSaleProduct rsp = returnSaleProductMapper.selectById(stockInRecord.getRecordId());
+        if (rsp == null || rsp.getReturnManagementId() == null) {
+            return;
+        }
+        ReturnManagement rm = returnManagementMapper.selectById(rsp.getReturnManagementId());
+        if (rm == null || rm.getShippingId() == null) {
+            return;
+        }
+        ShippingInfo shippingInfo = shippingInfoMapper.selectById(rm.getShippingId());
+        if (shippingInfo == null || shippingInfo.getSalesLedgerId() == null) {
+            return;
+        }
+        SalesLedger salesLedger = salesLedgerMapper.selectById(shippingInfo.getSalesLedgerId());
+        if (salesLedger == null || salesLedger.getContractAmount() == null) {
+            return;
+        }
+        BigDecimal refund = rsp.getAmount() != null ? rsp.getAmount() : BigDecimal.ZERO;
+        if (refund.compareTo(BigDecimal.ZERO) == 0) {
+            return;
+        }
+        BigDecimal baseAmount = salesLedger.getNetContractAmount() != null
+                ? salesLedger.getNetContractAmount()
+                : salesLedger.getContractAmount();
+        salesLedger.setNetContractAmount(baseAmount.subtract(refund));
+        salesLedgerMapper.updateById(salesLedger);
+    }
 }
diff --git a/src/main/resources/mapper/procurementrecord/ReturnManagementMapper.xml b/src/main/resources/mapper/procurementrecord/ReturnManagementMapper.xml
index c0e35da..6231005 100644
--- a/src/main/resources/mapper/procurementrecord/ReturnManagementMapper.xml
+++ b/src/main/resources/mapper/procurementrecord/ReturnManagementMapper.xml
@@ -14,9 +14,9 @@
                sl.sales_contract_no,
                sl.salesman,
                 CASE
+                    WHEN tempA.total_approval = 0 OR tempA.total_approval IS NULL THEN 0
                     WHEN tempA.approval_sum = tempA.total_approval THEN 1
-                    WHEN tempA.approval_sum &lt; tempA.total_approval AND tempA.approval_sum !=0 THEN 2
-                    ELSE 0
+                    ELSE 2
                 END AS stock_in_approval_status
         from return_management rm
                  left join shipping_info si on rm.shipping_id = si.id
diff --git a/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml b/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
index 074df74..9e6839f 100644
--- a/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
+++ b/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
@@ -167,6 +167,20 @@
             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

--
Gitblit v1.9.3