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);
}
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;
}
Aggregations