/// <summary> /// Function to Calculate Price /// </summary> /// <param name="shoppingCart"></param> /// <returns></returns> public decimal CalculateTotalPrice(List <char> shoppingCart) { decimal finalPrice = 0.0M; try { List <SKU> masterSKUList = GetMasterPriceList(); List <SKUQuntity> skuQuntities = new List <SKUQuntity>(); CalculatePrice calculatePrice = new CalculatePrice(); foreach (var item in shoppingCart) { SKU tempSKU = masterSKUList.Where(x => x.Unit.ToString().Equals(item.ToString())).FirstOrDefault(); if (tempSKU != null) { SKUQuntity tempSKUQuntity = skuQuntities.Where(x => x.SKU.Unit.ToString().Equals(item.ToString())).FirstOrDefault(); if (tempSKUQuntity != null) { tempSKUQuntity.Quntity++; } else { tempSKUQuntity = new SKUQuntity(); tempSKUQuntity.SKU = tempSKU; tempSKUQuntity.Quntity = 1; skuQuntities.Add(tempSKUQuntity); } } } finalPrice = calculatePrice.GetShoppingCartPriceList(skuQuntities); } catch (Exception) { //Add logging tool throw; } return(finalPrice); }
/// <summary> /// Function to get total price for shopping list based on Promotion /// </summary> /// <param name="cartItems"></param> /// <returns></returns> public decimal GetShoppingCartPriceList(List <SKUQuntity> cartItems) { decimal totalPrice = 0.0M; List <Promotion> activePromotions = _activePromotion.GetActivePromotion(); foreach (var item in cartItems) { if (item.ItemProcessed == false) { Promotion itemPromotion = activePromotions.Where(x => x.Unit.ToString().Equals(item.SKU.Unit.ToString())).FirstOrDefault(); if (itemPromotion != null)//Promotion Found for Product { int rem = (item.Quntity) % (itemPromotion.NumSKURequired); int remainingQuntity = (item.Quntity) / (itemPromotion.NumSKURequired); if (itemPromotion.IsDiscountInPercentage == false) //flat discount { if (itemPromotion.IsOtherSKURequired == false) //promotion is not depend of other SKU { if (remainingQuntity > 0) { decimal price = (item.Quntity * item.SKU.Price); totalPrice = totalPrice + price - (remainingQuntity * itemPromotion.FlatDiscount); } else { totalPrice = totalPrice + (item.Quntity * item.SKU.Price); } } else { //Depend on other so consider pricing with same item SKUQuntity secondSKU = cartItems.Where(x => x.SKU.Unit.Equals(itemPromotion.OtherSKULookUp)).FirstOrDefault(); if (secondSKU != null) { decimal firstProductPrice = (item.Quntity * item.SKU.Price); decimal secondProductPrice = (secondSKU.Quntity * secondSKU.SKU.Price); totalPrice = totalPrice + firstProductPrice + secondProductPrice - itemPromotion.FlatDiscount; secondSKU.ItemProcessed = true; } else { totalPrice = totalPrice + (item.Quntity * item.SKU.Price); } } } else { //Same logic can be apply to implement full logic like no of Quntity is required to get percentange discount decimal price = (item.Quntity * item.SKU.Price); totalPrice = totalPrice + price - ((price * itemPromotion.DiscountPercentage) / 100); } } else { totalPrice = totalPrice + (item.Quntity * item.SKU.Price); } } } return(totalPrice); }