public ActionResult DeleteCart(string id)
 {
     try
     {
         int userId = Convert.ToInt32(SessionManager.UserData.Id);
         ShoppingCartService shoppingCartService = new ShoppingCartService();
         if (shoppingCartService.DeleteCart(Convert.ToInt32(id), userId))
         {
             JSONViewModel json = new JSONViewModel();
             json.Message    = "Success";
             json.StatusCode = 200;
             return(Json(json));
         }
         else
         {
             JSONViewModel json = new JSONViewModel();
             json.Message    = "Fail to delete!";
             json.StatusCode = 500;
             return(Json(json));
         }
     }
     catch (Exception ex)
     {
         JSONViewModel json = new JSONViewModel();
         json.Message    = ex.Message;
         json.StatusCode = 500;
         return(Json(json));
     }
 }
 public ActionResult UpdateBookTotalInCart(int id, int quantity)
 {
     try
     {
         int userId = Convert.ToInt32(SessionManager.UserData.Id);
         ShoppingCartService shoppingCartService = new ShoppingCartService();
         if (shoppingCartService.UpdateTotalBookInCart(Convert.ToInt32(id), userId, quantity))
         {
             JSONViewModel json = new JSONViewModel();
             json.Message    = "Success";
             json.StatusCode = 200;
             return(Json(json));
         }
         else
         {
             JSONViewModel json = new JSONViewModel();
             json.Message    = "Fail to update!";
             json.StatusCode = 500;
             return(Json(json));
         }
     }
     catch (Exception ex)
     {
         JSONViewModel json = new JSONViewModel();
         json.Message    = ex.Message;
         json.StatusCode = 500;
         return(Json(json));
     }
 }
        public ActionResult CartStatus()
        {
            int           Id   = Convert.ToInt32(SessionManager.UserData.Id);
            JSONViewModel json = new JSONViewModel();

            if (Id != null)
            {
                ShoppingCartService cartService = new ShoppingCartService();
                json.Message    = cartService.GetShoppingCart(Id).Count.ToString();
                json.StatusCode = 200;
            }
            else
            {
                json.Message    = "Fail load total cart!";
                json.StatusCode = 500;
            }
            return(Json(json, JsonRequestBehavior.AllowGet));
        }
        public ActionResult Cart(CartViewModel CartViewModel)
        {
            ShoppingCartModel cartModel = new ShoppingCartModel();

            cartModel.UserId    = Convert.ToInt32(SessionManager.UserData.Id);
            cartModel.ProductId = CartViewModel.ProductId;
            cartModel.Quantity  = CartViewModel.Quantity;
            ShoppingCartService cartService = new ShoppingCartService();

            if (cartService.PushToCart(cartModel))
            {
                JSONViewModel json = new JSONViewModel();
                json.Message    = "Success";
                json.StatusCode = 200;
                return(Json(json));
            }
            else
            {
                JSONViewModel json = new JSONViewModel();
                json.Message    = "Fail to Add!";
                json.StatusCode = 500;
                return(Json(json));
            }
        }
        public ActionResult ConfirmShoppingCart()
        {
            try
            {
                ShoppingCartService shoppingCartService = new ShoppingCartService();
                int          userId       = Convert.ToInt32(SessionManager.UserData.Id);
                OrderService orderService = new OrderService();
                int?         orderId      = orderService.InsertOrder(DateTime.Now, userId);
                if (orderId != null && shoppingCartService.GetShoppingCart(userId).Count != 0)
                {
                    var shoppingCart        = new ShoppingCartService();
                    var shoppingCartDetails = shoppingCart.GetShoppingCartDetail(userId);
                    var productService      = new ProductService();

                    bool isValidQuantity = true;
                    foreach (var item in shoppingCartDetails)
                    {
                        if (!(item.ProductBalance >= item.Quantity))
                        {
                            isValidQuantity = false;
                        }
                    }
                    if (!isValidQuantity)
                    {
                        JSONViewModel json = new JSONViewModel();
                        json.Message    = "จำนวนสินค้าในคลังไม่เพียงพอ";
                        json.StatusCode = 400;
                        return(Json(json));
                    }

                    OrderDetailService orderDetailService = new OrderDetailService();
                    if (orderDetailService.InsertOrderDetail(Convert.ToInt32(orderId), userId))
                    {
                        foreach (var item in shoppingCartDetails)
                        {
                            productService.UpdateBalance(item.ProductId, item.ProductBalance - item.Quantity);
                        }

                        if (shoppingCartService.DeleteAllCart(userId))
                        {
                            JSONViewModel json = new JSONViewModel();
                            json.Message    = orderId.ToString();
                            json.StatusCode = 200;
                            return(Json(json));
                        }
                        else
                        {
                            JSONViewModel json = new JSONViewModel();
                            json.Message    = "Cannot Delete all item in cart";
                            json.StatusCode = 500;
                            return(Json(json));
                        }
                    }
                    else
                    {
                        JSONViewModel json = new JSONViewModel();
                        json.Message    = "Cannot Insert item to order detail";
                        json.StatusCode = 500;
                        return(Json(json));
                    }
                }
                else
                {
                    JSONViewModel json = new JSONViewModel();
                    json.Message    = "Order detail not found";
                    json.StatusCode = 500;
                    return(Json(json));
                }
            }
            catch (Exception ex)
            {
                JSONViewModel json = new JSONViewModel();
                json.Message    = ex.Message;
                json.StatusCode = 500;
                return(Json(json));
            }
        }