Search in sources :

Example 16 with PossibleUnitConversions

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

the class ProductBySizeGroupHooks method checkIfUnitOrConverterIsCompatible.

private boolean checkIfUnitOrConverterIsCompatible(final DataDefinition productBySizeGroupDD, final Entity productBySizeGroup) {
    Entity opic = productBySizeGroup.getBelongsToField(ProductBySizeGroupFields.OPERATION_PRODUCT_IN_COMPONENT);
    if (!opic.getBooleanField(OperationProductInComponentFields.VARIOUS_QUANTITIES_IN_PRODUCTS_BY_SIZE)) {
        String opicUnit = opic.getStringField(OperationProductInComponentFields.GIVEN_UNIT);
        String productBySizeGroupUnit = productBySizeGroup.getStringField(ProductBySizeGroupFields.GIVEN_UNIT);
        if (opicUnit.equals(productBySizeGroupUnit)) {
            return true;
        }
        PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(productBySizeGroupUnit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, productBySizeGroup.getBelongsToField(ProductBySizeGroupFields.PRODUCT))));
        if (unitConversions.isDefinedFor(opicUnit)) {
            return true;
        } else {
            productBySizeGroup.addGlobalError("technologies.productBySizeGroup.error.wrongUnitOrConversion");
            return false;
        }
    }
    return true;
}
Also used : Entity(com.qcadoo.model.api.Entity) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions)

Example 17 with PossibleUnitConversions

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

the class OPICDetailsListeners method calculateQuantity.

private void calculateQuantity(final ViewDefinitionState view, final Entity operationProductInComponent) {
    Entity technologyInputProductType = operationProductInComponent.getBelongsToField(OperationProductInComponentFields.TECHNOLOGY_INPUT_PRODUCT_TYPE);
    Entity product = operationProductInComponent.getBelongsToField(OperationProductInComponentFields.PRODUCT);
    String givenUnit = operationProductInComponent.getStringField(OperationProductInComponentFields.GIVEN_UNIT);
    FieldComponent unitField = (FieldComponent) view.getComponentByReference(OperationProductInComponentFields.UNIT);
    FieldComponent givenQuantityField = (FieldComponent) view.getComponentByReference(OperationProductInComponentFields.GIVEN_QUANTITY);
    if ((Objects.isNull(technologyInputProductType) && Objects.isNull(product)) || Objects.isNull(givenUnit) || givenUnit.isEmpty() || Objects.isNull(givenQuantityField.getFieldValue())) {
        return;
    }
    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)) {
                    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);
                    }
                }
            } else {
                operationProductInComponent.setField(OperationProductInComponentFields.QUANTITY, givenQuantity);
                operationProductInComponent.setField(OperationProductInComponentFields.UNIT, givenUnit);
                unitField.setFieldValue(givenUnit);
                unitField.requestComponentUpdateState();
            }
        } else {
            operationProductInComponent.setField(OperationProductInComponentFields.QUANTITY, null);
        }
    } else {
        operationProductInComponent.setField(OperationProductInComponentFields.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 18 with PossibleUnitConversions

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

the class RemoveTOCService method sumQuantities.

private void sumQuantities(Entity originalInProduct, Entity productComponentToAdd) {
    BigDecimal originalQuantity = originalInProduct.getDecimalField(OperationProductInComponentFields.QUANTITY);
    BigDecimal quantityToAdd = productComponentToAdd.getDecimalField(OperationProductInComponentFields.QUANTITY);
    String givenUnit = originalInProduct.getStringField(OperationProductInComponentFields.GIVEN_UNIT);
    Entity product = originalInProduct.getBelongsToField(OperationProductInComponentFields.PRODUCT);
    String baseUnit = product.getStringField(ProductFields.UNIT);
    BigDecimal quantity = originalQuantity.add(quantityToAdd);
    originalInProduct.setField(OperationProductInComponentFields.QUANTITY, quantity);
    if (baseUnit.equals(givenUnit)) {
        originalInProduct.setField(OperationProductInComponentFields.GIVEN_QUANTITY, quantity);
    } else {
        PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(baseUnit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, product)));
        if (unitConversions.isDefinedFor(givenUnit)) {
            BigDecimal convertedQuantity = unitConversions.convertTo(quantity, givenUnit);
            originalInProduct.setField(OperationProductInComponentFields.GIVEN_QUANTITY, convertedQuantity);
        } else {
            originalInProduct.addError(originalInProduct.getDataDefinition().getField(OperationProductInComponentFields.GIVEN_QUANTITY), "technologies.operationProductInComponent.validate.error.missingUnitConversion");
            originalInProduct.setField(OperationProductInComponentFields.GIVEN_QUANTITY, null);
        }
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) BigDecimal(java.math.BigDecimal)

Example 19 with PossibleUnitConversions

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

the class ProductService method convertQuantityFromProductUnit.

public BigDecimal convertQuantityFromProductUnit(final Entity product, final BigDecimal quantity, final String targetUnit) {
    String unit = product.getStringField(ProductFields.UNIT);
    PossibleUnitConversions possibleUnitConversions = findPossibleUnitConversions(targetUnit, product);
    return possibleUnitConversions.convertTo(quantity, unit);
}
Also used : PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions)

Example 20 with PossibleUnitConversions

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

the class ProductService method findPossibleUnitConversions.

private PossibleUnitConversions findPossibleUnitConversions(final String unitName, final Entity productEntity) {
    CustomRestriction belongsToProductRestriction = new CustomRestriction() {

        @Override
        public void addRestriction(final SearchCriteriaBuilder scb) {
            scb.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, productEntity));
        }
    };
    PossibleUnitConversions possibleUnitConversions = unitConversionService.getPossibleConversions(unitName, belongsToProductRestriction);
    if (Objects.isNull(possibleUnitConversions)) {
        possibleUnitConversions = unitConversionService.getPossibleConversions(unitName);
    }
    return possibleUnitConversions;
}
Also used : SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) CustomRestriction(com.qcadoo.model.api.search.CustomRestriction)

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