Search in sources :

Example 1 with ProductWithQuantityAndCost

use of com.qcadoo.mes.costNormsForMaterials.orderRawMaterialCosts.domain.ProductWithQuantityAndCost in project mes by qcadoo.

the class ProductionTrackingListenerServicePFTD method updateCostsForOrder.

public void updateCostsForOrder(final Entity order) {
    DataDefinition positionDD = getPositionDD();
    SearchQueryBuilder searchQueryBuilder = positionDD.find("SELECT pr.id AS product, SUM(p.quantity) AS quantity, SUM(p.quantity * p.price) AS price " + "FROM #materialFlowResources_position p JOIN p.document AS d join p.product AS pr " + "WHERE d.order = :order_id AND d.type = :type " + "GROUP BY d.order, d.type, pr.id");
    searchQueryBuilder.setLong("order_id", order.getId());
    searchQueryBuilder.setString("type", DocumentType.INTERNAL_OUTBOUND.getStringValue());
    SearchResult result = searchQueryBuilder.list();
    List<ProductWithQuantityAndCost> productsWithQuantitiesAndCosts = Lists.newArrayList();
    for (Entity costsForProduct : result.getEntities()) {
        Long product = (Long) costsForProduct.getField(PositionFields.PRODUCT);
        BigDecimal quantity = costsForProduct.getDecimalField(PositionFields.QUANTITY);
        BigDecimal cost = costsForProduct.getDecimalField(PositionFields.PRICE);
        productsWithQuantitiesAndCosts.add(new ProductWithQuantityAndCost(product, quantity, cost));
    }
    List<Entity> updatedCosts = costNormsForMaterialsService.updateCostsForProductInOrder(order, productsWithQuantitiesAndCosts);
    order.setField(OrderFieldsCNFM.TECHNOLOGY_INST_OPER_PRODUCT_IN_COMPS, updatedCosts);
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchQueryBuilder(com.qcadoo.model.api.search.SearchQueryBuilder) SearchResult(com.qcadoo.model.api.search.SearchResult) DataDefinition(com.qcadoo.model.api.DataDefinition) BigDecimal(java.math.BigDecimal) ProductWithQuantityAndCost(com.qcadoo.mes.costNormsForMaterials.orderRawMaterialCosts.domain.ProductWithQuantityAndCost)

Example 2 with ProductWithQuantityAndCost

use of com.qcadoo.mes.costNormsForMaterials.orderRawMaterialCosts.domain.ProductWithQuantityAndCost in project mes by qcadoo.

the class CostNormsForMaterialsService method updateCostsForProductInOrder.

public List<Entity> updateCostsForProductInOrder(Entity order, Collection<ProductWithQuantityAndCost> productsInfo) {
    List<Entity> result = Lists.newArrayList();
    if (!productsInfo.isEmpty()) {
        Map<Long, ProductWithQuantityAndCost> productsInfoGroupedByProductId = productsInfo.stream().collect(Collectors.toMap(ProductWithQuantityAndCost::getProductId, Function.identity()));
        List<Entity> orderMaterialCostsList = orderMaterialCostsDataProvider.findAll(OrderMaterialCostsCriteria.forOrder(order.getId()).setProductCriteria(in("id", productsInfoGroupedByProductId.keySet())));
        Map<Long, List<Entity>> orderMaterialCostsGroupedByProductId = orderMaterialCostsList.stream().collect(Collectors.groupingBy(e -> e.getBelongsToField(TechnologyInstOperProductInCompFields.PRODUCT).getId()));
        for (Map.Entry<Long, ProductWithQuantityAndCost> entry : productsInfoGroupedByProductId.entrySet()) {
            Long productId = entry.getKey();
            if (orderMaterialCostsGroupedByProductId.containsKey(productId)) {
                List<Entity> orderMaterialCostsForProduct = orderMaterialCostsGroupedByProductId.get(productId);
                Entity orderMaterialCosts = orderMaterialCostsForProduct.get(0);
                Optional<BigDecimal> costForOrder = entry.getValue().getCostOpt();
                orderMaterialCosts.setField(TechnologyInstOperProductInCompFields.COST_FOR_ORDER, numberService.setScaleWithDefaultMathContext(costForOrder.orElse(BigDecimal.ZERO)));
                result.add(orderMaterialCosts.getDataDefinition().save(orderMaterialCosts));
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(String.format("TechnologyInstanceOperationProductInComponent (order material costs entity) not found for product: %d order: %d", productId, order.getId()));
                }
            }
        }
    }
    return result;
}
Also used : SearchRestrictions.in(com.qcadoo.model.api.search.SearchRestrictions.in) OrderFieldsCNFM(com.qcadoo.mes.costNormsForMaterials.constants.OrderFieldsCNFM) DataDefinitionService(com.qcadoo.model.api.DataDefinitionService) ComponentState(com.qcadoo.view.api.ComponentState) QcadooViewConstants(com.qcadoo.view.constants.QcadooViewConstants) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) OrdersConstants(com.qcadoo.mes.orders.constants.OrdersConstants) TechnologyService(com.qcadoo.mes.technologies.TechnologyService) Function(java.util.function.Function) BigDecimal(java.math.BigDecimal) ViewDefinitionState(com.qcadoo.view.api.ViewDefinitionState) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Lists(com.google.common.collect.Lists) Service(org.springframework.stereotype.Service) FormComponent(com.qcadoo.view.api.components.FormComponent) Map(java.util.Map) OrderMaterialCostsCriteria(com.qcadoo.mes.costNormsForMaterials.orderRawMaterialCosts.dataProvider.OrderMaterialCostsCriteria) Logger(org.slf4j.Logger) OperationProductInComponentFields(com.qcadoo.mes.technologies.constants.OperationProductInComponentFields) Collection(java.util.Collection) OrderMaterialsCostDataGenerator(com.qcadoo.mes.costNormsForMaterials.orderRawMaterialCosts.OrderMaterialsCostDataGenerator) Collectors(java.util.stream.Collectors) TechnologyInstOperProductInCompFields(com.qcadoo.mes.costNormsForMaterials.constants.TechnologyInstOperProductInCompFields) GridComponent(com.qcadoo.view.api.components.GridComponent) Entity(com.qcadoo.model.api.Entity) List(java.util.List) OrderMaterialCostsDataProvider(com.qcadoo.mes.costNormsForMaterials.orderRawMaterialCosts.dataProvider.OrderMaterialCostsDataProvider) NumberService(com.qcadoo.model.api.NumberService) Optional(java.util.Optional) TechnologiesConstants(com.qcadoo.mes.technologies.constants.TechnologiesConstants) ProductWithQuantityAndCost(com.qcadoo.mes.costNormsForMaterials.orderRawMaterialCosts.domain.ProductWithQuantityAndCost) Entity(com.qcadoo.model.api.Entity) List(java.util.List) Map(java.util.Map) BigDecimal(java.math.BigDecimal) ProductWithQuantityAndCost(com.qcadoo.mes.costNormsForMaterials.orderRawMaterialCosts.domain.ProductWithQuantityAndCost)

