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;
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
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());
}
Aggregations