Search in sources :

Example 26 with PossibleUnitConversions

use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.

the class ProductionCountingDocumentService method preparePositionForUsedBatch.

private ProductionCountingQuantityHolder preparePositionForUsedBatch(Entity inProductRecord, Entity inProduct, final Entity usedBatch) {
    ProductionCountingQuantityHolder holder = new ProductionCountingQuantityHolder();
    BigDecimal usedQuantity = usedBatch.getDecimalField(UsedBatchFields.QUANTITY);
    BigDecimal givenQuantity = productionTrackingService.calculateGivenQuantity(inProductRecord, usedQuantity).orElse(usedQuantity);
    BigDecimal conversion = BigDecimal.ONE;
    String unit = inProduct.getStringField(ProductFields.UNIT);
    String givenUnit = inProductRecord.getStringField(TrackingOperationProductOutComponentFields.GIVEN_UNIT);
    if (Objects.nonNull(usedQuantity)) {
        PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(unit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, inProduct)));
        if (unitConversions.isDefinedFor(givenUnit)) {
            conversion = numberService.setScaleWithDefaultMathContext(unitConversions.asUnitToConversionMap().get(givenUnit));
        }
    }
    holder.setProductId(inProduct.getId());
    holder.setProduct(inProduct);
    holder.setUsedQuantity(usedQuantity);
    holder.setConversion(conversion);
    holder.setGivenQuantity(givenQuantity);
    holder.setGivenUnit(givenUnit);
    holder.setBatchId(usedBatch.getBelongsToField(UsedBatchFields.BATCH).getId());
    holder.setBatch(usedBatch.getBelongsToField(UsedBatchFields.BATCH));
    return holder;
}
Also used : PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) BigDecimal(java.math.BigDecimal)

Example 27 with PossibleUnitConversions

use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.

the class ProductionTrackingListenerServicePFTD method updateInternalInboundDocumentForFinalProducts.

