src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
@@ -5,18 +5,21 @@
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.StockQualifiedRecordTypeEnum;
import com.ruoyi.common.enums.StockUnQualifiedRecordTypeEnum;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.basic.mapper.ProductModelMapper;
import com.ruoyi.basic.pojo.ProductModel;
import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum;
import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
import com.ruoyi.common.enums.StockInUnQualifiedRecordTypeEnum;
import com.ruoyi.framework.web.domain.R;
import com.ruoyi.procurementrecord.utils.StockUtils;
import com.ruoyi.production.dto.ProductStructureDto;
import com.ruoyi.production.mapper.*;
import com.ruoyi.production.pojo.*;
import com.ruoyi.production.service.impl.SalesLedgerProductionAccountingServiceImpl;
import com.ruoyi.production.service.impl.ProductOrderServiceImpl;
import com.ruoyi.purchase.dto.SimpleReturnOrderGroupDto;
import com.ruoyi.purchase.mapper.PurchaseLedgerMapper;
import com.ruoyi.purchase.mapper.PurchaseReturnOrderProductsMapper;
import com.ruoyi.purchase.pojo.PurchaseLedger;
import com.ruoyi.quality.mapper.QualityInspectMapper;
import com.ruoyi.quality.pojo.QualityInspect;
@@ -26,12 +29,13 @@
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.mapper.ShippingInfoMapper;
import com.ruoyi.sales.pojo.InvoiceRegistrationProduct;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import com.ruoyi.sales.pojo.ShippingInfo;
import com.ruoyi.sales.pojo.*;
import com.ruoyi.sales.service.ISalesLedgerProductProcessBindService;
import com.ruoyi.sales.service.ISalesLedgerProductService;
import com.ruoyi.stock.mapper.StockInventoryMapper;
import com.ruoyi.stock.mapper.StockInRecordMapper;
import com.ruoyi.stock.dto.StockInventoryDto;
import com.ruoyi.stock.pojo.StockInRecord;
import com.ruoyi.stock.pojo.StockInventory;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
@@ -65,6 +69,7 @@
    private SalesLedgerMapper salesLedgerMapper;
    private PurchaseLedgerMapper purchaseLedgerMapper;
    private PurchaseReturnOrderProductsMapper purchaseReturnOrderProductsMapper;
    private ProductOrderMapper productOrderMapper;
@@ -76,6 +81,7 @@
    private ProcessRouteMapper processRouteMapper;
    private ProductProcessRouteMapper productProcessRouteMapper;
    private ProductModelMapper productModelMapper;
    private ProductWorkOrderMapper productWorkOrderMapper;
    private ProductionProductMainMapper productionProductMainMapper;
@@ -87,18 +93,24 @@
    private StockUtils stockUtils;
    private StockInRecordMapper stockInRecordMapper;
    private final ISalesLedgerProductProcessBindService salesLedgerProductProcessBindService;
    @Autowired
    private ProductStructureMapper productStructureMapper;
    @Autowired
    private StockInventoryMapper stockInventoryMapper;
    @Autowired
    private SalesLedgerProductionAccountingServiceImpl salesLedgerProductionAccountingServiceImpl;
    private ProductOrderServiceImpl productOrderServiceImpl;
    @Override
    public SalesLedgerProduct selectSalesLedgerProductById(Long id) {
        return salesLedgerProductMapper.selectById(id);
        SalesLedgerProduct row = salesLedgerProductMapper.selectById(id);
        if (row != null) {
            row.fillRemainingQuantity();
        }
        return row;
    }
    @Override
