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/purchase/service/impl/PurchaseLedgerServiceImpl.java |  446 ++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 385 insertions(+), 61 deletions(-)

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 37ca018..3e3bafc 100644
--- a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
@@ -1,29 +1,28 @@
 package com.ruoyi.purchase.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.ruoyi.account.pojo.AccountExpense;
-import com.ruoyi.account.pojo.AccountIncome;
 import com.ruoyi.account.service.AccountExpenseService;
-import com.ruoyi.account.service.AccountIncomeService;
 import com.ruoyi.approve.pojo.ApproveProcess;
 import com.ruoyi.approve.service.impl.ApproveProcessServiceImpl;
 import com.ruoyi.approve.vo.ApproveProcessVO;
 import com.ruoyi.basic.mapper.ProductMapper;
 import com.ruoyi.basic.mapper.ProductModelMapper;
 import com.ruoyi.basic.mapper.SupplierManageMapper;
-import com.ruoyi.basic.pojo.Customer;
 import com.ruoyi.basic.pojo.Product;
 import com.ruoyi.basic.pojo.ProductModel;
 import com.ruoyi.basic.pojo.SupplierManage;
 import com.ruoyi.common.enums.FileNameType;
+import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
+import com.ruoyi.common.enums.StockInUnQualifiedRecordTypeEnum;
+import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum;
+import com.ruoyi.common.enums.StockOutUnQualifiedRecordTypeEnum;
+import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.exception.base.BaseException;
-import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
@@ -32,26 +31,33 @@
 import com.ruoyi.other.mapper.TempFileMapper;
 import com.ruoyi.other.pojo.TempFile;
 import com.ruoyi.procurementrecord.mapper.ProcurementRecordMapper;
+import com.ruoyi.procurementrecord.utils.StockUtils;
 import com.ruoyi.procurementrecord.pojo.ProcurementRecordStorage;
 import com.ruoyi.project.system.domain.SysUser;
 import com.ruoyi.project.system.mapper.SysUserMapper;
 import com.ruoyi.purchase.dto.PurchaseLedgerDto;
 import com.ruoyi.purchase.dto.PurchaseLedgerImportDto;
 import com.ruoyi.purchase.dto.PurchaseLedgerProductImportDto;
+import com.ruoyi.purchase.dto.PurchaseScanStockDto;
 import com.ruoyi.purchase.mapper.*;
 import com.ruoyi.purchase.pojo.*;
 import com.ruoyi.purchase.service.IPurchaseLedgerService;
 import com.ruoyi.quality.mapper.*;
 import com.ruoyi.quality.pojo.*;
-import com.ruoyi.sales.dto.SalesLedgerImportDto;
-import com.ruoyi.sales.dto.SalesLedgerProductImportDto;
 import com.ruoyi.sales.mapper.*;
 import com.ruoyi.sales.pojo.CommonFile;
 import com.ruoyi.sales.pojo.InvoiceRegistrationProduct;
 import com.ruoyi.sales.pojo.SalesLedger;
 import com.ruoyi.sales.pojo.SalesLedgerProduct;
 import com.ruoyi.sales.service.impl.CommonFileServiceImpl;
