From b83811618d78d7a9832b325334b7a35cd6a3b64b Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期一, 08 六月 2026 13:21:01 +0800
Subject: [PATCH] 采购台账手动入库,保存差额;入库审核 重新计算含水量

---
 src/main/java/com/ruoyi/account/bean/vo/purchase/PurchaseInboundVo.java      |   12 ++++
 src/main/java/com/ruoyi/stock/dto/StockInventoryDto.java                     |    3 +
 src/main/java/com/ruoyi/stock/pojo/StockInRecord.java                        |    4 +
 src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java |   75 +++++++++++++++++++-----
 src/main/resources/mapper/stock/StockInRecordMapper.xml                      |   34 ++++++++++-
 src/main/java/com/ruoyi/procurementrecord/utils/StockUtils.java              |    3 +
 src/main/java/com/ruoyi/purchase/dto/PurchaseStockInDto.java                 |    2 
 src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java     |   24 ++++++++
 src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java    |    1 
 9 files changed, 138 insertions(+), 20 deletions(-)

diff --git a/src/main/java/com/ruoyi/account/bean/vo/purchase/PurchaseInboundVo.java b/src/main/java/com/ruoyi/account/bean/vo/purchase/PurchaseInboundVo.java
index 48831e1..db3f263 100644
--- a/src/main/java/com/ruoyi/account/bean/vo/purchase/PurchaseInboundVo.java
+++ b/src/main/java/com/ruoyi/account/bean/vo/purchase/PurchaseInboundVo.java
@@ -42,6 +42,18 @@
     @Excel(name = "閲戦")
     private BigDecimal InboundAmount;
 
+    @Schema(description = "鐞嗚鍏ュ簱鏁伴噺")
+    @Excel(name = "鐞嗚鍏ュ簱鏁伴噺")
+    private BigDecimal theoryStockInNum;
+
+    @Schema(description = "瀹為檯鍏ュ簱鏁伴噺")
+    @Excel(name = "瀹為檯鍏ュ簱鏁伴噺")
+    private BigDecimal stockInNum;
+
+    @Schema(description = "宸")
+    @Excel(name = "宸")
+    private BigDecimal differenceNum;
+
     @Schema(description = "閲囪喘璁㈠崟鍙�")
     @Excel(name = "閲囪喘璁㈠崟鍙�")
     private String purchaseContractNumber;
diff --git a/src/main/java/com/ruoyi/procurementrecord/utils/StockUtils.java b/src/main/java/com/ruoyi/procurementrecord/utils/StockUtils.java
index f12c260..d5e8c22 100644
--- a/src/main/java/com/ruoyi/procurementrecord/utils/StockUtils.java
+++ b/src/main/java/com/ruoyi/procurementrecord/utils/StockUtils.java
@@ -156,6 +156,9 @@
         stockInventoryDto.setIsContainsWater(isContainsWater);
         stockInventoryDto.setWaterContent(waterContent);
         stockInventoryDto.setTheoryStockInNum(theoryStockInNum);
+        if (theoryStockInNum != null && actualStockInNum != null) {
+            stockInventoryDto.setDifferenceNum(theoryStockInNum.subtract(actualStockInNum));
+        }
         stockInventoryService.addStockInRecordOnly(stockInventoryDto);
     }
 
diff --git a/src/main/java/com/ruoyi/purchase/dto/PurchaseStockInDto.java b/src/main/java/com/ruoyi/purchase/dto/PurchaseStockInDto.java
index 0f4cb5d..740491a 100644
--- a/src/main/java/com/ruoyi/purchase/dto/PurchaseStockInDto.java
+++ b/src/main/java/com/ruoyi/purchase/dto/PurchaseStockInDto.java
@@ -35,7 +35,7 @@
         @Schema(description = "鍚按閲�(%)")
         private BigDecimal waterContent;
 
-        @Schema(description = "瀹為檯鍏ュ簱鏁伴噺")
+        @Schema(description = "瀹為檯鍏ュ簱鏁伴噺锛屽悗绔細鎸夌悊璁烘暟閲忓拰鍚按閲忛噸鏂拌绠�")
         private BigDecimal actualInboundQuantity;
     }
 }
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 8c1fe41..a3458b8 100644
--- a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
@@ -22,7 +22,6 @@
 import com.ruoyi.basic.pojo.ProductModel;
 import com.ruoyi.basic.pojo.SupplierManage;
 import com.ruoyi.basic.utils.FileUtil;
-import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
 import com.ruoyi.common.exception.base.BaseException;
 import com.ruoyi.common.enums.ApprovalStatusEnum;
 import com.ruoyi.common.enums.ReviewStatusEnum;
