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/sales/service/impl/SalesLedgerServiceImpl.java |   73 ++++++++++++++++++++++++++----------
 1 files changed, 53 insertions(+), 20 deletions(-)

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);
         }
     }
 }

--
Gitblit v1.9.3