From 3481d209ec847542b73fa16616ffe0e13c949e80 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期六, 18 四月 2026 18:11:03 +0800
Subject: [PATCH] fix: 入库与出库数量绑定

---
 src/main/java/com/ruoyi/approve/service/impl/ApproveNodeServiceImpl.java      |    3 
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java |   26 +++-
 src/main/java/com/ruoyi/sales/service/impl/ShippingInfoServiceImpl.java       |    3 
 src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java  |   88 ++++++++++++++---
 src/main/java/com/ruoyi/sales/pojo/SalesLedgerProduct.java                    |   31 +++++
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java        |   73 ++++++++++----
 src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java     |   14 +-
 doc/河南鹤壁天沐钢化玻璃厂.sql                                                           |   12 ++
 8 files changed, 194 insertions(+), 56 deletions(-)

diff --git "a/doc/\346\262\263\345\215\227\351\271\244\345\243\201\345\244\251\346\262\220\351\222\242\345\214\226\347\216\273\347\222\203\345\216\202.sql" "b/doc/\346\262\263\345\215\227\351\271\244\345\243\201\345\244\251\346\262\220\351\222\242\345\214\226\347\216\273\347\222\203\345\216\202.sql"
index b305a1e..17e9e2a 100644
--- "a/doc/\346\262\263\345\215\227\351\271\244\345\243\201\345\244\251\346\262\220\351\222\242\345\214\226\347\216\273\347\222\203\345\216\202.sql"
+++ "b/doc/\346\262\263\345\215\227\351\271\244\345\243\201\345\244\251\346\262\220\351\222\242\345\214\226\347\216\273\347\222\203\345\216\202.sql"
@@ -128,4 +128,14 @@
     ADD COLUMN `stocked_quantity` decimal(18, 2) DEFAULT 0.00 COMMENT '宸插叆搴撴暟閲�' AFTER `product_stock_status`;
 
 ALTER TABLE `sales_ledger_product`
