gongchunyi
3 天以前 0b7c77568b6a47c4c824130d6f5288b0bab5cdef
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -31,6 +31,7 @@
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.EnumUtil;
import com.ruoyi.common.utils.OrderUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
@@ -2145,9 +2146,13 @@
        if (salesLedger.getDeliveryStatus() != null && salesLedger.getDeliveryStatus() == 5) {
            throw new ServiceException("出库失败,该销售订单已发货");
        }
        if (salesLedger.getDeliveryStatus() != null && salesLedger.getDeliveryStatus() == 2) {
            throw new ServiceException("出库失败,该销售订单已发起发货审批,请先完成审批");
        }
        if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
            throw new ServiceException("销售订单出库失败,出库产品不能为空");
        }
        String scanShippingNo = OrderUtils.countTodayByCreateTime(shippingInfoMapper, "SCAN");
        int saleType = SaleEnum.SALE.getCode();
        Map<Long, BigDecimal> outboundQtyByLineId = new LinkedHashMap<>();
        for (SalesLedgerProduct line : dto.getSalesLedgerProductList()) {
@@ -2180,6 +2185,11 @@
            if (dbProduct.getProductModelId() == null) {
                throw new ServiceException("出库失败,产品规格未维护,无法出库");
            }
            BigDecimal orderQty = defaultDecimal(dbProduct.getQuantity());
            BigDecimal prevShipped = defaultDecimal(dbProduct.getShippedQuantity());
            if (prevShipped.add(outboundThisLine).compareTo(orderQty) > 0) {
                throw new ServiceException("出库失败,本次出库后累计发货数量不能大于该产品订单数量");
            }
            stockUtils.assertQualifiedAvailable(dbProduct.getProductModelId(), outboundThisLine);
            StockInventoryDto stockInventoryDto = new StockInventoryDto();
@@ -2203,7 +2213,74 @@
        });
        boolean allLinesFull = ledgerAllProducts.stream().allMatch(p -> Objects.equals(p.getProductStockStatus(), 2));
        salesLedger.setStockStatus(allLinesFull ? 2 : (anyInbound ? 1 : 0));
        List<SalesLedgerProduct> saleLines = ledgerAllProducts.stream()
                .filter(p -> Objects.equals(p.getType(), saleType))
                .collect(Collectors.toList());
        boolean allDelivered = !saleLines.isEmpty() && saleLines.stream().allMatch(p -> {
            BigDecimal q = defaultDecimal(p.getQuantity());
            BigDecimal s = defaultDecimal(p.getShippedQuantity());
            return q.compareTo(BigDecimal.ZERO) <= 0 || s.compareTo(q) >= 0;
        });
        if (allDelivered) {
            salesLedger.setDeliveryStatus(5);
        } else {
            boolean anyLineShipped = saleLines.stream()
                    .anyMatch(p -> defaultDecimal(p.getShippedQuantity()).compareTo(BigDecimal.ZERO) > 0);
            if (anyLineShipped) {
                salesLedger.setDeliveryStatus(6);
            }
        }
        baseMapper.updateById(salesLedger);
        syncShippingLedgerAfterQualifiedScan(ledgerId, scanShippingNo);
    }
    /**
     * 扫码合格出库后同步发货台账记录
     */
    private void syncShippingLedgerAfterQualifiedScan(Long ledgerId, String shippingBatchNo) {
        if (shippingBatchNo == null) {
            shippingBatchNo = OrderUtils.countTodayByCreateTime(shippingInfoMapper, "SCAN");
        }
        int saleType = SaleEnum.SALE.getCode();
        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(
                Wrappers.<SalesLedgerProduct>lambdaQuery()
                        .eq(SalesLedgerProduct::getSalesLedgerId, ledgerId)
                        .eq(SalesLedgerProduct::getType, saleType));
        Date now = new Date();
        for (SalesLedgerProduct p : products) {
            if (p.getShippedQuantity() == null || p.getShippedQuantity().compareTo(BigDecimal.ZERO) <= 0) {
                continue;
            }
            ShippingInfo row = shippingInfoMapper.selectOne(new LambdaQueryWrapper<ShippingInfo>()
                    .eq(ShippingInfo::getSalesLedgerProductId, p.getId())
                    .orderByDesc(ShippingInfo::getId)
                    .last("LIMIT 1"));
            BigDecimal lineQty = defaultDecimal(p.getQuantity());
            BigDecimal shipped = defaultDecimal(p.getShippedQuantity());
            boolean lineFullyShipped = lineQty.compareTo(BigDecimal.ZERO) <= 0 || shipped.compareTo(lineQty) >= 0;
            String lineShipStatus = lineFullyShipped ? "已发货" : "部分发货";
            if (row == null) {
                ShippingInfo insert = new ShippingInfo();
                insert.setSalesLedgerId(ledgerId);
                insert.setSalesLedgerProductId(p.getId());
                insert.setShippingNo(shippingBatchNo);
                insert.setType("扫码出库");
                insert.setStatus(lineShipStatus);
                insert.setShippingDate(now);
                shippingInfoMapper.insert(insert);
            } else {
                if (!StringUtils.hasText(row.getType())) {
                    row.setType("扫码出库");
                }
                row.setStatus(lineShipStatus);
                row.setShippingDate(now);
                if (!StringUtils.hasText(row.getShippingNo())) {
                    row.setShippingNo(shippingBatchNo);
                }
                shippingInfoMapper.updateById(row);
            }
        }
    }
    @Override