From 0962b43c6b8874e181107b2b6be21cdd6d198c09 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 24 四月 2026 18:01:34 +0800
Subject: [PATCH] feat: 导入已发货的数据时也新增对应的出入库记录

---
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java |   74 ++++++++++++++++++++++++++++++++++---
 1 files changed, 68 insertions(+), 6 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 c9e8734..3f24246 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -465,9 +465,10 @@
             salesLedger.setReceiptPaymentAmountTotal(receiptPaymentAmountTotal);
             salesLedger.setNoReceiptAmount(noReceiptPaymentAmountTotal);
 
-            boolean hasInvoiceOperation = invoiceTotal.compareTo(BigDecimal.ZERO) > 0;
-            boolean hasReceiptOperation = receiptPaymentAmountTotal.compareTo(BigDecimal.ZERO) > 0;
-            salesLedger.setIsEdit(!(hasInvoiceOperation || hasReceiptOperation));
+            boolean isFh = salesLedger.getDeliveryStatus() != null && salesLedger.getDeliveryStatus() == 5;
+            salesLedger.setIsFh(isFh);
+
+            salesLedger.setIsEdit(!isFh);
         }
 
         if (salesLedgerDto.getStatus() != null && salesLedgerDto.getStatus()) {
@@ -1866,6 +1867,12 @@
             if (selectedProduct.getProductModelId() == null) {
                 throw new ServiceException("鍏ュ簱澶辫触,浜у搧瑙勬牸鏈淮鎶�,鏃犳硶鍏ュ簱");
             }
+            BigDecimal orderQty = selectedProduct.getQuantity() == null ? BigDecimal.ZERO : selectedProduct.getQuantity();
+            BigDecimal qualifiedStocked = selectedProduct.getStockedQuantity() == null ? BigDecimal.ZERO : selectedProduct.getStockedQuantity();
+            BigDecimal inboundQty = inboundQtyByLineId.getOrDefault(selectedProduct.getId(), BigDecimal.ZERO);
+            if (inboundQty.compareTo(BigDecimal.ZERO) > 0 && qualifiedStocked.add(inboundQty).compareTo(orderQty) > 0) {
+                throw new ServiceException("鍏ュ簱澶辫触,鍚堟牸鍏ュ簱鏁伴噺涔嬪拰涓嶈兘澶т簬璁㈠崟鏁伴噺");
+            }
         }
         String approveUserIds = resolveApproveUserIds(dto.getApproveUserIds(), salesLedger.getId(), INBOUND_BIZ_TYPE_SCAN_QUALIFIED);
         if (StringUtils.isEmpty(approveUserIds)) {
@@ -1923,6 +1930,10 @@
             }
             BigDecimal oldStocked = dbProduct.getStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getStockedQuantity();
             BigDecimal newStocked = oldStocked.add(inboundThisLine);
+            BigDecimal orderQty = dbProduct.getQuantity() == null ? BigDecimal.ZERO : dbProduct.getQuantity();
+            if (newStocked.compareTo(orderQty) > 0) {
+                throw new ServiceException("鍏ュ簱澶辫触,鍚堟牸鍏ュ簱鏁伴噺涔嬪拰涓嶈兘澶т簬璁㈠崟鏁伴噺");
+            }
 
             StockInventoryDto stockInventoryDto = new StockInventoryDto();
             stockInventoryDto.setRecordId(dbProduct.getId());
@@ -1933,7 +1944,6 @@
             stockInventoryDto.setSalesLedgerProductId(dbProduct.getId());
             stockInventoryService.addstockInventory(stockInventoryDto);
 