-    ADD COLUMN `remaining_quantity` decimal(18, 2) DEFAULT '0.00' COMMENT '鍓╀綑寰呭叆搴撴暟閲�' AFTER `stocked_quantity`
\ No newline at end of file
+    ADD COLUMN `remaining_quantity` decimal(18, 2) DEFAULT '0.00' COMMENT '鍓╀綑寰呭叆搴撴暟閲�' AFTER `stocked_quantity`;
+
+ALTER TABLE `sales_ledger_product`
+    ADD COLUMN `shipped_quantity` decimal(18, 2) DEFAULT '0.00' COMMENT '宸插嚭搴撴暟閲�' AFTER `stocked_quantity`;
+
+ALTER TABLE `sales_ledger_product`
+    ADD COLUMN `remaining_shipped_quantity` decimal(18, 2) DEFAULT '0.00' COMMENT '鍓╀綑寰呭嚭搴撴暟閲�' AFTER `shipped_quantity`;
+
+ALTER TABLE `sales_ledger_product`
+    ADD COLUMN `unqualified_stocked_quantity` decimal(18, 2) DEFAULT '0.00' COMMENT '涓嶅悎鏍煎叆搴撴暟閲�' AFTER `stocked_quantity`,
+    ADD COLUMN `unqualified_shipped_quantity` decimal(18, 2) DEFAULT '0.00' COMMENT '涓嶅悎鏍煎嚭搴撴暟閲�' AFTER `shipped_quantity`;
\ No newline at end of file
diff --git a/src/main/java/com/ruoyi/approve/service/impl/ApproveNodeServiceImpl.java b/src/main/java/com/ruoyi/approve/service/impl/ApproveNodeServiceImpl.java
index 84aea4e..d8802c7 100644
--- a/src/main/java/com/ruoyi/approve/service/impl/ApproveNodeServiceImpl.java
+++ b/src/main/java/com/ruoyi/approve/service/impl/ApproveNodeServiceImpl.java
@@ -214,7 +214,8 @@
                             purchaseLedgerServiceImpl.addQualityInspect(purchaseLedger, salesLedgerProduct);
                         }else {
                             //鐩存帴鍏ュ簱
-                            stockUtils.addStock(salesLedgerProduct.getProductModelId(), salesLedgerProduct.getQuantity(), StockInQualifiedRecordTypeEnum.PURCHASE_STOCK_IN.getCode(), purchaseLedger.getId());
+                            stockUtils.addStock(null, salesLedgerProduct.getId(), salesLedgerProduct.getProductModelId(),
+                                    salesLedgerProduct.getQuantity(), StockInQualifiedRecordTypeEnum.PURCHASE_STOCK_IN.getCode(), salesLedgerProduct.getId());
                         }
                     }
                 } else if (status.equals(3)) {
diff --git a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
index 2d0ceae..3e3bafc 100644
--- a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
@@ -51,7 +51,13 @@
 import com.ruoyi.sales.pojo.SalesLedgerProduct;
 import com.ruoyi.sales.service.impl.CommonFileServiceImpl;
 import com.ruoyi.stock.dto.StockInventoryDto;
+import com.ruoyi.stock.mapper.StockInRecordMapper;
+import com.ruoyi.stock.mapper.StockOutRecordMapper;
+import com.ruoyi.stock.pojo.StockInRecord;
+import com.ruoyi.stock.pojo.StockOutRecord;
+import com.ruoyi.stock.service.StockInRecordService;
 import com.ruoyi.stock.service.StockInventoryService;
+import com.ruoyi.stock.service.StockOutRecordService;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FilenameUtils;
 import org.springframework.beans.BeanUtils;
@@ -154,6 +160,14 @@
     private SalesLedgerProductTemplateMapper salesLedgerProductTemplateMapper;
     @Autowired
     private StockInventoryService stockInventoryService;
+    @Autowired
+    private StockInRecordMapper stockInRecordMapper;
+    @Autowired
+    private StockOutRecordMapper stockOutRecordMapper;
+    @Autowired
+    private StockInRecordService stockInRecordService;
+    @Autowired
+    private StockOutRecordService stockOutRecordService;
     @Autowired
     private StockUtils stockUtils;
     @Value("${file.upload-dir}")
@@ -447,6 +461,34 @@
                 .eq(SalesLedgerProduct::getType, 2);
         List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(salesLedgerProductQueryWrapper);
         if (CollectionUtils.isNotEmpty(salesLedgerProducts)) {
+            List<Long> productIds = salesLedgerProducts.stream().map(SalesLedgerProduct::getId).filter(Objects::nonNull).collect(Collectors.toList());
+            // 鍒犻櫎鍙拌处鍓嶅厛鍥為��搴撳瓨璁板綍锛堝厛鍒犲嚭搴撳啀鍒犲叆搴擄級锛岄伩鍏嶅簱瀛樺洖閫�鏍¢獙琚鎷�
+            if (CollectionUtils.isNotEmpty(productIds)) {
+                List<Long> stockOutRecordIds = stockOutRecordMapper.selectList(new LambdaQueryWrapper<StockOutRecord>()
+                                .and(w -> w
+                                        .in(StockOutRecord::getSalesLedgerProductId, productIds)
+                                        .or(q -> q.in(StockOutRecord::getRecordId, productIds)
+                                                .in(StockOutRecord::getRecordType, Arrays.asList(
+                                                        StockOutUnQualifiedRecordTypeEnum.PURCHASE_SCAN_UNSTOCK_OUT.getCode()
+                                                ))))
+                                .select(StockOutRecord::getId))
+                        .stream().map(StockOutRecord::getId).collect(Collectors.toList());
+                if (CollectionUtils.isNotEmpty(stockOutRecordIds)) {
+                    stockOutRecordService.batchDelete(stockOutRecordIds);
+                }
+                List<Long> stockInRecordIds = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
+                                .and(w -> w
+                                        .in(StockInRecord::getSalesLedgerProductId, productIds)
+                                        .or(q -> q.in(StockInRecord::getRecordId, productIds)
+                                                .in(StockInRecord::getRecordType, Arrays.asList(
+                                                        StockInUnQualifiedRecordTypeEnum.PURCHASE_SCAN_UNSTOCK_IN.getCode()
+                                                ))))
+                                .select(StockInRecord::getId))
+                        .stream().map(StockInRecord::getId).collect(Collectors.toList());
+                if (CollectionUtils.isNotEmpty(stockInRecordIds)) {
+                    stockInRecordService.batchDelete(stockInRecordIds);
+                }
+            }
             salesLedgerProducts.stream().forEach(salesLedgerProduct -> {
                 // 鎵归噺鍒犻櫎鍏宠仈鐨勯噰璐彴璐︿骇鍝�
                 LambdaQueryWrapper<ProcurementRecordStorage> queryWrapper = new LambdaQueryWrapper<>();
@@ -828,6 +870,9 @@
         if (purchaseLedger == null) {
             throw new ServiceException("鍏ュ簱澶辫触,閲囪喘鍙拌处涓嶅瓨鍦�");
         }
+        if (!Objects.equals(purchaseLedger.getApprovalStatus(), 3)) {
+            throw new ServiceException("鍏ュ簱澶辫触,閲囪喘璁㈠崟鏈鎵归�氳繃,涓嶅厑璁告壂鐮佸叆搴�");
+        }
         if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
             throw new ServiceException("閲囪喘鍏ュ簱澶辫触,鍏ュ簱浜у搧涓嶈兘涓虹┖");
         }
@@ -901,6 +946,9 @@
         if (purchaseLedger == null) {
             throw new ServiceException("鍑哄簱澶辫触,閲囪喘鍙拌处涓嶅瓨鍦�");
         }
+        if (!Objects.equals(purchaseLedger.getApprovalStatus(), 3)) {
+            throw new ServiceException("鍑哄簱澶辫触,閲囪喘璁㈠崟鏈鎵归�氳繃,涓嶅厑璁告壂鐮佸嚭搴�");
+        }
         if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
             throw new ServiceException("閲囪喘鍑哄簱澶辫触,鍑哄簱浜у搧涓嶈兘涓虹┖");
         }
@@ -938,12 +986,6 @@
             }
             stockUtils.assertQualifiedAvailable(dbProduct.getProductModelId(), outboundThisLine);
 
