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;
@@ -135,6 +141,8 @@
    @Autowired
    private QualityInspectMapper qualityInspectMapper;
    @Autowired
    private QualityUnqualifiedMapper qualityUnqualifiedMapper;
    @Autowired
    private CommonFileServiceImpl commonFileService;
    @Autowired
    private QualityTestStandardBindingMapper qualityTestStandardBindingMapper;
@@ -154,6 +162,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}")
@@ -198,6 +214,7 @@
        purchaseLedger.setRecorderName(sysUser.getNickName());
        purchaseLedger.setPhoneNumber(sysUser.getPhonenumber());
        purchaseLedger.setApprovalStatus(1);
        purchaseLedger.setStockStatus(0);
        // 3. 新增或更新主表
        if (purchaseLedger.getId() == null) {
            purchaseLedgerMapper.insert(purchaseLedger);
@@ -322,6 +339,7 @@
        if (!updateList.isEmpty()) {
            for (SalesLedgerProduct product : updateList) {
                product.setType(type);
                product.setProductStockStatus(calculateProductStockStatus(product));
                product.fillRemainingQuantity();
                salesLedgerProductMapper.updateById(product);
            }
@@ -337,6 +355,7 @@
                salesLedgerProduct.setFutureTickets(salesLedgerProduct.getQuantity());
                salesLedgerProduct.setFutureTicketsAmount(salesLedgerProduct.getTaxInclusiveTotalPrice());
                salesLedgerProduct.setPendingTicketsTotal(salesLedgerProduct.getTaxInclusiveTotalPrice());
                salesLedgerProduct.setProductStockStatus(calculateProductStockStatus(salesLedgerProduct));
                salesLedgerProduct.fillRemainingQuantity();
                salesLedgerProductMapper.insert(salesLedgerProduct);
            }
@@ -447,6 +466,35 @@
                .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(),
                                                        StockInUnQualifiedRecordTypeEnum.PURCHASE_SCAN_QUALITY_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<>();
@@ -525,7 +573,7 @@
        productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, purchaseLedger.getId())
                .eq(SalesLedgerProduct::getType, purchaseLedgerDto.getType());
        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper);
        products.forEach(SalesLedgerProduct::fillRemainingQuantity);
        applyQualityInboundToProducts(purchaseLedger.getId(), products);
        // 3.查询上传文件
        LambdaQueryWrapper<CommonFile> salesLedgerFileWrapper = new LambdaQueryWrapper<>();
@@ -795,7 +843,7 @@
        productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, purchaseLedger.getId())
                .eq(SalesLedgerProduct::getType, 2);
        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper);
        products.forEach(SalesLedgerProduct::fillRemainingQuantity);
        applyQualityInboundToProducts(purchaseLedger.getId(), products);
        // 4. 转换 DTO
        PurchaseLedgerDto resultDto = new PurchaseLedgerDto();
@@ -827,6 +875,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("采购入库失败,入库产品不能为空");
@@ -863,7 +914,57 @@
            if (dbProduct.getProductModelId() == null) {
                throw new ServiceException("入库失败,产品规格未维护,无法入库");
            }
            BigDecimal orderQty = dbProduct.getQuantity() == null ? BigDecimal.ZERO : dbProduct.getQuantity();
            if (orderQty.compareTo(BigDecimal.ZERO) <= 0) {
                throw new ServiceException("入库失败,采购产品数量异常");
            }
            //  需要质检:扫码入库进入原材料检验,不直接入合格库存
            if (Boolean.TRUE.equals(dbProduct.getIsChecked())) {
                //  存在未通过/未处理的原材料检验单,则禁止继续扫码入库
                Long pendingInspectCount = qualityInspectMapper.selectCount(new LambdaQueryWrapper<QualityInspect>()
                        .eq(QualityInspect::getInspectType, 0)
                        .eq(QualityInspect::getPurchaseLedgerId, purchaseId)
                        .eq(QualityInspect::getProductModelId, dbProduct.getProductModelId())
                        .and(w -> w
                                .isNull(QualityInspect::getInspectState)
                                .or(q0 -> q0.eq(QualityInspect::getInspectState, 0))
                                // inspect_state=1 也视为“未处理”
                                .or(q1 -> q1.eq(QualityInspect::getInspectState, 1)
                                        .isNull(QualityInspect::getCheckResult))));
                if (pendingInspectCount != null && pendingInspectCount > 0) {
                    throw new ServiceException("入库失败,存在未通过或未处理的质检记录,请先处理后再扫码入库");
                }
                //  需要质检时,按“待检/已合格”的检验数量控制扫码上限
                BigDecimal inspectQty = qualityInspectMapper.selectList(new LambdaQueryWrapper<QualityInspect>()
                                .eq(QualityInspect::getInspectType, 0)
                                .eq(QualityInspect::getPurchaseLedgerId, purchaseId)
                                .eq(QualityInspect::getProductModelId, dbProduct.getProductModelId())
                                .and(w -> w
                                        .isNull(QualityInspect::getInspectState)
                                        .or(q0 -> q0.eq(QualityInspect::getInspectState, 0))
                                        .or(q1 -> q1.eq(QualityInspect::getInspectState, 1)
                                                .and(r -> r.isNull(QualityInspect::getCheckResult)
                                                        .or()
                                                        .eq(QualityInspect::getCheckResult, "合格")))))
                        .stream()
                        .map(QualityInspect::getQuantity)
                        .filter(Objects::nonNull)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                if (inspectQty.add(inboundThisLine).compareTo(orderQty) > 0) {
                    throw new ServiceException("入库失败,扫码合格入库数量不能超过采购产品数量");
                }
                SalesLedgerProduct scanInspectProduct = new SalesLedgerProduct();
                BeanUtils.copyProperties(dbProduct, scanInspectProduct);
                scanInspectProduct.setQuantity(inboundThisLine);
                addQualityInspect(purchaseLedger, scanInspectProduct);
                continue;
            }
            // 不需要质检:扫码直接入库(允许多入库,不做上限限制)
            BigDecimal oldStocked = dbProduct.getStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getStockedQuantity();
            if (oldStocked.add(inboundThisLine).compareTo(orderQty) > 0) {
                throw new ServiceException("入库失败,扫码合格入库数量不能超过采购产品数量");
            }
            BigDecimal newStocked = oldStocked.add(inboundThisLine);
            StockInventoryDto stockInventoryDto = new StockInventoryDto();
