Пример #1
0
        public ViewCartItem applyCoupon(Coupons coupon)
        {
            List <Carts>        cartItems        = new List <Carts>();
            List <Products>     productItems     = new List <Products>();
            List <CartProducts> cartProductItems = new List <CartProducts>();

            cartProductItems = _cartProductDAL.GetList();
            cartItems        = _cartDAL.GetList();
            productItems     = _productDAL.GetList();

            ViewCartItem viewCartItem = new ViewCartItem();
            decimal      _totalPrice  = 0;

            var cartProducts = //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 { Cart.TotalPrice, CartProduct.ProductId, CartProduct.CartId, ProductName = Product.Title, UnitPrice = Product.Price };


            foreach (var item in cartProducts.ToList())
            {
                _totalPrice = Convert.ToDecimal(item.TotalPrice);

                //sepetteki tutar kupondaki limite eşit veya fazla ise
                if (coupon.Price <= _totalPrice)
                {
                    if (_couponDAL.GetList(x => x.DiscountRate == coupon.DiscountRate && x.DiscountTypeId == coupon.DiscountTypeId && x.Price == coupon.Price).Count == 0)
                    {
                        _couponDAL.Add(coupon);//kupon eklendi
                    }
                    //sepetteki ürün
                    var discountedProduct = _productDAL.Get(x => x.Id == item.ProductId);

                    if (coupon.DiscountTypeId == (int)EnumDiscountTypes.Rate)//Convert.ToInt32(discountTypeItems.Where(a => a.Title == "Rate").Select(x => x.Id).ToList()))
                    {
                        //kuponun oran bazında indirimli halinin sepet tutarına yansıtılmış hali
                        var discountedPriceForRate = _totalPrice - (_totalPrice * coupon.DiscountRate) / 100;
                        discountedProduct.Price = discountedPriceForRate;
                    }

                    else if (coupon.DiscountTypeId == (int)EnumDiscountTypes.Amount)//Convert.ToInt32(discountTypeItems.Where(a => a.Title == "Amount").Select(x => x.Id).ToList()))
                    {
                        //kuponun miktar bazında indirimli halinin sepet tutarına yansıtılmış hali
                        var discountedPriceForAmount = _totalPrice - coupon.DiscountRate;
                        discountedProduct.Price = discountedPriceForAmount;
                    }

                    cartItems = _cartDAL.GetList(x => x.Id == item.CartId).ToList();
                    // indirim sepetteki ürünlere yansıtıldı.
                    foreach (var cartItem in cartItems)
                    {
                        cartItem.Id         = discountedProduct.Id;
                        cartItem.TotalPrice = discountedProduct.Price;
                        _cartDAL.Update(cartItem);
                    }
                    viewCartItem.Amount = cartItems.FirstOrDefault().Amount;
                    viewCartItem.CartId = cartItems.FirstOrDefault().Id;
                    viewCartItem.DiscountedTotalPrice = cartItems.FirstOrDefault().TotalPrice;
                    viewCartItem.ProductName          = cartProducts.FirstOrDefault().ProductName;
                    viewCartItem.TotalPrice           = cartItems.FirstOrDefault().Amount *cartProducts.FirstOrDefault().UnitPrice;
                    viewCartItem.UnitPrice            = cartProducts.FirstOrDefault().UnitPrice;
                    viewCartItem.TotalPriceInCart     = discountedProduct.Price;
                }
                else
                {
                    viewCartItem.TotalPriceInCart = _totalPrice;
                    viewCartItem.ErrorInfo        = "Sepetteki toplam tutar, kupon limitinden az! Sepete en az " + (coupon.Price - _totalPrice).ToString() + "TL" + " tutarında daha ürün eklemelisiniz !";
                }
            }

            return(viewCartItem);
        }
Пример #2
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);
        }