private BigDecimal applyPricesForLastNode(Map<Long, CalculationPriceSetItemDto> typeIdToCalculationPriceSetItemMap, Map<Long, CalculationPriceSetItemDto> existingTypeIdToCalculationPriceSetItemMap, Long parentQuantity, CalculationTreeNode calculationTreeNode) {
List<CalculationItemDto> calculationItems = calculationTreeNode.getCalculationItems();
BigDecimal totalPrice = BigDecimal.ZERO;
for (CalculationItemDto calculationItem : calculationItems) {
CalculationPriceSetItemDto calculationPriceSetItem = getOrCreateCalculationPriceSetItem(typeIdToCalculationPriceSetItemMap, existingTypeIdToCalculationPriceSetItemMap, calculationItem);
Long quantity = calculateQuantityForCalculationItem(calculationItem);
calculationItem.setQuantity(quantity);
calculationItem.setParentQuantity(parentQuantity);
Long priceSetItemQuantity = calculationPriceSetItem.getQuantity() + quantity * parentQuantity;
calculationPriceSetItem.setQuantity(priceSetItemQuantity);
calculationPriceSetItem.setTotalPrice(calculationPriceSetItem.getPrice().multiply(BigDecimal.valueOf(priceSetItemQuantity)).multiply(calculationItem.getDamagePerJob()));
totalPrice = totalPrice.add(calculationPriceSetItem.getPrice().multiply(BigDecimal.valueOf(quantity)).multiply(calculationItem.getDamagePerJob()));
BigDecimal totalPriceForParent = calculationPriceSetItem.getPrice().multiply(BigDecimal.valueOf(quantity)).multiply(BigDecimal.valueOf(parentQuantity)).multiply(calculationItem.getDamagePerJob());
calculationItem.setPrice(calculationPriceSetItem.getPrice());
calculationItem.setTotalPrice(totalPrice);
calculationItem.setTotalPriceForParent(totalPriceForParent);
}
return totalPrice;
}