Пример #1
0
        public ViewCartItem applyDiscounts(List <Campaigns> campaigns)
        {
            List <Carts>        cartItems        = new List <Carts>();
            List <Products>     productItems     = new List <Products>();
            List <Categories>   categoryItems    = new List <Categories>();
            List <CartProducts> cartProductItems = new List <CartProducts>();


            ViewCartItem viewCartItem = new ViewCartItem();

            cartProductItems = _cartProductDAL.GetList();
            cartItems        = _cartDAL.GetList();
            categoryItems    = _categoryDAL.GetList();
            productItems     = _productDAL.GetList();
            // discountTypeItems = _discountType.GetList();

            var totalPriceInCart = //sepetteki toplam tutar
                                   from CartProduct in cartProductItems
                                   join Cart in cartItems on CartProduct.CartId equals Cart.Id
                                   join Product in productItems on CartProduct.ProductId equals Product.Id

                                   select new { TotalPrice = Cart.TotalPrice, Product.CategoryId, ProductName = Product.Title, UnitPrice = Product.Price, Amount = Cart.Amount };

            var totalAmountInCart = //sepetteki toplam adet
                                    from CartProduct in cartProductItems
                                    join Cart in cartItems on CartProduct.CartId equals Cart.Id
                                    join Product in productItems on CartProduct.ProductId equals Product.Id

                                    select new { TotalAmount = Cart.Amount, Product.CategoryId, ProductId = Product.Id, ProductName = Product.Title, CartId = Cart.Id, UnitPrice = Product.Price, Amount = Cart.Amount };

            // tanımlanacak kampanyalar içinden sepetteki ürünlerde geçerliliğinin kontrolü
            foreach (var campaignCategory in campaigns)
            {
                //kampanyalı kategorinin toplam tutar limiti
                var totalPricebyCampaignCategory = totalPriceInCart.Where(c => c.CategoryId == campaignCategory.CategoryId).Select(p => p.TotalPrice).ToList().FirstOrDefault();


                //kampanyalı kategorinin miktar limiti
                var totalAmountbyCampaignCategory = totalAmountInCart.Where(c => c.CategoryId == campaignCategory.CategoryId).Select(a => a.TotalAmount).ToList().FirstOrDefault();


                //sepette kampanyalı kategori varsa ve kampanya miktar koşulu sağlanıyorsa indirim uygulanacak
                if (totalAmountbyCampaignCategory >= campaignCategory.AmountLimit)
                {
                    int isExist = _campaignDAL.GetList().Where(x => x.Id == campaignCategory.CategoryId).Count();

                    if (isExist < 1)
                    {
                        _campaignDAL.Add(campaignCategory);// kampanya eklendi
                    }

                    var discountedProducts = totalAmountInCart.Where(x => x.CategoryId == campaignCategory.CategoryId);

                    foreach (var item in discountedProducts)
                    {
                        var discountedInCartId = _cartProductDAL.Get(x => x.ProductId == item.ProductId).CartId;
                        var updatedCartItem    = _cartDAL.Get(x => x.Id == discountedInCartId);

                        if (campaignCategory.DiscountTypeId == (int)EnumDiscountTypes.Rate)//Convert.ToInt32(discountTypeItems.Where(a => a.Title == "Rate").Select(x => x.Id).ToList()))
                        {
                            //kampanyanın oran bazında indirimli halinin sepet tutarına yansıtılmış hali
                            var discountedPriceForRate = totalPricebyCampaignCategory - (totalPricebyCampaignCategory * campaignCategory.DiscountRate) / 100;
                            updatedCartItem.TotalPrice = discountedPriceForRate;
                        }
                        else if (campaignCategory.DiscountTypeId == (int)EnumDiscountTypes.Amount) //Convert.ToInt32(discountTypeItems.Where(a => a.Title == "Amount").Select(x => x.Id).ToList()))
                        {
                            //kampanyanın miktar bazında indirimli halinin sepet tutarına yansıtılmış hali
                            var discountedPriceForAmount = totalPricebyCampaignCategory - campaignCategory.DiscountRate;
                            updatedCartItem.TotalPrice = discountedPriceForAmount;
                        }
                        _cartDAL.Update(updatedCartItem);
                        viewCartItem.TotalPriceInCart     = updatedCartItem.TotalPrice;
                        viewCartItem.DiscountedTotalPrice = updatedCartItem.TotalPrice;
                        viewCartItem.Amount      = item.TotalAmount;
                        viewCartItem.CartId      = item.CartId;
                        viewCartItem.ProductName = item.ProductName;
                        viewCartItem.TotalPrice  = item.TotalAmount * item.UnitPrice;
                        viewCartItem.UnitPrice   = totalPriceInCart.FirstOrDefault().UnitPrice;
                    }
                }
                else
                {
                    viewCartItem.TotalPriceInCart = totalPriceInCart.FirstOrDefault().TotalPrice;
                    viewCartItem.ErrorInfo        = "Sepette kampanyalı olan kategorilere ait yeterli veya hiç ürün yok !";
                    //sepette kampanyalı kategoriye ait ürün yok!
                }
            }
            return(viewCartItem);
        }
Пример #2
0
 public List <Campaigns> getCampaignInfo(int categoryId)
 {
     return(_campaignDAL.GetList(x => x.CategoryId == categoryId));
 }