Search in sources :

Example 31 with PossibleUnitConversions

use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.

the class DeliveryDetailsListeners method getConversion.

public BigDecimal getConversion(final Entity product) {
    String unit = product.getStringField(ProductFields.UNIT);
    String additionalUnit = product.getStringField(ProductFields.ADDITIONAL_UNIT);
    if (Objects.isNull(additionalUnit)) {
        return BigDecimal.ONE;
    }
    PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(unit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, product)));
    if (unitConversions.isDefinedFor(additionalUnit)) {
        return unitConversions.asUnitToConversionMap().get(additionalUnit);
    } else {
        return BigDecimal.ZERO;
    }
}
Also used : PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions)

Example 32 with PossibleUnitConversions

use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.

the class OPICDetailsListeners method calculateQuantityFormula.

public void calculateQuantityFormula(final ViewDefinitionState view, final ComponentState state, final String[] args) {
    FormComponent operationProductInComponentForm = (FormComponent) view.getComponentByReference(QcadooViewConstants.L_FORM);
    Entity operationProductInComponent = operationProductInComponentForm.getPersistedEntityWithIncludedFormValues();
    String formula = operationProductInComponent.getStringField(OperationProductInComponentFields.QUANTITY_FORMULA);
    formula = formula.replace(",", ".");
    if (StringUtils.isNotBlank(formula)) {
        try {
            Expression expression = new ExpressionBuilder(formula).build();
            ValidationResult result = expression.validate();
            if (result.isValid()) {
                BigDecimal value = new BigDecimal(expression.evaluate());
                value = numberService.setScaleWithDefaultMathContext(value);
                operationProductInComponent.setField(OperationProductInComponentFields.GIVEN_QUANTITY, value);
                Entity product = operationProductInComponent.getBelongsToField(OperationProductInComponentFields.PRODUCT);
                String givenUnit = operationProductInComponent.getStringField(OperationProductInComponentFields.GIVEN_UNIT);
                BigDecimal givenQuantity = value;
                String baseUnit = product.getStringField(ProductFields.UNIT);
                if (baseUnit.equals(givenUnit)) {
                    operationProductInComponent.setField(OperationProductInComponentFields.QUANTITY, givenQuantity);
                } else {
                    PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(givenUnit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, product)));
                    if (unitConversions.isDefinedFor(baseUnit)) {
                        BigDecimal convertedQuantity = unitConversions.convertTo(givenQuantity, baseUnit);
                        operationProductInComponent.setField(OperationProductInComponentFields.QUANTITY, convertedQuantity);
                    } else {
                        operationProductInComponent.addError(operationProductInComponent.getDataDefinition().getField(OperationProductInComponentFields.GIVEN_QUANTITY), "technologies.operationProductInComponent.validate.error.missingUnitConversion");
                        operationProductInComponent.setField(OperationProductInComponentFields.QUANTITY, null);
                    }
                }
                operationProductInComponentForm.setEntity(operationProductInComponent);
            } else {
                operationProductInComponent.addError(operationProductInComponent.getDataDefinition().getField(OperationProductInComponentFields.QUANTITY_FORMULA), "technologies.operationProductInComponent.validate.error.badFormula");
            }
        } catch (Exception ex) {
            operationProductInComponent.addError(operationProductInComponent.getDataDefinition().getField(OperationProductInComponentFields.QUANTITY_FORMULA), "technologies.operationProductInComponent.validate.error.badFormula");
        } finally {
            operationProductInComponentForm.setEntity(operationProductInComponent);
        }
    }
}
Also used : FormComponent(com.qcadoo.view.api.components.FormComponent) Entity(com.qcadoo.model.api.Entity) Expression(net.objecthunter.exp4j.Expression) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) ExpressionBuilder(net.objecthunter.exp4j.ExpressionBuilder) ValidationResult(net.objecthunter.exp4j.ValidationResult) BigDecimal(java.math.BigDecimal)