@@ -875,7 +976,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;
@@ -889,6 +989,7 @@
            dbProduct.fillRemainingQuantity();
            salesLedgerProductMapper.updateById(dbProduct);
        }
        refreshPurchaseLedgerStockStatus(purchaseLedger.getId());
    }
    @Override
@@ -900,6 +1001,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("采购出库失败,出库产品不能为空");
@@ -938,12 +1042,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 +1051,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 +1067,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("采购不合格入库失败,入库产品不能为空");
@@ -1015,7 +1107,33 @@
                throw new ServiceException("不合格入库失败,产品规格未维护,无法入库");
            }
            stockUtils.addUnStock(null, null, dbProduct.getProductModelId(), inboundThisLine,
                    StockInUnQualifiedRecordTypeEnum.PURCHASE_SCAN_UNSTOCK_IN.getCode(), dbProduct.getId());
                    Boolean.TRUE.equals(dbProduct.getIsChecked())
                            ? StockInUnQualifiedRecordTypeEnum.PURCHASE_SCAN_QUALITY_UNSTOCK_IN.getCode()
                            : StockInUnQualifiedRecordTypeEnum.PURCHASE_SCAN_UNSTOCK_IN.getCode(),
                    dbProduct.getId());
            // 采购不合格入库后,自动进入不合格管理,等待用户处理
            QualityUnqualified qualityUnqualified = new QualityUnqualified();
            qualityUnqualified.setInspectType(0); // 原材料不合格
            qualityUnqualified.setInspectState(0); // 待处理
            qualityUnqualified.setCheckTime(new Date());
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (loginUser != null && loginUser.getUser() != null) {
                qualityUnqualified.setCheckName(loginUser.getUser().getNickName());
            }
            qualityUnqualified.setProductId(dbProduct.getProductId());
            qualityUnqualified.setProductModelId(dbProduct.getProductModelId());
            qualityUnqualified.setProductName(dbProduct.getProductCategory());
            qualityUnqualified.setModel(dbProduct.getSpecificationModel());
            qualityUnqualified.setUnit(dbProduct.getUnit());
            qualityUnqualified.setQuantity(inboundThisLine);
            qualityUnqualified.setDefectivePhenomena("采购订单扫码不合格入库,待处理");
            qualityUnqualifiedMapper.insert(qualityUnqualified);
            BigDecimal oldUnStocked = dbProduct.getUnqualifiedStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedStockedQuantity();
            dbProduct.setUnqualifiedStockedQuantity(oldUnStocked.add(inboundThisLine));
            dbProduct.fillRemainingQuantity();
            salesLedgerProductMapper.updateById(dbProduct);
        }
    }
