huminmin
3 小时以前 ea1e515180e0c3e85cfc88bcd41a3882c34ce538
src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
@@ -27,6 +27,7 @@
import com.ruoyi.common.enums.ReviewStatusEnum;
import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.FreightUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
@@ -678,6 +679,7 @@
        // 执行更新操作
        if (!updateList.isEmpty()) {
            for (SalesLedgerProduct product : updateList) {
                FreightUtils.fillFreightAmount(product);
                product.setType(ledgerType);
                salesLedgerProductMapper.updateById(product);
            }
@@ -685,6 +687,7 @@
        // 执行插入操作
        if (!insertList.isEmpty()) {
            for (SalesLedgerProduct salesLedgerProduct : insertList) {
                FreightUtils.fillFreightAmount(salesLedgerProduct);
                salesLedgerProduct.setType(ledgerType);
                Date entryDate = purchaseLedger.getEntryDate();
@@ -974,6 +977,7 @@
                for (PurchaseLedgerProductImportDto salesLedgerProductImportDto : salesLedgerProductImportDtos) {
                    SalesLedgerProduct salesLedgerProduct = new SalesLedgerProduct();
                    BeanUtils.copyProperties(salesLedgerProductImportDto, salesLedgerProduct);
                    FreightUtils.fillFreightAmount(salesLedgerProduct);
                    salesLedgerProduct.setSalesLedgerId(salesLedger.getId());
                    salesLedgerProduct.setType(2);
                    // 计算不含税总价
@@ -1109,13 +1113,13 @@
                throw new BaseException("理论入库数量必须大于0");
            }
            BigDecimal approvedStockInNum = getApprovedPurchaseStockInNum(
            BigDecimal occupiedStockInNum = getOccupiedPurchaseStockInNum(
                    purchaseLedger.getId(),
                    purchaseLedger.getPurchaseContractNumber(),
                    salesLedgerProduct.getId(),
                    salesLedgerProduct.getProductModelId()
            );
            BigDecimal remainingStockInNum = salesLedgerProduct.getQuantity().subtract(approvedStockInNum);
            BigDecimal remainingStockInNum = salesLedgerProduct.getQuantity().subtract(occupiedStockInNum);
            if (remainingStockInNum.compareTo(BigDecimal.ZERO) <= 0) {
                throw new BaseException("产品【" +  productModel.getModel() + "】已完成入库");
            }
@@ -1147,15 +1151,17 @@
        return count;
    }
    private BigDecimal getApprovedPurchaseStockInNum(Long purchaseLedgerId, String purchaseContractNumber, Long productId, Long productModelId) {
    private BigDecimal getOccupiedPurchaseStockInNum(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(item -> item.getApprovalStatus() == null
                        || ReviewStatusEnum.PENDING_REVIEW.getCode().equals(item.getApprovalStatus())
                        || ReviewStatusEnum.APPROVED.getCode().equals(item.getApprovalStatus()))
                .map(item -> item.getTheoryStockInNum() != null ? item.getTheoryStockInNum() : item.getStockInNum())
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }