Beispiel #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);
        }
Beispiel #2
0
 public void createCoupon(Coupons coupon)
 {
     _couponDAL.Add(coupon);
 }