//Calculation of subtotals public shoppingBagViewModel FindShoppingBagWithItems(int id) { shoppingBagViewModel bag = new shoppingBagViewModel(); bag.shoppingBag = FindShoppingBagById(id);; bag.shoppingItems = shoppingItemRepository.GetItemsPerShoppingBag(id); foreach (var item in bag.shoppingItems) { item.SISubTotal = item.SIQuantity * item.Product.PPrice; } bag.shoppingBag.SBTotalQuantity = bag.shoppingItems.Sum(sb => sb.SIQuantity); bag.shoppingBag.SBSubTotal = bag.shoppingItems.Sum(sb => sb.SISubTotal); //Calculation of the discount but only if items are higher then 2 => 5% or higher then 5 => 10% if (bag.shoppingBag.SBTotalQuantity > 5) { bag.shoppingBag.SBDiscountPct = 10; bag.shoppingBag.SBDiscount = bag.shoppingBag.SBSubTotal * bag.shoppingBag.SBDiscountPct / 100; bag.shoppingBag.SBTotal = bag.shoppingBag.SBSubTotal - bag.shoppingBag.SBDiscount; } else { if (bag.shoppingBag.SBTotalQuantity > 2) { bag.shoppingBag.SBDiscountPct = 5; bag.shoppingBag.SBDiscount = bag.shoppingBag.SBSubTotal * bag.shoppingBag.SBDiscountPct / 100; bag.shoppingBag.SBTotal = bag.shoppingBag.SBSubTotal - bag.shoppingBag.SBDiscount; } else { bag.shoppingBag.SBDiscount = 0; bag.shoppingBag.SBTotal = bag.shoppingBag.SBSubTotal; } } return(bag); }
public List <ShoppingItem> GetItemsPerShoppingBag(int id) { return(repository.GetItemsPerShoppingBag(id)); }