Example 33 with PossibleUnitConversions

use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.

the class ProductBySizeGroupDetailsListeners method onProductChange.

public void onProductChange(final ViewDefinitionState view, final ComponentState state, final String[] args) {
    LookupComponent productLookup = (LookupComponent) view.getComponentByReference(OperationProductInComponentFields.PRODUCT);
    Entity product = productLookup.getEntity();
    if (Objects.nonNull(product)) {
        FormComponent form = (FormComponent) view.getComponentByReference(QcadooViewConstants.L_FORM);
        Entity productBySizeGroup = form.getPersistedEntityWithIncludedFormValues();
        Entity opic = productBySizeGroup.getBelongsToField(ProductBySizeGroupFields.OPERATION_PRODUCT_IN_COMPONENT);
        FieldComponent givenUnitField = (FieldComponent) view.getComponentByReference(ProductBySizeGroupFields.GIVEN_UNIT);
        FieldComponent unitField = (FieldComponent) view.getComponentByReference(ProductBySizeGroupFields.UNIT);
        unitField.setFieldValue(product.getStringField(ProductFields.UNIT));
        if (!opic.getBooleanField(OperationProductInComponentFields.VARIOUS_QUANTITIES_IN_PRODUCTS_BY_SIZE)) {
            String opicUnit = opic.getStringField(OperationProductInComponentFields.GIVEN_UNIT);
            String productBySizeGroupUnit = productBySizeGroup.getBelongsToField(ProductBySizeGroupFields.PRODUCT).getStringField(ProductFields.UNIT);
            FieldComponent givenQuantityField = (FieldComponent) view.getComponentByReference(ProductBySizeGroupFields.GIVEN_QUANTITY);
            if (opicUnit.equals(productBySizeGroupUnit)) {
                givenUnitField.setFieldValue(productBySizeGroupUnit);
                givenQuantityField.setFieldValue(BigDecimalUtils.toString(opic.getDecimalField(OperationProductInComponentFields.GIVEN_QUANTITY), 5));
                calculateQuantity(view, state, args);
            } else {
                PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(productBySizeGroupUnit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, productBySizeGroup.getBelongsToField(ProductBySizeGroupFields.PRODUCT))));
                if (unitConversions.isDefinedFor(opicUnit)) {
                    givenUnitField.setFieldValue(opicUnit);
                    givenQuantityField.setFieldValue(opic.getDecimalField(OperationProductInComponentFields.GIVEN_QUANTITY));
                    calculateQuantity(view, state, args);
                } else {
                    productBySizeGroup.addError(productBySizeGroup.getDataDefinition().getField(ProductBySizeGroupFields.GIVEN_QUANTITY), "technologies.operationProductInComponent.validate.error.missingUnitConversion");
                    productBySizeGroup.setField(ProductBySizeGroupFields.QUANTITY, null);
                    form.setEntity(productBySizeGroup);
                }
            }
        } else {
            calculateQuantity(view, state, args);
        }
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) FormComponent(com.qcadoo.view.api.components.FormComponent) LookupComponent(com.qcadoo.view.api.components.LookupComponent) FieldComponent(com.qcadoo.view.api.components.FieldComponent) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions)

Example 34 with PossibleUnitConversions

use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.

the class ProductBySizeGroupDetailsListeners method calculateQuantity.