Aggregations

ProductWithQuantityAndCost (com.qcadoo.mes.costNormsForMaterials.orderRawMaterialCosts.domain.ProductWithQuantityAndCost)2 Entity (com.qcadoo.model.api.Entity)2 BigDecimal (java.math.BigDecimal)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Lists (com.google.common.collect.Lists)1 OrderFieldsCNFM (com.qcadoo.mes.costNormsForMaterials.constants.OrderFieldsCNFM)1 TechnologyInstOperProductInCompFields (com.qcadoo.mes.costNormsForMaterials.constants.TechnologyInstOperProductInCompFields)1 OrderMaterialsCostDataGenerator (com.qcadoo.mes.costNormsForMaterials.orderRawMaterialCosts.OrderMaterialsCostDataGenerator)1 OrderMaterialCostsCriteria (com.qcadoo.mes.costNormsForMaterials.orderRawMaterialCosts.dataProvider.OrderMaterialCostsCriteria)1 OrderMaterialCostsDataProvider (com.qcadoo.mes.costNormsForMaterials.orderRawMaterialCosts.dataProvider.OrderMaterialCostsDataProvider)1 OrdersConstants (com.qcadoo.mes.orders.constants.OrdersConstants)1 TechnologyService (com.qcadoo.mes.technologies.TechnologyService)1 OperationProductInComponentFields (com.qcadoo.mes.technologies.constants.OperationProductInComponentFields)1 TechnologiesConstants (com.qcadoo.mes.technologies.constants.TechnologiesConstants)1 DataDefinition (com.qcadoo.model.api.DataDefinition)1 DataDefinitionService (com.qcadoo.model.api.DataDefinitionService)1 NumberService (com.qcadoo.model.api.NumberService)1 SearchQueryBuilder (com.qcadoo.model.api.search.SearchQueryBuilder)1 SearchRestrictions.in (com.qcadoo.model.api.search.SearchRestrictions.in)1 SearchResult (com.qcadoo.model.api.search.SearchResult)1