@@ -1093,35 +1092,44 @@
             if (salesLedgerProduct == null) {
                 throw new BaseException("閲囪喘浜у搧涓嶅瓨鍦�");
             }
+            if (!purchaseLedger.getId().equals(salesLedgerProduct.getSalesLedgerId()) || !Integer.valueOf(2).equals(salesLedgerProduct.getType())) {
+                throw new BaseException("閲囪喘浜у搧涓庨噰璐彴璐︿笉鍖归厤");
+            }
 
-            // 鑾峰彇鐞嗚鍏ュ簱鏁伴噺锛堝墠绔紶鍏ョ殑inboundQuantity锛�
+            ProductModel productModel = productModelMapper.selectById(salesLedgerProduct.getProductModelId());
+            if (Boolean.TRUE.equals(salesLedgerProduct.getIsChecked())) {
+                throw new BaseException("浜у搧銆�" + productModel.getModel() + "銆戦渶瑕佽川妫�锛岃璧拌川妫�鍏ュ簱娴佺▼");
+            }
+
             BigDecimal theoryStockInNum = item.getInboundQuantity();
             if (theoryStockInNum == null) {
                 theoryStockInNum = salesLedgerProduct.getQuantity();
             }
-            // 绌哄�煎拰闈炴鏁版牎楠�
             if (theoryStockInNum == null || theoryStockInNum.compareTo(BigDecimal.ZERO) <= 0) {
                 throw new BaseException("鐞嗚鍏ュ簱鏁伴噺蹇呴』澶т簬0");
             }
 
-            // 鑾峰彇瀹為檯鍏ュ簱鏁伴噺锛堝墠绔紶鍏ョ殑actualInboundQuantity锛�
-            BigDecimal actualStockInNum = item.getActualInboundQuantity();
-            if (actualStockInNum == null) {
-                actualStockInNum = theoryStockInNum;
+            BigDecimal approvedStockInNum = getApprovedPurchaseStockInNum(
+                    purchaseLedger.getId(),
+                    purchaseLedger.getPurchaseContractNumber(),
+                    salesLedgerProduct.getId(),
+                    salesLedgerProduct.getProductModelId()
+            );
+            BigDecimal remainingStockInNum = salesLedgerProduct.getQuantity().subtract(approvedStockInNum);
+            if (remainingStockInNum.compareTo(BigDecimal.ZERO) <= 0) {
+                throw new BaseException("浜у搧銆�" +  productModel.getModel() + "銆戝凡瀹屾垚鍏ュ簱");
             }
-            // 闈炴鏁版牎楠�
+            if (theoryStockInNum.compareTo(remainingStockInNum) > 0) {
+                throw new BaseException("浜у搧銆�" +  productModel.getModel() + "銆戠悊璁哄叆搴撴暟閲忎笉鑳藉ぇ浜庡緟鍏ュ簱鏁伴噺" + remainingStockInNum.stripTrailingZeros().toPlainString());
+            }
+
+            Boolean isContainsWater = Boolean.TRUE.equals(item.getIsContainsWater());
+            BigDecimal waterContent = normalizeWaterContent(isContainsWater, item.getWaterContent());
+            BigDecimal actualStockInNum = calculateActualStockInNum(theoryStockInNum, isContainsWater, waterContent);
             if (actualStockInNum.compareTo(BigDecimal.ZERO) <= 0) {
                 throw new BaseException("瀹為檯鍏ュ簱鏁伴噺蹇呴』澶т簬0");
             }
 
-            // 鑾峰彇鏄惁鍚按鍜屽惈姘撮噺
-            Boolean isContainsWater = item.getIsContainsWater();
-            BigDecimal waterContent = item.getWaterContent();
-            if (waterContent == null) {
-                waterContent = BigDecimal.ZERO;
-            }
-
-            // 璋冪敤StockUtils杩涜鍏ュ簱锛堝甫鍚按閲忎俊鎭級
             stockUtils.addStockWithBatchNo(
                     salesLedgerProduct.getProductModelId(),
                     actualStockInNum,
@@ -1139,6 +1147,41 @@
         return count;
     }
 
+    private BigDecimal getApprovedPurchaseStockInNum(Long purchaseLedgerId, String purchaseContractNumber, Long productId, Long productModelId) {
+        if (purchaseLedgerId == null || productId == null || productModelId == null || !StringUtils.hasText(purchaseContractNumber)) {
+            return BigDecimal.ZERO;
+        }
+        List<StockInRecord> stockRecords = findDirectStockRecords(purchaseLedgerId, purchaseContractNumber, productModelId, productId);
+        return stockRecords.stream()
+                .filter(Objects::nonNull)
+                .filter(item -> ReviewStatusEnum.APPROVED.getCode().equals(item.getApprovalStatus()))
+                .map(StockInRecord::getStockInNum)
+                .filter(Objects::nonNull)
+                .reduce(BigDecimal.ZERO, BigDecimal::add);
+    }
+
+    private BigDecimal normalizeWaterContent(Boolean isContainsWater, BigDecimal waterContent) {
+        if (!Boolean.TRUE.equals(isContainsWater)) {
+            return BigDecimal.ZERO;
+        }
+        if (waterContent == null) {
+            throw new BaseException("鍚按鏃跺繀椤诲~鍐欏惈姘撮噺");
+        }
+        if (waterContent.compareTo(BigDecimal.ZERO) < 0 || waterContent.compareTo(new BigDecimal("100")) >= 0) {
+            throw new BaseException("鍚按閲忓繀椤诲湪0鍒�100涔嬮棿");
+        }
+        return waterContent;
+    }
+
+    private BigDecimal calculateActualStockInNum(BigDecimal theoryStockInNum, Boolean isContainsWater, BigDecimal waterContent) {
+        if (!Boolean.TRUE.equals(isContainsWater)) {
+            return theoryStockInNum;
+        }
+        BigDecimal percent = waterContent.divide(new BigDecimal("100"), 6, RoundingMode.HALF_UP);
+        BigDecimal actualStockInNum = theoryStockInNum.multiply(BigDecimal.ONE.subtract(percent));
+        return actualStockInNum.setScale(4, RoundingMode.HALF_UP);
+    }
+
     /**
      * 涓嬪垝绾垮懡鍚嶈浆椹煎嘲鍛藉悕
      */
diff --git a/src/main/java/com/ruoyi/stock/dto/StockInventoryDto.java b/src/main/java/com/ruoyi/stock/dto/StockInventoryDto.java
index e6ae203..446b931 100644
--- a/src/main/java/com/ruoyi/stock/dto/StockInventoryDto.java
+++ b/src/main/java/com/ruoyi/stock/dto/StockInventoryDto.java
@@ -101,4 +101,7 @@
 
     @Schema(description = "鐞嗚鍏ュ簱鏁伴噺")
     private BigDecimal theoryStockInNum;
+
+    @Schema(description = "宸锛堢悊璁哄叆搴撴暟閲�-瀹為檯鍏ュ簱鏁伴噺锛�")
+    private BigDecimal differenceNum;
 }
