use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.
the class WarehouseIssueDocumentsService method buildDocumentPositions.
private void buildDocumentPositions(final Collection positions, final DocumentBuilder documentBuilder) {
for (Object obj : positions) {
Entity issue = (Entity) obj;
Entity product = issue.getBelongsToField(IssueFields.PRODUCT);
BigDecimal quantity = issue.getDecimalField(IssueFields.ISSUE_QUANTITY);
BigDecimal conversion = BigDecimal.ONE;
Entity position = dataDefinitionService.get(MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER, MaterialFlowResourcesConstants.MODEL_POSITION).create();
String additionalUnit = product.getStringField(ProductFields.ADDITIONAL_UNIT);
String unit = product.getStringField(ProductFields.UNIT);
if (issue.getDecimalField(IssueFields.CONVERSION) != null) {
conversion = issue.getDecimalField(IssueFields.CONVERSION);
position.setField(PositionFields.CONVERSION, conversion);
if (StringUtils.isEmpty(additionalUnit)) {
position.setField(PositionFields.GIVEN_UNIT, unit);
position.setField(PositionFields.GIVEN_QUANTITY, quantity);
} else {
position.setField(PositionFields.GIVEN_UNIT, additionalUnit);
BigDecimal newAdditionalQuantity = calculationQuantityService.calculateAdditionalQuantity(quantity, conversion, additionalUnit);
position.setField(PositionFields.GIVEN_QUANTITY, newAdditionalQuantity);
}
} else 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);
position.setField(PositionFields.GIVEN_QUANTITY, convertedQuantity);
position.setField(PositionFields.GIVEN_UNIT, additionalUnit);
position.setField(PositionFields.CONVERSION, numberService.setScaleWithDefaultMathContext(getConversion(product, unit, additionalUnit)));
}
} else {
position.setField(PositionFields.GIVEN_UNIT, unit);
position.setField(PositionFields.GIVEN_QUANTITY, quantity);
position.setField(PositionFields.CONVERSION, conversion);
}
position.setField(PositionFields.QUANTITY, quantity);
position.setField(PositionFields.PRODUCT, product);
position.setField(PositionFields.ADDITIONAL_CODE, issue.getBelongsToField(IssueFields.ADDITIONAL_CODE));
if (documentBuilder.getDocumentType().getStringValue().equals(DocumentType.INTERNAL_INBOUND.getStringValue()) || documentBuilder.getDocumentType().getStringValue().equals(DocumentType.RECEIPT.getStringValue())) {
position.setField(PositionFields.STORAGE_LOCATION, issue.getBelongsToField(IssueFields.STORAGE_LOCATION));
}
position.setField(PositionFields.DOCUMENT, documentBuilder.getDocument());
documentBuilder.addPosition(position);
}
}
use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.
the class OrderStatesListenerServicePFTD method createTransferDocumentsForUnusedMaterials.
private Either<String, Void> createTransferDocumentsForUnusedMaterials(final Entity locationFrom, final Entity locationTo, final Map<Entity, BigDecimal> products, final Entity order) {
DocumentBuilder document = documentManagementService.getDocumentBuilder().transfer(locationFrom, locationTo);
if (products.isEmpty()) {
return Either.right(null);
}
for (Map.Entry<Entity, BigDecimal> entry : products.entrySet()) {
Entity product = entry.getKey();
BigDecimal quantity = entry.getValue();
if (quantity.compareTo(BigDecimal.ZERO) > 0) {
BigDecimal givenQuantity = quantity;
String givenUnit = product.getStringField(ProductFields.ADDITIONAL_UNIT);
BigDecimal conversion = BigDecimal.ONE;
if (!StringUtils.isEmpty(givenUnit)) {
PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(product.getStringField(ProductFields.UNIT), searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, product)));
if (unitConversions.isDefinedFor(givenUnit)) {
givenQuantity = unitConversions.convertTo(quantity, givenUnit);
conversion = unitConversions.asUnitToConversionMap().get(givenUnit);
}
} else {
givenUnit = product.getStringField(ProductFields.UNIT);
}
Entity position = document.createPosition(product, quantity, givenQuantity, givenUnit, conversion, null, null, null, null, null);
document.addPosition(position);
}
}
Entity acceptedDocument = acceptTransferDocument(document, order);
if (!acceptedDocument.isValid()) {
for (ErrorMessage error : acceptedDocument.getGlobalErrors()) {
order.addGlobalError(error.getMessage(), error.getVars());
}
order.addGlobalError(L_ACCEPT_INBOUND_DOCUMENT_ERROR);
return Either.left(L_ACCEPT_INBOUND_DOCUMENT_ERROR);
}
return Either.right(null);
}
use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.
the class ProductionTrackingListenerServicePFTD method preparePositionForUsedBatch.
private Entity preparePositionForUsedBatch(final DataDefinition positionDD, final Entity trackingOperationProductInComponent, final Entity product, final Entity usedBatch) {
Entity position = positionDD.create();
BigDecimal usedQuantity = usedBatch.getDecimalField(UsedBatchFields.QUANTITY);
BigDecimal givenQuantity = productionTrackingService.calculateGivenQuantity(trackingOperationProductInComponent, usedQuantity).orElse(usedQuantity);
BigDecimal conversion = BigDecimal.ONE;
String unit = product.getStringField(ProductFields.UNIT);
String givenUnit = trackingOperationProductInComponent.getStringField(TrackingOperationProductOutComponentFields.GIVEN_UNIT);
if (Objects.nonNull(usedQuantity)) {
PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(unit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, product)));
if (unitConversions.isDefinedFor(givenUnit)) {
conversion = numberService.setScaleWithDefaultMathContext(unitConversions.asUnitToConversionMap().get(givenUnit));
}
}
position.setField(PositionFields.GIVEN_UNIT, trackingOperationProductInComponent.getStringField(TrackingOperationProductInComponentFields.GIVEN_UNIT));
position.setField(PositionFields.PRODUCT, product);
position.setField(PositionFields.QUANTITY, usedQuantity);
position.setField(PositionFields.GIVEN_QUANTITY, givenQuantity);
position.setField(PositionFields.CONVERSION, conversion);
position.setField(PositionFields.BATCH, usedBatch.getBelongsToField(UsedBatchFields.BATCH).getId());
return position;
}
use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.
the class ProductionTrackingListenerServicePFTD method createInternalInboundDocumentForFinalProducts.
private Entity createInternalInboundDocumentForFinalProducts(final Entity locationTo, final Entity order, final Collection<Entity> trackingOperationProductOutComponents, final boolean isBasedOnNominalCost, Entity user) {
DocumentBuilder internalInboundBuilder = documentManagementService.getDocumentBuilder(user);
internalInboundBuilder.internalInbound(locationTo);
boolean isFinalProduct = false;
Entity productionTracking = null;
for (Entity trackingOperationProductOutComponent : trackingOperationProductOutComponents) {
Entity outProduct = trackingOperationProductOutComponent.getBelongsToField(TrackingOperationProductOutComponentFields.PRODUCT);
if (Objects.isNull(productionTracking)) {
productionTracking = trackingOperationProductOutComponent.getBelongsToField(TrackingOperationProductOutComponentFields.PRODUCTION_TRACKING);
}
if (isFinalProductForOrder(order, outProduct)) {
isFinalProduct = true;
}
Entity position = getPositionDD().create();
BigDecimal usedQuantity = trackingOperationProductOutComponent.getDecimalField(TrackingOperationProductOutComponentFields.USED_QUANTITY);
BigDecimal givenQuantity = trackingOperationProductOutComponent.getDecimalField(TrackingOperationProductOutComponentFields.GIVEN_QUANTITY);
BigDecimal conversion = BigDecimal.ONE;
String unit = outProduct.getStringField(ProductFields.UNIT);
String givenUnit = trackingOperationProductOutComponent.getStringField(TrackingOperationProductOutComponentFields.GIVEN_UNIT);
if (Objects.nonNull(usedQuantity) && Objects.nonNull(givenQuantity)) {
PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(unit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, outProduct)));
if (unitConversions.isDefinedFor(givenUnit)) {
conversion = numberService.setScaleWithDefaultMathContext(unitConversions.asUnitToConversionMap().get(givenUnit));
}
}
position.setField(PositionFields.PRODUCT, outProduct);
position.setField(PositionFields.QUANTITY, usedQuantity);
position.setField(PositionFields.CONVERSION, conversion);
position.setField(PositionFields.GIVEN_QUANTITY, givenQuantity);
position.setField(PositionFields.GIVEN_UNIT, trackingOperationProductOutComponent.getStringField(TrackingOperationProductOutComponentFields.GIVEN_UNIT));
if (isBasedOnNominalCost) {
position.setField(PositionFields.PRICE, getNominalCost(outProduct));
}
position.setField(PositionFields.PRODUCTION_DATE, new Date());
Entity batch = productionTracking.getBelongsToField(ProductionTrackingFields.BATCH);
if (Objects.nonNull(batch) && batch.getBelongsToField(BatchFields.PRODUCT).getId().equals(outProduct.getId())) {
position.setField(PositionFields.BATCH, productionTracking.getBelongsToField(ProductionTrackingFields.BATCH).getId());
position.setField(PositionFields.QUALITY_RATING, productionTracking.getStringField(L_QUALITY_RATING));
} else if (order.getBelongsToField(OrderFields.PRODUCT).getId().equals(outProduct.getId())) {
position.setField(PositionFields.QUALITY_RATING, productionTracking.getStringField(L_QUALITY_RATING));
}
if (Objects.nonNull(trackingOperationProductOutComponent.getBelongsToField(TrackingOperationProductOutComponentFields.STORAGE_LOCATION))) {
position.setField(PositionFields.STORAGE_LOCATION, trackingOperationProductOutComponent.getBelongsToField(TrackingOperationProductOutComponentFields.STORAGE_LOCATION).getId());
}
if (Objects.nonNull(trackingOperationProductOutComponent.getBelongsToField(TrackingOperationProductOutComponentFields.PALLET_NUMBER))) {
position.setField(PositionFields.PALLET_NUMBER, trackingOperationProductOutComponent.getBelongsToField(TrackingOperationProductOutComponentFields.PALLET_NUMBER).getId());
}
if (order.getBelongsToField(OrderFields.PRODUCT).getId().equals(outProduct.getId())) {
position.setField(PositionFields.EXPIRATION_DATE, productionTracking.getDateField(ProductionTrackingFields.EXPIRATION_DATE));
}
fillAttributes(trackingOperationProductOutComponent, position);
internalInboundBuilder.addPosition(position);
}
internalInboundBuilder.setField(DocumentFieldsPFTD.ORDER, order);
String receiptOfProducts = parameterService.getParameter().getStringField(ParameterFieldsPC.RECEIPT_OF_PRODUCTS);
if (ReceiptOfProducts.ON_ACCEPTANCE_REGISTRATION_RECORD.getStringValue().equals(receiptOfProducts) && (OrderState.COMPLETED.equals(OrderState.of(order)) || !isFinalProduct || isBasedOnNominalCost || orderClosingHelper.orderShouldBeClosedWithRecalculate(productionTracking)) || orderClosingHelper.orderShouldBeClosedWithRecalculate(productionTracking)) {
internalInboundBuilder.setAccepted();
}
return internalInboundBuilder.buildWithEntityRuntimeException();
}
use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.
the class ProductionTrackingListenerServicePFTD method preparePositionForInProduct.
private Entity preparePositionForInProduct(final DataDefinition positionDD, final Entity trackingOperationProductInComponent, final Entity product) {
Entity position = positionDD.create();
BigDecimal usedQuantity = trackingOperationProductInComponent.getDecimalField(TrackingOperationProductInComponentFields.USED_QUANTITY);
BigDecimal givenQuantity = trackingOperationProductInComponent.getDecimalField(TrackingOperationProductInComponentFields.GIVEN_QUANTITY);
BigDecimal conversion = BigDecimal.ONE;
String unit = product.getStringField(ProductFields.UNIT);
String givenUnit = trackingOperationProductInComponent.getStringField(TrackingOperationProductOutComponentFields.GIVEN_UNIT);
if (Objects.nonNull(usedQuantity) && Objects.nonNull(givenQuantity)) {
PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(unit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, product)));
if (unitConversions.isDefinedFor(givenUnit)) {
conversion = numberService.setScaleWithDefaultMathContext(unitConversions.asUnitToConversionMap().get(givenUnit));
}
}
position.setField(PositionFields.GIVEN_UNIT, trackingOperationProductInComponent.getStringField(TrackingOperationProductInComponentFields.GIVEN_UNIT));
position.setField(PositionFields.PRODUCT, product);
position.setField(PositionFields.QUANTITY, usedQuantity);
position.setField(PositionFields.GIVEN_QUANTITY, givenQuantity);
position.setField(PositionFields.CONVERSION, conversion);
return position;
}
Aggregations