public Entity updateInternalInboundDocumentForFinalProducts(final Entity order, final Entity existingInboundDocument, final Collection<Entity> trackingOperationProductOutComponents, boolean isFinalProduct, boolean cleanPositionsQuantity) {
    DataDefinition positionDD = getPositionDD();
    List<Entity> positions = Lists.newArrayList(existingInboundDocument.getHasManyField(DocumentFields.POSITIONS));
    if (cleanPositionsQuantity) {
        positions.forEach(position -> {
            position.setField(PositionFields.QUANTITY, BigDecimal.ZERO);
            position.setField(PositionFields.GIVEN_QUANTITY, BigDecimal.ZERO);
        });
    }
    for (Entity trackingOperationProductOutComponent : trackingOperationProductOutComponents) {
        Entity outProduct = trackingOperationProductOutComponent.getBelongsToField(TrackingOperationProductOutComponentFields.PRODUCT);
        Entity outBatch = trackingOperationProductOutComponent.getBelongsToField(TrackingOperationProductOutComponentFields.PRODUCTION_TRACKING).getBelongsToField(ProductionTrackingFields.BATCH);
        if (!isFinalProduct) {
            outBatch = null;
        }
        Entity storageLocation = trackingOperationProductOutComponent.getBelongsToField(TrackingOperationProductOutComponentFields.STORAGE_LOCATION);
        Entity palletNumber = trackingOperationProductOutComponent.getBelongsToField(TrackingOperationProductOutComponentFields.PALLET_NUMBER);
        java.util.Optional<BigDecimal> usedQuantity = Optional.ofNullable(trackingOperationProductOutComponent.getDecimalField(TrackingOperationProductInComponentFields.USED_QUANTITY));
        java.util.Optional<BigDecimal> givenQuantity = Optional.ofNullable(trackingOperationProductOutComponent.getDecimalField(TrackingOperationProductInComponentFields.GIVEN_QUANTITY));
        java.util.Optional<String> givenUnit = Optional.ofNullable(trackingOperationProductOutComponent.getStringField(TrackingOperationProductInComponentFields.GIVEN_UNIT));
        Entity existingPosition = filterPosition(positions, outProduct, givenUnit.orElse(null), outBatch, storageLocation, palletNumber);
        if (Objects.nonNull(existingPosition)) {
            java.util.Optional<BigDecimal> quantity = Optional.ofNullable(existingPosition.getDecimalField(PositionFields.QUANTITY));
            java.util.Optional<BigDecimal> givenQuantityFromPosition = Optional.ofNullable(existingPosition.getDecimalField(PositionFields.GIVEN_QUANTITY));
            existingPosition.setField(PositionFields.QUANTITY, quantity.orElse(BigDecimal.ZERO).add(usedQuantity.orElse(BigDecimal.ZERO)));
            if (givenQuantity.isPresent()) {
                existingPosition.setField(PositionFields.GIVEN_QUANTITY, givenQuantity.orElse(BigDecimal.ZERO).add(givenQuantityFromPosition.orElse(BigDecimal.ZERO)));
            }
            fillAttributes(trackingOperationProductOutComponent, existingPosition);
            existingPosition.setField(PositionFields.GIVEN_UNIT, givenUnit.get());
        } else {
            Entity productionTracking = trackingOperationProductOutComponent.getBelongsToField(TrackingOperationProductOutComponentFields.PRODUCTION_TRACKING);
            Entity position = positionDD.create();
            position.setField(PositionFields.PRODUCT, outProduct);
            position.setField(PositionFields.QUANTITY, usedQuantity.get());
            BigDecimal conversion = BigDecimal.ONE;
            String unit = outProduct.getStringField(ProductFields.UNIT);
            if (givenQuantity.isPresent()) {
                PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(unit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, outProduct)));
                if (unitConversions.isDefinedFor(givenUnit.get())) {
                    conversion = numberService.setScaleWithDefaultMathContext(unitConversions.asUnitToConversionMap().get(givenUnit.get()));
                }
                position.setField(PositionFields.GIVEN_QUANTITY, givenQuantity.get());
            }
            position.setField(PositionFields.GIVEN_UNIT, givenUnit.get());
            position.setField(PositionFields.CONVERSION, conversion);
            position.setField(PositionFields.PRODUCTION_DATE, new Date());
            String priceBasedOn = parameterService.getParameter().getStringField(ParameterFieldsPC.PRICE_BASED_ON);
            boolean isNominalProductCost = Objects.nonNull(priceBasedOn) && priceBasedOn.equals(PriceBasedOn.NOMINAL_PRODUCT_COST.getStringValue());
            if (isNominalProductCost) {
                position.setField(PositionFields.PRICE, getNominalCost(outProduct));
            }
            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);
            positions.add(position);
        }
    }
    existingInboundDocument.setField(DocumentFields.POSITIONS, positions);
    return existingInboundDocument.getDataDefinition().save(existingInboundDocument);
}
Also used : Entity(com.qcadoo.model.api.Entity) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) DataDefinition(com.qcadoo.model.api.DataDefinition) BigDecimal(java.math.BigDecimal) Date(java.util.Date)

Example 28 with PossibleUnitConversions

use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.

the class OperationProductInComponentHooks method copyQuantityToProductsBySize.

private void copyQuantityToProductsBySize(final DataDefinition operationProductInComponentDD, final Entity operationProductInComponent) {
    if (Objects.isNull(operationProductInComponent.getField(OperationProductInComponentFields.VARIOUS_QUANTITIES_IN_PRODUCTS_BY_SIZE))) {
        operationProductInComponent.setField(OperationProductInComponentFields.VARIOUS_QUANTITIES_IN_PRODUCTS_BY_SIZE, false);
    }
    if (operationProductInComponent.getBooleanField(OperationProductInComponentFields.DIFFERENT_PRODUCTS_IN_DIFFERENT_SIZES) && !operationProductInComponent.getBooleanField(OperationProductInComponentFields.VARIOUS_QUANTITIES_IN_PRODUCTS_BY_SIZE) && Objects.nonNull(operationProductInComponent.getId())) {
        Entity operationProductInComponentDb = operationProductInComponentDD.get(operationProductInComponent.getId());
        if (Objects.isNull(operationProductInComponentDb.getDecimalField(OperationProductInComponentFields.QUANTITY)) || !operationProductInComponentDb.getDecimalField(OperationProductInComponentFields.QUANTITY).equals(operationProductInComponent.getDecimalField(OperationProductInComponentFields.QUANTITY))) {
            operationProductInComponent.getHasManyField(OperationProductInComponentFields.PRODUCT_BY_SIZE_GROUPS).forEach(pbs -> {
                String opicUnit = operationProductInComponent.getStringField(OperationProductInComponentFields.GIVEN_UNIT);
                String productBySizeGroupUnit = pbs.getStringField(ProductBySizeGroupFields.GIVEN_UNIT);
                if (opicUnit.equals(productBySizeGroupUnit)) {
                    pbs.setField(ProductBySizeGroupFields.GIVEN_QUANTITY, operationProductInComponent.getDecimalField(OperationProductInComponentFields.GIVEN_QUANTITY));
                    pbs.setField(ProductBySizeGroupFields.GIVEN_UNIT, operationProductInComponent.getStringField(OperationProductInComponentFields.GIVEN_UNIT));
                    calculateQuantityForPBSG(pbs);
                } else {
                    PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(opicUnit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, pbs.getBelongsToField(ProductBySizeGroupFields.PRODUCT))));
                    if (unitConversions.isDefinedFor(productBySizeGroupUnit)) {
                        BigDecimal convertedQuantity = unitConversions.convertTo(operationProductInComponent.getDecimalField(OperationProductInComponentFields.GIVEN_QUANTITY), productBySizeGroupUnit);
                        pbs.setField(ProductBySizeGroupFields.GIVEN_QUANTITY, convertedQuantity);
                        calculateQuantityForPBSG(pbs);
                    }
                }
                pbs.getDataDefinition().save(pbs);
            });
        }
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions) BigDecimal(java.math.BigDecimal)