-            BigDecimal oldStocked = dbProduct.getStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getStockedQuantity();
-            BigDecimal newStocked = oldStocked.subtract(outboundThisLine);
-            if (newStocked.compareTo(BigDecimal.ZERO) < 0) {
-                newStocked = BigDecimal.ZERO;
-            }
-
             StockInventoryDto stockInventoryDto = new StockInventoryDto();
             stockInventoryDto.setRecordId(dbProduct.getId());
             stockInventoryDto.setRecordType(StockOutQualifiedRecordTypeEnum.PURCHASE_SCAN_STOCK_OUT.getCode());
@@ -953,17 +995,8 @@
             stockInventoryDto.setSalesLedgerProductId(dbProduct.getId());
             stockInventoryService.subtractStockInventory(stockInventoryDto);
 
-            BigDecimal orderQty = dbProduct.getQuantity() == null ? BigDecimal.ZERO : dbProduct.getQuantity();
-            int lineStockStatus;
-            if (newStocked.compareTo(BigDecimal.ZERO) <= 0) {
-                lineStockStatus = 0;
-            } else if (orderQty.compareTo(BigDecimal.ZERO) > 0 && newStocked.compareTo(orderQty) < 0) {
-                lineStockStatus = 1;
-            } else {
-                lineStockStatus = 2;
-            }
-            dbProduct.setStockedQuantity(newStocked);
-            dbProduct.setProductStockStatus(lineStockStatus);
+            BigDecimal oldShipped = dbProduct.getShippedQuantity() == null ? BigDecimal.ZERO : dbProduct.getShippedQuantity();
+            dbProduct.setShippedQuantity(oldShipped.add(outboundThisLine));
             dbProduct.fillRemainingQuantity();
             salesLedgerProductMapper.updateById(dbProduct);
         }
