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