Пример #1
0
        private ProductAddToOrderViewModel ValidateCartItem(string Username, int productId, int quantity = 1)
        {
            var responseModel = new ProductAddToOrderViewModel();

            var customer = this.GetCustomer(Username);
            var product  = this.GetProduct(productId);

            if (product == null)
            {
                responseModel.Message = "product Invalid";
                responseModel.Succeed = false;

                return(responseModel);
            }
            if (product.Quantity <= quantity)
            {
                responseModel.Message = "Product is not available";
                responseModel.Succeed = false;

                return(responseModel);
            }

            responseModel.Message = "item is Valid";
            responseModel.Succeed = true;

            return(responseModel);
        }
Пример #2
0
        //add product to cart with quantity it check if exist increase the quantity
        public ProductAddToOrderViewModel addToCart(string userName, int productId, int quantity = 1)
        {
            var customer = this.GetCustomer(userName);

            ProductAddToOrderViewModel respons = new ProductAddToOrderViewModel();
            var product = this.GetProduct(productId);

            if (customer == null)
            {
                respons.Message = "Customer is wrong";
                respons.Succeed = false;

                return(respons);
            }


            if (product == null)
            {
                respons.Message = "Product is wrong";
                respons.Succeed = false;

                return(respons);
            }
            var cart = new CartItem()
            {
                Product   = product,
                AddedTime = DateTime.Now,
                Customer  = customer,
                Quantity  = quantity,
            };

            if (!db.CartItems.Any(c => c.Customer.Id == customer.Id && c.Product.Id == product.Id && !c.RemoveDate.HasValue && !c.DeletedDate.HasValue))
            {
                db.CartItems.Add(cart);
            }
            else
            {
                db.CartItems.Where(c => c.Customer.Id == customer.Id && c.Product.Id == product.Id && !c.RemoveDate.HasValue && !c.DeletedDate.HasValue)
                .FirstOrDefault().Quantity += quantity;
            }
            db.SaveChanges();


            respons.Message = "Product added to cart";
            respons.Succeed = true;

            return(respons);
        }
Пример #3
0
        //add cart item to temp cart for un authorize users
        public ProductAddToOrderViewModel addToTempCart(string tempCartId, int productId, int quantity = 1)
        {
            ProductAddToOrderViewModel respons = new ProductAddToOrderViewModel();
            var product = this.GetProduct(productId);

            if (tempCartId == null)
            {
                respons.Message = "id is wrong";
                respons.Succeed = false;

                return(respons);
            }


            if (product == null)
            {
                respons.Message = "Product is wrong";
                respons.Succeed = false;

                return(respons);
            }
            var cart = new TempCartItem()
            {
                Product    = product,
                AddedTime  = DateTime.Now,
                TempCartId = tempCartId,
                Quantity   = quantity,
            };

            if (!db.TempCartItems.Any(c => c.TempCartId == tempCartId && c.Product.Id == product.Id && !c.DeletedDate.HasValue))
            {
                db.TempCartItems.Add(cart);
            }
            else
            {
                db.TempCartItems.Where(c => c.TempCartId == tempCartId && c.Product.Id == product.Id && !c.DeletedDate.HasValue)
                .FirstOrDefault().Quantity += quantity;
            }
            db.SaveChanges();


            respons.Message = "Product added to Tempcart";
            respons.Succeed = true;

            return(respons);
        }
Пример #4
0
        //add all cart item to order and set delete from cart it use when user submit the order
        public int AddProductToCart(string UserName)
        {
            Customer customer = GetCustomer(UserName);

            if (customer == null)
            {
                return(0);
            }
            List <CartItem> cartItems = GetCartList(customer.UserName);
            List <ProductAddToOrderViewModel> respose = new List <ProductAddToOrderViewModel>();

            foreach (CartItem item in cartItems)
            {
                ProductAddToOrderViewModel res = AddCartToOrder(item.Customer.UserName, item.Product.Id, item.Quantity);
                db.CartItems.Where(x => x.Id == item.Id).SingleOrDefault().RemoveDate = DateTime.Now;

                respose.Add(res);
            }

            db.SaveChanges();
            return(respose.Count(x => x.Succeed));
        }
Пример #5
0
        //add cart to order and delete from cart (mark as delete) or increase the quentity if exist
        public ProductAddToOrderViewModel AddCartToOrder(string Username, int productId, int quantity = 1)
        {
            var responseModel = new ProductAddToOrderViewModel();

            var   customer = this.GetCustomer(Username);
            var   product  = this.GetProduct(productId);
            Order order;

            if (customer == null)
            {
                responseModel.Message = "Customer is wrong";
                responseModel.Succeed = false;

                return(responseModel);
            }


            if (product == null)
            {
                responseModel.Message = "Product is wrong";
                responseModel.Succeed = false;

                return(responseModel);
            }
            if (product.Quantity <= quantity)
            {
                responseModel.Message = "Product is not available";
                responseModel.Succeed = false;

                return(responseModel);
            }

            order = GetOrder(Username);
            if (order == null)
            {
                order = new Order()
                {
                    OrderDate = DateTime.Now,
                    Customer  = customer,
                };
                db.Orders.Add(order);
            }

            var orderDetail = new OrderDetail()
            {
                Order        = order,
                Product      = product,
                UnitPriceBuy = product.Price,
                Tax          = 0,
                Discount     = 0,
                Quantity     = quantity,
                CreationDate = DateTime.Now
            };

            if (db.OrderDetails.Any(x => x.Order.Id == order.Id && x.Product.Id == product.Id))
            {
                int deletedQuantity = db.OrderDetails.Where(x => x.Order.Id == order.Id && x.Product.Id == product.Id && !x.DeleteDate.HasValue).FirstOrDefault().Quantity;
                db.OrderDetails.Where(x => x.Order.Id == order.Id && x.Product.Id == product.Id && !x.DeleteDate.HasValue).FirstOrDefault().DeleteDate = DateTime.Now;
                db.Products.Where(x => x.Id == product.Id).FirstOrDefault().Quantity += deletedQuantity;
            }
            db.OrderDetails.Add(orderDetail);
            db.Products.Where(x => x.Id == product.Id).FirstOrDefault().Quantity -= quantity;

            order.AmountBuy += orderDetail.UnitPriceBuy * orderDetail.Quantity;
            db.SaveChanges();


            responseModel.Message = "Product added to order";
            responseModel.Succeed = true;

            return(responseModel);
        }