| | |
| | | import com.ruoyi.common.enums.FileNameType; |
| | | import com.ruoyi.common.enums.SaleEnum; |
| | | import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum; |
| | | import com.ruoyi.common.enums.StockInUnQualifiedRecordTypeEnum; |
| | | import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum; |
| | | import com.ruoyi.common.enums.StockOutUnQualifiedRecordTypeEnum; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.exception.base.BaseException; |
| | | import com.ruoyi.common.utils.DateUtils; |
| | |
| | | import com.ruoyi.project.system.domain.SysUser; |
| | | import com.ruoyi.project.system.mapper.SysDeptMapper; |
| | | import com.ruoyi.project.system.mapper.SysUserMapper; |
| | | import com.ruoyi.procurementrecord.utils.StockUtils; |
| | | import com.ruoyi.purchase.dto.SimpleReturnOrderGroupDto; |
| | | import com.ruoyi.purchase.mapper.PurchaseReturnOrderProductsMapper; |
| | | import com.ruoyi.quality.mapper.QualityInspectMapper; |
| | |
| | | private final StockOutRecordMapper stockOutRecordMapper; |
| | | private final StockInRecordService stockInRecordService; |
| | | private final StockOutRecordService stockOutRecordService; |
| | | private final StockUtils stockUtils; |
| | | |
| | | @Autowired |
| | | private SysDeptMapper sysDeptMapper; |
| | |
| | | product.setActualTotalArea(pieceArea.multiply(quantity).setScale(2, RoundingMode.HALF_UP)); |
| | | } |
| | | } |
| | | product.fillRemainingQuantity(); |
| | | } |
| | | |
| | | // 3.查询上传文件 |
| | |
| | | salesLedgerProduct.setNoInvoiceNum(quantity); |
| | | salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxExclusiveTotalPrice()); |
| | | salesLedgerProduct.setPendingInvoiceTotal(taxInclusiveTotalPrice); |
| | | salesLedgerProduct.fillRemainingQuantity(); |
| | | salesLedgerProductMapper.insert(salesLedgerProduct); |
| | | if (!processList.isEmpty()) { |
| | | salesLedgerProductProcessBindService.updateProductProcessBind(processList, salesLedgerProduct.getId()); |
| | |
| | | for (SalesLedgerProduct product : updateList) { |
| | | product.setType(type.getCode()); |
| | | product.setProductStockStatus(0); |
| | | product.fillRemainingQuantity(); |
| | | salesLedgerProductMapper.updateById(product); |
| | | // 清空销售产品绑定的加工 |
| | | salesLedgerProductProcessBindService.updateProductProcessBind(product.getSalesProductProcessList(), product.getId()); |
| | |
| | | salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxInclusiveTotalPrice()); |
| | | salesLedgerProduct.setPendingInvoiceTotal(salesLedgerProduct.getTaxInclusiveTotalPrice()); |
| | | salesLedgerProduct.setProductStockStatus(0); |
| | | salesLedgerProduct.fillRemainingQuantity(); |
| | | salesLedgerProductMapper.insert(salesLedgerProduct); |
| | | // 绑定产品额外加工 |
| | | // 清空销售产品绑定的加工 |
| | |
| | | List<SalesProcessCardDto.ProcessNodeDto> nodeDtos = new ArrayList<>(); |
| | | |
| | | if (CollectionUtils.isEmpty(salesLedgerProcessRoutes)) { |
| | | // 无自定义路线,取默认路线 |
| | | ProcessRoute defaultRoute = processRouteMapper.selectOne( |
| | | // 无自定义路线:先查询默认工艺路线;若无默认,获取创建时间最近的一条 |
| | | ProcessRoute fallbackRoute = processRouteMapper.selectOne( |
| | | new LambdaQueryWrapper<ProcessRoute>().eq(ProcessRoute::getIsDefault, 1).last("LIMIT 1")); |
| | | if (defaultRoute != null) { |
| | | if (fallbackRoute == null) { |
| | | fallbackRoute = processRouteMapper.selectOne( |
| | | new LambdaQueryWrapper<ProcessRoute>() |
| | | .orderByDesc(ProcessRoute::getCreateTime) |
| | | .last("LIMIT 1")); |
| | | } |
| | | if (fallbackRoute != null) { |
| | | List<ProcessRouteItem> routeItems = processRouteItemMapper.selectList( |
| | | new LambdaQueryWrapper<ProcessRouteItem>() |
| | | .eq(ProcessRouteItem::getRouteId, defaultRoute.getId()) |
| | | .eq(ProcessRouteItem::getRouteId, fallbackRoute.getId()) |
| | | .orderByAsc(ProcessRouteItem::getDragSort)); |
| | | for (ProcessRouteItem i : routeItems) { |
| | | SalesProcessCardDto.ProcessNodeDto node = new SalesProcessCardDto.ProcessNodeDto(); |
| | |
| | | throw new ServiceException("入库失败,未查询到该销售订单的销售产品"); |
| | | } |
| | | for (SalesLedgerProduct product : salesLedgerProducts) { |
| | | if (!Objects.equals(product.getSalesLedgerId(), ledger.getId())) { |
| | | throw new ServiceException("入库失败,存在不属于当前销售订单的产品"); |
| | | } |
| | | if (product.getProductModelId() == null) { |
| | | continue; |
| | | } |
| | | BigDecimal orderQty = product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity(); |
| | | BigDecimal oldStocked = product.getStockedQuantity() == null ? BigDecimal.ZERO : product.getStockedQuantity(); |
| | | BigDecimal inboundQty = orderQty.subtract(oldStocked); |
| | | if (inboundQty.compareTo(BigDecimal.ZERO) < 0) { |
| | | inboundQty = BigDecimal.ZERO; |
| | | } |
| | | if (inboundQty.compareTo(BigDecimal.ZERO) <= 0) { |
| | | continue; |
| | | } |
| | | StockInventoryDto stockInventoryDto = new StockInventoryDto(); |
| | | stockInventoryDto.setRecordId(product.getId()); |
| | | stockInventoryDto.setRecordType(StockInQualifiedRecordTypeEnum.SALE_STOCK_IN.getCode()); |
| | | stockInventoryDto.setQualitity(product.getQuantity()); |
| | | stockInventoryDto.setQualitity(inboundQty); |
| | | stockInventoryDto.setProductModelId(product.getProductModelId()); |
| | | stockInventoryDto.setSalesLedgerId(ledger.getId()); |
| | | stockInventoryDto.setSalesLedgerProductId(product.getId()); |
| | | stockInventoryService.addstockInventory(stockInventoryDto); |
| | | |
| | | BigDecimal newStocked = oldStocked.add(inboundQty); |
| | | 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; |
| | | } |
| | | product.setStockedQuantity(newStocked); |
| | | product.setProductStockStatus(lineStockStatus); |
| | | product.fillRemainingQuantity(); |
| | | salesLedgerProductMapper.updateById(product); |
| | | } |
| | | // 按销售订单产品入库情况更新主单入库状态:1-部分入库,2-已入库 |
| | | List<SalesLedgerProduct> ledgerAllProducts = salesLedgerProductMapper.selectList(Wrappers.<SalesLedgerProduct>lambdaQuery().eq(SalesLedgerProduct::getSalesLedgerId, ledger.getId())); |
| | | boolean hasStocked = CollectionUtils.isNotEmpty(ledgerAllProducts) && ledgerAllProducts.stream().anyMatch(item -> Objects.equals(item.getProductStockStatus(), 1)); |
| | | boolean allStocked = CollectionUtils.isNotEmpty(ledgerAllProducts) && ledgerAllProducts.stream().allMatch(item -> Objects.equals(item.getProductStockStatus(), 1)); |
| | | boolean hasStocked = CollectionUtils.isNotEmpty(ledgerAllProducts) && ledgerAllProducts.stream().anyMatch(item -> { |
| | | BigDecimal sq = item.getStockedQuantity(); |
| | | return sq != null && sq.compareTo(BigDecimal.ZERO) > 0; |
| | | }); |
| | | boolean allStocked = CollectionUtils.isNotEmpty(ledgerAllProducts) && ledgerAllProducts.stream().allMatch(item -> { |
| | | BigDecimal orderQty = item.getQuantity() == null ? BigDecimal.ZERO : item.getQuantity(); |
| | | BigDecimal stockedQty = item.getStockedQuantity() == null ? BigDecimal.ZERO : item.getStockedQuantity(); |
| | | return orderQty.compareTo(BigDecimal.ZERO) <= 0 || stockedQty.compareTo(orderQty) >= 0; |
| | | }); |
| | | ledger.setStockStatus(allStocked ? 2 : (hasStocked ? 1 : 0)); |
| | | baseMapper.updateById(ledger); |
| | | } |
| | |
| | | .in(Customer::getId, customerIds) |
| | | .orderByAsc(Customer::getCustomerName)); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void scanInbound(SalesScanInboundDto dto) { |
| | | if (dto == null || dto.getSalesLedgerId() == null) { |
| | | throw new ServiceException("销售订单入库失败,入库数据不能为空"); |
| | | } |
| | | |
| | | SalesLedger salesLedger = baseMapper.selectById(dto.getSalesLedgerId()); |
| | | if (salesLedger == null) { |
| | | throw new ServiceException("入库失败,销售订单不存在"); |
| | | } |
| | | if (salesLedger.getStockStatus() == null) { |
| | | throw new ServiceException("入库失败,销售订单状态异常"); |
| | | } |
| | | if (salesLedger.getStockStatus() == 2) { |
| | | throw new ServiceException("入库失败,该销售订单已全部入库"); |
| | | } |
| | | if (salesLedger.getDeliveryStatus() == 5) { |
| | | throw new ServiceException("入库失败,该销售订单已发货"); |
| | | } |
| | | if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) { |
| | | throw new ServiceException("销售订单入库失败,入库产品不能为空"); |
| | | } |
| | | // 本次入库数量可以大于订单数量,但不能为负 |
| | | Map<Long, BigDecimal> inboundQtyByLineId = new LinkedHashMap<>(); |
| | | for (SalesLedgerProduct inbound : dto.getSalesLedgerProductList()) { |
| | | if (inbound == null || inbound.getId() == null) { |
| | | throw new ServiceException("入库失败,销售产品信息不完整"); |
| | | } |
| | | BigDecimal inboundQty = inbound.getStockedQuantity(); |
| | | if (inboundQty == null) { |
| | | throw new ServiceException("入库失败,入库数量不能为空"); |
| | | } |
| | | if (inboundQty.compareTo(BigDecimal.ZERO) < 0) { |
| | | throw new ServiceException("入库失败,入库数量不能为负数"); |
| | | } |
| | | inboundQtyByLineId.merge(inbound.getId(), inboundQty, BigDecimal::add); |
| | | } |
| | | Long ledgerId = salesLedger.getId(); |
| | | for (Map.Entry<Long, BigDecimal> entry : inboundQtyByLineId.entrySet()) { |
| | | Long salesLedgerProductId = entry.getKey(); |
| | | BigDecimal inboundThisLine = entry.getValue(); |
| | | if (inboundThisLine.compareTo(BigDecimal.ZERO) == 0) { |
| | | continue; |
| | | } |
| | | SalesLedgerProduct dbProduct = salesLedgerProductMapper.selectById(salesLedgerProductId); |
| | | if (dbProduct == null) { |
| | | throw new ServiceException("入库失败,销售产品不存在"); |
| | | } |
| | | if (!Objects.equals(dbProduct.getSalesLedgerId(), ledgerId)) { |
| | | throw new ServiceException("入库失败,销售产品与订单不匹配"); |
| | | } |
| | | if (!Objects.equals(dbProduct.getType(), SaleEnum.SALE.getCode())) { |
| | | throw new ServiceException("入库失败,仅支持销售订单产品行"); |
| | | } |
| | | if (dbProduct.getProductModelId() == null) { |
| | | throw new ServiceException("入库失败,产品规格未维护,无法入库"); |
| | | } |
| | | BigDecimal oldStocked = dbProduct.getStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getStockedQuantity(); |
| | | BigDecimal newStocked = oldStocked.add(inboundThisLine); |
| | | |
| | | StockInventoryDto stockInventoryDto = new StockInventoryDto(); |
| | | stockInventoryDto.setRecordId(dbProduct.getId()); |
| | | stockInventoryDto.setRecordType(StockInQualifiedRecordTypeEnum.SALE_SCAN_STOCK_IN.getCode()); |
| | | stockInventoryDto.setQualitity(inboundThisLine); |
| | | stockInventoryDto.setProductModelId(dbProduct.getProductModelId()); |
| | | stockInventoryDto.setSalesLedgerId(ledgerId); |
| | | 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; |
| | | } else if (orderQty.compareTo(BigDecimal.ZERO) > 0 && newStocked.compareTo(orderQty) < 0) { |
| | | lineStockStatus = 1; |
| | | } else { |
| | | lineStockStatus = 2; |
| | | } |
| | | dbProduct.setStockedQuantity(newStocked); |
| | | dbProduct.setProductStockStatus(lineStockStatus); |
| | | dbProduct.fillRemainingQuantity(); |
| | | salesLedgerProductMapper.updateById(dbProduct); |
| | | } |
| | | List<SalesLedgerProduct> ledgerAllProducts = salesLedgerProductMapper.selectList( |
| | | Wrappers.<SalesLedgerProduct>lambdaQuery().eq(SalesLedgerProduct::getSalesLedgerId, ledgerId)); |
| | | boolean anyInbound = ledgerAllProducts.stream().anyMatch(p -> { |
| | | BigDecimal sq = p.getStockedQuantity(); |
| | | return sq != null && sq.compareTo(BigDecimal.ZERO) > 0; |
| | | }); |
| | | boolean allLinesFull = ledgerAllProducts.stream().allMatch(p -> Objects.equals(p.getProductStockStatus(), 2)); |
| | | salesLedger.setStockStatus(allLinesFull ? 2 : (anyInbound ? 1 : 0)); |
| | | baseMapper.updateById(salesLedger); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void scanInboundUnqualified(SalesScanInboundDto dto) { |
| | | if (dto == null || dto.getSalesLedgerId() == null) { |
| | | throw new ServiceException("销售订单不合格入库失败,入库数据不能为空"); |
| | | } |
| | | SalesLedger salesLedger = baseMapper.selectById(dto.getSalesLedgerId()); |
| | | if (salesLedger == null) { |
| | | throw new ServiceException("不合格入库失败,销售订单不存在"); |
| | | } |
| | | if (salesLedger.getDeliveryStatus() != null && salesLedger.getDeliveryStatus() == 5) { |
| | | throw new ServiceException("不合格入库失败,该销售订单已发货"); |
| | | } |
| | | if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) { |
| | | throw new ServiceException("销售订单不合格入库失败,入库产品不能为空"); |
| | | } |
| | | Map<Long, BigDecimal> inboundQtyByLineId = new LinkedHashMap<>(); |
| | | for (SalesLedgerProduct inbound : dto.getSalesLedgerProductList()) { |
| | | if (inbound == null || inbound.getId() == null) { |
| | | throw new ServiceException("不合格入库失败,销售产品信息不完整"); |
| | | } |
| | | BigDecimal inboundQty = inbound.getStockedQuantity(); |
| | | if (inboundQty == null) { |
| | | throw new ServiceException("不合格入库失败,入库数量不能为空"); |
| | | } |
| | | if (inboundQty.compareTo(BigDecimal.ZERO) < 0) { |
| | | throw new ServiceException("不合格入库失败,入库数量不能为负数"); |
| | | } |
| | | inboundQtyByLineId.merge(inbound.getId(), inboundQty, BigDecimal::add); |
| | | } |
| | | Long ledgerId = salesLedger.getId(); |
| | | for (Map.Entry<Long, BigDecimal> entry : inboundQtyByLineId.entrySet()) { |
| | | Long salesLedgerProductId = entry.getKey(); |
| | | BigDecimal inboundThisLine = entry.getValue(); |
| | | if (inboundThisLine.compareTo(BigDecimal.ZERO) == 0) { |
| | | continue; |
| | | } |
| | | SalesLedgerProduct dbProduct = salesLedgerProductMapper.selectById(salesLedgerProductId); |
| | | if (dbProduct == null) { |
| | | throw new ServiceException("不合格入库失败,销售产品不存在"); |
| | | } |
| | | if (!Objects.equals(dbProduct.getSalesLedgerId(), ledgerId)) { |
| | | throw new ServiceException("不合格入库失败,销售产品与订单不匹配"); |
| | | } |
| | | if (!Objects.equals(dbProduct.getType(), SaleEnum.SALE.getCode())) { |
| | | throw new ServiceException("不合格入库失败,仅支持销售订单产品行"); |
| | | } |
| | | if (dbProduct.getProductModelId() == null) { |
| | | throw new ServiceException("不合格入库失败,产品规格未维护,无法入库"); |
| | | } |
| | | stockUtils.addUnStock(ledgerId, dbProduct.getId(), dbProduct.getProductModelId(), inboundThisLine, |
| | | StockInUnQualifiedRecordTypeEnum.SALES_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); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void scanOutbound(SalesScanInboundDto dto) { |
| | | if (dto == null || dto.getSalesLedgerId() == null) { |
| | | throw new ServiceException("销售订单出库失败,出库数据不能为空"); |
| | | } |
| | | SalesLedger salesLedger = baseMapper.selectById(dto.getSalesLedgerId()); |
| | | if (salesLedger == null) { |
| | | throw new ServiceException("出库失败,销售订单不存在"); |
| | | } |
| | | if (salesLedger.getDeliveryStatus() != null && salesLedger.getDeliveryStatus() == 5) { |
| | | throw new ServiceException("出库失败,该销售订单已发货"); |
| | | } |
| | | if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) { |
| | | throw new ServiceException("销售订单出库失败,出库产品不能为空"); |
| | | } |
| | | int saleType = SaleEnum.SALE.getCode(); |
| | | Map<Long, BigDecimal> outboundQtyByLineId = new LinkedHashMap<>(); |
| | | for (SalesLedgerProduct line : dto.getSalesLedgerProductList()) { |
| | | if (line == null || line.getId() == null) { |
| | | throw new ServiceException("出库失败,销售产品信息不完整"); |
| | | } |
| | | BigDecimal outboundQty = line.getStockedQuantity(); |
| | | if (outboundQty == null) { |
| | | throw new ServiceException("出库失败,出库数量不能为空"); |
| | | } |
| | | if (outboundQty.compareTo(BigDecimal.ZERO) < 0) { |
| | | throw new ServiceException("出库失败,出库数量不能为负数"); |
| | | } |
| | | outboundQtyByLineId.merge(line.getId(), outboundQty, BigDecimal::add); |
| | | } |
| | | Long ledgerId = salesLedger.getId(); |
| | | for (Map.Entry<Long, BigDecimal> entry : outboundQtyByLineId.entrySet()) { |
| | | Long salesLedgerProductId = entry.getKey(); |
| | | BigDecimal outboundThisLine = entry.getValue(); |
| | | if (outboundThisLine.compareTo(BigDecimal.ZERO) == 0) { |
| | | continue; |
| | | } |
| | | SalesLedgerProduct dbProduct = salesLedgerProductMapper.selectById(salesLedgerProductId); |
| | | if (dbProduct == null) { |
| | | throw new ServiceException("出库失败,销售产品不存在"); |
| | | } |
| | | if (!Objects.equals(dbProduct.getSalesLedgerId(), ledgerId) || !Objects.equals(dbProduct.getType(), saleType)) { |
| | | throw new ServiceException("出库失败,销售产品与订单不匹配"); |
| | | } |
| | | if (dbProduct.getProductModelId() == null) { |
| | | throw new ServiceException("出库失败,产品规格未维护,无法出库"); |
| | | } |
| | | stockUtils.assertQualifiedAvailable(dbProduct.getProductModelId(), outboundThisLine); |
| | | |
| | | StockInventoryDto stockInventoryDto = new StockInventoryDto(); |
| | | stockInventoryDto.setRecordId(dbProduct.getId()); |
| | | stockInventoryDto.setRecordType(StockOutQualifiedRecordTypeEnum.SALE_SCAN_STOCK_OUT.getCode()); |
| | | stockInventoryDto.setQualitity(outboundThisLine); |
| | | stockInventoryDto.setProductModelId(dbProduct.getProductModelId()); |
| | | stockInventoryDto.setSalesLedgerId(ledgerId); |
| | | stockInventoryDto.setSalesLedgerProductId(dbProduct.getId()); |
| | | stockInventoryService.subtractStockInventory(stockInventoryDto); |
| | | |
| | | BigDecimal oldShipped = dbProduct.getShippedQuantity() == null ? BigDecimal.ZERO : dbProduct.getShippedQuantity(); |
| | | dbProduct.setShippedQuantity(oldShipped.add(outboundThisLine)); |
| | | dbProduct.fillRemainingQuantity(); |
| | | salesLedgerProductMapper.updateById(dbProduct); |
| | | } |
| | | List<SalesLedgerProduct> ledgerAllProducts = salesLedgerProductMapper.selectList( |
| | | Wrappers.<SalesLedgerProduct>lambdaQuery().eq(SalesLedgerProduct::getSalesLedgerId, ledgerId)); |
| | | boolean anyInbound = ledgerAllProducts.stream().anyMatch(p -> { |
| | | BigDecimal sq = p.getStockedQuantity(); |
| | | return sq != null && sq.compareTo(BigDecimal.ZERO) > 0; |
| | | }); |
| | | boolean allLinesFull = ledgerAllProducts.stream().allMatch(p -> Objects.equals(p.getProductStockStatus(), 2)); |
| | | salesLedger.setStockStatus(allLinesFull ? 2 : (anyInbound ? 1 : 0)); |
| | | baseMapper.updateById(salesLedger); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void scanOutboundUnqualified(SalesScanInboundDto dto) { |
| | | if (dto == null || dto.getSalesLedgerId() == null) { |
| | | throw new ServiceException("销售订单不合格出库失败,出库数据不能为空"); |
| | | } |
| | | SalesLedger salesLedger = baseMapper.selectById(dto.getSalesLedgerId()); |
| | | if (salesLedger == null) { |
| | | throw new ServiceException("不合格出库失败,销售订单不存在"); |
| | | } |
| | | if (salesLedger.getDeliveryStatus() != null && salesLedger.getDeliveryStatus() == 5) { |
| | | throw new ServiceException("不合格出库失败,该销售订单已发货"); |
| | | } |
| | | if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) { |
| | | throw new ServiceException("销售订单不合格出库失败,出库产品不能为空"); |
| | | } |
| | | int saleType = SaleEnum.SALE.getCode(); |
| | | Map<Long, BigDecimal> outboundQtyByLineId = new LinkedHashMap<>(); |
| | | for (SalesLedgerProduct line : dto.getSalesLedgerProductList()) { |
| | | if (line == null || line.getId() == null) { |
| | | throw new ServiceException("不合格出库失败,销售产品信息不完整"); |
| | | } |
| | | BigDecimal outboundQty = line.getStockedQuantity(); |
| | | if (outboundQty == null) { |
| | | throw new ServiceException("不合格出库失败,出库数量不能为空"); |
| | | } |
| | | if (outboundQty.compareTo(BigDecimal.ZERO) < 0) { |
| | | throw new ServiceException("不合格出库失败,出库数量不能为负数"); |
| | | } |
| | | outboundQtyByLineId.merge(line.getId(), outboundQty, BigDecimal::add); |
| | | } |
| | | Long ledgerId = salesLedger.getId(); |
| | | for (Map.Entry<Long, BigDecimal> entry : outboundQtyByLineId.entrySet()) { |
| | | Long salesLedgerProductId = entry.getKey(); |
| | | BigDecimal outboundThisLine = entry.getValue(); |
| | | if (outboundThisLine.compareTo(BigDecimal.ZERO) == 0) { |
| | | continue; |
| | | } |
| | | SalesLedgerProduct dbProduct = salesLedgerProductMapper.selectById(salesLedgerProductId); |
| | | if (dbProduct == null) { |
| | | throw new ServiceException("不合格出库失败,销售产品不存在"); |
| | | } |
| | | if (!Objects.equals(dbProduct.getSalesLedgerId(), ledgerId) || !Objects.equals(dbProduct.getType(), saleType)) { |
| | | throw new ServiceException("不合格出库失败,销售产品与订单不匹配"); |
| | | } |
| | | 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(ledgerId, dbProduct.getId(), dbProduct.getProductModelId(), outboundThisLine, |
| | | StockOutUnQualifiedRecordTypeEnum.SALE_SCAN_UNSTOCK_OUT.getCode(), dbProduct.getId()); |
| | | |
| | | dbProduct.setUnqualifiedShippedQuantity(unShipped.add(outboundThisLine)); |
| | | dbProduct.fillRemainingQuantity(); |
| | | salesLedgerProductMapper.updateById(dbProduct); |
| | | } |
| | | } |
| | | } |