-            BigDecimal orderQty = dbProduct.getQuantity() == null ? BigDecimal.ZERO : dbProduct.getQuantity();
             int lineStockStatus;
             if (newStocked.compareTo(BigDecimal.ZERO) <= 0) {
                 lineStockStatus = 0;
@@ -2004,6 +2014,20 @@
             if (selectedProduct.getProductModelId() == null) {
                 throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,浜у搧瑙勬牸鏈淮鎶�,鏃犳硶鍏ュ簱");
             }
+            BigDecimal orderQty = selectedProduct.getQuantity() == null ? BigDecimal.ZERO : selectedProduct.getQuantity();
+            BigDecimal qualifiedStocked = selectedProduct.getStockedQuantity() == null ? BigDecimal.ZERO : selectedProduct.getStockedQuantity();
+            BigDecimal unqualifiedStocked = selectedProduct.getUnqualifiedStockedQuantity() == null ? BigDecimal.ZERO : selectedProduct.getUnqualifiedStockedQuantity();
+            BigDecimal inboundQty = inboundQtyByLineId.getOrDefault(selectedProduct.getId(), BigDecimal.ZERO);
+            BigDecimal remainForUnqualified = orderQty.subtract(qualifiedStocked);
+            if (remainForUnqualified.compareTo(BigDecimal.ZERO) <= 0 && inboundQty.compareTo(BigDecimal.ZERO) > 0) {
+                throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,璇ヤ骇鍝佸凡鏃犲彲鍏ヤ笉鍚堟牸搴撴暟閲�");
+            }
+            if (inboundQty.compareTo(BigDecimal.ZERO) > 0 && unqualifiedStocked.add(inboundQty).compareTo(orderQty) > 0) {
+                throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,涓嶅悎鏍煎叆搴撴暟閲忎箣鍜屼笉鑳藉ぇ浜庤鍗曟暟閲�");
+            }
+            if (inboundQty.compareTo(BigDecimal.ZERO) > 0 && unqualifiedStocked.add(inboundQty).compareTo(remainForUnqualified) > 0) {
+                throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,涓嶅悎鏍煎叆搴撴暟閲忎笉鑳藉ぇ浜庤鍗曟暟閲忓噺鍘诲悎鏍煎叆搴撴暟閲�");
+            }
         }
         String approveUserIds = resolveApproveUserIds(dto.getApproveUserIds(), salesLedger.getId(), INBOUND_BIZ_TYPE_SCAN_UNQUALIFIED);
         if (StringUtils.isEmpty(approveUserIds)) {
@@ -2059,9 +2083,22 @@
             if (dbProduct.getProductModelId() == null) {
                 throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,浜у搧瑙勬牸鏈淮鎶�,鏃犳硶鍏ュ簱");
             }
-            stockUtils.addUnStock(ledgerId, dbProduct.getId(), dbProduct.getProductModelId(), inboundThisLine, StockInUnQualifiedRecordTypeEnum.SALES_SCAN_UNSTOCK_IN.getCode(), dbProduct.getId());
+            BigDecimal orderQty = dbProduct.getQuantity() == null ? BigDecimal.ZERO : dbProduct.getQuantity();
+            BigDecimal qualifiedStocked = dbProduct.getStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getStockedQuantity();
             BigDecimal oldUnStocked = dbProduct.getUnqualifiedStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedStockedQuantity();
-            dbProduct.setUnqualifiedStockedQuantity(oldUnStocked.add(inboundThisLine));
+            BigDecimal newUnStocked = oldUnStocked.add(inboundThisLine);
+            BigDecimal remainForUnqualified = orderQty.subtract(qualifiedStocked);
+            if (remainForUnqualified.compareTo(BigDecimal.ZERO) <= 0) {
+                throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,璇ヤ骇鍝佸凡鏃犲彲鍏ヤ笉鍚堟牸搴撴暟閲�");
+            }
+            if (newUnStocked.compareTo(orderQty) > 0) {
+                throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,涓嶅悎鏍煎叆搴撴暟閲忎箣鍜屼笉鑳藉ぇ浜庤鍗曟暟閲�");
+            }
+            if (newUnStocked.compareTo(remainForUnqualified) > 0) {
+                throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,涓嶅悎鏍煎叆搴撴暟閲忎笉鑳藉ぇ浜庤鍗曟暟閲忓噺鍘诲悎鏍煎叆搴撴暟閲�");
+            }
+            stockUtils.addUnStock(ledgerId, dbProduct.getId(), dbProduct.getProductModelId(), inboundThisLine, StockInUnQualifiedRecordTypeEnum.SALES_SCAN_UNSTOCK_IN.getCode(), dbProduct.getId());
+            dbProduct.setUnqualifiedStockedQuantity(newUnStocked);
             dbProduct.fillRemainingQuantity();
             salesLedgerProductMapper.updateById(dbProduct);
         }
@@ -2277,10 +2314,28 @@
                     if (dbProduct.getProductModelId() == null) {
                         throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]浜у搧瑙勬牸鏈淮鎶�,鏃犳硶琛ュ綍鍑哄簱");
                     }
+                    // 鍘嗗彶宸插彂璐цˉ褰曪細鐩存帴鍐欏叆鍏ュ簱+鍑哄簱璁板綍
+                    stockUtils.addStock(
+                            ledger.getId(),
+                            dbProduct.getId(),
+                            dbProduct.getProductModelId(),
+                            allocQty,
+                            StockInQualifiedRecordTypeEnum.SALE_STOCK_IN.getCode(),
+                            dbProduct.getId()
+                    );
+                    stockUtils.substractStock(
+                            ledger.getId(),
+                            dbProduct.getId(),
+                            dbProduct.getProductModelId(),
+                            allocQty,
+                            StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(),
+                            dbProduct.getId()
+                    );
                     BigDecimal oldShipped = defaultDecimal(dbProduct.getShippedQuantity());
                     BigDecimal newShipped = oldShipped.add(allocQty);
                     dbProduct.setStockedQuantity(defaultDecimal(dbProduct.getQuantity()));
                     dbProduct.setShippedQuantity(newShipped);
+                    dbProduct.setApproveStatus(3);
                     updateProductStockStatus(dbProduct);
                     dbProduct.fillRemainingQuantity();
                     updateProductShipStatus(dbProduct);
@@ -2308,9 +2363,16 @@
                 BigDecimal shipped = defaultDecimal(p.getShippedQuantity());
                 return shipped.compareTo(qty) >= 0;
             });
+            boolean anyInbound = CollectionUtils.isNotEmpty(latestProducts) && latestProducts.stream().anyMatch(p -> defaultDecimal(p.getStockedQuantity()).compareTo(BigDecimal.ZERO) > 0);
+            boolean allInbound = CollectionUtils.isNotEmpty(latestProducts) && latestProducts.stream().allMatch(p -> {
+                BigDecimal qty = defaultDecimal(p.getQuantity());
+                BigDecimal stocked = defaultDecimal(p.getStockedQuantity());
+                return qty.compareTo(BigDecimal.ZERO) <= 0 || stocked.compareTo(qty) >= 0;
+            });
             if (allShipped && rowList.get(0).getReportDate() != null) {
                 ledger.setDeliveryDate(DateUtils.toLocalDate(rowList.get(0).getReportDate()));
             }
+            ledger.setStockStatus(allInbound ? 2 : (anyInbound ? 1 : 0));
             ledger.setDeliveryStatus(allShipped ? 5 : 1);
             salesLedgerMapper.updateById(ledger);
         }

--
Gitblit v1.9.3