public void ApplyDiscount(List <Item> items)
        {
            //Start temp code as data is not getting from DB
            ProductActions productActions = new ProductActions();

            productActions.CreateAllProducts();
            productActions.CreateAllCombinationPromotions();
            var allProducts = productActions.GetAllProducts();
            //End temp code as data is not getting from DB

            var allSkus = items.Select(m => m.SkuId).ToList();



            var promotion = productActions.GetPromotionBySkuId(allSkus);

            if (promotion != null)
            {
                for (int iteration = 0; iteration < promotion.SkuIds.Count; iteration++)
                {
                    Item item = items.Find(m => m.SkuId == promotion.SkuIds[iteration]);
                    item.Price = 0;
                    if (iteration == promotion.SkuIds.Count - 1)
                    {
                        item.Price = promotion.PromotionPrice;
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void ApplyDiscount(List <Item> items)
        {
            double price = 0;
            //Start temp code as data is not getting from DB
            ProductActions productActions = new ProductActions();

            productActions.CreateAllProducts();
            productActions.CreateAllQuantityPromotions();

            var allProducts = productActions.GetAllProducts();

            //End temp code as data is not getting from DB


            foreach (Item item in items)
            {
                var promotion = productActions.GetPromotionBySkuId(item.SkuId);

                if (promotion != null)
                {
                    if (promotion.OnNoOfProducts == item.Quantity)
                    {
                        price = promotion.PromotionPrice;
                    }
                    else if (promotion.OnNoOfProducts < item.Quantity)
                    {
                        int quantity = item.Quantity / promotion.OnNoOfProducts;
                        price    = promotion.PromotionPrice * quantity;
                        quantity = item.Quantity % promotion.OnNoOfProducts;
                        price    = price + (quantity * item.Price);
                    }
                    else
                    {
                        price = item.Quantity * item.Price;
                    }
                }
                else
                {
                    price = item.Quantity * item.Price;
                }
                item.Price = price;
            }
        }