private void calculateQuantity(final ViewDefinitionState view, final Entity productBySizeGroup) {
    Entity product = productBySizeGroup.getBelongsToField(ProductBySizeGroupFields.PRODUCT);
    String givenUnit = productBySizeGroup.getStringField(ProductBySizeGroupFields.GIVEN_UNIT);
    FieldComponent unitField = (FieldComponent) view.getComponentByReference(ProductBySizeGroupFields.UNIT);
    FieldComponent givenQuantityField = (FieldComponent) view.getComponentByReference(ProductBySizeGroupFields.GIVEN_QUANTITY);
    Either<Exception, Optional<BigDecimal>> maybeQuantity = BigDecimalUtils.tryParse((String) givenQuantityField.getFieldValue(), view.getLocale());
    if (maybeQuantity.isRight()) {
        if (maybeQuantity.getRight().isPresent()) {
            BigDecimal givenQuantity = maybeQuantity.getRight().get();
            if (Objects.nonNull(product)) {
                String baseUnit = product.getStringField(ProductFields.UNIT);
                if (baseUnit.equals(givenUnit)) {
                    productBySizeGroup.setField(ProductBySizeGroupFields.QUANTITY, givenQuantity);
                } else {
                    PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(givenUnit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, product)));
                    if (unitConversions.isDefinedFor(baseUnit)) {
                        BigDecimal convertedQuantity = unitConversions.convertTo(givenQuantity, baseUnit);
                        productBySizeGroup.setField(ProductBySizeGroupFields.QUANTITY, convertedQuantity);
                    } else {
                        productBySizeGroup.addError(productBySizeGroup.getDataDefinition().getField(ProductBySizeGroupFields.GIVEN_QUANTITY), "technologies.operationProductInComponent.validate.error.missingUnitConversion");
                        productBySizeGroup.setField(ProductBySizeGroupFields.QUANTITY, null);
                    }
                }
            } else {
                productBySizeGroup.setField(ProductBySizeGroupFields.QUANTITY, givenQuantity);
                productBySizeGroup.setField(ProductBySizeGroupFields.UNIT, givenUnit);
                unitField.setFieldValue(givenUnit);
                unitField.requestComponentUpdateState();
            }
        } else {
            productBySizeGroup.setField(ProductBySizeGroupFields.QUANTITY, null);
        }
    } else {
        productBySizeGroup.setField(ProductBySizeGroupFields.QUANTITY, null);
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) Optional(com.google.common.base.Optional) FieldComponent(com.qcadoo.view.api.components.FieldComponent) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) BigDecimal(java.math.BigDecimal)

Example 35 with PossibleUnitConversions

use of com.qcadoo.model.api.units.PossibleUnitConversions in project qcadoo by qcadoo.

the class UnitConversionServiceImplTest method shouldReturnEmptyConversionsSet.

@Test
public final void shouldReturnEmptyConversionsSet() {
    // given
    stubGetAll(Collections.<Entity>emptyList());
    // when
    final PossibleUnitConversions result = unitConversionService.getPossibleConversions("m");
    // then
    assertTrue(result.isEmpty());
}
Also used : PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) Test(org.junit.Test)

Aggregations

PossibleUnitConversions (com.qcadoo.model.api.units.PossibleUnitConversions)35 Entity (com.qcadoo.model.api.Entity)26 BigDecimal (java.math.BigDecimal)21 FieldComponent (com.qcadoo.view.api.components.FieldComponent)5 Test (org.junit.Test)5 Optional (com.google.common.base.Optional)4 DocumentBuilder (com.qcadoo.mes.materialFlowResources.service.DocumentBuilder)4 Ignore (org.junit.Ignore)4 FormComponent (com.qcadoo.view.api.components.FormComponent)3 DataDefinition (com.qcadoo.model.api.DataDefinition)2 ErrorMessage (com.qcadoo.model.api.validators.ErrorMessage)2 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 TranslationService (com.qcadoo.localization.api.TranslationService)1 CalculationQuantityService (com.qcadoo.mes.basic.CalculationQuantityService)1 ProductFields (com.qcadoo.mes.basic.constants.ProductFields)1 UnitConversionItemFieldsB (com.qcadoo.mes.basic.constants.UnitConversionItemFieldsB)1 LocationFields (com.qcadoo.mes.materialFlow.constants.LocationFields)1 DocumentFields (com.qcadoo.mes.materialFlowResources.constants.DocumentFields)1