Search in sources :

Example 1 with SUPPLIER

use of com.qcadoo.mes.deliveries.constants.DeliveryFields.SUPPLIER 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);
    }
}
Also used : LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) UnitConversionService(com.qcadoo.model.api.units.UnitConversionService) MaterialRequirementCoverageFields(com.qcadoo.mes.orderSupplies.constants.MaterialRequirementCoverageFields) StringUtils(org.apache.commons.lang3.StringUtils) ParameterFields(com.qcadoo.mes.basic.constants.ParameterFields) FileService(com.qcadoo.model.api.file.FileService) BigDecimal(java.math.BigDecimal) ReportService(com.qcadoo.report.api.ReportService) JSONException(org.json.JSONException) OrderSuppliesConstants(com.qcadoo.mes.orderSupplies.constants.OrderSuppliesConstants) JSONObject(org.json.JSONObject) Locale(java.util.Locale) BasicConstants(com.qcadoo.mes.basic.constants.BasicConstants) Map(java.util.Map) CoverageProductSelectedFields(com.qcadoo.mes.orderSupplies.constants.CoverageProductSelectedFields) SearchRestrictions(com.qcadoo.model.api.search.SearchRestrictions) DeliveriesConstants(com.qcadoo.mes.deliveries.constants.DeliveriesConstants) Collectors(java.util.stream.Collectors) DataDefinition(com.qcadoo.model.api.DataDefinition) Objects(java.util.Objects) List(java.util.List) Entity(com.qcadoo.model.api.Entity) CoverageAnalysisForOrderService(com.qcadoo.mes.orderSupplies.coverage.coverageAnalysis.CoverageAnalysisForOrderService) ParameterService(com.qcadoo.mes.basic.ParameterService) EXTERNAL_SYNCHRONIZED(com.qcadoo.mes.deliveries.constants.DeliveryFields.EXTERNAL_SYNCHRONIZED) CompanyFieldsD(com.qcadoo.mes.deliveries.constants.CompanyFieldsD) Optional(java.util.Optional) MessageType(com.qcadoo.view.api.ComponentState.MessageType) DataDefinitionService(com.qcadoo.model.api.DataDefinitionService) ComponentState(com.qcadoo.view.api.ComponentState) QcadooViewConstants(com.qcadoo.view.constants.QcadooViewConstants) SimpleDateFormat(java.text.SimpleDateFormat) NUMBER(com.qcadoo.mes.deliveries.constants.DeliveryFields.NUMBER) DateUtils(com.qcadoo.localization.api.utils.DateUtils) CURRENCY(com.qcadoo.mes.deliveries.constants.DeliveryFields.CURRENCY) DeliveriesService(com.qcadoo.mes.deliveries.DeliveriesService) ViewDefinitionState(com.qcadoo.view.api.ViewDefinitionState) Lists(com.google.common.collect.Lists) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) NumberGeneratorService(com.qcadoo.view.api.utils.NumberGeneratorService) OrderSuppliesService(com.qcadoo.mes.orderSupplies.OrderSuppliesService) Service(org.springframework.stereotype.Service) FormComponent(com.qcadoo.view.api.components.FormComponent) SecurityService(com.qcadoo.security.api.SecurityService) CoverageProductFields(com.qcadoo.mes.orderSupplies.constants.CoverageProductFields) MaterialRequirementCoverageReportPdfService(com.qcadoo.mes.orderSupplies.print.MaterialRequirementCoverageReportPdfService) IOException(java.io.IOException) UnitConversionItemFieldsB(com.qcadoo.mes.basic.constants.UnitConversionItemFieldsB) DocumentException(com.lowagie.text.DocumentException) OrderedProductFields(com.qcadoo.mes.deliveries.constants.OrderedProductFields) GridComponent(com.qcadoo.view.api.components.GridComponent) CoverageLocationFields(com.qcadoo.mes.orderSupplies.constants.CoverageLocationFields) MaterialRequirementCoverageService(com.qcadoo.mes.orderSupplies.coverage.MaterialRequirementCoverageService) NumberService(com.qcadoo.model.api.NumberService) SUPPLIER(com.qcadoo.mes.deliveries.constants.DeliveryFields.SUPPLIER) ProductFields(com.qcadoo.mes.basic.constants.ProductFields) PageSize(com.lowagie.text.PageSize) CoverageProductGeneratedFields(com.qcadoo.mes.orderSupplies.constants.CoverageProductGeneratedFields) Transactional(org.springframework.transaction.annotation.Transactional) Entity(com.qcadoo.model.api.Entity) Optional(java.util.Optional) GridComponent(com.qcadoo.view.api.components.GridComponent) DataDefinition(com.qcadoo.model.api.DataDefinition) BigDecimal(java.math.BigDecimal) List(java.util.List) Map(java.util.Map)

Aggregations

Lists (com.google.common.collect.Lists)1 DocumentException (com.lowagie.text.DocumentException)1 PageSize (com.lowagie.text.PageSize)1 DateUtils (com.qcadoo.localization.api.utils.DateUtils)1 ParameterService (com.qcadoo.mes.basic.ParameterService)1 BasicConstants (com.qcadoo.mes.basic.constants.BasicConstants)1 ParameterFields (com.qcadoo.mes.basic.constants.ParameterFields)1 ProductFields (com.qcadoo.mes.basic.constants.ProductFields)1 UnitConversionItemFieldsB (com.qcadoo.mes.basic.constants.UnitConversionItemFieldsB)1 DeliveriesService (com.qcadoo.mes.deliveries.DeliveriesService)1 CompanyFieldsD (com.qcadoo.mes.deliveries.constants.CompanyFieldsD)1 DeliveriesConstants (com.qcadoo.mes.deliveries.constants.DeliveriesConstants)1 CURRENCY (com.qcadoo.mes.deliveries.constants.DeliveryFields.CURRENCY)1 EXTERNAL_SYNCHRONIZED (com.qcadoo.mes.deliveries.constants.DeliveryFields.EXTERNAL_SYNCHRONIZED)1 NUMBER (com.qcadoo.mes.deliveries.constants.DeliveryFields.NUMBER)1 SUPPLIER (com.qcadoo.mes.deliveries.constants.DeliveryFields.SUPPLIER)1 OrderedProductFields (com.qcadoo.mes.deliveries.constants.OrderedProductFields)1 OrderSuppliesService (com.qcadoo.mes.orderSupplies.OrderSuppliesService)1 CoverageLocationFields (com.qcadoo.mes.orderSupplies.constants.CoverageLocationFields)1 CoverageProductFields (com.qcadoo.mes.orderSupplies.constants.CoverageProductFields)1