use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.
the class EventDocumentsService method createDocumentForLocation.
private Entity createDocumentForLocation(final Entity event, final EventType eventType, final Entity warehouse, final Collection<Entity> machinePartsForLocation) {
DocumentBuilder documentBuilder = documentManagementService.getDocumentBuilder().internalOutbound(warehouse);
DataDefinition positionDD = dataDefinitionService.get(MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER, MaterialFlowResourcesConstants.MODEL_POSITION);
for (Entity machinePartForLocation : machinePartsForLocation) {
Entity position = positionDD.create();
Entity product = machinePartForLocation.getBelongsToField(MachinePartForEventFields.MACHINE_PART);
BigDecimal quantity = machinePartForLocation.getDecimalField(MachinePartForEventFields.PLANNED_QUANTITY);
String additionalUnit = product.getStringField(ProductFields.ADDITIONAL_UNIT);
String unit = product.getStringField(ProductFields.UNIT);
BigDecimal conversion = BigDecimal.ONE;
if (!StringUtils.isEmpty(additionalUnit)) {
PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(unit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, product)));
if (unitConversions.isDefinedFor(additionalUnit)) {
BigDecimal convertedQuantity = unitConversions.convertTo(quantity, additionalUnit);
conversion = convertedQuantity.divide(quantity, numberService.getMathContext());
position.setField(PositionFields.GIVEN_QUANTITY, convertedQuantity);
position.setField(PositionFields.GIVEN_UNIT, additionalUnit);
}
} else {
position.setField(PositionFields.GIVEN_UNIT, unit);
position.setField(PositionFields.GIVEN_QUANTITY, quantity);
}
position.setField(PositionFields.PRODUCT, product);
position.setField(PositionFields.CONVERSION, conversion);
position.setField(PositionFields.QUANTITY, quantity);
documentBuilder.addPosition(position);
}
if (eventType.getModelName().equals(CmmsMachinePartsConstants.MODEL_MAINTENANCE_EVENT)) {
documentBuilder.setField(DocumentFieldsCMP.MAINTENANCE_EVENT, event);
} else if (eventType.getModelName().equals(CmmsMachinePartsConstants.MODEL_PLANNED_EVENT)) {
documentBuilder.setField(DocumentFieldsCMP.PLANNED_EVENT, event);
} else {
throw new IllegalArgumentException(String.format("Unsupported model type: '%s'", eventType.getModelName()));
}
return documentBuilder.setAccepted().build();
}
use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.
the class DeliveriesMinStateHelper 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 SalesPlanMaterialRequirementDetailsListeners method getConversion.
private 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 ProductService method conversionForProductUnit.
public void conversionForProductUnit(final Entity product) {
String unit = product.getStringField(ProductFields.UNIT);
List<Entity> conversionItems = product.getHasManyField(ProductFields.CONVERSION_ITEMS);
if (StringUtils.isNotEmpty(unit) && conversionItems.isEmpty()) {
PossibleUnitConversions conversions = unitConversionService.getPossibleConversions(unit);
product.setField(ProductFields.CONVERSION_ITEMS, conversions.asEntities(UnitConversionItemFieldsB.PRODUCT, product));
}
}
use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.
the class GenerateMaterialRequirementCoverageListeners method getConversion.
private BigDecimal getConversion(Integer productId) {
Entity product = dataDefinitionService.get(BasicConstants.PLUGIN_IDENTIFIER, BasicConstants.MODEL_PRODUCT).get(productId.longValue());
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;
}
}
Aggregations