示例#1
0
        public Carts addItem(Carts cart, int productId)
        {
            decimal productPrice   = _productDAL.Get(x => x.Id == productId).Price;
            int     lastCartItemId = 0;
            int     isExist        = _cartDAL.GetList().Where(x => x.Id == cart.Id).Count();

            Carts cartItem = new Carts();

            if (isExist == 0)
            {
                cartItem.Amount     = cart.Amount;
                cartItem.TotalPrice = productPrice * cart.Amount;

                _cartDAL.Add(cartItem);
                lastCartItemId = _cartDAL.GetList().OrderByDescending(x => x.Id).ToList().FirstOrDefault().Id;
            }
            else
            {
                var thisCartItem = _cartDAL.GetList().OrderByDescending(x => x.Id).ToList().FirstOrDefault();

                cartItem.Id         = cart.Id;
                cartItem.Amount     = thisCartItem.Amount + cart.Amount;
                cartItem.TotalPrice = (productPrice * (thisCartItem.Amount + cart.Amount));

                _cartDAL.Update(cartItem);
                lastCartItemId = cart.Id;
            }
            if (_cartProductDAL.GetList(x => x.CartId == lastCartItemId && x.ProductId == productId).Count() < 1)
            {
                CartProducts cartProduct = new CartProducts()
                {
                    CartId    = lastCartItemId,
                    ProductId = productId
                };
                _cartProductDAL.Add(cartProduct);
            }
            else
            {
                int          lastCartId  = _cartDAL.GetList().Where(x => x.Id == lastCartItemId).FirstOrDefault().Id;
                CartProducts cartProduct = new CartProducts()
                {
                    Id        = lastCartItemId,
                    CartId    = lastCartItemId,
                    ProductId = productId
                };
                _cartProductDAL.Update(cartProduct);
            }
            return(cartItem);
        }
示例#2
0
        public DeliveryInfo calculateFor(CartProducts carts)
        {
            List <Carts>        cartItems        = new List <Carts>();
            List <Products>     productItems     = new List <Products>();
            List <Categories>   categoryItems    = new List <Categories>();
            List <CartProducts> cartProductItems = new List <CartProducts>();


            double costForDelivery        = 5.99; //anlaşmalı kargo teslimatı birim fiyatı
            double fixedCost              = 2.99; // sabit fiyat
            double calculatedDeliveryCost = 0.0;

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

            var itemsInCart = //sepetteki ürünler ve kategorileri
                              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.Id, CartProduct.ProductId, Product.CategoryId, Product.Price };

            var thisCartItem = itemsInCart.Where(x => x.Id == carts.CartId).ToList();                                                            // bu sepetteki ürünlerin

            var numberOfProduct  = itemsInCart.Where(x => x.Id == carts.CartId).ToList().Distinct().Select(p => p.ProductId).Count();            // sepette kaç farklı ürün var
            var numberOfDelivery = itemsInCart.Where(x => x.ProductId == carts.ProductId).ToList().Distinct().Select(c => c.CategoryId).Count(); // sepette kaç farklı ürün kategorisi var


            for (int i = 0; i < numberOfProduct; i++)
            {
                var costPerProduct = itemsInCart.Where(x => x.ProductId == carts.ProductId).ToList().Distinct().Select(p => p.Price).FirstOrDefault(); // sepetteki her bir ürünün birim fiyatı
                calculatedDeliveryCost = (costForDelivery * numberOfDelivery) + (Convert.ToDouble(costPerProduct) * numberOfProduct) + fixedCost;      // teslimat tutarının hesaplanması

                Deliveries delivery = new Deliveries()
                {
                    CartId          = itemsInCart.Select(c => c.Id).FirstOrDefault(),
                    CostPerDelivery = costForDelivery,
                    CostPerProduct  = Convert.ToDouble(costPerProduct),
                    FixedCost       = fixedCost
                };

                _deliveryDAL.Add(delivery); // teslimatın eklenmesi
            }
            DeliveryInfo deliveryInfo = new DeliveryInfo()
            {
                Info  = "Kargo Teslimat Tutarı",
                Price = calculatedDeliveryCost
            };

            return(deliveryInfo);
        }
 public List <CartProducts> getCartProducts()
 {
     return(_cartProductDAL.GetList());
 }