use of com.qcadoo.mes.deliveries.constants.DeliveryFields.EXTERNAL_SYNCHRONIZED in project mes by qcadoo.
the class GenerateMaterialRequirementCoverageListeners method createDelivery.
/**
* createOffer event handler
*
* @param view
* @param state
* @param args
*/
public void createDelivery(final ViewDefinitionState view, final ComponentState state, final String[] args) {
init();
GridComponent grid = (GridComponent) view.getComponentByReference(L_GRID);
Entity parameter = parameterService.getParameter();
Entity systemCurrency = parameter.getBelongsToField(ParameterFields.CURRENCY);
DataDefinition companyDataDefinition = dataDefinitionService.get(BasicConstants.PLUGIN_IDENTIFIER, BasicConstants.MODEL_COMPANY);
Map<Optional<Integer>, List<Entity>> groupedCoverageProducts = grid.getSelectedEntities().stream().collect(Collectors.groupingBy(e -> Optional.ofNullable(e.getIntegerField("companyId"))));
StringBuilder deliveryNumbers = new StringBuilder();
for (Map.Entry<Optional<Integer>, List<Entity>> entry : groupedCoverageProducts.entrySet()) {
String number = numberGeneratorService.generateNumber(DeliveriesConstants.PLUGIN_IDENTIFIER, DeliveriesConstants.MODEL_DELIVERY);
Entity delivery = deliveriesService.getDeliveryDD().create();
delivery.setField(NUMBER, number);
Entity currency = null;
if (entry.getKey().isPresent()) {
Entity supplier = companyDataDefinition.get(entry.getKey().get().longValue());
delivery.setField(SUPPLIER, supplier);
currency = supplier.getBelongsToField(CompanyFieldsD.CURRENCY);
}
if (currency == null) {
currency = systemCurrency;
}
delivery.setField(CURRENCY, currency);
delivery.setField(EXTERNAL_SYNCHRONIZED, true);
Entity saved = deliveriesService.getDeliveryDD().save(delivery);
if (saved.isValid()) {
deliveryNumbers.append("<br/>").append(number);
entry.getValue().forEach(coverageProduct -> {
Integer product = coverageProduct.getIntegerField("productId");
BigDecimal reserveMissingQuantity = coverageProduct.getDecimalField(CoverageProductFields.RESERVE_MISSING_QUANTITY);
BigDecimal orderedQuantity = reserveMissingQuantity.min(BigDecimal.ZERO).abs();
BigDecimal conversion = getConversion(product);
Entity orderedProduct = orderedProductDataDefinition.create();
orderedProduct.setField("delivery", saved);
orderedProduct.setField("product", product.longValue());
orderedProduct.setField("orderedQuantity", reserveMissingQuantity.min(BigDecimal.ZERO).abs());
orderedProduct.setField(OrderedProductFields.CONVERSION, conversion);
orderedProduct.setField(OrderedProductFields.ADDITIONAL_QUANTITY, orderedQuantity.multiply(conversion, numberService.getMathContext()));
orderedProductDataDefinition.save(orderedProduct);
});
}
if (parameter.getBooleanField(L_REQUIRE_SUPPLIER_IDENTIFICATION) && Objects.isNull(delivery.getBelongsToField(SUPPLIER))) {
state.addMessage("orderSupplies.materialRequirementCoverage.deliveries.requireSupplierIdentification", MessageType.INFO, false);
}
}
if (StringUtils.isNoneEmpty(deliveryNumbers.toString())) {
state.addMessage("orderSupplies.materialRequirementCoverage.deliveries.created", MessageType.SUCCESS, false, deliveryNumbers.toString());
} else {
state.addMessage("orderSupplies.materialRequirementCoverage.deliveries.notCreated", MessageType.INFO, false);
}
}
Aggregations