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