diff --git a/src/main/java/com/ruoyi/stock/pojo/StockInRecord.java b/src/main/java/com/ruoyi/stock/pojo/StockInRecord.java
index b06df49..8c2e664 100644
--- a/src/main/java/com/ruoyi/stock/pojo/StockInRecord.java
+++ b/src/main/java/com/ruoyi/stock/pojo/StockInRecord.java
@@ -88,4 +88,8 @@
 
     @Schema(description = "鐞嗚鍏ュ簱鏁伴噺")
     private BigDecimal theoryStockInNum;
+
+    @TableField(exist = false)
+    @Schema(description = "宸锛堢悊璁哄叆搴撴暟閲�-瀹為檯鍏ュ簱鏁伴噺锛�")
+    private BigDecimal differenceNum;
 }
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 2436940..e0aa340 100644
--- a/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
+++ b/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
@@ -36,6 +36,7 @@
 import org.springframework.util.CollectionUtils;
 
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.List;
 
 @Service
@@ -231,6 +232,7 @@
             final BigDecimal finalStockInNum;
             if (item.getStockInNum() != null && item.getStockInNum().compareTo(BigDecimal.ZERO) > 0) {
                 finalStockInNum = item.getStockInNum();
+                adjustPurchaseInboundAuditFields(stockInRecord, finalStockInNum);
                 // 鏇存柊鍏ュ簱璁板綍鐨勫叆搴撴暟閲�
                 stockInRecord.setStockInNum(finalStockInNum);
             } else {
@@ -296,6 +298,28 @@
         return items.size();
     }
 
