Search in sources :

Example 11 with PossibleUnitConversions

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

the class TrackingOperationProductComponentDetailsListeners method calculateQuantityToGiven.

public void calculateQuantityToGiven(final ViewDefinitionState view, final ComponentState componentState, final String[] args) {
    FormComponent form = (FormComponent) view.getComponentByReference(QcadooViewConstants.L_FORM);
    Entity productComponent = form.getPersistedEntityWithIncludedFormValues();
    Entity product = productComponent.getBelongsToField(TrackingOperationProductInComponentFields.PRODUCT);
    if (product == null) {
        return;
    }
    String unit = product.getStringField(ProductFields.UNIT);
    FieldComponent quantityField = (FieldComponent) view.getComponentByReference(TrackingOperationProductInComponentFields.USED_QUANTITY);
    if (StringUtils.isEmpty(unit) || quantityField.getFieldValue() == null) {
        return;
    }
    Either<Exception, Optional<BigDecimal>> maybeQuantity = BigDecimalUtils.tryParse((String) quantityField.getFieldValue(), view.getLocale());
    if (maybeQuantity.isRight()) {
        if (maybeQuantity.getRight().isPresent()) {
            BigDecimal quantity = maybeQuantity.getRight().get();
            String givenUnit = productComponent.getStringField(TrackingOperationProductInComponentFields.GIVEN_UNIT);
            if (givenUnit.equals(unit)) {
                productComponent.setField(TrackingOperationProductInComponentFields.GIVEN_QUANTITY, quantity);
            } else {
                PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(unit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, product)));
                if (unitConversions.isDefinedFor(givenUnit)) {
                    BigDecimal convertedQuantity = unitConversions.convertTo(quantity, givenUnit, BigDecimal.ROUND_FLOOR);
                    productComponent.setField(TrackingOperationProductInComponentFields.GIVEN_QUANTITY, convertedQuantity);
                } else {
                    productComponent.addError(productComponent.getDataDefinition().getField(TrackingOperationProductInComponentFields.USED_QUANTITY), "technologies.operationProductInComponent.validate.error.missingUnitConversion");
                    productComponent.setField(TrackingOperationProductInComponentFields.GIVEN_QUANTITY, null);
                }
            }
        } else {
            productComponent.setField(TrackingOperationProductInComponentFields.GIVEN_QUANTITY, null);
        }
    } else {
        productComponent.setField(TrackingOperationProductInComponentFields.GIVEN_QUANTITY, null);
    }
    form.setEntity(productComponent);
}
Also used : FormComponent(com.qcadoo.view.api.components.FormComponent) 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 12 with PossibleUnitConversions

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

the class ProductionTrackingDetailsListeners method copyPlannedQuantityToUsedQuantity.

private void copyPlannedQuantityToUsedQuantity(final List<Entity> trackingOperationProductComponents) {
    for (Entity trackingOperationProductComponent : trackingOperationProductComponents) {
        Entity product = trackingOperationProductComponent.getBelongsToField(TrackingOperationProductInComponentFields.PRODUCT);
        Entity recordOperationProductComponentDto;
        if (ProductionCountingConstants.MODEL_TRACKING_OPERATION_PRODUCT_IN_COMPONENT.equals(trackingOperationProductComponent.getDataDefinition().getName())) {
            recordOperationProductComponentDto = dataDefinitionService.get(ProductionCountingConstants.PLUGIN_IDENTIFIER, ProductionCountingConstants.MODEL_TRACKING_OPERATION_PRODUCT_IN_COMPONENT_DTO).get(trackingOperationProductComponent.getId());
        } else {
            recordOperationProductComponentDto = dataDefinitionService.get(ProductionCountingConstants.PLUGIN_IDENTIFIER, ProductionCountingConstants.MODEL_TRACKING_OPERATION_PRODUCT_OUT_COMPONENT_DTO).get(trackingOperationProductComponent.getId());
        }
        BigDecimal plannedQuantity = BigDecimalUtils.convertNullToZero(recordOperationProductComponentDto.getDecimalField(TrackingOperationProductInComponentDtoFields.PLANNED_QUANTITY));
        trackingOperationProductComponent.setField(TrackingOperationProductInComponentFields.USED_QUANTITY, numberService.setScaleWithDefaultMathContext(plannedQuantity));
        String additionalUnit = product.getStringField(ProductFields.ADDITIONAL_UNIT);
        String baseUnit = product.getStringField(ProductFields.UNIT);
        if (StringUtils.isEmpty(additionalUnit)) {
            trackingOperationProductComponent.setField(TrackingOperationProductInComponentFields.GIVEN_QUANTITY, numberService.setScaleWithDefaultMathContext(plannedQuantity));
            trackingOperationProductComponent.setField(TrackingOperationProductInComponentFields.GIVEN_UNIT, baseUnit);
        } else {
            PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(baseUnit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, product)));
            if (unitConversions.isDefinedFor(additionalUnit)) {
                BigDecimal convertedQuantity = unitConversions.convertTo(plannedQuantity, additionalUnit);
                trackingOperationProductComponent.setField(TrackingOperationProductInComponentFields.GIVEN_QUANTITY, convertedQuantity);
            } else {
                trackingOperationProductComponent.setField(TrackingOperationProductInComponentFields.GIVEN_QUANTITY, numberService.setScaleWithDefaultMathContext(plannedQuantity));
            }
            trackingOperationProductComponent.setField(TrackingOperationProductInComponentFields.GIVEN_UNIT, additionalUnit);
        }
        trackingOperationProductComponent.getDataDefinition().save(trackingOperationProductComponent);
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) BigDecimal(java.math.BigDecimal)

Example 13 with PossibleUnitConversions

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

the class TrackingOperationProductComponentHooks method validatesWith.

public boolean validatesWith(final DataDefinition dataDefinition, final Entity trackingOperationProduct) {
    String givenUnit = trackingOperationProduct.getStringField(TrackingOperationProductInComponentFields.GIVEN_UNIT);
    if (givenUnit == null) {
        return true;
    }
    Entity product = trackingOperationProduct.getBelongsToField(TrackingOperationProductInComponentFields.PRODUCT);
    String unit = product.getStringField(ProductFields.UNIT);
    if (unit.equals(givenUnit)) {
        return true;
    }
    PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(unit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, product)));
    if (!unitConversions.isDefinedFor(givenUnit)) {
        trackingOperationProduct.addError(dataDefinition.getField(TrackingOperationProductInComponentFields.GIVEN_QUANTITY), "technologies.operationProductInComponent.validate.error.missingUnitConversion");
        return false;
    }
    return true;
}
Also used : Entity(com.qcadoo.model.api.Entity) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions)

Example 14 with PossibleUnitConversions

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

the class OfferDetailsListeners method getConversion.

private BigDecimal getConversion(Entity product) {
    String unit = product.getStringField(ProductFields.UNIT);
    String additionalUnit = product.getStringField(ProductFields.ADDITIONAL_UNIT);
    if (additionalUnit == null) {
        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 15 with PossibleUnitConversions

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

the class OperationProductInComponentHooks method calculateQuantityForPBSG.

private void calculateQuantityForPBSG(final Entity productBySizeGroup) {
    Entity product = productBySizeGroup.getBelongsToField(ProductBySizeGroupFields.PRODUCT);
    String givenUnit = productBySizeGroup.getStringField(ProductBySizeGroupFields.GIVEN_UNIT);
    BigDecimal givenQuantity = productBySizeGroup.getDecimalField(ProductBySizeGroupFields.GIVEN_QUANTITY);
    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);
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) BigDecimal(java.math.BigDecimal)

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