From 3066a8f5c1f475997bfe23857ba4bae9395900c4 Mon Sep 17 00:00:00 2001
From: yuan <123@>
Date: 星期四, 13 八月 2026 10:19:24 +0800
Subject: [PATCH] fix: 领料功能修改,移除订单领料,添加工序领料功能,支持按工序查询物料与已领料记录

---
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java |   96 +++++++++++++++++++++--------------------------
 1 files changed, 43 insertions(+), 53 deletions(-)

diff --git a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
index fa60c68..4dae74c 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
@@ -4,6 +4,7 @@
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.utils.AmountUtils;
 import com.ruoyi.framework.web.domain.R;
 import com.ruoyi.procurementrecord.utils.StockUtils;
 import com.ruoyi.production.mapper.*;
@@ -35,7 +36,6 @@
 import java.math.RoundingMode;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
-import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
 import java.util.Arrays;
 import java.util.List;
@@ -90,7 +90,9 @@
 
     @Override
     public SalesLedgerProduct selectSalesLedgerProductById(Long id) {
-        return salesLedgerProductMapper.selectById(id);
+        SalesLedgerProduct product = salesLedgerProductMapper.selectById(id);
+        AmountUtils.normalizeSalesLedgerProduct(product);
+        return product;
     }
 
     @Override
@@ -98,29 +100,17 @@
         List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectSalesLedgerProductList(salesLedgerProduct);
         if(!CollectionUtils.isEmpty(salesLedgerProducts)){
             salesLedgerProducts.forEach(item -> {
-                // 鍙戣揣淇℃伅锛堝彇鏈�鏂颁竴鏉★紝鍙煡璇㈠鏍搁�氳繃鎴栧凡鍙戣揣鐨勮褰曪級
-                ShippingInfo latestShippingInfo = shippingInfoMapper.selectOne(new LambdaQueryWrapper<ShippingInfo>()
+                AmountUtils.normalizeSalesLedgerProduct(item);
+                // 鍙戣揣淇℃伅
+                ShippingInfo shippingInfo = shippingInfoMapper.selectOne(new LambdaQueryWrapper<ShippingInfo>()
                         .eq(ShippingInfo::getSalesLedgerProductId, item.getId())
-                        .in(ShippingInfo::getStatus, "瀹℃牳閫氳繃", "宸插彂璐�")
                         .orderByDesc(ShippingInfo::getCreateTime)
                         .last("limit 1"));
-                if(latestShippingInfo != null){
-                    item.setShippingDate(latestShippingInfo.getShippingDate());
-                    item.setExpressCompany(latestShippingInfo.getExpressCompany());
-                    item.setExpressNumber(latestShippingInfo.getExpressNumber());
-                }
-                // 杞︾墝鍙凤細鍙栨墍鏈夊鏍搁�氳繃鎴栧凡鍙戣揣璁板綍鐨勮溅鐗屽彿锛岄�楀彿鎷兼帴
-                List<ShippingInfo> allShippingInfos = shippingInfoMapper.selectList(new LambdaQueryWrapper<ShippingInfo>()
-                        .eq(ShippingInfo::getSalesLedgerProductId, item.getId())
-                        .in(ShippingInfo::getStatus, "瀹℃牳閫氳繃", "宸插彂璐�")
-                        .orderByAsc(ShippingInfo::getCreateTime));
-                if(!CollectionUtils.isEmpty(allShippingInfos)){
-                    String carNumbers = allShippingInfos.stream()
-                            .map(ShippingInfo::getShippingCarNumber)
-                            .filter(carNo -> carNo != null && !carNo.trim().isEmpty())
-                            .distinct()
-                            .collect(Collectors.joining(","));
-                    item.setShippingCarNumber(carNumbers);
+                if(shippingInfo != null){
+                    item.setShippingDate(shippingInfo.getShippingDate());
+                    item.setShippingCarNumber(shippingInfo.getShippingCarNumber());
+                    item.setExpressCompany(shippingInfo.getExpressCompany());
+                    item.setExpressNumber(shippingInfo.getExpressNumber());
                 }
             });
         }
@@ -178,6 +168,7 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public int addOrUpdateSalesLedgerProduct(SalesLedgerProduct salesLedgerProduct) {
+        AmountUtils.normalizeSalesLedgerProduct(salesLedgerProduct);
 
         int result;
         Long salesLedgerId = salesLedgerProduct.getSalesLedgerId();
@@ -229,22 +220,20 @@
      */
     public void addProductionData(SalesLedgerProduct salesLedgerProduct) {
         //鍏堝垽鏂浜у搧鏄惁闇�瑕佺敓浜�
-        if (!salesLedgerProduct.getIsProduction()) {
-            return;
+        if (Boolean.TRUE.equals(salesLedgerProduct.getIsProduction())) {
+            SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerProduct.getSalesLedgerId());
+            ProductionPlan productionPlan = new ProductionPlan();
+            productionPlan.setSalesLedgerId(salesLedgerProduct.getSalesLedgerId());
+            productionPlan.setSalesLedgerProductId(salesLedgerProduct.getId());
+            productionPlan.setMpsNo(generateNextPlanNo(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))));
+            productionPlan.setProductModelId(salesLedgerProduct.getProductModelId());
+            productionPlan.setQtyRequired(salesLedgerProduct.getQuantity());
+            productionPlan.setSource("閿�鍞�");
+            productionPlan.setStatus(0);
+            productionPlan.setRequiredDate(salesLedger.getDeliveryDate());//闇�姹傛棩鏈�=浜よ揣鏃ユ湡
+            productionPlan.setPromisedDeliveryDate(salesLedger.getDeliveryDate());//鎵胯鏃ユ湡=浜よ揣鏃ユ湡
+            productionPlanMapper.insert(productionPlan);
         }
-        SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerProduct.getSalesLedgerId());
-        ProductionPlan productionPlan = new ProductionPlan();
-        productionPlan.setSalesLedgerId(salesLedgerProduct.getSalesLedgerId());
-        productionPlan.setSalesLedgerProductId(salesLedgerProduct.getId());
-        productionPlan.setMpsNo(generateNextPlanNo(com.ruoyi.common.utils.DateUtils.toLocalDate(salesLedger.getEntryDate())
-                .format(DateTimeFormatter.ofPattern("yyyyMMdd"))));
-        productionPlan.setProductModelId(salesLedgerProduct.getProductModelId());
-        productionPlan.setQtyRequired(salesLedgerProduct.getQuantity());
-        productionPlan.setSource("閿�鍞�");
-        productionPlan.setStatus(0);
-        productionPlan.setRequiredDate(salesLedger.getDeliveryDate());//闇�姹傛棩鏈�=浜よ揣鏃ユ湡
-        productionPlan.setPromisedDeliveryDate(salesLedger.getDeliveryDate());//鎵胯鏃ユ湡=浜よ揣鏃ユ湡
-        productionPlanMapper.insert(productionPlan);
 
     }
 