-import lombok.RequiredArgsConstructor;
+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;
@@ -78,6 +84,8 @@
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
+import static com.ruoyi.common.enums.SaleEnum.PURCHASE;
+
 /**
  * 閲囪喘鍙拌处Service涓氬姟灞傚鐞�
  *
@@ -88,68 +96,80 @@
 @Slf4j
 public class PurchaseLedgerServiceImpl extends ServiceImpl<PurchaseLedgerMapper, PurchaseLedger> implements IPurchaseLedgerService {
     @Autowired
-    private  AccountExpenseService accountExpenseService;
+    private AccountExpenseService accountExpenseService;
     @Autowired
-    private  PurchaseLedgerMapper purchaseLedgerMapper;
+    private PurchaseLedgerMapper purchaseLedgerMapper;
 
     @Autowired
-    private  SalesLedgerMapper salesLedgerMapper;
+    private SalesLedgerMapper salesLedgerMapper;
     @Autowired
-    private  SalesLedgerProductMapper salesLedgerProductMapper;
+    private SalesLedgerProductMapper salesLedgerProductMapper;
 
     @Autowired
-    private  SysUserMapper userMapper;
+    private SysUserMapper userMapper;
 
     @Autowired
-    private  TempFileMapper tempFileMapper;
+    private TempFileMapper tempFileMapper;
 
     @Autowired
-    private  CommonFileMapper commonFileMapper;
+    private CommonFileMapper commonFileMapper;
 
     @Autowired
-    private  SupplierManageMapper supplierManageMapper;
+    private SupplierManageMapper supplierManageMapper;
 
     @Autowired
-    private  ProductMapper productMapper;
+    private ProductMapper productMapper;
 
     @Autowired
-    private  ProductModelMapper productModelMapper;
+    private ProductModelMapper productModelMapper;
 
     @Autowired
-    private  SysUserMapper sysUserMapper;
+    private SysUserMapper sysUserMapper;
 
     @Autowired
-    private  TicketRegistrationMapper ticketRegistrationMapper;
+    private TicketRegistrationMapper ticketRegistrationMapper;
 
     @Autowired
-    private  ProductRecordMapper productRecordMapper;
+    private ProductRecordMapper productRecordMapper;
 
     @Autowired
-    private  PaymentRegistrationMapper paymentRegistrationMapper;
+    private PaymentRegistrationMapper paymentRegistrationMapper;
     @Autowired
-    private  InvoiceRegistrationProductMapper invoiceRegistrationProductMapper;
+    private InvoiceRegistrationProductMapper invoiceRegistrationProductMapper;
     @Autowired
-    private  StringRedisTemplate redisTemplate;
+    private StringRedisTemplate redisTemplate;
     @Autowired
-    private  QualityInspectMapper qualityInspectMapper;
+    private QualityInspectMapper qualityInspectMapper;
     @Autowired
-    private  CommonFileServiceImpl commonFileService;
+    private CommonFileServiceImpl commonFileService;
     @Autowired
-    private  QualityTestStandardBindingMapper qualityTestStandardBindingMapper;
+    private QualityTestStandardBindingMapper qualityTestStandardBindingMapper;
     @Autowired
-    private  QualityTestStandardParamMapper qualityTestStandardParamMapper;
+    private QualityTestStandardParamMapper qualityTestStandardParamMapper;
     @Autowired
-    private  QualityTestStandardMapper qualityTestStandardMapper;
+    private QualityTestStandardMapper qualityTestStandardMapper;
     @Autowired
-    private  QualityInspectParamMapper qualityInspectParamMapper;
+    private QualityInspectParamMapper qualityInspectParamMapper;
     @Autowired
-    private  ApproveProcessServiceImpl approveProcessService;
+    private ApproveProcessServiceImpl approveProcessService;
     @Autowired
-    private  ProcurementRecordMapper procurementRecordStorageMapper;
+    private ProcurementRecordMapper procurementRecordStorageMapper;
     @Autowired
-    private  PurchaseLedgerTemplateMapper purchaseLedgerTemplateMapper;
+    private PurchaseLedgerTemplateMapper purchaseLedgerTemplateMapper;
     @Autowired
-    private  SalesLedgerProductTemplateMapper salesLedgerProductTemplateMapper;
+    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}")
     private String uploadDir;
 
@@ -158,6 +178,12 @@
         LambdaQueryWrapper<PurchaseLedger> queryWrapper = new LambdaQueryWrapper<>();
         if (StringUtils.isNotBlank(purchaseLedger.getPurchaseContractNumber())) {
             queryWrapper.like(PurchaseLedger::getPurchaseContractNumber, purchaseLedger.getPurchaseContractNumber());
+        }
+        if (purchaseLedger.getSupplierId() != null) {
+            queryWrapper.eq(PurchaseLedger::getSupplierId, purchaseLedger.getSupplierId());
+        }
+        if (purchaseLedger.getApprovalStatus() != null) {
+            queryWrapper.eq(PurchaseLedger::getApprovalStatus, purchaseLedger.getApprovalStatus());
         }
         return purchaseLedgerMapper.selectList(queryWrapper);
     }
@@ -209,15 +235,15 @@
         if (productList != null && !productList.isEmpty()) {
             handleSalesLedgerProducts(purchaseLedger.getId(), productList, purchaseLedgerDto.getType());
         }
-        //鏂板鍘熸潗鏂欐楠�
-        if (productList != null) {
-            for (SalesLedgerProduct saleProduct : productList) {
-                //鏄惁鎺ㄩ�佽川妫�锛屽鏋渢rue灏辨坊鍔�
-                if (saleProduct.getIsChecked()) {
-                    addQualityInspect(purchaseLedger, saleProduct);
-                }
-            }
-        }
+        //鏂板鍘熸潗鏂欐楠�  瀹℃壒涔嬪悗鎵嶇敓鎴愭楠�
+//        if (productList != null) {
+//            for (SalesLedgerProduct saleProduct : productList) {
+//                //鏄惁鎺ㄩ�佽川妫�锛屽鏋渢rue灏辨坊鍔�
+//                if (saleProduct.getIsChecked()) {
+//                    addQualityInspect(purchaseLedger, saleProduct);
+//                }
+//            }
+//        }
         // 5. 杩佺Щ涓存椂鏂囦欢鍒版寮忕洰褰�
         if (purchaseLedgerDto.getTempFileIds() != null && !purchaseLedgerDto.getTempFileIds().isEmpty()) {
             migrateTempFilesToFormal(purchaseLedger.getId(), purchaseLedgerDto.getTempFileIds());
@@ -238,12 +264,12 @@
         qualityInspect.setUnit(saleProduct.getUnit());
         qualityInspect.setQuantity(saleProduct.getQuantity());
         qualityInspectMapper.insert(qualityInspect);
-        List<QualityTestStandard> qualityTestStandard = qualityTestStandardMapper.getQualityTestStandardByProductId(saleProduct.getProductId(), 0,null);
-        if (qualityTestStandard.size()>0){
+        List<QualityTestStandard> qualityTestStandard = qualityTestStandardMapper.getQualityTestStandardByProductId(saleProduct.getProductId(), 0, null);
+        if (qualityTestStandard.size() > 0) {
             qualityInspect.setTestStandardId(qualityTestStandard.get(0).getId());
             qualityInspectMapper.updateById(qualityInspect);
             qualityTestStandardParamMapper.selectList(Wrappers.<QualityTestStandardParam>lambdaQuery()
-                    .eq(QualityTestStandardParam::getTestStandardId,qualityTestStandard.get(0).getId()))
+                            .eq(QualityTestStandardParam::getTestStandardId, qualityTestStandard.get(0).getId()))
                     .forEach(qualityTestStandardParam -> {
                         QualityInspectParam param = new QualityInspectParam();
                         com.ruoyi.common.utils.bean.BeanUtils.copyProperties(qualityTestStandardParam, param);
@@ -310,6 +336,7 @@
         if (!updateList.isEmpty()) {
             for (SalesLedgerProduct product : updateList) {
                 product.setType(type);
+                product.fillRemainingQuantity();
                 salesLedgerProductMapper.updateById(product);
             }
         }
@@ -324,6 +351,7 @@
                 salesLedgerProduct.setFutureTickets(salesLedgerProduct.getQuantity());
                 salesLedgerProduct.setFutureTicketsAmount(salesLedgerProduct.getTaxInclusiveTotalPrice());
                 salesLedgerProduct.setPendingTicketsTotal(salesLedgerProduct.getTaxInclusiveTotalPrice());
+                salesLedgerProduct.fillRemainingQuantity();
                 salesLedgerProductMapper.insert(salesLedgerProduct);
             }
         }
@@ -419,12 +447,12 @@
     @Transactional(rollbackFor = Exception.class)
     public int deletePurchaseLedgerByIds(Long[] ids) {
         if (ids == null || ids.length == 0) {
-           throw new BaseException("璇烽�変腑鑷冲皯涓�鏉℃暟鎹�");
+            throw new BaseException("璇烽�変腑鑷冲皯涓�鏉℃暟鎹�");
         }
         for (Long id : ids) {
             PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(id);
             if (purchaseLedger.getApprovalStatus().equals(3)) {
-                throw new BaseException(purchaseLedger.getPurchaseContractNumber()+"宸茬粡瀹℃壒閫氳繃锛屼笉鍏佽鍒犻櫎");
+                throw new BaseException(purchaseLedger.getPurchaseContractNumber() + "宸茬粡瀹℃壒閫氳繃锛屼笉鍏佽鍒犻櫎");
             }
         }
         // 鎵归噺鍒犻櫎鍏宠仈鐨勯噰璐叆搴撹褰�
@@ -433,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<>();
@@ -447,11 +503,11 @@
         salesLedgerProductMapper.delete(queryWrapper);
         // 鎵归噺鍒犻櫎鍏宠仈鐨勯噰璐彴璐︾殑鏉ョエ鐧昏
         LambdaQueryWrapper<TicketRegistration> ticketRegistrationLambdaQueryWrapper = new LambdaQueryWrapper<>();
-        ticketRegistrationLambdaQueryWrapper.in(TicketRegistration::getPurchaseLedgerId,ids);
+        ticketRegistrationLambdaQueryWrapper.in(TicketRegistration::getPurchaseLedgerId, ids);
         ticketRegistrationMapper.delete(ticketRegistrationLambdaQueryWrapper);
         // 鎵归噺鍒犻櫎鍏宠仈鐨勯噰璐彴璐︾殑鏉ョエ鐧昏璁板綍
         LambdaQueryWrapper<ProductRecord> productRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
-        productRecordLambdaQueryWrapper.in(ProductRecord::getPurchaseLedgerId,ids);
+        productRecordLambdaQueryWrapper.in(ProductRecord::getPurchaseLedgerId, ids);
         productRecordMapper.delete(productRecordLambdaQueryWrapper);
         // 鎵归噺鍒犻櫎浠樻鐧昏
         LambdaQueryWrapper<PaymentRegistration> paymentRegistrationLambdaQueryWrapper = new LambdaQueryWrapper<>();
@@ -463,7 +519,7 @@
 
         List<QualityInspect> qualityInspects = qualityInspectMapper.selectList(materialInspectLambdaQueryWrapper);
         qualityInspects.stream().forEach(qualityInspect -> {
-            if (ObjectUtils.isNotEmpty(qualityInspect.getInspectState())&&qualityInspect.getInspectState().equals(1)) {
+            if (ObjectUtils.isNotEmpty(qualityInspect.getInspectState()) && qualityInspect.getInspectState().equals(1)) {
                 throw new BaseException("宸叉彁浜ょ殑妫�楠屽崟涓嶈兘鍒犻櫎");
             }
         });
@@ -479,7 +535,7 @@
         // 鍒犻櫎閲囪喘瀹℃壒璁板綍
         for (Long id : ids) {
             PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(id);
-            if(purchaseLedger != null){
+            if (purchaseLedger != null) {
                 ApproveProcess one = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>()
                         .eq(ApproveProcess::getApproveType, 5)
                         .eq(ApproveProcess::getApproveDelete, 0)
@@ -511,11 +567,12 @@
         productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, purchaseLedger.getId())
                 .eq(SalesLedgerProduct::getType, purchaseLedgerDto.getType());
         List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper);
+        products.forEach(SalesLedgerProduct::fillRemainingQuantity);
 
         // 3.鏌ヨ涓婁紶鏂囦欢
         LambdaQueryWrapper<CommonFile> salesLedgerFileWrapper = new LambdaQueryWrapper<>();
         salesLedgerFileWrapper.eq(CommonFile::getCommonId, purchaseLedger.getId())
-                .eq(CommonFile::getType,FileNameType.PURCHASE.getValue());
+                .eq(CommonFile::getType, FileNameType.PURCHASE.getValue());
         List<CommonFile> salesLedgerFiles = commonFileMapper.selectList(salesLedgerFileWrapper);
 
         // 4. 杞崲 DTO
@@ -571,6 +628,7 @@
             product.setTicketsAmount(null);
             product.setTempFutureTickets(product.getFutureTickets());
             product.setTempFutureTicketsAmount(product.getFutureTicketsAmount());
+            product.fillRemainingQuantity();
         });
         resultDto.setProductData(productList);
         return resultDto;
@@ -666,7 +724,7 @@
             // 渚涘簲鍟嗘暟鎹�
             List<SupplierManage> customers = supplierManageMapper.selectList(new LambdaQueryWrapper<SupplierManage>().in(SupplierManage::getSupplierName,
                     salesLedgerImportDtoList.stream().map(PurchaseLedgerImportDto::getSupplierName).collect(Collectors.toList())));
-            List<Map<String,Object>> list = productModelMapper.getProductAndModelList();
+            List<Map<String, Object>> list = productModelMapper.getProductAndModelList();
             // 褰曞叆浜烘暟鎹�
             List<SysUser> sysUsers = sysUserMapper.selectList(new LambdaQueryWrapper<SysUser>().in(SysUser::getNickName,
                     salesLedgerImportDtoList.stream().map(PurchaseLedgerImportDto::getRecorderName).collect(Collectors.toList())));
@@ -674,7 +732,7 @@
                 PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectOne(new LambdaQueryWrapper<PurchaseLedger>()
                         .eq(PurchaseLedger::getPurchaseContractNumber, salesLedgerImportDto.getPurchaseContractNumber())
                         .last("limit 1"));
-                if(purchaseLedger != null){
+                if (purchaseLedger != null) {
                     continue;
                 }
                 PurchaseLedger salesLedger = new PurchaseLedger();
@@ -701,12 +759,12 @@
                     throw new RuntimeException("閲囪喘鍗曞彿:" + salesLedgerImportDto.getPurchaseContractNumber() + ",鏃犲搴斾骇鍝佹暟鎹紒");
                 salesLedger.setContractAmount(salesLedgerProductImportDtos.stream()
                         .map(PurchaseLedgerProductImportDto::getTaxInclusiveTotalPrice)
-                        .reduce(BigDecimal.ZERO,BigDecimal::add));
+                        .reduce(BigDecimal.ZERO, BigDecimal::add));
                 // 閫氳繃閿�鍞崟鍙风粦瀹氶攢鍞�
                 SalesLedger salesLedger1 = salesLedgerMapper.selectOne(new LambdaQueryWrapper<SalesLedger>()
                         .eq(SalesLedger::getSalesContractNo, salesLedger.getSalesContractNo())
                         .last("LIMIT 1"));
-                if(salesLedger1 != null){
+                if (salesLedger1 != null) {
                     salesLedger.setSalesLedgerId(salesLedger1.getId());
                 }
                 // 閲囪喘瀹℃牳
@@ -747,13 +805,14 @@
                     salesLedgerProduct.setPendingTicketsTotal(salesLedgerProductImportDto.getTaxInclusiveTotalPrice());
                     // 鏄惁璐ㄦ鍒ゆ柇
                     salesLedgerProduct.setIsChecked(salesLedgerProductImportDto.getIsChecked() == 1);
-                    if(salesLedgerProductImportDto.getIsChecked() == 1){
+                    if (salesLedgerProductImportDto.getIsChecked() == 1) {
                         addQualityInspect(salesLedger, salesLedgerProduct);
                     }
+                    salesLedgerProduct.fillRemainingQuantity();
                     salesLedgerProductMapper.insert(salesLedgerProduct);
                 }
                 // 閲囪喘瀹℃牳
-                addApproveByPurchase(loginUser,salesLedger);
+                addApproveByPurchase(loginUser, salesLedger);
             }
 
             return AjaxResult.success("瀵煎叆鎴愬姛");
@@ -778,6 +837,7 @@
         productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, purchaseLedger.getId())
                 .eq(SalesLedgerProduct::getType, 2);
         List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper);
+        products.forEach(SalesLedgerProduct::fillRemainingQuantity);
 
         // 4. 杞崲 DTO
         PurchaseLedgerDto resultDto = new PurchaseLedgerDto();
@@ -789,7 +849,7 @@
         return resultDto;
     }
 
-    public void addApproveByPurchase(LoginUser loginUser,PurchaseLedger purchaseLedger) throws Exception {
+    public void addApproveByPurchase(LoginUser loginUser, PurchaseLedger purchaseLedger) throws Exception {
         ApproveProcessVO approveProcessVO = new ApproveProcessVO();
         approveProcessVO.setApproveType(5);
         approveProcessVO.setApproveDeptId(loginUser.getCurrentDeptId());
@@ -800,6 +860,270 @@
         approveProcessService.addApprove(approveProcessVO);
     }
 
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void scanInbound(PurchaseScanStockDto dto) {
+        if (dto == null || dto.getPurchaseLedgerId() == null) {
+            throw new ServiceException("閲囪喘鍏ュ簱澶辫触,鍏ュ簱鏁版嵁涓嶈兘涓虹┖");
+        }
+        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("閲囪喘鍏ュ簱澶辫触,鍏ュ簱浜у搧涓嶈兘涓虹┖");
+        }
+        int purchaseType = PURCHASE.getCode();
+        Map<Long, BigDecimal> inboundQtyByLineId = new LinkedHashMap<>();
+        for (SalesLedgerProduct inbound : dto.getSalesLedgerProductList()) {
+            if (inbound == null || inbound.getId() == null) {
+                throw new ServiceException("鍏ュ簱澶辫触,閲囪喘浜у搧淇℃伅涓嶅畬鏁�");
+            }
+            BigDecimal inboundQty = inbound.getStockedQuantity();
+            if (inboundQty == null) {
+                throw new ServiceException("鍏ュ簱澶辫触,鍏ュ簱鏁伴噺涓嶈兘涓虹┖");
+            }
+            if (inboundQty.compareTo(BigDecimal.ZERO) < 0) {
+                throw new ServiceException("鍏ュ簱澶辫触,鍏ュ簱鏁伴噺涓嶈兘涓鸿礋鏁�");
+            }
+            inboundQtyByLineId.merge(inbound.getId(), inboundQty, BigDecimal::add);
+        }
+        Long purchaseId = purchaseLedger.getId();
+        for (Map.Entry<Long, BigDecimal> entry : inboundQtyByLineId.entrySet()) {
+            Long productLineId = entry.getKey();
+            BigDecimal inboundThisLine = entry.getValue();
+            if (inboundThisLine.compareTo(BigDecimal.ZERO) == 0) {
+                continue;
+            }
+            SalesLedgerProduct dbProduct = salesLedgerProductMapper.selectById(productLineId);
+            if (dbProduct == null) {
+                throw new ServiceException("鍏ュ簱澶辫触,閲囪喘浜у搧涓嶅瓨鍦�");
+            }
+            if (!Objects.equals(dbProduct.getSalesLedgerId(), purchaseId) || !Objects.equals(dbProduct.getType(), purchaseType)) {
+                throw new ServiceException("鍏ュ簱澶辫触,浜у搧涓庨噰璐彴璐︿笉鍖归厤");
+            }
+            if (dbProduct.getProductModelId() == null) {
+                throw new ServiceException("鍏ュ簱澶辫触,浜у搧瑙勬牸鏈淮鎶�,鏃犳硶鍏ュ簱");
+            }
+            BigDecimal oldStocked = dbProduct.getStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getStockedQuantity();
+            BigDecimal newStocked = oldStocked.add(inboundThisLine);
+
+            StockInventoryDto stockInventoryDto = new StockInventoryDto();
+            stockInventoryDto.setRecordId(dbProduct.getId());
+            stockInventoryDto.setRecordType(StockInQualifiedRecordTypeEnum.PURCHASE_SCAN_STOCK_IN.getCode());
+            stockInventoryDto.setQualitity(inboundThisLine);
+            stockInventoryDto.setProductModelId(dbProduct.getProductModelId());
+            stockInventoryDto.setSalesLedgerId(null);
+            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;
+            } else if (orderQty.compareTo(BigDecimal.ZERO) > 0 && newStocked.compareTo(orderQty) < 0) {
+                lineStockStatus = 1;
+            } else {
+                lineStockStatus = 2;
+            }
+            dbProduct.setStockedQuantity(newStocked);
+            dbProduct.setProductStockStatus(lineStockStatus);
+            dbProduct.fillRemainingQuantity();
+            salesLedgerProductMapper.updateById(dbProduct);
+        }
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void scanOutbound(PurchaseScanStockDto dto) {
+        if (dto == null || dto.getPurchaseLedgerId() == null) {
+            throw new ServiceException("閲囪喘鍑哄簱澶辫触,鍑哄簱鏁版嵁涓嶈兘涓虹┖");
+        }
+        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("閲囪喘鍑哄簱澶辫触,鍑哄簱浜у搧涓嶈兘涓虹┖");
+        }
+        int purchaseType = PURCHASE.getCode();
+        Map<Long, BigDecimal> outboundQtyByLineId = new LinkedHashMap<>();
+        for (SalesLedgerProduct line : dto.getSalesLedgerProductList()) {
+            if (line == null || line.getId() == null) {
+                throw new ServiceException("鍑哄簱澶辫触,閲囪喘浜у搧淇℃伅涓嶅畬鏁�");
+            }
+            BigDecimal outboundQty = line.getStockedQuantity();
+            if (outboundQty == null) {
+                throw new ServiceException("鍑哄簱澶辫触,鍑哄簱鏁伴噺涓嶈兘涓虹┖");
+            }
+            if (outboundQty.compareTo(BigDecimal.ZERO) < 0) {
+                throw new ServiceException("鍑哄簱澶辫触,鍑哄簱鏁伴噺涓嶈兘涓鸿礋鏁�");
+            }
+            outboundQtyByLineId.merge(line.getId(), outboundQty, BigDecimal::add);
+        }
+        Long purchaseId = purchaseLedger.getId();
+        for (Map.Entry<Long, BigDecimal> entry : outboundQtyByLineId.entrySet()) {
+            Long productLineId = entry.getKey();
+            BigDecimal outboundThisLine = entry.getValue();
+            if (outboundThisLine.compareTo(BigDecimal.ZERO) == 0) {
+                continue;
+            }
+            SalesLedgerProduct dbProduct = salesLedgerProductMapper.selectById(productLineId);
+            if (dbProduct == null) {
+                throw new ServiceException("鍑哄簱澶辫触,閲囪喘浜у搧涓嶅瓨鍦�");
+            }
+            if (!Objects.equals(dbProduct.getSalesLedgerId(), purchaseId) || !Objects.equals(dbProduct.getType(), purchaseType)) {
+                throw new ServiceException("鍑哄簱澶辫触,浜у搧涓庨噰璐彴璐︿笉鍖归厤");
+            }
+            if (dbProduct.getProductModelId() == null) {
+                throw new ServiceException("鍑哄簱澶辫触,浜у搧瑙勬牸鏈淮鎶�,鏃犳硶鍑哄簱");
+            }
+            stockUtils.assertQualifiedAvailable(dbProduct.getProductModelId(), outboundThisLine);
+
+            StockInventoryDto stockInventoryDto = new StockInventoryDto();
+            stockInventoryDto.setRecordId(dbProduct.getId());
+            stockInventoryDto.setRecordType(StockOutQualifiedRecordTypeEnum.PURCHASE_SCAN_STOCK_OUT.getCode());
+            stockInventoryDto.setQualitity(outboundThisLine);
+            stockInventoryDto.setProductModelId(dbProduct.getProductModelId());
+            stockInventoryDto.setSalesLedgerId(null);
+            stockInventoryDto.setSalesLedgerProductId(dbProduct.getId());
+            stockInventoryService.subtractStockInventory(stockInventoryDto);
+
+            BigDecimal oldShipped = dbProduct.getShippedQuantity() == null ? BigDecimal.ZERO : dbProduct.getShippedQuantity();
+            dbProduct.setShippedQuantity(oldShipped.add(outboundThisLine));
+            dbProduct.fillRemainingQuantity();
+            salesLedgerProductMapper.updateById(dbProduct);
+        }
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void scanInboundUnqualified(PurchaseScanStockDto dto) {
+        if (dto == null || dto.getPurchaseLedgerId() == null) {
+            throw new ServiceException("閲囪喘涓嶅悎鏍煎叆搴撳け璐�,鍏ュ簱鏁版嵁涓嶈兘涓虹┖");
+        }
+        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("閲囪喘涓嶅悎鏍煎叆搴撳け璐�,鍏ュ簱浜у搧涓嶈兘涓虹┖");
+        }
+        int purchaseType = PURCHASE.getCode();
+        Map<Long, BigDecimal> inboundQtyByLineId = new LinkedHashMap<>();
+        for (SalesLedgerProduct inbound : dto.getSalesLedgerProductList()) {
+            if (inbound == null || inbound.getId() == null) {
+                throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,閲囪喘浜у搧淇℃伅涓嶅畬鏁�");
+            }
+            BigDecimal inboundQty = inbound.getStockedQuantity();
+            if (inboundQty == null) {
+                throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,鍏ュ簱鏁伴噺涓嶈兘涓虹┖");
+            }
+            if (inboundQty.compareTo(BigDecimal.ZERO) < 0) {
+                throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,鍏ュ簱鏁伴噺涓嶈兘涓鸿礋鏁�");
+            }
+            inboundQtyByLineId.merge(inbound.getId(), inboundQty, BigDecimal::add);
+        }
+        Long purchaseId = purchaseLedger.getId();
+        for (Map.Entry<Long, BigDecimal> entry : inboundQtyByLineId.entrySet()) {
+            Long productLineId = entry.getKey();
+            BigDecimal inboundThisLine = entry.getValue();
+            if (inboundThisLine.compareTo(BigDecimal.ZERO) == 0) {
+                continue;
+            }
+            SalesLedgerProduct dbProduct = salesLedgerProductMapper.selectById(productLineId);
+            if (dbProduct == null) {
+                throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,閲囪喘浜у搧涓嶅瓨鍦�");
+            }
+            if (!Objects.equals(dbProduct.getSalesLedgerId(), purchaseId) || !Objects.equals(dbProduct.getType(), purchaseType)) {
+                throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,浜у搧涓庨噰璐彴璐︿笉鍖归厤");
+            }
+            if (dbProduct.getProductModelId() == null) {
+                throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,浜у搧瑙勬牸鏈淮鎶�,鏃犳硶鍏ュ簱");
+            }
+            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);
+        }
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void scanOutboundUnqualified(PurchaseScanStockDto dto) {
+        if (dto == null || dto.getPurchaseLedgerId() == null) {
+            throw new ServiceException("閲囪喘涓嶅悎鏍煎嚭搴撳け璐�,鍑哄簱鏁版嵁涓嶈兘涓虹┖");
+        }
+        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("閲囪喘涓嶅悎鏍煎嚭搴撳け璐�,鍑哄簱浜у搧涓嶈兘涓虹┖");
+        }
+        int purchaseType = PURCHASE.getCode();
+        Map<Long, BigDecimal> outboundQtyByLineId = new LinkedHashMap<>();
+        for (SalesLedgerProduct line : dto.getSalesLedgerProductList()) {
+            if (line == null || line.getId() == null) {
+                throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,閲囪喘浜у搧淇℃伅涓嶅畬鏁�");
+            }
+            BigDecimal outboundQty = line.getStockedQuantity();
+            if (outboundQty == null) {
+                throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,鍑哄簱鏁伴噺涓嶈兘涓虹┖");
+            }
+            if (outboundQty.compareTo(BigDecimal.ZERO) < 0) {
+                throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,鍑哄簱鏁伴噺涓嶈兘涓鸿礋鏁�");
+            }
+            outboundQtyByLineId.merge(line.getId(), outboundQty, BigDecimal::add);
+        }
+        Long purchaseId = purchaseLedger.getId();
+        for (Map.Entry<Long, BigDecimal> entry : outboundQtyByLineId.entrySet()) {
+            Long productLineId = entry.getKey();
+            BigDecimal outboundThisLine = entry.getValue();
+            if (outboundThisLine.compareTo(BigDecimal.ZERO) == 0) {
+                continue;
+            }
+            SalesLedgerProduct dbProduct = salesLedgerProductMapper.selectById(productLineId);
+            if (dbProduct == null) {
+                throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,閲囪喘浜у搧涓嶅瓨鍦�");
+            }
+            if (!Objects.equals(dbProduct.getSalesLedgerId(), purchaseId) || !Objects.equals(dbProduct.getType(), purchaseType)) {
+                throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,浜у搧涓庨噰璐彴璐︿笉鍖归厤");
+            }
+            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);
+        }
+    }
+
     /**
      * 涓嬪垝绾垮懡鍚嶈浆椹煎嘲鍛藉悕
      */

--
Gitblit v1.9.3