| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | |
| | | public Logger logger = LoggerFactory.getLogger(this.getClass()); |
| | | |
| | | /** 出库剩余量容差:小于等于此值视为已分配完,避免浮点误差产生多余“库存不足”行 */ |
| | | private static final BigDecimal ISSUED_TOLERANCE = new BigDecimal("0.000001"); |
| | | |
| | | @Autowired |
| | | private AsnOrderItemService asnOrderItemService; |
| | |
| | | if (StringUtils.isNotBlank(locItem.getFieldsIndex())) { |
| | | orderItemWrapper.eq(WkOrderItem::getFieldsIndex, locItem.getFieldsIndex()); |
| | | } |
| | | WkOrderItem orderItem = outStockItemService.getOne(orderItemWrapper); |
| | | // 同一出库单下同一物料可能有多条明细(如多行合并),用 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)) { |
| | |
| | | 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); |
| | | ExistDto existDto = new ExistDto().setBatch(locItem.getBatch()).setMatnr(locItem.getMatnrCode()).setLocNo(locItem.getLocCode()); |
| | | if (existDtos.add(existDto)) { |
| | | // 首次使用该库位:加入列表并扣减 issued |
| | | locItem.setOutQty(allocatable); |
| | | 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(); |