+    private void adjustPurchaseInboundAuditFields(StockInRecord stockInRecord, BigDecimal finalStockInNum) {
+        if (stockInRecord == null || finalStockInNum == null) {
+            return;
+        }
+        if (!StockInQualifiedRecordTypeEnum.PURCHASE_STOCK_IN.getCode().equals(stockInRecord.getRecordType())) {
+            return;
+        }
+        BigDecimal theoryStockInNum = stockInRecord.getTheoryStockInNum();
+        if (theoryStockInNum == null || theoryStockInNum.compareTo(BigDecimal.ZERO) <= 0) {
+            return;
+        }
+        if (finalStockInNum.compareTo(theoryStockInNum) > 0) {
+            throw new BaseException("閲囪喘鍏ュ簱瀹℃牳鏃讹紝瀹為檯鍏ュ簱鏁伴噺涓嶈兘澶т簬鐞嗚鍏ュ簱鏁伴噺");
+        }
+        if (Boolean.TRUE.equals(stockInRecord.getIsContainsWater())) {
+            BigDecimal waterContent = theoryStockInNum.subtract(finalStockInNum)
+                    .multiply(new BigDecimal("100"))
+                    .divide(theoryStockInNum, 4, RoundingMode.HALF_UP);
+            stockInRecord.setWaterContent(waterContent);
+        }
+    }
+
     private static @NonNull StockInventoryDto getStockInventoryDto(StockInRecord stockInRecord) {
         StockInventoryDto stockInventoryDto = new StockInventoryDto();
         stockInventoryDto.setProductModelId(stockInRecord.getProductModelId());
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 44302ec..dd0bcc2 100644
--- a/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
+++ b/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
@@ -194,6 +194,7 @@
         stockInRecordDto.setIsContainsWater(stockInventoryDto.getIsContainsWater());
         stockInRecordDto.setWaterContent(stockInventoryDto.getWaterContent());
         stockInRecordDto.setTheoryStockInNum(stockInventoryDto.getTheoryStockInNum());
+        stockInRecordDto.setDifferenceNum(stockInventoryDto.getDifferenceNum());
         stockInRecordService.add(stockInRecordDto);
         return true;
     }
diff --git a/src/main/resources/mapper/stock/StockInRecordMapper.xml b/src/main/resources/mapper/stock/StockInRecordMapper.xml
index a3bf3f9..b46ffc6 100644
--- a/src/main/resources/mapper/stock/StockInRecordMapper.xml
+++ b/src/main/resources/mapper/stock/StockInRecordMapper.xml
@@ -16,6 +16,10 @@
         )
         SELECT
         sir.*,
+        CASE
+            WHEN sir.theory_stock_in_num IS NULL THEN NULL
+            ELSE sir.theory_stock_in_num - sir.stock_in_num
+        END AS differenceNum,
         p.product_name as product_name,
         pm.model,
         pm.unit,
@@ -137,6 +141,10 @@
     <select id="listStockInRecordExportData" resultType="com.ruoyi.stock.execl.StockInRecordExportData">
         SELECT
         sir.*,
+        CASE
+            WHEN sir.theory_stock_in_num IS NULL THEN NULL
+            ELSE sir.theory_stock_in_num - sir.stock_in_num
+        END AS differenceNum,
         p.product_name as product_name,
         pm.model,
         pm.unit,
@@ -169,6 +177,12 @@
             DATE(sir.create_time) AS inboundDate,
             p.product_name,
             pm.model as specification_model,
+            sir.theory_stock_in_num,
+            sir.stock_in_num,
+            CASE
+                WHEN sir.theory_stock_in_num IS NULL THEN NULL
+                ELSE sir.theory_stock_in_num - sir.stock_in_num
+            END AS differenceNum,
             sir.stock_in_num * slp.tax_inclusive_unit_price AS InboundAmount,
             pl.purchase_contract_number
             FROM stock_in_record sir
@@ -177,12 +191,26 @@
             -- 鍔ㄦ�佸叧鑱旈噰璐紙鑷姩閫傞厤 7 鍜� 10锛�
             LEFT JOIN purchase_ledger pl
             ON pl.id = IF(sir.record_type = 7, sir.record_id, qi.purchase_ledger_id)
-            -- 浜у搧鍏宠仈涓嶅姩
-            LEFT JOIN sales_ledger_product slp ON pl.id = slp.sales_ledger_id
+            LEFT JOIN sales_ledger_product slp
+                ON slp.type = 2
+                AND (
+                    (sir.record_type = 7
+                        AND slp.sales_ledger_id = pl.id
+                        AND (
+                            (sir.batch_no IS NOT NULL AND sir.batch_no LIKE CONCAT('%-', slp.id))
+                            OR (sir.batch_no IS NULL AND slp.product_model_id = sir.product_model_id)
+                        )
+                    )
+                    OR
+                    (sir.record_type = 10
+                        AND slp.sales_ledger_id = pl.id
+                        AND slp.product_model_id = sir.product_model_id
+                    )
+                )
             LEFT JOIN product_model pm ON sir.product_model_id = pm.id
             LEFT JOIN product p ON pm.product_id = p.id
             -- 鏉′欢
-        WHERE sir.approval_status = 1 AND slp.type = 2
+        WHERE sir.approval_status = 1
         AND sir.record_type IN ('7','10')
         <if test="req.inboundBatches != null and req.inboundBatches != ''">
             AND sir.inbound_batches LIKE CONCAT('%',#{req.inboundBatches},'%')

--
Gitblit v1.9.3