@@ -107,50 +119,112 @@
//        queryWrapper.eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerProduct.getSalesLedgerId())
//                .eq(SalesLedgerProduct::getType, salesLedgerProduct.getType());
        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectSalesLedgerProductList(salesLedgerProduct);
        if(!CollectionUtils.isEmpty(salesLedgerProducts)){
            salesLedgerProducts.forEach(item -> {
                // 发货信息
                ShippingInfo shippingInfo = shippingInfoMapper.selectOne(new LambdaQueryWrapper<ShippingInfo>()
                        .eq(ShippingInfo::getSalesLedgerProductId, item.getId())
                        .orderByDesc(ShippingInfo::getCreateTime)
                        .last("limit 1"));
                if(shippingInfo != null){
                    item.setShippingDate(shippingInfo.getShippingDate());
                    item.setShippingCarNumber(shippingInfo.getShippingCarNumber());
                    item.setShippingStatus(shippingInfo.getStatus());
                    item.setExpressCompany(shippingInfo.getExpressCompany());
                    item.setExpressNumber(shippingInfo.getExpressNumber());
                }
            });
            // 开票
        if (CollectionUtils.isEmpty(salesLedgerProducts)) {
            return salesLedgerProducts;
        }
        salesLedgerProducts.forEach(item -> {
            ShippingInfo shippingInfo = shippingInfoMapper.selectOne(new LambdaQueryWrapper<ShippingInfo>()
                    .eq(ShippingInfo::getSalesLedgerProductId, item.getId())
                    .orderByDesc(ShippingInfo::getCreateTime)
                    .last("limit 1"));
            if (shippingInfo != null) {
                item.setShippingDate(shippingInfo.getShippingDate());
                item.setShippingCarNumber(shippingInfo.getShippingCarNumber());
                item.setShippingStatus(shippingInfo.getStatus());
                item.setExpressCompany(shippingInfo.getExpressCompany());
                item.setExpressNumber(shippingInfo.getExpressNumber());
            }
        });
        if (salesLedgerProduct.getSalesLedgerId() != null) {
            InvoiceRegistrationProductDto invoiceRegistrationProductDto = new InvoiceRegistrationProductDto();
            invoiceRegistrationProductDto.setSalesLedgerId(salesLedgerProduct.getSalesLedgerId().intValue());
            List<InvoiceRegistrationProductDto> invoiceRegistrationProductDtoList = invoiceRegistrationProductMapper.invoiceRegistrationProductList(invoiceRegistrationProductDto);
            // 统计开票登记产品的已开票数/已开票金额
            if (!CollectionUtils.isEmpty(invoiceRegistrationProductDtoList)) {
                for (SalesLedgerProduct ledgerProduct : salesLedgerProducts) {
                    BigDecimal invoiceNum = BigDecimal.ZERO;
                    BigDecimal invoiceAmount = BigDecimal.ZERO;
                    BigDecimal noInvoiceNum = BigDecimal.ZERO;
                    BigDecimal noInvoiceAmount = BigDecimal.ZERO;
                    for (InvoiceRegistrationProductDto registrationProductDto : invoiceRegistrationProductDtoList) {
                        if(ledgerProduct.getId().intValue() == registrationProductDto.getSalesLedgerProductId()){
                            invoiceNum =  invoiceNum.add(registrationProductDto.getInvoiceNum());
                        if (ledgerProduct.getId().intValue() == registrationProductDto.getSalesLedgerProductId()) {
                            invoiceNum = invoiceNum.add(registrationProductDto.getInvoiceNum());
                            invoiceAmount = invoiceAmount.add(registrationProductDto.getInvoiceAmount());
                        }
                    }
                    noInvoiceNum = ledgerProduct.getQuantity().subtract(invoiceNum);
                    noInvoiceAmount = ledgerProduct.getTaxInclusiveTotalPrice().subtract(invoiceAmount);
                    BigDecimal ledgerQuantity = ledgerProduct.getQuantity() == null ? BigDecimal.ZERO : ledgerProduct.getQuantity();
                    BigDecimal ledgerTotalPrice = ledgerProduct.getTaxInclusiveTotalPrice() == null ? BigDecimal.ZERO : ledgerProduct.getTaxInclusiveTotalPrice();
                    ledgerProduct.setInvoiceNum(invoiceNum);
                    ledgerProduct.setInvoiceAmount(invoiceAmount);
                    ledgerProduct.setNoInvoiceNum(noInvoiceNum);
                    ledgerProduct.setNoInvoiceAmount(noInvoiceAmount);
                    ledgerProduct.setNoInvoiceNum(ledgerQuantity.subtract(invoiceNum));
                    ledgerProduct.setNoInvoiceAmount(ledgerTotalPrice.subtract(invoiceAmount));
                }
            }
        }
        List<Long> productIds = salesLedgerProducts.stream()
                .map(SalesLedgerProduct::getId)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        Map<Long, BigDecimal> returnOrderGroupDtoMap = Collections.emptyMap();
        if (!CollectionUtils.isEmpty(productIds)) {
            List<SimpleReturnOrderGroupDto> groupListByProductIds = purchaseReturnOrderProductsMapper.getReturnOrderGroupListByProductIds(productIds);
            if (!CollectionUtils.isEmpty(groupListByProductIds)) {
                returnOrderGroupDtoMap = groupListByProductIds.stream()
                        .collect(Collectors.toMap(SimpleReturnOrderGroupDto::getSalesLedgerProductId,
                                SimpleReturnOrderGroupDto::getSumReturnQuantity,
                                BigDecimal::add));
            }
        }
        List<Long> productModelIds = salesLedgerProducts.stream()
                .map(SalesLedgerProduct::getProductModelId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        Map<Long, ProductModel> productModelMap = Collections.emptyMap();
        if (!CollectionUtils.isEmpty(productModelIds)) {
            List<ProductModel> productModels = productModelMapper.selectBatchIds(productModelIds);
            if (!CollectionUtils.isEmpty(productModels)) {
                productModelMap = productModels.stream()
                        .collect(Collectors.toMap(ProductModel::getId, Function.identity()));
            }
        }
        Map<Long, BigDecimal> finalReturnOrderGroupDtoMap = returnOrderGroupDtoMap;
        Map<Long, ProductModel> finalProductModelMap = productModelMap;
        salesLedgerProducts.forEach(item -> {
            if (item.getFutureTickets() == null || item.getFutureTickets().compareTo(BigDecimal.ZERO) == 0) {
                item.setFutureTickets(BigDecimal.ZERO);
            }
            if (item.getFutureTicketsAmount() == null || item.getFutureTicketsAmount().compareTo(BigDecimal.ZERO) == 0) {
                item.setFutureTicketsAmount(BigDecimal.ZERO);
            }
            BigDecimal returnQuality = finalReturnOrderGroupDtoMap.getOrDefault(item.getId(), BigDecimal.ZERO);
            item.setReturnQuality(returnQuality);
            BigDecimal quantity = item.getQuantity() == null ? BigDecimal.ZERO : item.getQuantity();
            BigDecimal shipped = item.getShippedQuantity() == null ? BigDecimal.ZERO : item.getShippedQuantity();
            BigDecimal available = quantity.subtract(returnQuality).subtract(shipped);
            item.setAvailableQuality(available.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : available);
            item.fillRemainingQuantity();
            if (item.getApproveStatus() == null || item.getApproveStatus() != 2) {
                BigDecimal remainingShipped = item.getRemainingShippedQuantity() == null ? BigDecimal.ZERO : item.getRemainingShippedQuantity();
                boolean hasOutbound = shipped.compareTo(BigDecimal.ZERO) > 0;
                if (hasOutbound && remainingShipped.compareTo(BigDecimal.ZERO) <= 0) {
                    item.setApproveStatus(0);
                } else {
                    Integer hasSufficientStock = item.getHasSufficientStock();
                    if (hasSufficientStock != null && hasSufficientStock == 0) {
                        item.setApproveStatus(0);
                    } else {
                        item.setApproveStatus(1);
                    }
                }
            }
            ProductModel productModel = finalProductModelMap.get(item.getProductModelId());
            if (productModel != null) {
                item.setThickness(productModel.getThickness());
            }
        });
        return salesLedgerProducts;
    }
@@ -169,7 +243,7 @@
        //删除发货信息
        List<ShippingInfo> shippingInfos = shippingInfoMapper.selectList(new LambdaQueryWrapper<ShippingInfo>()
                .in(ShippingInfo::getSalesLedgerProductId, Arrays.asList(ids)));
        if(!CollectionUtils.isEmpty(shippingInfos)){
        if (!CollectionUtils.isEmpty(shippingInfos)) {
            shippingInfoService.delete(shippingInfos.stream().map(ShippingInfo::getId).collect(Collectors.toList()));
        }
@@ -206,12 +280,12 @@
    @Transactional(rollbackFor = Exception.class)
    public int addOrUpdateSalesLedgerProduct(SalesLedgerProduct salesLedgerProduct) {
        // 待回款,付款
        if(salesLedgerProduct.getType().equals(1)){
        if (salesLedgerProduct.getType().equals(1)) {
            salesLedgerProduct.setPendingInvoiceTotal(salesLedgerProduct.getTaxInclusiveTotalPrice().subtract(salesLedgerProduct.getInvoiceTotal()));
            //未开票数量+金额
            salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getQuantity());
            salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxInclusiveTotalPrice());
        }else{
        } else {
            salesLedgerProduct.setPendingTicketsTotal(salesLedgerProduct.getTaxInclusiveTotalPrice().subtract(salesLedgerProduct.getTicketsTotal()));
            // 未来票数量+金额
            salesLedgerProduct.setFutureTickets(salesLedgerProduct.getQuantity());
@@ -220,14 +294,38 @@
        int result;
        Long salesLedgerId = salesLedgerProduct.getSalesLedgerId();
        salesLedgerProduct.fillRemainingQuantity();
        if (salesLedgerProduct.getId() == null) {
            salesLedgerProduct.setRegisterDate(LocalDateTime.now());
            result = salesLedgerProductMapper.insert(salesLedgerProduct);
            addProductionData(salesLedgerProduct);
        } else {
            SalesLedgerProduct dbBefore = salesLedgerProductMapper.selectById(salesLedgerProduct.getId());
            if (dbBefore == null) {
                throw new RuntimeException("销售产品不存在,无法编辑");
            }
            // 不能把订单数量改到小于已发货数量,否则账实不一致
            BigDecimal shipped = dbBefore.getShippedQuantity() == null ? BigDecimal.ZERO : dbBefore.getShippedQuantity();
            BigDecimal newOrderQty = salesLedgerProduct.getQuantity() == null ? BigDecimal.ZERO : salesLedgerProduct.getQuantity();
            if (newOrderQty.compareTo(shipped) < 0) {
                throw new RuntimeException("编辑失败,订单数量不能小于已发货数量");
            }
            //查询原本的产品型号id
            salesLedgerProduct.setFutureTickets(salesLedgerProduct.getQuantity());
            result = salesLedgerProductMapper.updateById(salesLedgerProduct);
            // 允许“多入库”:当订单数量被下调时,只回退“下调差额”对应的入库数量
            // 例:原订单100,已入库120;改为50 -> 只回退50,最终已入库70(不强压到50)
            BigDecimal oldOrderQty = dbBefore.getQuantity() == null ? BigDecimal.ZERO : dbBefore.getQuantity();
            BigDecimal stocked = dbBefore.getStockedQuantity() == null ? BigDecimal.ZERO : dbBefore.getStockedQuantity();
            BigDecimal reduced = oldOrderQty.subtract(newOrderQty);
            if (reduced.compareTo(BigDecimal.ZERO) > 0) {
                BigDecimal rollbackQty = stocked.min(reduced);
                if (rollbackQty.compareTo(BigDecimal.ZERO) > 0) {
                    rollbackExcessStockIn(dbBefore, rollbackQty);
                }
            }
            /*删除对应的生产数据并重新新增*/
            deleteProductionData(Arrays.asList(salesLedgerProduct.getId()));
            // 删除生产核算数据
@@ -261,8 +359,109 @@
                        PurchaseLedger.class
                );
            }
            //  清空销售产品绑定的加工
            salesLedgerProductProcessBindService.updateProductProcessBind(salesLedgerProduct.getSalesProductProcessList(), salesLedgerProduct.getId());
            // 新增/编辑产品后,刷新销售主单入库状态(避免因新增未入库行导致状态不准)
            refreshSalesLedgerStockStatus(salesLedgerId);
        }
        return result;
    }
    private void rollbackExcessStockIn(SalesLedgerProduct dbBefore, BigDecimal rollbackQtyTotal) {
        if (dbBefore == null || rollbackQtyTotal == null || rollbackQtyTotal.compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }
        if (dbBefore.getProductModelId() == null) {
            throw new RuntimeException("回退入库失败,产品规格未维护");
        }
        Long productId = dbBefore.getId();
        //  倒序回退:优先回退“扫码入库”,再回退“手动入库”
        List<String> recordTypes = Arrays.asList(
                StockInQualifiedRecordTypeEnum.SALE_SCAN_STOCK_IN.getCode(),
                StockInQualifiedRecordTypeEnum.SALE_STOCK_IN.getCode()
        );
        List<StockInRecord> records = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
                .eq(StockInRecord::getSalesLedgerProductId, productId)
                .eq(StockInRecord::getType, "0")
                .in(StockInRecord::getRecordType, recordTypes)
                .orderByDesc(StockInRecord::getCreateTime));
        BigDecimal remaining = rollbackQtyTotal;
        for (StockInRecord r : records) {
            if (remaining.compareTo(BigDecimal.ZERO) <= 0) {
                break;
            }
            BigDecimal inNum = r.getStockInNum() == null ? BigDecimal.ZERO : r.getStockInNum();
            if (inNum.compareTo(BigDecimal.ZERO) <= 0) {
                continue;
            }
            BigDecimal rollbackQty = inNum.min(remaining);
            StockInventoryDto stockInventoryDto = new StockInventoryDto();
            stockInventoryDto.setProductModelId(dbBefore.getProductModelId());
            stockInventoryDto.setQualitity(rollbackQty);
            int affectRows = stockInventoryMapper.updateSubtractStockInventory(stockInventoryDto);
            if (affectRows <= 0) {
                throw new RuntimeException("回退入库失败,当前库存不足,无法扣回超入库数量");
            }
            BigDecimal newInNum = inNum.subtract(rollbackQty);
            if (newInNum.compareTo(BigDecimal.ZERO) <= 0) {
                stockInRecordMapper.deleteById(r.getId());
            } else {
                r.setStockInNum(newInNum);
                stockInRecordMapper.updateById(r);
            }
            remaining = remaining.subtract(rollbackQty);
        }
        if (remaining.compareTo(BigDecimal.ZERO) > 0) {
            throw new RuntimeException("回退入库失败,未找到足够的入库记录用于扣回超入库数量");
        }
        //  回写产品行已入库数量与状态
        SalesLedgerProduct fresh = salesLedgerProductMapper.selectById(dbBefore.getId());
        if (fresh == null) {
            return;
        }
        BigDecimal newStocked = (fresh.getStockedQuantity() == null ? BigDecimal.ZERO : fresh.getStockedQuantity()).subtract(rollbackQtyTotal);
        if (newStocked.compareTo(BigDecimal.ZERO) < 0) {
            newStocked = BigDecimal.ZERO;
        }
        BigDecimal orderQty = fresh.getQuantity() == null ? BigDecimal.ZERO : fresh.getQuantity();
        int stockStatus;
        if (newStocked.compareTo(BigDecimal.ZERO) <= 0) stockStatus = 0;
        else if (orderQty.compareTo(BigDecimal.ZERO) > 0 && newStocked.compareTo(orderQty) < 0) stockStatus = 1;
        else stockStatus = 2;
        fresh.setStockedQuantity(newStocked);
        fresh.setProductStockStatus(stockStatus);
        fresh.fillRemainingQuantity();
        salesLedgerProductMapper.updateById(fresh);
    }
    private void refreshSalesLedgerStockStatus(Long salesLedgerId) {
        if (salesLedgerId == null) {
            return;
        }
        SalesLedger ledger = salesLedgerMapper.selectById(salesLedgerId);
        if (ledger == null) {
            return;
        }
        List<SalesLedgerProduct> allProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
                .eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerId)
                .eq(SalesLedgerProduct::getType, 1));
        if (CollectionUtils.isEmpty(allProducts)) {
            ledger.setStockStatus(0);
            salesLedgerMapper.updateById(ledger);
            return;
        }
        boolean anyInbound = allProducts.stream().anyMatch(p -> {
            BigDecimal sq = p.getStockedQuantity();
            return sq != null && sq.compareTo(BigDecimal.ZERO) > 0;
        });
        boolean allFull = allProducts.stream().allMatch(p -> Objects.equals(p.getProductStockStatus(), 2));
        ledger.setStockStatus(allFull ? 2 : (anyInbound ? 1 : 0));
        salesLedgerMapper.updateById(ledger);
    }
    /**
@@ -273,7 +472,8 @@
        productOrder.setSalesLedgerId(salesLedgerProduct.getSalesLedgerId());
        productOrder.setProductModelId(salesLedgerProduct.getProductModelId());
        productOrder.setSaleLedgerProductId(salesLedgerProduct.getId());
        productOrder.setNpsNo("SC" + String.format("%08d", salesLedgerProduct.getId()));
        String string = productOrderServiceImpl.generateNextOrderNo(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        productOrder.setNpsNo(string);
        productOrder.setQuantity(salesLedgerProduct.getQuantity());//需求数量
        productOrder.setCompleteQuantity(BigDecimal.ZERO);//完成数量
        productOrderMapper.insert(productOrder);
@@ -281,7 +481,7 @@
        List<ProcessRoute> processRoutes = processRouteMapper.selectList(new QueryWrapper<ProcessRoute>().lambda()
                .eq(ProcessRoute::getProductModelId, salesLedgerProduct.getProductModelId())
                .orderByDesc(ProcessRoute::getCreateTime));
        if (processRoutes.size()>0){
        if (processRoutes.size() > 0) {
            ProcessRoute processRoute = processRoutes.get(0);
            //新增生产订单工艺路线主表
            ProductProcessRoute productProcessRoute = new ProductProcessRoute();
@@ -304,13 +504,7 @@
                int insert = productProcessRouteItemMapper.insert(productProcessRouteItem);
                if (insert > 0) {
                    // 查询今日已存在的最大工单号
                    QueryWrapper<ProductWorkOrder> queryWrapper = new QueryWrapper<>();
                    queryWrapper.likeRight("work_order_no", datePrefix)
                            .orderByDesc("work_order_no")
                            .last("LIMIT 1");
                    ProductWorkOrder lastWorkOrder = productWorkOrderMapper.selectOne(queryWrapper);
                    ProductWorkOrder lastWorkOrder = productWorkOrderMapper.selectMax(datePrefix);
                    int sequenceNumber = 1; // 默认序号
                    if (lastWorkOrder != null && lastWorkOrder.getWorkOrderNo() != null) {
                        String lastNo = lastWorkOrder.getWorkOrderNo().toString();
@@ -324,7 +518,7 @@
                        }
                    }
                    // 生成完整的工单号
                    String workOrderNoStr ="GD"+ String.format("%s%03d", datePrefix, sequenceNumber);
                    String workOrderNoStr = "GD" + String.format("%s%03d", datePrefix, sequenceNumber);
                    ProductWorkOrder productWorkOrder = new ProductWorkOrder();
                    productWorkOrder.setProductProcessRouteItemId(productProcessRouteItem.getId());
                    productWorkOrder.setProductOrderId(productOrder.getId());
@@ -397,9 +591,9 @@
                        //删除出库记录
                        for (Long productMainId : productMainIds) {
                            //删除生产出库记录
                            stockUtils.deleteStockOutRecord(productMainId, StockQualifiedRecordTypeEnum.PRODUCTION_REPORT_STOCK_OUT.getCode());
                            stockUtils.deleteStockOutRecord(productMainId, StockOutQualifiedRecordTypeEnum.PRODUCTION_REPORT_STOCK_OUT.getCode());
                            //删除报废的入库记录
                            stockUtils.deleteStockInRecord(productMainId, StockUnQualifiedRecordTypeEnum.PRODUCTION_SCRAP.getCode());
                            stockUtils.deleteStockInRecord(productMainId, StockInUnQualifiedRecordTypeEnum.PRODUCTION_SCRAP.getCode());
                        }
                        qualityInspects.forEach(qualityInspect -> {
                            //inspectState=1 已提交 不能删除
@@ -439,11 +633,12 @@
        IPage<SalesLedgerProductDto> salesLedgerProductDtoIPage = salesLedgerProductMapper.listPage(page, salesLedgerProduct);
        salesLedgerProductDtoIPage.getRecords().forEach(item -> {
            // 判断状态
            if(item.getTaxInclusiveTotalPrice().compareTo(item.getInvoiceTotal()) == 0){
            if (item.getTaxInclusiveTotalPrice().compareTo(item.getInvoiceTotal()) == 0) {
                item.setStatusName("已完成付款");
            }else{
            } else {
                item.setStatusName("未完成付款");
            }
            item.fillRemainingQuantity();
        });
        return salesLedgerProductDtoIPage;
    }
@@ -453,11 +648,12 @@
        IPage<SalesLedgerProductDto> salesLedgerProductDtoIPage = salesLedgerProductMapper.listPagePurchaseLedger(page, salesLedgerProduct);
        salesLedgerProductDtoIPage.getRecords().forEach(item -> {
            // 判断状态
            if(item.getTaxInclusiveTotalPrice().compareTo(item.getInvoiceTotal()) == 0){
            if (item.getTaxInclusiveTotalPrice().compareTo(item.getTicketsTotal()) == 0) {
                item.setStatusName("已完成付款");
            }else{
            } else {
                item.setStatusName("未完成付款");
            }
            item.fillRemainingQuantity();
        });
        return salesLedgerProductDtoIPage;
    }
@@ -496,6 +692,7 @@
            throw new RuntimeException("动态更新主表金额失败", e);
        }
    }
    @Override
    public R judgmentInventory(SalesLedgerProduct salesLedgerProduct) {
        //获取产品最新的工艺路线
@@ -514,7 +711,7 @@
            //所需数量
            BigDecimal multiply = salesLedgerProduct.getQuantity().multiply(productStructureDto.getUnitQuantity());
            BigDecimal subtract =stockInventory.getQualitity().subtract(stockInventory.getLockedQuantity()).subtract(multiply).divide(BigDecimal.ONE, 2, RoundingMode.CEILING);
            BigDecimal subtract = stockInventory.getQualitity().subtract(stockInventory.getLockedQuantity()).subtract(multiply).divide(BigDecimal.ONE, 2, RoundingMode.CEILING);
            if (subtract.compareTo(BigDecimal.ZERO) <= 0) {
                count++;
                stringBuffer.append(productStructureDto.getProductName())
@@ -525,9 +722,9 @@
                        .append(System.lineSeparator());
            }
        }
        if (count>0) {
        if (count > 0) {
            return R.fail(stringBuffer.toString());
        }else {
        } else {
            return R.ok();
        }
    }