gongchunyi
4 小时以前 3481d209ec847542b73fa16616ffe0e13c949e80
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);
        }
    }