| | |
| | | import com.vincent.rsf.server.manager.utils.OptimalAlgorithmUtil; |
| | | import com.vincent.rsf.server.system.constant.SerialRuleCode; |
| | | import com.vincent.rsf.server.system.utils.SerialRuleUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | * @return |
| | | * @time 2025/3/7 08:02 |
| | | */ |
| | | @Slf4j |
| | | @Service("outStockServiceImpl") |
| | | public class OutStockServiceImpl extends ServiceImpl<AsnOrderMapper, WkOrder> implements OutStockService { |
| | | |
| | | public Logger logger = LoggerFactory.getLogger(this.getClass()); |
| | | |
| | | /** 出库剩余量容差:小于等于此值视为已分配完,避免浮点误差产生多余“库存不足”行 */ |
| | | private static final BigDecimal ISSUED_TOLERANCE = new BigDecimal("0.000001"); |
| | | |
| | | @Autowired |
| | | private AsnOrderItemService asnOrderItemService; |
| | |
| | | private LocService locService; |
| | | @Autowired |
| | | private WaveOrderRelaServiceImpl waveOrderRelaService; |
| | | @Autowired |
| | | private TaskItemService taskItemService; |
| | | |
| | | |
| | | /** |
| | |
| | | for (WkOrderItem orderItem : orderItems) { |
| | | if (!Objects.isNull(orderItem.getPoDetlId())) { |
| | | DeliveryItem deliveryItem = deliveryItemService.getById(orderItem.getPoDetlId()); |
| | | Double workQty = Math.round((deliveryItem.getWorkQty() - orderItem.getAnfme()) * 10000) / 10000.0; |
| | | Double workQty = Math.round((deliveryItem.getWorkQty() - orderItem.getAnfme()) * 1000000) / 1000000.0; |
| | | deliveryItem.setWorkQty(workQty.compareTo(0.0) >= 0 ? workQty : 0); |
| | | if (!deliveryItemService.updateById(deliveryItem)) { |
| | | throw new CoolException("DO单明细更新失败!!"); |
| | |
| | | |
| | | Delivery delivery = deliveryService.getOne(new LambdaQueryWrapper<Delivery>().eq(Delivery::getCode, orderItem.getPoCode())); |
| | | if (!Objects.isNull(delivery)) { |
| | | Double wkQty = Math.round((delivery.getWorkQty() - delivery.getAnfme()) * 10000) / 10000.0; |
| | | Double wkQty = Math.round((delivery.getWorkQty() - delivery.getAnfme()) * 1000000) / 1000000.0; |
| | | delivery.setWorkQty(wkQty.compareTo(0.0) >= 0 ? wkQty : 0).setExceStatus(POExceStatus.PO_EXCE_STATUS_UN_EXCE.val); |
| | | if (!deliveryService.updateById(delivery)) { |
| | | throw new CoolException("DO单据修改失败!!"); |
| | |
| | | if (item.getAnfme().compareTo(0.0) <= 0) { |
| | | throw new CoolException("出库数量不能小于或等于零!!"); |
| | | } |
| | | Double anfme = Math.round((deliveryItem.getAnfme() - item.getAnfme()) * 10000) / 10000.0; |
| | | Double anfme = Math.round((deliveryItem.getAnfme() - item.getAnfme()) * 1000000) / 1000000.0; |
| | | if (anfme.compareTo(0.0) < 0) { |
| | | throw new CoolException("出库数量不足!!"); |
| | | } |
| | |
| | | exceStatus = AsnExceStatus.ASN_EXCE_STATUS_TASK_DONE.val; |
| | | } |
| | | |
| | | Double wkQty = Math.round((delivery.getWorkQty() + sum) * 10000) / 10000.0; |
| | | Double wkQty = Math.round((delivery.getWorkQty() + sum) * 1000000) / 1000000.0; |
| | | if (!deliveryService.update(new LambdaUpdateWrapper<Delivery>() |
| | | .set(Delivery::getExceStatus, exceStatus) |
| | | .set(Delivery::getWorkQty, wkQty) |
| | |
| | | } |
| | | Double sum = orders.stream().mapToDouble(WkOrder::getAnfme).sum(); |
| | | Double workQty = orders.stream().mapToDouble(WkOrder::getWorkQty).sum(); |
| | | Double anfme = Math.round((sum - workQty) * 10000) / 10000.0; |
| | | Double anfme = Math.round((sum - workQty) * 1000000) / 1000000.0; |
| | | Wave wave = new Wave(); |
| | | String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_WAVE_TYPE, null); |
| | | if (StringUtils.isBlank(ruleCode)) { |
| | |
| | | throw new CoolException("单据不存在!!"); |
| | | } |
| | | |
| | | //合并物料,生成波次明细 |
| | | //根据物料编码, 物料批次, 物料索引合并物料,生成波次明细 |
| | | List<WaveItem> waveItems = mergeWave(orderItems, wave); |
| | | |
| | | if (!waveItemService.saveBatch(waveItems)) { |
| | |
| | | .setWaveId(wave.getId()) |
| | | .setWaveItemId(item.getId()); |
| | | if (!waveOrderRelaService.saveOrUpdate(orderRela)) { |
| | | throw new CoolException("<UNK>"); |
| | | throw new CoolException("波次订单关联失败!!"); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | if (!asnOrderItemService.saveOrUpdateBatch(orderItems)) { |
| | | throw new CoolException("出库单执行数量修改失败!!"); |
| | | } |
| | | for (WkOrder order : orders) { |
| | | Double wkQty = Math.round((order.getWorkQty() + order.getAnfme()) * 10000) / 10000.0; |
| | | Double wkQty = Math.round((order.getWorkQty() + order.getAnfme()) * 1000000) / 1000000.0; |
| | | if (!this.update(new LambdaUpdateWrapper<WkOrder>() |
| | | .set(WkOrder::getWaveId, wave.getId()) |
| | | .set(WkOrder::getWorkQty, wkQty) |
| | |
| | | * @time 2025/4/29 13:47 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public R updateOrderItem(AsnOrderAndItemsParams params, Long loginUserId) { |
| | | WkOrder orders = params.getOrders(); |
| | | if (Objects.isNull(orders)) { |
| | |
| | | if (Objects.isNull(params.getItems()) || params.getItems().isEmpty()) { |
| | | throw new CoolException("明细参数不能为空!!"); |
| | | } |
| | | // 删除/修改明细前收集当前单据下的明细 id,用于校验与同步库位状态 |
| | | List<Long> existingIds = asnOrderItemService.list( |
| | | new LambdaQueryWrapper<WkOrderItem>().eq(WkOrderItem::getOrderId, orders.getId())) |
| | | .stream().map(WkOrderItem::getId).collect(Collectors.toList()); |
| | | Set<Long> requestedIds = params.getItems().stream() |
| | | .map(item -> item.get("id")) |
| | | .filter(Objects::nonNull) |
| | | .map(id -> Long.valueOf(id.toString())) |
| | | .collect(Collectors.toSet()); |
| | | // 已生成工作档的明细不允许删除 |
| | | for (Long existingId : existingIds) { |
| | | if (!requestedIds.contains(existingId) && hasGeneratedTask(orders.getId(), existingId)) { |
| | | throw new CoolException("该明细已生成工作档,不能删除"); |
| | | } |
| | | } |
| | | // 已生成工作档的明细不允许修改 |
| | | for (Map<String, Object> item : params.getItems()) { |
| | | Object idObj = item.get("id"); |
| | | if (idObj != null && hasGeneratedTask(orders.getId(), Long.valueOf(idObj.toString()))) { |
| | | throw new CoolException("该明细已生成工作档,不能修改"); |
| | | } |
| | | } |
| | | try { |
| | | svaeOrUpdateOrderItem(params, loginUserId); |
| | | } catch (Exception e) { |
| | | throw new CoolException(e.getMessage()); |
| | | } |
| | | // 对本次被删除的明细(仅初始化状态):先同步库位状态,再删除明细记录 |
| | | for (Long existingId : existingIds) { |
| | | if (!requestedIds.contains(existingId)) { |
| | | syncLocStatusOnOrderItemRemoved(orders.getId(), existingId, loginUserId); |
| | | outStockItemService.removeById(existingId); |
| | | } |
| | | } |
| | | // 重新汇总主单数量(删除明细后) |
| | | List<WkOrderItem> afterItems = asnOrderItemService.list(new LambdaQueryWrapper<WkOrderItem>() |
| | | .eq(WkOrderItem::getOrderId, orders.getId())); |
| | | Double sum = afterItems.stream().mapToDouble(WkOrderItem::getAnfme).sum(); |
| | | orders.setAnfme(sum); |
| | | this.updateById(orders); |
| | | return R.ok(); |
| | | } |
| | | |
| | | /** |
| | | * 判断出库单明细是否已生成工作档(存在关联的任务明细) |
| | | */ |
| | | private boolean hasGeneratedTask(Long orderId, Long orderItemId) { |
| | | return taskItemService.count(new LambdaQueryWrapper<TaskItem>() |
| | | .eq(TaskItem::getSourceId, orderId) |
| | | .eq(TaskItem::getOrderItemId, orderItemId)) > 0; |
| | | } |
| | | |
| | | /** |
| | | * 出库单明细被删除时同步库位状态:释放该明细关联的库位预约、回滚 LocItem.workQty、恢复库位为在库(F) |
| | | */ |
| | | private void syncLocStatusOnOrderItemRemoved(Long orderId, Long orderItemId, Long loginUserId) { |
| | | List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>() |
| | | .eq(TaskItem::getSourceId, orderId) |
| | | .eq(TaskItem::getOrderItemId, orderItemId)); |
| | | if (taskItems.isEmpty()) { |
| | | return; |
| | | } |
| | | Set<Long> affectedLocIds = new HashSet<>(); |
| | | Date now = new Date(); |
| | | for (TaskItem taskItem : taskItems) { |
| | | if (taskItem.getSource() == null) { |
| | | continue; |
| | | } |
| | | LocItem locItem = locItemService.getById(taskItem.getSource()); |
| | | if (locItem == null) { |
| | | continue; |
| | | } |
| | | Double anfme = taskItem.getAnfme() != null ? taskItem.getAnfme() : 0.0; |
| | | Double newWorkQty = Math.round((locItem.getWorkQty() - anfme) * 1000000) / 1000000.0; |
| | | locItem.setWorkQty(newWorkQty >= 0 ? newWorkQty : 0) |
| | | .setOrderId(null) |
| | | .setOrderItemId(null) |
| | | .setUpdateBy(loginUserId) |
| | | .setUpdateTime(now); |
| | | locItemService.updateById(locItem); |
| | | affectedLocIds.add(locItem.getLocId()); |
| | | } |
| | | for (Long locId : affectedLocIds) { |
| | | long stillReserved = locItemService.count(new LambdaQueryWrapper<LocItem>() |
| | | .eq(LocItem::getLocId, locId) |
| | | .isNotNull(LocItem::getOrderId)); |
| | | if (stillReserved == 0) { |
| | | Loc loc = locService.getById(locId); |
| | | if (loc != null && LocStsType.LOC_STS_TYPE_R.type.equals(loc.getUseStatus())) { |
| | | loc.setUseStatus(LocStsType.LOC_STS_TYPE_F.type) |
| | | .setUpdateBy(loginUserId) |
| | | .setUpdateTime(now); |
| | | locService.updateById(loc); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | if (Objects.isNull(param) || StringUtils.isBlank(param.getLocCode())) { |
| | | continue; |
| | | } |
| | | Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, param.getLocCode()).eq(Loc::getBarcode, param.getBarcode())); |
| | | // 修复:构建库位查询条件,如果barcode为空,只按locCode查询 |
| | | LambdaQueryWrapper<Loc> locQueryWrapper = new LambdaQueryWrapper<Loc>() |
| | | .eq(Loc::getCode, param.getLocCode()); |
| | | if (StringUtils.isNotBlank(param.getBarcode())) { |
| | | locQueryWrapper.eq(Loc::getBarcode, param.getBarcode()); |
| | | } |
| | | Loc loc = locService.getOne(locQueryWrapper); |
| | | if (!Objects.isNull(loc)) { |
| | | List<LocItem> locItems = new ArrayList<>(); |
| | | LocItem locItem = locItemService.getById(param.getId()); |
| | | |
| | | if (Objects.isNull(locItem)) { |
| | | throw new CoolException("库位明细不存在,ID:" + param.getId()); |
| | | } |
| | | |
| | | WkOrderItem orderItem = outStockItemService.getOne(new LambdaQueryWrapper<WkOrderItem>() |
| | | // 修复:构建查询条件,先构建基础条件,再根据值是否为空动态添加 |
| | | // 优先使用供应商批次匹配,如果没有则使用库存批次匹配 |
| | | LambdaQueryWrapper<WkOrderItem> orderItemWrapper = new LambdaQueryWrapper<WkOrderItem>() |
| | | .eq(WkOrderItem::getOrderId, outId) |
| | | .eq(StringUtils.isNotBlank(locItem.getBatch()), WkOrderItem::getSplrBatch, locItem.getBatch()) |
| | | .eq(StringUtils.isNotBlank(locItem.getFieldsIndex()), WkOrderItem::getFieldsIndex, locItem.getFieldsIndex()) |
| | | .eq(WkOrderItem::getMatnrId, locItem.getMatnrId())); |
| | | .eq(WkOrderItem::getMatnrId, locItem.getMatnrId()); |
| | | |
| | | // 优先使用供应商批次匹配 |
| | | if (StringUtils.isNotBlank(locItem.getSplrBatch())) { |
| | | orderItemWrapper.eq(WkOrderItem::getSplrBatch, locItem.getSplrBatch()); |
| | | } else if (StringUtils.isNotBlank(locItem.getBatch())) { |
| | | // 如果LocItem只有batch字段,尝试同时匹配WkOrderItem的batch和splrBatch字段 |
| | | // 因为某些情况下LocItem的batch可能对应WkOrderItem的splrBatch |
| | | orderItemWrapper.and(wrapper -> wrapper |
| | | .eq(WkOrderItem::getBatch, locItem.getBatch()) |
| | | .or() |
| | | .eq(WkOrderItem::getSplrBatch, locItem.getBatch()) |
| | | ); |
| | | } |
| | | |
| | | if (StringUtils.isNotBlank(locItem.getFieldsIndex())) { |
| | | orderItemWrapper.eq(WkOrderItem::getFieldsIndex, locItem.getFieldsIndex()); |
| | | } |
| | | // 同一出库单下同一物料可能有多条明细(如多行合并),用 list 取仍有剩余数量的第一条,避免 getOne 返回多条抛 TooManyResultsException |
| | | List<WkOrderItem> orderItemCandidates = outStockItemService.list(orderItemWrapper); |
| | | WkOrderItem orderItem = orderItemCandidates.stream() |
| | | .filter(o -> o.getAnfme() != null && o.getWorkQty() != null && o.getAnfme().compareTo(o.getWorkQty()) > 0) |
| | | .findFirst() |
| | | .orElse(null); |
| | | |
| | | // 如果找不到单据明细,且LocItem来自库存调整,则自动创建WkOrderItem |
| | | if (Objects.isNull(orderItem)) { |
| | | throw new CoolException("单据明细不存在!!"); |
| | | // 检查是否是库存调整产生的库存 |
| | | if (locItem.getWkType() != null && |
| | | locItem.getWkType().equals(Short.parseShort(OrderWorkType.ORDER_WORK_TYPE_STOCK_REVISE.type))) { |
| | | // 获取出库单信息 |
| | | WkOrder outOrder = outStockService.getById(outId); |
| | | if (Objects.isNull(outOrder)) { |
| | | throw new CoolException("出库单据不存在!!"); |
| | | } |
| | | |
| | | log.info("库存调整产生的库存,自动创建WkOrderItem - 出库单ID:{},物料ID:{},批次:{}", |
| | | outId, locItem.getMatnrId(), locItem.getBatch()); |
| | | |
| | | // 创建WkOrderItem |
| | | orderItem = new WkOrderItem(); |
| | | orderItem.setOrderId(outId) |
| | | .setOrderCode(outOrder.getCode()) |
| | | .setMatnrId(locItem.getMatnrId()) |
| | | .setMatnrCode(locItem.getMatnrCode()) |
| | | .setMaktx(locItem.getMaktx()) |
| | | .setBatch(StringUtils.isNotBlank(param.getBatch()) ? param.getBatch() : locItem.getBatch()) |
| | | .setSplrBatch(StringUtils.isNotBlank(locItem.getSplrBatch()) ? locItem.getSplrBatch() : |
| | | (StringUtils.isNotBlank(locItem.getBatch()) ? locItem.getBatch() : null)) |
| | | .setFieldsIndex(locItem.getFieldsIndex()) |
| | | .setAnfme(param.getOutQty()) |
| | | .setWorkQty(0.0) |
| | | .setStockUnit(locItem.getUnit()) |
| | | .setPurUnit(locItem.getUnit()) |
| | | .setSpec(locItem.getSpec()) |
| | | .setModel(locItem.getModel()) |
| | | .setCreateBy(loginUserId) |
| | | .setUpdateBy(loginUserId) |
| | | .setCreateTime(new Date()) |
| | | .setUpdateTime(new Date()); |
| | | |
| | | if (!outStockItemService.save(orderItem)) { |
| | | throw new CoolException("库存调整单据明细创建失败!!"); |
| | | } |
| | | |
| | | log.info("WkOrderItem创建成功 - ID:{},出库单ID:{},物料ID:{}", |
| | | orderItem.getId(), outId, locItem.getMatnrId()); |
| | | } else { |
| | | throw new CoolException("单据明细不存在!!出库单ID:" + outId + ",物料ID:" + locItem.getMatnrId() + |
| | | (StringUtils.isNotBlank(locItem.getSplrBatch()) ? ",供应商批次:" + locItem.getSplrBatch() : |
| | | StringUtils.isNotBlank(locItem.getBatch()) ? ",库存批次:" + locItem.getBatch() : "") + |
| | | (StringUtils.isNotBlank(locItem.getFieldsIndex()) ? ",字段索引:" + locItem.getFieldsIndex() : "")); |
| | | } |
| | | } |
| | | |
| | | locItem.setOutQty(param.getOutQty()) |
| | | .setBatch(param.getBatch()) |
| | | .setOrderId(outId) |
| | | .setOrderItemId(orderItem.getId()) |
| | | .setSourceId(outId) |
| | | .setSourceCode(orderItem.getOrderCode()) |
| | | .setSource(orderItem.getId()); |
| | |
| | | throw new CoolException(e.getMessage()); |
| | | } |
| | | |
| | | |
| | | Double workQty = Math.round((orderItem.getWorkQty() + locItem.getOutQty()) * 10000) / 10000.0; |
| | | Double workQty = Math.round((orderItem.getWorkQty() + locItem.getOutQty()) * 1000000) / 1000000.0; |
| | | |
| | | orderItem.setUpdateBy(loginUserId).setUpdateTime(new Date()).setWorkQty(workQty); |
| | | |
| | |
| | | if (Objects.isNull(outOrder)) { |
| | | throw new CoolException("出库单据不存在!!"); |
| | | } |
| | | Double workQty = Math.round((outOrder.getWorkQty() + sum) * 10000) / 10000.0; |
| | | Double workQty = Math.round((outOrder.getWorkQty() + sum) * 1000000) / 1000000.0; |
| | | |
| | | outOrder.setWorkQty(workQty).setExceStatus(AsnExceStatus.OUT_STOCK_STATUS_TASK_CREATE.val); |
| | | |
| | |
| | | TaskType.TASK_TYPE_OUT.type, |
| | | TaskType.TASK_TYPE_MERGE_OUT.type, |
| | | TaskType.TASK_TYPE_PICK_AGAIN_OUT.type); |
| | | List<DeviceSite> sites = deviceSiteService.list(new LambdaQueryWrapper<DeviceSite>().in(DeviceSite::getType, list).groupBy(DeviceSite::getSite)); |
| | | // 先查全部再按 site 去重,避免 GROUP BY 与 only_full_group_by 冲突 |
| | | List<DeviceSite> all = deviceSiteService.list(new LambdaQueryWrapper<DeviceSite>().in(DeviceSite::getType, list)); |
| | | List<DeviceSite> sites = all.stream() |
| | | .collect(Collectors.toMap(DeviceSite::getSite, d -> d, (a, b) -> a)) |
| | | .values().stream() |
| | | .collect(Collectors.toList()); |
| | | return R.ok(sites); |
| | | } |
| | | |
| | |
| | | if (!items.isEmpty()) { |
| | | for (WkOrderItem orderItem : items) { |
| | | DeliveryItem deliveryItem = deliveryItemService.getById(orderItem.getPoDetlId()); |
| | | Double workQty = Math.round((deliveryItem.getWorkQty() - orderItem.getAnfme()) * 10000) / 10000.0; |
| | | Double workQty = Math.round((deliveryItem.getWorkQty() - orderItem.getAnfme()) * 1000000) / 1000000.0; |
| | | deliveryItem.setWorkQty(workQty.compareTo(0.0) >= 0 ? workQty : 0); |
| | | if (!deliveryItemService.updateById(deliveryItem)) { |
| | | throw new CoolException("DO单明细更新失败!!"); |
| | |
| | | |
| | | Delivery delivery = deliveryService.getOne(new LambdaQueryWrapper<Delivery>().eq(Delivery::getCode, orderItem.getPoCode())); |
| | | if (!Objects.isNull(delivery)) { |
| | | Double wkQty = Math.round((delivery.getWorkQty() - delivery.getAnfme()) * 10000) / 10000.0; |
| | | Double wkQty = Math.round((delivery.getWorkQty() - delivery.getAnfme()) * 1000000) / 1000000.0; |
| | | delivery.setWorkQty(wkQty.compareTo(0.0) >= 0 ? wkQty : 0).setExceStatus(POExceStatus.PO_EXCE_STATUS_UN_EXCE.val); |
| | | if (!deliveryService.updateById(delivery)) { |
| | | throw new CoolException("DO单据修改失败!!"); |
| | |
| | | Set<ExistDto> existDtos = new HashSet<>(); |
| | | for (WkOrderItem wkOrderItem : wkOrderItems) { |
| | | BigDecimal issued = new BigDecimal(wkOrderItem.getAnfme().toString()) |
| | | .subtract(new BigDecimal(wkOrderItem.getWorkQty().toString()) |
| | | ); |
| | | if (issued.doubleValue() <= 0) { |
| | | .subtract(new BigDecimal(wkOrderItem.getWorkQty().toString())); |
| | | if (issued.compareTo(ISSUED_TOLERANCE) <= 0) { |
| | | continue; |
| | | } |
| | | List<LocItem> locItems = new ArrayList<>(); |
| | |
| | | for (LocItem locItem : locItems) { |
| | | Loc loc = locService.getById(locItem.getLocId()); |
| | | List<LocItem> itemList = locItemService.list(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocCode, locItem.getLocCode())); |
| | | if (issued.doubleValue() > 0) { |
| | | ExistDto existDto = new ExistDto().setBatch(locItem.getBatch()).setMatnr(locItem.getMatnrCode()).setLocNo(locItem.getLocCode()); |
| | | if (existDtos.add(existDto)) { |
| | | locItem.setOutQty(issued.doubleValue() >= locItem.getAnfme() ? locItem.getAnfme() : issued.doubleValue()); |
| | | locItem.setBarcode(loc.getBarcode()); |
| | | OrderOutItemDto orderOutItemDto = new OrderOutItemDto(); |
| | | orderOutItemDto.setLocItem(locItem); |
| | | if (issued.compareTo(ISSUED_TOLERANCE) <= 0) { |
| | | break; |
| | | } |
| | | // 该库位可分配数量:取本行待分配与库位库存的较小值 |
| | | double allocatable = Math.min(issued.doubleValue(), locItem.getAnfme() != null ? locItem.getAnfme() : 0); |
| | | // 当分配量等于库位库存时,使用库位库存精度作为出库数量,避免截断导致界面显示/库存校验不一致(如库存15.123457被截成15.123) |
| | | double outQtyToSet = (locItem.getAnfme() != null && Math.abs(allocatable - locItem.getAnfme()) < 1e-6) |
| | | ? locItem.getAnfme() : allocatable; |
| | | ExistDto existDto = new ExistDto().setBatch(locItem.getBatch()).setMatnr(locItem.getMatnrCode()).setLocNo(locItem.getLocCode()); |
| | | if (existDtos.add(existDto)) { |
| | | // 首次使用该库位:加入列表并扣减 issued |
| | | locItem.setOutQty(outQtyToSet); |
| | | locItem.setBarcode(loc.getBarcode()); |
| | | OrderOutItemDto orderOutItemDto = new OrderOutItemDto(); |
| | | orderOutItemDto.setLocItem(locItem); |
| | | |
| | | List<DeviceSite> deviceSites = deviceSiteService.list(new LambdaQueryWrapper<DeviceSite>() |
| | | .eq(DeviceSite::getChannel, loc.getChannel()) |
| | | .eq(DeviceSite::getType, issued.doubleValue() >= locItem.getAnfme() && itemList.size() == 1 ? TaskType.TASK_TYPE_OUT.type : TaskType.TASK_TYPE_PICK_AGAIN_OUT.type) |
| | | ); |
| | | List<DeviceSite> deviceSites = deviceSiteService.list(new LambdaQueryWrapper<DeviceSite>() |
| | | .eq(DeviceSite::getChannel, loc.getChannel()) |
| | | .eq(DeviceSite::getType, issued.doubleValue() >= locItem.getAnfme() && itemList.size() == 1 ? TaskType.TASK_TYPE_OUT.type : TaskType.TASK_TYPE_PICK_AGAIN_OUT.type) |
| | | ); |
| | | |
| | | if (!deviceSites.isEmpty()) { |
| | | List<OrderOutItemDto.staListDto> maps = new ArrayList<>(); |
| | | for (DeviceSite sta : deviceSites) { |
| | | OrderOutItemDto.staListDto staListDto = new OrderOutItemDto.staListDto(); |
| | | staListDto.setStaNo(sta.getSite()); |
| | | staListDto.setStaName(sta.getSite()); |
| | | maps.add(staListDto); |
| | | } |
| | | orderOutItemDto.setStaNos(maps); |
| | | //默认获取第一站点 |
| | | DeviceSite deviceSite = deviceSites.stream().findFirst().get(); |
| | | orderOutItemDto.setSiteNo(deviceSite.getSite()); |
| | | if (!deviceSites.isEmpty()) { |
| | | List<OrderOutItemDto.staListDto> maps = new ArrayList<>(); |
| | | for (DeviceSite sta : deviceSites) { |
| | | OrderOutItemDto.staListDto staListDto = new OrderOutItemDto.staListDto(); |
| | | staListDto.setStaNo(sta.getSite()); |
| | | staListDto.setStaName(sta.getSite()); |
| | | maps.add(staListDto); |
| | | } |
| | | |
| | | list.add(orderOutItemDto); |
| | | |
| | | issued = issued.subtract(new BigDecimal(locItem.getAnfme().toString())); |
| | | orderOutItemDto.setStaNos(maps); |
| | | //默认获取第一站点 |
| | | DeviceSite deviceSite = deviceSites.stream().findFirst().get(); |
| | | orderOutItemDto.setSiteNo(deviceSite.getSite()); |
| | | } |
| | | |
| | | list.add(orderOutItemDto); |
| | | issued = issued.subtract(new BigDecimal(locItem.getAnfme().toString())); |
| | | } else { |
| | | // 该库位已被前序订单行占用:只扣减 issued,不重复加入列表,避免产生“库存不足”脏数据 |
| | | issued = issued.subtract(new BigDecimal(String.valueOf(allocatable))); |
| | | } |
| | | } |
| | | if (issued.doubleValue() > 0) { |
| | | if (issued.compareTo(ISSUED_TOLERANCE) > 0) { |
| | | double remaining = issued.setScale(6, RoundingMode.HALF_UP).doubleValue(); |
| | | LocItem locItem = new LocItem() |
| | | .setId(new Random().nextLong()) |
| | | .setMatnrCode(wkOrderItem.getMatnrCode()) |
| | | .setMaktx(wkOrderItem.getMaktx()) |
| | | .setAnfme(0.00) |
| | | .setWorkQty(issued.doubleValue()) |
| | | .setOutQty(issued.doubleValue()) |
| | | .setWorkQty(remaining) |
| | | .setOutQty(remaining) |
| | | .setUnit(wkOrderItem.getStockUnit()) |
| | | .setBatch(wkOrderItem.getSplrBatch()); |
| | | OrderOutItemDto orderOutItemDto = new OrderOutItemDto(); |
| | |
| | | private List<WaveItem> mergeWave(List<WkOrderItem> orderItems, Wave wave) { |
| | | List<WaveItem> items = new ArrayList<>(); |
| | | orderItems.forEach(order -> { |
| | | Double anfme = Math.round((order.getAnfme() - order.getWorkQty()) * 10000) / 10000.0; |
| | | Double anfme = Math.round((order.getAnfme() - order.getWorkQty()) * 1000000) / 1000000.0; |
| | | WaveItem item = new WaveItem(); |
| | | BeanUtils.copyProperties(order, item); |
| | | item.setId(null) |
| | |
| | | p1.getUnit(), |
| | | p1.getTrackCode(), |
| | | p1.getFieldsIndex(), |
| | | Math.round((p1.getAnfme() + p2.getAnfme()) * 10000) / 10000.0, |
| | | Math.round((p1.getAnfme() + p2.getAnfme())* 1000000) / 1000000.0, |
| | | p1.getWorkQty(), |
| | | p1.getTenantId(), |
| | | p1.getStatus(), |
| | |
| | | p1.getUpdateBy(), |
| | | p1.getMemo() |
| | | ), |
| | | WaveItem::getSplrBatch, WaveItem::getMatnrCode, WaveItem::getFieldsIndex |
| | | WaveItem::getMatnrCode |
| | | ); |
| | | |
| | | return waveItems; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @param id |
| | | * @return |
| | | * @author Ryan |
| | | * @description 完成出库单 |
| | | * @time 2025/4/25 10:07 |
| | | */ |
| | | @Override |
| | | public R completeOutOrderById(Long id) { |
| | | WkOrder order = this.getById(id); |
| | | if (Objects.isNull(order)) { |
| | | return R.error("出库单不存在!!"); |
| | | } |
| | | order.setExceStatus(AsnExceStatus.OUT_STOCK_STATUS_TASK_DONE.val); |
| | | if (!this.updateById(order)) { |
| | | throw new CoolException("完成出库单失败!!"); |
| | | } |
| | | return R.ok("完成出库单成功!!"); |
| | | } |
| | | } |