Search in sources :

Example 6 with PossibleUnitConversions

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);
    }
}
Also used : LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) DataDefinitionService(com.qcadoo.model.api.DataDefinitionService) UserService(com.qcadoo.security.api.UserService) ReservationsServiceForProductsToIssue(com.qcadoo.mes.productFlowThruDivision.reservation.ReservationsServiceForProductsToIssue) DocumentType(com.qcadoo.mes.materialFlowResources.constants.DocumentType) IssueFields(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.constans.IssueFields) Autowired(org.springframework.beans.factory.annotation.Autowired) OrderFields(com.qcadoo.mes.orders.constants.OrderFields) CreationDocumentResponse(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.CreationDocumentResponse) DocumentManagementService(com.qcadoo.mes.materialFlowResources.service.DocumentManagementService) UnitConversionService(com.qcadoo.model.api.units.UnitConversionService) DocumentsStatus(com.qcadoo.mes.productFlowThruDivision.constants.DocumentsStatus) StringUtils(org.apache.commons.lang3.StringUtils) BigDecimal(java.math.BigDecimal) Strings(com.google.common.base.Strings) MaterialFlowResourcesConstants(com.qcadoo.mes.materialFlowResources.constants.MaterialFlowResourcesConstants) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) Lists(com.google.common.collect.Lists) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) Service(org.springframework.stereotype.Service) WarehouseIssueFields(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.constans.WarehouseIssueFields) LocationFields(com.qcadoo.mes.materialFlow.constants.LocationFields) SecurityService(com.qcadoo.security.api.SecurityService) SearchRestrictions(com.qcadoo.model.api.search.SearchRestrictions) WarehouseIssueParameterService(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.WarehouseIssueParameterService) PositionFields(com.qcadoo.mes.materialFlowResources.constants.PositionFields) Collection(java.util.Collection) DocumentBuildException(com.qcadoo.mes.materialFlowResources.exceptions.DocumentBuildException) Set(java.util.Set) UnitConversionItemFieldsB(com.qcadoo.mes.basic.constants.UnitConversionItemFieldsB) TranslationService(com.qcadoo.localization.api.TranslationService) Sets(com.google.common.collect.Sets) DocumentBuilder(com.qcadoo.mes.materialFlowResources.service.DocumentBuilder) DocumentFields(com.qcadoo.mes.materialFlowResources.constants.DocumentFields) List(java.util.List) DrawnDocuments(com.qcadoo.mes.productFlowThruDivision.constants.DrawnDocuments) Entity(com.qcadoo.model.api.Entity) StringJoiner(java.util.StringJoiner) NumberService(com.qcadoo.model.api.NumberService) ProductFields(com.qcadoo.mes.basic.constants.ProductFields) CalculationQuantityService(com.qcadoo.mes.basic.CalculationQuantityService) Entity(com.qcadoo.model.api.Entity) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) BigDecimal(java.math.BigDecimal)

Example 7 with PossibleUnitConversions

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);
}
Also used : Entity(com.qcadoo.model.api.Entity) DocumentBuilder(com.qcadoo.mes.materialFlowResources.service.DocumentBuilder) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) MultiHashMap(org.apache.commons.collections.MultiHashMap) MultiMap(org.apache.commons.collections.MultiMap) BigDecimal(java.math.BigDecimal)

Example 8 with PossibleUnitConversions

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;
}
Also used : Entity(com.qcadoo.model.api.Entity) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) BigDecimal(java.math.BigDecimal)

Example 9 with PossibleUnitConversions

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();
}
Also used : Entity(com.qcadoo.model.api.Entity) DocumentBuilder(com.qcadoo.mes.materialFlowResources.service.DocumentBuilder) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) BigDecimal(java.math.BigDecimal) Date(java.util.Date)

Example 10 with PossibleUnitConversions

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;
}
Also used : Entity(com.qcadoo.model.api.Entity) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) BigDecimal(java.math.BigDecimal)

Aggregations

PossibleUnitConversions (com.qcadoo.model.api.units.PossibleUnitConversions)35 Entity (com.qcadoo.model.api.Entity)26 BigDecimal (java.math.BigDecimal)21 FieldComponent (com.qcadoo.view.api.components.FieldComponent)5 Test (org.junit.Test)5 Optional (com.google.common.base.Optional)4 DocumentBuilder (com.qcadoo.mes.materialFlowResources.service.DocumentBuilder)4 Ignore (org.junit.Ignore)4 FormComponent (com.qcadoo.view.api.components.FormComponent)3 DataDefinition (com.qcadoo.model.api.DataDefinition)2 ErrorMessage (com.qcadoo.model.api.validators.ErrorMessage)2 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 TranslationService (com.qcadoo.localization.api.TranslationService)1 CalculationQuantityService (com.qcadoo.mes.basic.CalculationQuantityService)1 ProductFields (com.qcadoo.mes.basic.constants.ProductFields)1 UnitConversionItemFieldsB (com.qcadoo.mes.basic.constants.UnitConversionItemFieldsB)1 LocationFields (com.qcadoo.mes.materialFlow.constants.LocationFields)1 DocumentFields (com.qcadoo.mes.materialFlowResources.constants.DocumentFields)1