@@ -978,6 +1011,9 @@
         PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(dto.getPurchaseLedgerId());
         if (purchaseLedger == null) {
             throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,閲囪喘鍙拌处涓嶅瓨鍦�");
+        }
+        if (!Objects.equals(purchaseLedger.getApprovalStatus(), 3)) {
+            throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,閲囪喘璁㈠崟鏈鎵归�氳繃,涓嶅厑璁告壂鐮佸叆搴�");
         }
         if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
             throw new ServiceException("閲囪喘涓嶅悎鏍煎叆搴撳け璐�,鍏ュ簱浜у搧涓嶈兘涓虹┖");
@@ -1016,6 +1052,11 @@
             }
             stockUtils.addUnStock(null, null, dbProduct.getProductModelId(), inboundThisLine,
                     StockInUnQualifiedRecordTypeEnum.PURCHASE_SCAN_UNSTOCK_IN.getCode(), dbProduct.getId());
+
+            BigDecimal oldUnStocked = dbProduct.getUnqualifiedStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedStockedQuantity();
+            dbProduct.setUnqualifiedStockedQuantity(oldUnStocked.add(inboundThisLine));
+            dbProduct.fillRemainingQuantity();
+            salesLedgerProductMapper.updateById(dbProduct);
         }
     }
 
@@ -1028,6 +1069,9 @@
         PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(dto.getPurchaseLedgerId());
         if (purchaseLedger == null) {
             throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,閲囪喘鍙拌处涓嶅瓨鍦�");
+        }
+        if (!Objects.equals(purchaseLedger.getApprovalStatus(), 3)) {
+            throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,閲囪喘璁㈠崟鏈鎵归�氳繃,涓嶅厑璁告壂鐮佸嚭搴�");
         }
         if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
             throw new ServiceException("閲囪喘涓嶅悎鏍煎嚭搴撳け璐�,鍑哄簱浜у搧涓嶈兘涓虹┖");
@@ -1064,9 +1108,19 @@
             if (dbProduct.getProductModelId() == null) {
                 throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,浜у搧瑙勬牸鏈淮鎶�,鏃犳硶鍑哄簱");
             }
+            BigDecimal unStocked = dbProduct.getUnqualifiedStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedStockedQuantity();
+            BigDecimal unShipped = dbProduct.getUnqualifiedShippedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedShippedQuantity();
+            BigDecimal canUnShip = unStocked.subtract(unShipped);
+            if (outboundThisLine.compareTo(canUnShip) > 0) {
+                throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,鍑哄簱鏁伴噺涓嶈兘澶т簬涓嶅悎鏍煎叆搴撴暟閲�");
+            }
             stockUtils.assertUnqualifiedAvailable(dbProduct.getProductModelId(), outboundThisLine);
             stockUtils.subtractUnStock(null, null, dbProduct.getProductModelId(), outboundThisLine,
                     StockOutUnQualifiedRecordTypeEnum.PURCHASE_SCAN_UNSTOCK_OUT.getCode(), dbProduct.getId());
+
+            dbProduct.setUnqualifiedShippedQuantity(unShipped.add(outboundThisLine));
+            dbProduct.fillRemainingQuantity();
+            salesLedgerProductMapper.updateById(dbProduct);
         }
     }
 
diff --git a/src/main/java/com/ruoyi/sales/pojo/SalesLedgerProduct.java b/src/main/java/com/ruoyi/sales/pojo/SalesLedgerProduct.java
index 6bb145e..58132a9 100644
--- a/src/main/java/com/ruoyi/sales/pojo/SalesLedgerProduct.java
+++ b/src/main/java/com/ruoyi/sales/pojo/SalesLedgerProduct.java
@@ -78,6 +78,11 @@
      */
     private BigDecimal stockedQuantity;
 
