@Override public List<Entity> getResourcesForWarehouseProductAndAlgorithm( Entity warehouse, Entity product, Entity position, WarehouseAlgorithm warehouseAlgorithm) { List<Entity> resources = Lists.newArrayList(); Entity resource = position.getBelongsToField(PositionFields.RESOURCE); Entity additionalCode = position.getBelongsToField(PositionFields.ADDITIONAL_CODE); if (resource != null) { Entity reservation = reservationsService.getReservationForPosition(position); if (reservation != null) { BigDecimal reservationQuantity = reservation.getDecimalField(ReservationFields.QUANTITY); BigDecimal resourceAvailableQuantity = resource.getDecimalField(ResourceFields.AVAILABLE_QUANTITY); resource.setField( ResourceFields.AVAILABLE_QUANTITY, resourceAvailableQuantity.add(reservationQuantity)); } resources.add(resource); } else if (WarehouseAlgorithm.FIFO.equals(warehouseAlgorithm)) { resources = getResourcesForLocationAndProductFIFO(warehouse, product, additionalCode, position); } else if (WarehouseAlgorithm.LIFO.equals(warehouseAlgorithm)) { resources = getResourcesForLocationAndProductLIFO(warehouse, product, additionalCode, position); } else if (WarehouseAlgorithm.FEFO.equals(warehouseAlgorithm)) { resources = getResourcesForLocationAndProductFEFO(warehouse, product, additionalCode, position); } else if (WarehouseAlgorithm.LEFO.equals(warehouseAlgorithm)) { resources = getResourcesForLocationAndProductLEFO(warehouse, product, additionalCode, position); } else if (WarehouseAlgorithm.MANUAL.equals(warehouseAlgorithm)) { resources = getResourcesForLocationAndProductMANUAL(warehouse, product, position, additionalCode); } return resources; }
public Multimap<Long, BigDecimal> getQuantitiesInWarehouse( final Entity warehouse, Multimap<Entity, Entity> productsAndPositions) { Multimap<Long, BigDecimal> result = ArrayListMultimap.create(); String algorithm = warehouse.getStringField(LocationFieldsMFR.ALGORITHM); for (Map.Entry<Entity, Entity> productAndPosition : productsAndPositions.entries()) { Entity resource = productAndPosition.getValue().getBelongsToField(PositionFields.RESOURCE); if (algorithm.equalsIgnoreCase(WarehouseAlgorithm.MANUAL.getStringValue()) && resource != null) { result.put( productAndPosition.getKey().getId(), resource.getDecimalField(ResourceFields.AVAILABLE_QUANTITY)); } else { Entity additionalCode = productAndPosition.getValue().getBelongsToField(PositionFields.ADDITIONAL_CODE); BigDecimal conversion = productAndPosition.getValue().getDecimalField(PositionFields.CONVERSION); Entity reservation = reservationsService.getReservationForPosition(productAndPosition.getValue()); List<Entity> resources = Lists.newArrayList(); if (additionalCode != null) { SearchCriteriaBuilder scb = getSearchCriteriaForResourceForProductAndWarehouse( productAndPosition.getKey(), warehouse); if (!StringUtils.isEmpty( productAndPosition.getKey().getStringField(ProductFields.ADDITIONAL_UNIT))) { scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, conversion)); } else { scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, BigDecimal.ONE)); } resources = scb.add(SearchRestrictions.belongsTo(ResourceFields.ADDITIONAL_CODE, additionalCode)) .list() .getEntities(); scb = getSearchCriteriaForResourceForProductAndWarehouse( productAndPosition.getKey(), warehouse); if (!StringUtils.isEmpty( productAndPosition.getKey().getStringField(ProductFields.ADDITIONAL_UNIT))) { scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, conversion)); } else { scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, BigDecimal.ONE)); } resources.addAll( scb.add( SearchRestrictions.or( SearchRestrictions.isNull(ResourceFields.ADDITIONAL_CODE), SearchRestrictions.ne("additionalCode.id", additionalCode.getId()))) .list() .getEntities()); } if (resources.isEmpty()) { SearchCriteriaBuilder scb = getSearchCriteriaForResourceForProductAndWarehouse( productAndPosition.getKey(), warehouse); if (!StringUtils.isEmpty( productAndPosition.getKey().getStringField(ProductFields.ADDITIONAL_UNIT))) { scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, conversion)); } else { scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, BigDecimal.ONE)); } resources = scb.list().getEntities(); } BigDecimal reservedQuantity = BigDecimal.ZERO; if (reservation != null) { reservedQuantity = reservation.getDecimalField(ReservationFields.QUANTITY); } if (result.containsKey(productAndPosition.getKey().getId())) { BigDecimal currentQuantity = result .get(productAndPosition.getKey().getId()) .stream() .reduce(reservedQuantity, BigDecimal::add); result.put( productAndPosition.getKey().getId(), (resources .stream() .map(res -> res.getDecimalField(ResourceFields.AVAILABLE_QUANTITY)) .reduce(BigDecimal.ZERO, BigDecimal::add)) .add(currentQuantity)); } else { result.put( productAndPosition.getKey().getId(), resources .stream() .map(res -> res.getDecimalField(ResourceFields.AVAILABLE_QUANTITY)) .reduce(reservedQuantity, BigDecimal::add)); } } } return result; }