@@ -1028,6 +1146,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,12 +1185,121 @@
            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);
        }
    }
    private void applyQualityInboundToProducts(Long purchaseLedgerId, List<SalesLedgerProduct> products) {
        if (CollectionUtils.isEmpty(products)) {
            return;
        }
        Map<Long, BigDecimal> qualityInboundQtyByLine = getQualifiedInspectInboundQtyByLine(purchaseLedgerId);
        for (SalesLedgerProduct product : products) {
            product.fillRemainingQuantity();
            if (!Boolean.TRUE.equals(product.getIsChecked())) {
                continue;
            }
            BigDecimal orderQty = product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity();
            BigDecimal scanInboundQty = product.getStockedQuantity() == null ? BigDecimal.ZERO : product.getStockedQuantity();
            BigDecimal qualityInboundQty = qualityInboundQtyByLine.getOrDefault(product.getId(), BigDecimal.ZERO);
            BigDecimal totalQualifiedInbound = qualityInboundQty.add(scanInboundQty);
            BigDecimal remainingInbound = orderQty.subtract(totalQualifiedInbound);
            product.setRemainingQuantity(remainingInbound.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : remainingInbound);
            BigDecimal shippedQty = product.getShippedQuantity() == null ? BigDecimal.ZERO : product.getShippedQuantity();
            BigDecimal remainingShipped = totalQualifiedInbound.subtract(shippedQty);
            product.setRemainingShippedQuantity(remainingShipped.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : remainingShipped);
        }
    }
    private Map<Long, BigDecimal> getQualifiedInspectInboundQtyByLine(Long purchaseLedgerId) {
        Map<Long, BigDecimal> qualityInboundByModel = qualityInspectMapper.selectList(new LambdaQueryWrapper<QualityInspect>()
                        .eq(QualityInspect::getInspectType, 0)
                        .eq(QualityInspect::getPurchaseLedgerId, purchaseLedgerId)
                        .eq(QualityInspect::getInspectState, 1)
                        .eq(QualityInspect::getCheckResult, "合格"))
                .stream()
                .filter(qualityInspect -> qualityInspect.getProductModelId() != null && qualityInspect.getQuantity() != null)
                .collect(Collectors.groupingBy(QualityInspect::getProductModelId,
                        Collectors.reducing(BigDecimal.ZERO, QualityInspect::getQuantity, BigDecimal::add)));
        if (qualityInboundByModel.isEmpty()) {
            return Collections.emptyMap();
        }
        List<SalesLedgerProduct> purchaseProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
                .eq(SalesLedgerProduct::getSalesLedgerId, purchaseLedgerId)
                .eq(SalesLedgerProduct::getType, PURCHASE.getCode()));
        Map<Long, BigDecimal> qualityInboundByLine = new HashMap<>();
        for (SalesLedgerProduct product : purchaseProducts) {
            if (product.getId() == null || product.getProductModelId() == null) {
                continue;
            }
            qualityInboundByLine.put(product.getId(), qualityInboundByModel.getOrDefault(product.getProductModelId(), BigDecimal.ZERO));
        }
        return qualityInboundByLine;
    }
    private void refreshPurchaseLedgerStockStatus(Long purchaseLedgerId) {
        if (purchaseLedgerId == null) {
            return;
        }
        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
                .eq(SalesLedgerProduct::getSalesLedgerId, purchaseLedgerId)
                .eq(SalesLedgerProduct::getType, PURCHASE.getCode()));
        if (CollectionUtils.isEmpty(products)) {
            return;
        }
        Map<Long, BigDecimal> qualityInboundQtyByLine = getQualifiedInspectInboundQtyByLine(purchaseLedgerId);
        boolean allInbound = true;
        boolean anyInbound = false;
        for (SalesLedgerProduct product : products) {
            BigDecimal orderQty = product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity();
            BigDecimal scanInboundQty = product.getStockedQuantity() == null ? BigDecimal.ZERO : product.getStockedQuantity();
            BigDecimal qualityInboundQty = qualityInboundQtyByLine.getOrDefault(product.getId(), BigDecimal.ZERO);
            BigDecimal totalInboundQty = scanInboundQty.add(qualityInboundQty);
            if (totalInboundQty.compareTo(BigDecimal.ZERO) > 0) {
                anyInbound = true;
            }
            if (totalInboundQty.compareTo(orderQty) < 0) {
                allInbound = false;
            }
        }
        PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(purchaseLedgerId);
        if (purchaseLedger == null) {
            return;
        }
        int targetStockStatus = allInbound ? 2 : (anyInbound ? 1 : 0);
        if (!Objects.equals(purchaseLedger.getStockStatus(), targetStockStatus)) {
            purchaseLedger.setStockStatus(targetStockStatus);
            purchaseLedgerMapper.updateById(purchaseLedger);
        }
    }
    private Integer calculateProductStockStatus(SalesLedgerProduct product) {
        if (product == null) {
            return 0;
        }
        BigDecimal orderQty = product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity();
        BigDecimal stockedQty = product.getStockedQuantity() == null ? BigDecimal.ZERO : product.getStockedQuantity();
        if (stockedQty.compareTo(BigDecimal.ZERO) <= 0) {
            return 0;
        }
        if (orderQty.compareTo(BigDecimal.ZERO) > 0 && stockedQty.compareTo(orderQty) < 0) {
            return 1;
        }
        return 2;
    }
    /**
     * 下划线命名转驼峰命名
     */