Example 29 with PossibleUnitConversions

use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.

the class AdditionalUnitService method getQuantityAfterConversion.

public BigDecimal getQuantityAfterConversion(final Entity order, String givenUnit, BigDecimal quantity, String baseUnit) {
    Entity product = order.getBelongsToField(OrderFields.PRODUCT);
    PossibleUnitConversions unitConversions = unitConversionService.getPossibleConversions(baseUnit, searchCriteriaBuilder -> searchCriteriaBuilder.add(SearchRestrictions.belongsTo(UnitConversionItemFieldsB.PRODUCT, product)));
    if (quantity == null) {
        return BigDecimal.ZERO;
    }
    if (!baseUnit.equals(givenUnit)) {
        if (unitConversions.isDefinedFor(givenUnit)) {
            return unitConversions.convertTo(quantity, givenUnit);
        }
    }
    return quantity;
}
Also used : Entity(com.qcadoo.model.api.Entity) PossibleUnitConversions(com.qcadoo.model.api.units.PossibleUnitConversions)

Example 30 with PossibleUnitConversions

use of com.qcadoo.model.api.units.PossibleUnitConversions in project mes by qcadoo.

the class TrackingOperationProductComponentDetailsListeners method calculateQuantity.

public void calculateQuantity(final ViewDefinitionState view, final ComponentState componentState, final String[] args) {
    FormComponent form = (FormComponent) view.getComponentByReference(QcadooViewConstants.L_FORM);
    Entity productComponent = form.getPersistedEntityWithIncludedFormValues();
    String givenUnit = productComponent.getStringField(TrackingOperationProductInComponentFields.GIVEN_UNIT);
    Entity product = productComponent.getBelongsToField(TrackingOperationProductInComponentFields.PRODUCT);
    FieldComponent givenQuantityField = (FieldComponent) view.getComponentByReference(TrackingOperationProductInComponentFields.GIVEN_QUANTITY);
    if (product == null || givenUnit == null || givenUnit.isEmpty() || givenQuantityField.getFieldValue() == null) {
        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();
            String baseUnit = product.getStringField(ProductFields.UNIT);
            if (baseUnit.equals(givenUnit)) {
                productComponent.setField(TrackingOperationProductInComponentFields.USED_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, BigDecimal.ROUND_FLOOR);
                    productComponent.setField(TrackingOperationProductInComponentFields.USED_QUANTITY, convertedQuantity);
                } else {
                    productComponent.addError(productComponent.getDataDefinition().getField(TrackingOperationProductInComponentFields.GIVEN_QUANTITY), "technologies.operationProductInComponent.validate.error.missingUnitConversion");
                    productComponent.setField(TrackingOperationProductInComponentFields.USED_QUANTITY, null);
                }
            }
        } else {
            productComponent.setField(TrackingOperationProductInComponentFields.USED_QUANTITY, null);
        }
    } else {
        productComponent.setField(TrackingOperationProductInComponentFields.USED_QUANTITY, null);
    }
    form.setEntity(productComponent);
}
Also used : FormComponent(com.qcadoo.view.api.components.FormComponent) Entity(com.qcadoo.model.api.Entity) Optional(com.google.common.base.Optional) FieldComponent(com.qcadoo.view.api.components.FieldComponent) 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