+    /**
+     * 宸插嚭搴撴暟閲�
+     */
+    private BigDecimal shippedQuantity;
+
     @Excel(name = "鏈�浣庡簱瀛樻暟閲�")
     private BigDecimal minStock;
 
@@ -87,6 +92,22 @@
     @Excel(name = "鍓╀綑鏁伴噺")
     @ApiModelProperty("鍓╀綑寰呭叆搴撴暟閲�(璁㈠崟鏁伴噺-鍚堟牸宸插叆搴�)")
     private BigDecimal remainingQuantity;
+
+    /**
+     * 鍓╀綑鍑哄簱鏁伴噺
+     */
+    private BigDecimal remainingShippedQuantity;
+
+    /**
+     * 涓嶅悎鏍煎叆搴撴暟閲�
+     */
+    private BigDecimal unqualifiedStockedQuantity;
+
+
+    /**
+     * 涓嶅悎鏍煎嚭搴撴暟閲�
+     */
+    private BigDecimal unqualifiedShippedQuantity;
 
     /**
      * 绋庣巼
@@ -330,8 +351,14 @@
 
     public void fillRemainingQuantity() {
         BigDecimal q = this.quantity == null ? BigDecimal.ZERO : this.quantity;
+
         BigDecimal stocked = this.stockedQuantity == null ? BigDecimal.ZERO : this.stockedQuantity;
-        BigDecimal rem = q.subtract(stocked);
-        this.remainingQuantity = rem.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : rem;
+        BigDecimal remIn = q.subtract(stocked);
+        this.remainingQuantity = remIn.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : remIn;
+
+        BigDecimal shipped = this.shippedQuantity == null ? BigDecimal.ZERO : this.shippedQuantity;
+        // 鍓╀綑鍑哄簱鏁伴噺锛氫互鈥滃凡鍏ュ簱鏁伴噺鈥濅负鍩哄噯锛堝嚭搴撳彧鑳戒粠宸插叆搴撲腑鎵e噺锛�
+        BigDecimal remOut = stocked.subtract(shipped);
+        this.remainingShippedQuantity = remOut.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : remOut;
     }
 }
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 ab45874..ac28c29 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
@@ -193,19 +193,27 @@
             if (item.getFutureTicketsAmount() == null || item.getFutureTicketsAmount().compareTo(BigDecimal.ZERO) == 0) {
                 item.setFutureTicketsAmount(BigDecimal.ZERO);
             }
-            if (item.getApproveStatus() == null || item.getApproveStatus() != 2) {
-                Integer hasSufficientStock = item.getHasSufficientStock();
-                if (hasSufficientStock != null && hasSufficientStock == 0) {
-                    item.setApproveStatus(0);
-                } else {
-                    item.setApproveStatus(1);
-                }
-            }
             BigDecimal returnQuality = finalReturnOrderGroupDtoMap.getOrDefault(item.getId(), BigDecimal.ZERO);
             item.setReturnQuality(returnQuality);
             BigDecimal quantity = item.getQuantity() == null ? BigDecimal.ZERO : item.getQuantity();
-            item.setAvailableQuality(quantity.subtract(returnQuality));
+            BigDecimal shipped = item.getShippedQuantity() == null ? BigDecimal.ZERO : item.getShippedQuantity();
+            BigDecimal available = quantity.subtract(returnQuality).subtract(shipped);
+            item.setAvailableQuality(available.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : available);
             item.fillRemainingQuantity();
+            if (item.getApproveStatus() == null || item.getApproveStatus() != 2) {
+                BigDecimal remainingShipped = item.getRemainingShippedQuantity() == null ? BigDecimal.ZERO : item.getRemainingShippedQuantity();
+                boolean hasOutbound = shipped.compareTo(BigDecimal.ZERO) > 0;
+                if (hasOutbound && remainingShipped.compareTo(BigDecimal.ZERO) <= 0) {
+                    item.setApproveStatus(0);
+                } else {
+                    Integer hasSufficientStock = item.getHasSufficientStock();
+                    if (hasSufficientStock != null && hasSufficientStock == 0) {
+                        item.setApproveStatus(0);
+                    } else {
+                        item.setApproveStatus(1);
+                    }
+                }
+            }
             ProductModel productModel = finalProductModelMap.get(item.getProductModelId());
             if (productModel != null) {
                 item.setThickness(productModel.getThickness());
diff --git a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
index 0a277b1..1a371f9 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -1690,22 +1690,55 @@
             throw new ServiceException("鍏ュ簱澶辫触,鏈煡璇㈠埌璇ラ攢鍞鍗曠殑閿�鍞骇鍝�");
         }
         for (SalesLedgerProduct product : salesLedgerProducts) {
+            if (!Objects.equals(product.getSalesLedgerId(), ledger.getId())) {
+                throw new ServiceException("鍏ュ簱澶辫触,瀛樺湪涓嶅睘浜庡綋鍓嶉攢鍞鍗曠殑浜у搧");
+            }
             if (product.getProductModelId() == null) {
+                continue;
+            }
+            BigDecimal orderQty = product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity();
+            BigDecimal oldStocked = product.getStockedQuantity() == null ? BigDecimal.ZERO : product.getStockedQuantity();
+            BigDecimal inboundQty = orderQty.subtract(oldStocked);
+            if (inboundQty.compareTo(BigDecimal.ZERO) < 0) {
+                inboundQty = BigDecimal.ZERO;
+            }
+            if (inboundQty.compareTo(BigDecimal.ZERO) <= 0) {
                 continue;
             }
             StockInventoryDto stockInventoryDto = new StockInventoryDto();
             stockInventoryDto.setRecordId(product.getId());
             stockInventoryDto.setRecordType(StockInQualifiedRecordTypeEnum.SALE_STOCK_IN.getCode());
-            stockInventoryDto.setQualitity(product.getQuantity());
+            stockInventoryDto.setQualitity(inboundQty);
             stockInventoryDto.setProductModelId(product.getProductModelId());
             stockInventoryDto.setSalesLedgerId(ledger.getId());
             stockInventoryDto.setSalesLedgerProductId(product.getId());
             stockInventoryService.addstockInventory(stockInventoryDto);
+
+            BigDecimal newStocked = oldStocked.add(inboundQty);
+            int lineStockStatus;
+            if (newStocked.compareTo(BigDecimal.ZERO) <= 0) {
+                lineStockStatus = 0;
+            } else if (orderQty.compareTo(BigDecimal.ZERO) > 0 && newStocked.compareTo(orderQty) < 0) {
+                lineStockStatus = 1;
+            } else {
+                lineStockStatus = 2;
+            }
+            product.setStockedQuantity(newStocked);
+            product.setProductStockStatus(lineStockStatus);
+            product.fillRemainingQuantity();
+            salesLedgerProductMapper.updateById(product);
         }
         //  鎸夐攢鍞鍗曚骇鍝佸叆搴撴儏鍐垫洿鏂颁富鍗曞叆搴撶姸鎬侊細1-閮ㄥ垎鍏ュ簱锛�2-宸插叆搴�
         List<SalesLedgerProduct> ledgerAllProducts = salesLedgerProductMapper.selectList(Wrappers.<SalesLedgerProduct>lambdaQuery().eq(SalesLedgerProduct::getSalesLedgerId, ledger.getId()));
-        boolean hasStocked = CollectionUtils.isNotEmpty(ledgerAllProducts) && ledgerAllProducts.stream().anyMatch(item -> Objects.equals(item.getProductStockStatus(), 1));
-        boolean allStocked = CollectionUtils.isNotEmpty(ledgerAllProducts) && ledgerAllProducts.stream().allMatch(item -> Objects.equals(item.getProductStockStatus(), 1));
+        boolean hasStocked = CollectionUtils.isNotEmpty(ledgerAllProducts) && ledgerAllProducts.stream().anyMatch(item -> {
+            BigDecimal sq = item.getStockedQuantity();
+            return sq != null && sq.compareTo(BigDecimal.ZERO) > 0;
+        });
+        boolean allStocked = CollectionUtils.isNotEmpty(ledgerAllProducts) && ledgerAllProducts.stream().allMatch(item -> {
+            BigDecimal orderQty = item.getQuantity() == null ? BigDecimal.ZERO : item.getQuantity();
+            BigDecimal stockedQty = item.getStockedQuantity() == null ? BigDecimal.ZERO : item.getStockedQuantity();
+            return orderQty.compareTo(BigDecimal.ZERO) <= 0 || stockedQty.compareTo(orderQty) >= 0;
+        });
         ledger.setStockStatus(allStocked ? 2 : (hasStocked ? 1 : 0));
         baseMapper.updateById(ledger);
     }
@@ -1874,6 +1907,11 @@
             }
             stockUtils.addUnStock(ledgerId, dbProduct.getId(), dbProduct.getProductModelId(), inboundThisLine,
                     StockInUnQualifiedRecordTypeEnum.SALES_SCAN_UNSTOCK_IN.getCode(), dbProduct.getId());
+
+            BigDecimal oldUnStocked = dbProduct.getUnqualifiedStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedStockedQuantity();
+            dbProduct.setUnqualifiedStockedQuantity(oldUnStocked.add(inboundThisLine));
+            dbProduct.fillRemainingQuantity();
+            salesLedgerProductMapper.updateById(dbProduct);
         }
     }
 
@@ -1927,12 +1965,6 @@
             }
             stockUtils.assertQualifiedAvailable(dbProduct.getProductModelId(), outboundThisLine);
 
-            BigDecimal oldStocked = dbProduct.getStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getStockedQuantity();
-            BigDecimal newStocked = oldStocked.subtract(outboundThisLine);
-            if (newStocked.compareTo(BigDecimal.ZERO) < 0) {
-                newStocked = BigDecimal.ZERO;
-            }
-
             StockInventoryDto stockInventoryDto = new StockInventoryDto();
             stockInventoryDto.setRecordId(dbProduct.getId());
             stockInventoryDto.setRecordType(StockOutQualifiedRecordTypeEnum.SALE_SCAN_STOCK_OUT.getCode());
@@ -1942,17 +1974,8 @@
             stockInventoryDto.setSalesLedgerProductId(dbProduct.getId());
             stockInventoryService.subtractStockInventory(stockInventoryDto);
 
-            BigDecimal orderQty = dbProduct.getQuantity() == null ? BigDecimal.ZERO : dbProduct.getQuantity();
-            int lineStockStatus;
-            if (newStocked.compareTo(BigDecimal.ZERO) <= 0) {
-                lineStockStatus = 0;
-            } else if (orderQty.compareTo(BigDecimal.ZERO) > 0 && newStocked.compareTo(orderQty) < 0) {
-                lineStockStatus = 1;
-            } else {
-                lineStockStatus = 2;
-            }
-            dbProduct.setStockedQuantity(newStocked);
-            dbProduct.setProductStockStatus(lineStockStatus);
+            BigDecimal oldShipped = dbProduct.getShippedQuantity() == null ? BigDecimal.ZERO : dbProduct.getShippedQuantity();
+            dbProduct.setShippedQuantity(oldShipped.add(outboundThisLine));
             dbProduct.fillRemainingQuantity();
             salesLedgerProductMapper.updateById(dbProduct);
         }
@@ -2015,9 +2038,19 @@
             if (dbProduct.getProductModelId() == null) {
                 throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,浜у搧瑙勬牸鏈淮鎶�,鏃犳硶鍑哄簱");
             }
+            BigDecimal unStocked = dbProduct.getUnqualifiedStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedStockedQuantity();
+            BigDecimal unShipped = dbProduct.getUnqualifiedShippedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedShippedQuantity();
+            BigDecimal canUnShip = unStocked.subtract(unShipped);
+            if (outboundThisLine.compareTo(canUnShip) > 0) {
+                throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,鍑哄簱鏁伴噺涓嶈兘澶т簬涓嶅悎鏍煎叆搴撴暟閲�");
+            }
             stockUtils.assertUnqualifiedAvailable(dbProduct.getProductModelId(), outboundThisLine);
             stockUtils.subtractUnStock(ledgerId, dbProduct.getId(), dbProduct.getProductModelId(), outboundThisLine,
                     StockOutUnQualifiedRecordTypeEnum.SALE_SCAN_UNSTOCK_OUT.getCode(), dbProduct.getId());
+
+            dbProduct.setUnqualifiedShippedQuantity(unShipped.add(outboundThisLine));
+            dbProduct.fillRemainingQuantity();
+            salesLedgerProductMapper.updateById(dbProduct);
         }
     }
 }
diff --git a/src/main/java/com/ruoyi/sales/service/impl/ShippingInfoServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/ShippingInfoServiceImpl.java
index fe8e425..521222f 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/ShippingInfoServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/ShippingInfoServiceImpl.java
@@ -88,6 +88,9 @@
             SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(byId.getSalesLedgerProductId());
             if (salesLedgerProduct != null) {
                 stockUtils.substractStock(salesLedgerProduct.getSalesLedgerId(), salesLedgerProduct.getId(), salesLedgerProduct.getProductModelId(), salesLedgerProduct.getQuantity(), StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(), req.getId());
+                BigDecimal shipped = salesLedgerProduct.getShippedQuantity() == null ? BigDecimal.ZERO : salesLedgerProduct.getShippedQuantity();
+                salesLedgerProduct.setShippedQuantity(shipped.add(salesLedgerProduct.getQuantity() == null ? BigDecimal.ZERO : salesLedgerProduct.getQuantity()));
+                salesLedgerProductMapper.updateById(salesLedgerProduct);
             }
         }
         byId.setExpressNumber(req.getExpressNumber());
diff --git a/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java b/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
index 5208f92..c1743d4 100644
--- a/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
+++ b/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
@@ -58,13 +58,15 @@
     @Transactional(rollbackFor = Exception.class)
     public Boolean addstockInventory(StockInventoryDto stockInventoryDto) {
         //鏂板鍏ュ簱璁板綍鍐嶆坊鍔犲簱瀛�
-        //  鏇存柊浜у搧鍏ュ簱鐘舵��
-        SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(stockInventoryDto.getSalesLedgerProductId());
-        if (salesLedgerProduct == null) {
-            throw new ServiceException("鍏ュ簱澶辫触,閿�鍞骇鍝佷笉瀛樺湪");
+        //  鏇存柊璁㈠崟浜у搧鍏ュ簱鐘舵�侊紙浠呭綋浼犲叆浜嗕骇鍝佽ID鏃讹級
+        if (stockInventoryDto.getSalesLedgerProductId() != null) {
+            SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(stockInventoryDto.getSalesLedgerProductId());
+            if (salesLedgerProduct == null) {
+                throw new ServiceException("鍏ュ簱澶辫触,閿�鍞骇鍝佷笉瀛樺湪");
+            }
+            salesLedgerProduct.setProductStockStatus(1);
+            salesLedgerProductMapper.updateById(salesLedgerProduct);
         }
-        salesLedgerProduct.setProductStockStatus(1);
-        salesLedgerProductMapper.updateById(salesLedgerProduct);
 
         StockInRecordDto stockInRecordDto = new StockInRecordDto();
         stockInRecordDto.setRecordId(stockInventoryDto.getRecordId());

--
Gitblit v1.9.3