use of com.qcadoo.mes.productFlowThruDivision.deliveries.helpers.CreationWarehouseIssueState in project mes by qcadoo.
the class DeliveryStatePFTDService method tryCreateIssuesForDeliveriesReservations.
public void tryCreateIssuesForDeliveriesReservations(final StateChangeContext context) {
if (warehouseIssueParameterService.generateWarehouseIssuesToDeliveries()) {
Entity delivery = context.getOwner();
List<Entity> reservationsForDelivery = findReservationsForDelivery(delivery);
if (reservationsForDelivery.isEmpty()) {
return;
}
Multimap<Long, Entity> reservationsMapGroupedByWarehouse = ArrayListMultimap.create();
for (Entity res : reservationsForDelivery) {
reservationsMapGroupedByWarehouse.put(res.getBelongsToField(DeliveredProductReservationFields.LOCATION).getId(), res);
}
CreationWarehouseIssueState creationWarehouseIssueState = new CreationWarehouseIssueState();
createWarehouseIssues(creationWarehouseIssueState, delivery, delivery.getBelongsToField(DeliveryFields.LOCATION), reservationsMapGroupedByWarehouse);
context.addMessage("productFlowThruDivision.deliveries.warehouseIssue.issuesCreated.success", StateMessageType.SUCCESS);
}
}
use of com.qcadoo.mes.productFlowThruDivision.deliveries.helpers.CreationWarehouseIssueState 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