@@ -296,7 +285,7 @@
 
             Field amountField = mainEntityClass.getDeclaredField("contractAmount");
             amountField.setAccessible(true);
-            amountField.set(entity, totalAmount);
+            amountField.set(entity, AmountUtils.scaleAmount(totalAmount));
 
             mainMapper.updateById(entity);
         } catch (Exception e) {
@@ -319,29 +308,30 @@
         }
         int count = 0;
         StringBuilder stringBuffer = new StringBuilder();
+        // 鎵佸钩BOM鍙淮鎶も�滅墿鏂欒鏍� + 娑堣�楀伐搴忊�濓紝涓嶉厤缃敤閲忥紝
+        // 鍥犳杩欓噷鍙牎楠屾瘡涓墿鏂欐槸鍚︽湁鍙敤搴撳瓨锛屼笉鍐嶆寜鍗曚綅鐢ㄩ噺鏍哥畻闇�姹傛暟閲忋��
         for (TechnologyBomStructureVo structure : structureList) {
-            if (structure.getParentId() == null) {
+            if (structure == null || structure.getProductModelId() == null) {
                 continue;
             }
-            StockInventory stockInventory = stockInventoryMapper.selectOne(
-                    new QueryWrapper<StockInventory>().lambda().eq(StockInventory::getProductModelId, structure.getProductModelId()));
-            if (stockInventory == null) {
-                count++;
-                stringBuffer.append(structure.getProductName()).append("-").append(structure.getModel()).append("搴撳瓨涓嶈冻").append(System.lineSeparator());
-                continue;
+            List<StockInventory> stockList = stockInventoryMapper.selectList(
+                    new QueryWrapper<StockInventory>().lambda()
+                            .eq(StockInventory::getProductModelId, structure.getProductModelId()));
+            BigDecimal available = BigDecimal.ZERO;
+            for (StockInventory stockInventory : stockList) {
+                if (stockInventory == null) {
+                    continue;
+                }
+                BigDecimal quantity = stockInventory.getQualitity() == null ? BigDecimal.ZERO : stockInventory.getQualitity();
+                BigDecimal locked = stockInventory.getLockedQuantity() == null ? BigDecimal.ZERO : stockInventory.getLockedQuantity();
+                available = available.add(quantity.subtract(locked));
             }
-            BigDecimal required = salesLedgerProduct.getQuantity().multiply(structure.getUnitQuantity() == null ? BigDecimal.ZERO : structure.getUnitQuantity());
-            BigDecimal remain = stockInventory.getQualitity()
-                    .subtract(stockInventory.getLockedQuantity())
-                    .subtract(required)
-                    .divide(BigDecimal.ONE, 2, RoundingMode.CEILING);
-            if (remain.compareTo(BigDecimal.ZERO) < 0) {
+            if (available.compareTo(BigDecimal.ZERO) <= 0) {
                 count++;
                 stringBuffer.append(structure.getProductName())
                         .append("-")
                         .append(structure.getModel())
-                        .append("搴撳瓨涓嶈冻锛屽皯")
-                        .append(remain.abs())
+                        .append("鏃犲彲鐢ㄥ簱瀛�")
                         .append(System.lineSeparator());
             }
         }

--
Gitblit v1.9.3