use of com.qcadoo.mes.productFlowThruDivision.deliveries.helpers.DeliveredProductReservationKeyObject in project mes by qcadoo.
the class DeliveryStatePFTDService method flatReservations.
private Multimap<DeliveredProductReservationKeyObject, DeliveredProductReservationObject> flatReservations(final List<Entity> reservations) {
Multimap<DeliveredProductReservationKeyObject, DeliveredProductReservationObject> reservationsMapGroupedByWarehouse = ArrayListMultimap.create();
reservations.forEach(res -> {
Entity deliveredProduct = res.getBelongsToField(DeliveredProductReservationFields.DELIVERED_PRODUCT);
Entity product = deliveredProduct.getBelongsToField(DeliveredProductFields.PRODUCT);
Entity additionalCode = deliveredProduct.getBelongsToField(DeliveredProductFields.ADDITIONAL_CODE);
BigDecimal conversion = deliveredProduct.getDecimalField(DeliveredProductFields.CONVERSION);
DeliveredProductReservationKeyObject key = new DeliveredProductReservationKeyObject(product, additionalCode, conversion);
DeliveredProductReservationObject value = new DeliveredProductReservationObject(deliveredProduct, res);
reservationsMapGroupedByWarehouse.put(key, value);
});
return reservationsMapGroupedByWarehouse;
}
use of com.qcadoo.mes.productFlowThruDivision.deliveries.helpers.DeliveredProductReservationKeyObject in project mes by qcadoo.
the class DeliveryStatePFTDService method createWarehouseIssue.
private void createWarehouseIssue(final Long warehouse, final List<Entity> reservations, final CreationWarehouseIssueState creationWarehouseIssueState, final Entity delivery, final Entity placeOfIssue) {
Multimap<DeliveredProductReservationKeyObject, DeliveredProductReservationObject> flatReservations = flatReservations(reservations);
Map<DeliveredProductReservationKeyObject, Collection<DeliveredProductReservationObject>> simpleFlatReservations = flatReservations.asMap();
Entity warehouseIssue = createNewWarehouseIssue(placeOfIssue, delivery);
if (!warehouseIssue.isValid()) {
for (Map.Entry<String, ErrorMessage> entry : warehouseIssue.getErrors().entrySet()) {
creationWarehouseIssueState.getErrors().add(entry.getValue());
}
throw new IllegalStateException("Undone creation warehouse issue for delivery : " + delivery.getStringField(DeliveryFields.NUMBER));
}
Entity location = getLocationDD().get(warehouse);
List<Entity> createdProductsToIssue = Lists.newArrayList();
for (Map.Entry<DeliveredProductReservationKeyObject, Collection<DeliveredProductReservationObject>> entry : simpleFlatReservations.entrySet()) {
DeliveredProductReservationKeyObject key = entry.getKey();
Collection<DeliveredProductReservationObject> value = entry.getValue();
Entity product = key.getProduct();
Entity productToIssue = getProductsToIssueDD().create();
productToIssue.setField(ProductsToIssueFields.PRODUCT, product);
productToIssue.setField(ProductsToIssueFields.ADDITIONAL_CODE, key.getAdditionalCode());
productToIssue.setField(ProductsToIssueFields.CONVERSION, key.getConversion());
productToIssue.setField(ProductsToIssueFields.LOCATION, location);
if (productAlreadyCreated(createdProductsToIssue, product.getId(), Optional.ofNullable(key.getAdditionalCode()).orElse(productToIssue).getId(), location.getId(), key.getConversion())) {
warehouseIssue = createNewWarehouseIssue(placeOfIssue, delivery);
if (!warehouseIssue.isValid()) {
for (Map.Entry<String, ErrorMessage> e : warehouseIssue.getErrors().entrySet()) {
creationWarehouseIssueState.getErrors().add(e.getValue());
}
throw new IllegalStateException("Undone creation warehouse issue for delivery : " + delivery.getStringField(DeliveryFields.NUMBER));
}
createdProductsToIssue = Lists.newArrayList();
}
productToIssue.setField(ProductsToIssueFields.WAREHOUSE_ISSUE, warehouseIssue);
Optional<Entity> storageLocation = findStorageLocationForProduct(product, location);
if (storageLocation.isPresent()) {
productToIssue.setField(ProductsToIssueFields.STORAGE_LOCATION, storageLocation.get());
}
BigDecimal dQuantity = value.stream().filter(Objects::nonNull).map(val -> val.getReservationForDeliveredProduct().getDecimalField(DeliveredProductReservationFields.DELIVERED_QUANTITY)).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal aQuantity = value.stream().filter(Objects::nonNull).map(val -> val.getReservationForDeliveredProduct().getDecimalField(DeliveredProductReservationFields.ADDITIONAL_QUANTITY)).reduce(BigDecimal.ZERO, BigDecimal::add);
productToIssue.setField(ProductsToIssueFields.DEMAND_QUANTITY, dQuantity);
productToIssue.setField(ProductsToIssueFields.ADDITIONAL_DEMAND_QUANTITY, aQuantity);
productToIssue = productToIssue.getDataDefinition().save(productToIssue);
createdProductsToIssue.add(productToIssue);
if (!productToIssue.isValid()) {
for (Map.Entry<String, ErrorMessage> e : productToIssue.getErrors().entrySet()) {
creationWarehouseIssueState.getErrors().add(e.getValue());
}
throw new IllegalStateException("Undone creation warehouse issue for delivery : " + delivery.getStringField(DeliveryFields.NUMBER));
}
}
}
Aggregations