Ejemplo n.º 1
0
        public ActionResult ConfirmCart()
        {
            Order    cart     = Session["CART"] as Order;
            PhoneDAO dao      = new PhoneDAO();
            bool     isEnough = true;
            string   name     = "bủh";
            CartDAO  cartDAO  = new CartDAO();

            for (int i = 0; i < cart.detailList.Count; i++)
            {
                if (cart.detailList[i].Quantity > dao.GetProductDetail(cart.detailList[i].IDProduct).Quantity)
                {
                    isEnough = false;
                    name     = cart.detailList[i].ProductName;
                }
            }
            if (!isEnough)
            {
                ViewBag.INSUFF = "There is not enough quantity of " + name + " for you! Please choose another products or decrease the amount!";
                return(View("ViewCart"));
            }
            else
            {
                bool     isCartOk = cartDAO.AddCart(cart);
                PhoneDAO phoneDao = new PhoneDAO();
                bool     isAllOk  = true;
                if (isCartOk)
                {
                    bool[] isDetailsOk = new bool[cart.detailList.Count];
                    bool[] isUpdateOk  = new bool[cart.detailList.Count];
                    for (int i = 0; i < isDetailsOk.Length; i++)
                    {
                        isDetailsOk[i] = cartDAO.AddOrder(cart.detailList[i]);
                    }
                    for (int i = 0; i < isDetailsOk.Length; i++)
                    {
                        Product p = new Product(cart.detailList[i].IDProduct, cart.detailList[i].Store - cart.detailList[i].Quantity);
                        isUpdateOk[i] = phoneDao.UpdateProductAfterAddCart(p);
                    }
                    for (int i = 0; i < isDetailsOk.Length; i++)
                    {
                        if (!isDetailsOk[i])
                        {
                            isAllOk = false;
                        }
                    }
                    for (int i = 0; i < isUpdateOk.Length; i++)
                    {
                        if (!isUpdateOk[i])
                        {
                            isAllOk = false;
                        }
                    }
                    if (!isAllOk)
                    {
                        return(Redirect(Url.Action("Error", "Phone")));
                    }
                    else
                    {
                        Session.Remove("CART");
                        return(Redirect(Url.Action("LoadList", "Phone")));
                    }
                }
                else
                {
                    return(Redirect(Url.Action("Error", "Phone")));
                }
            }
        }