Search in sources :

Example 21 with PossibleUnitConversions

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

Example 22 with PossibleUnitConversions

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

Example 23 with PossibleUnitConversions

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

Example 24 with PossibleUnitConversions

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

Example 25 with PossibleUnitConversions

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

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