public IActionResult Index()
        {
            PrincipalDAO dao      = new PrincipalDAO(_context);
            string       username = HttpContext.Session.GetString("USER");
            Principal    dto      = dao.FindByUsername(username);

            if (dto != null)
            {
                ViewData["deliveryAddress"] = dto.Address;
                ViewData["deliveryPhone"]   = dto.Phone;
            }
            else
            {
                ViewData["msg"] = "Load checkout failed";
                return(View("Error Page"));
            }
            Dictionary <string, DTO.Accessory> shoppingCart = HttpContext.Session.GetCollectionFromJson <Dictionary <string, DTO.Accessory> >("cart");
            Dictionary <string, DTO.Accessory> cart         = new Dictionary <string, DTO.Accessory>();

            foreach (var item in shoppingCart)
            {
                string        id         = item.Key;
                DTO.Accessory dtoSession = item.Value;
                AccessoryDAO  accDAO     = new AccessoryDAO(_context);
                DTO.Accessory accDTO     = accDAO.FindByPrimaryKey(id);
                accDTO.Quantity = dtoSession.Quantity;
                cart.Add(id, accDTO);
            }
            return(View(cart));
        }
Ejemplo n.º 2
0
        public IActionResult RestoreStorage([FromBody] string accTranID)
        {
            AccessoryDAO      accDAO = new AccessoryDAO(_context);
            TransactionRelDAO trDAO  = new TransactionRelDAO(_context);
            List <AccessoryTransactionRel> tranList = trDAO.FindByAccTranID(accTranID);
            List <DTO.Accessory>           accList  = new List <DTO.Accessory>();

            foreach (AccessoryTransactionRel tranRel in tranList)
            {
                DTO.Accessory accDTO = accDAO.FindByPrimaryKey(tranRel.AccId);
                accDTO.Quantity += tranRel.Quantity + accDTO.AvailableQuantity;
                accList.Add(accDTO);
            }
            string msg;

            if (accDAO.Restore(accList))
            {
                msg = "Restore all canceled accessories success";
            }
            else
            {
                msg = "Restore all canceled accessories failed";
            }
            return(new JsonResult(msg));
        }
        public IActionResult AddCartDetail([FromBody] DTO.AccessoryJSON acc)
        {
            string msg;
            Dictionary <string, DTO.Accessory> shoppingCart = HttpContext.Session.GetCollectionFromJson <Dictionary <string, DTO.Accessory> >("cart");

            if (shoppingCart == null)
            {
                shoppingCart = new Dictionary <string, DTO.Accessory>();
            }
            AccessoryDAO dao = new AccessoryDAO(_context);

            DTO.Accessory dto = dao.FindByPrimaryKey(acc.AccId);
            dto.Quantity = int.Parse(acc.Quantity);
            if (DTO.ShoppingCart.AddCart(dto, shoppingCart))
            {
                msg = "Added to cart";
            }
            else
            {
                msg = "Available quantity is not enough to buy.";
            }
            var obj = new
            {
                msg   = msg,
                count = shoppingCart.Count
            };

            HttpContext.Session.SetCollectionAsJson("cart", shoppingCart);
            return(new JsonResult(obj));
        }
        public IActionResult AddCart([FromBody] string accID)
        {
            string msg;
            Dictionary <string, DTO.Accessory> cart = HttpContext.Session.GetCollectionFromJson <Dictionary <string, DTO.Accessory> >("cart") ?? new Dictionary <string, DTO.Accessory>();
            AccessoryDAO dao = new AccessoryDAO(_context);

            DTO.Accessory dto = dao.FindByPrimaryKey(accID);
            dto.Quantity = 1;
            if (DTO.ShoppingCart.AddCart(dto, cart))
            {
                msg = "Added to cart";
            }
            else
            {
                msg = "Available quantity is not enough to buy";
            }
            var obj = new
            {
                msg   = msg,
                count = cart.Count
            };

            HttpContext.Session.SetCollectionAsJson("cart", cart);
            return(new JsonResult(obj));
        }
        public IActionResult FindAccById(string accIdSearch)
        {
            AccessoryDAO dao = new AccessoryDAO(_context);

            DTO.Accessory dto = dao.FindByPrimaryKey(accIdSearch);
            if (dto != null)
            {
                FavoriteDAO          fDAO     = new FavoriteDAO(_context);
                int                  countFav = fDAO.CountFavoriteByAccId(accIdSearch);
                AccessoryCategoryDAO acDAO    = new AccessoryCategoryDAO(_context);
                string               accCat   = acDAO.FindByPrimaryKey(dto.AccCatId);
                if (accCat.Equals(""))
                {
                    ViewData["msg"] = "Load category of accessory failed";
                    return(View("Error Page"));
                }
                ViewData["countFav"]    = countFav;
                ViewData["accCatName"]  = accCat;
                ViewData["accIdSearch"] = accIdSearch;
                return(View(dto));
            }
            else
            {
                ViewData["msg"] = "Accessory load failed";
                return(View("Error Page"));
            }
        }
        public IActionResult LoadCart()
        {
            Dictionary <string, DTO.Accessory> shoppingCart = HttpContext.Session.GetCollectionFromJson <Dictionary <string, DTO.Accessory> >("cart");
            Dictionary <string, DTO.Accessory> cart         = new Dictionary <string, DTO.Accessory>();

            foreach (var item in shoppingCart)
            {
                string        id         = item.Key;
                DTO.Accessory dtoSession = item.Value;
                AccessoryDAO  dao        = new AccessoryDAO(_context);
                DTO.Accessory accDTO     = dao.FindByPrimaryKey(id);
                accDTO.Quantity = dtoSession.Quantity;
                cart.Add(id, accDTO);
            }
            return(new JsonResult(cart));
        }
        public IActionResult CheckOut([Bind("DeliveryAddress", "DeliveryPhone")] DTO.Transaction tran)
        {
            string generateAccTranID()
            {
                string         id;
                Random         rd  = new Random();
                TransactionDAO dao = new TransactionDAO(_context);

                do
                {
                    id = "AT";
                    for (int i = 0; i < 4; i++)
                    {
                        id += rd.Next(10);
                    }
                } while (dao.CheckExisted(id));
                return(id);
            }

            AccessoryDAO accDAO   = new AccessoryDAO(_context);
            string       userName = HttpContext.Session.GetString("USER");
            Dictionary <string, DTO.Accessory> shoppingCart = HttpContext.Session.GetCollectionFromJson <Dictionary <string, DTO.Accessory> >("cart");

            foreach (var item in shoppingCart)
            {
                string        id         = item.Key;
                DTO.Accessory dtoSession = item.Value;
                DTO.Accessory accDTO     = accDAO.FindByPrimaryKey(id);
                if (accDTO.IsDelete || accDTO.AvailableQuantity == 0 || accDTO.AvailableQuantity < dtoSession.Quantity)
                {
                    return(RedirectToAction("Index", "ShoppingCart"));
                }
            }
            TransactionDAO tranDAO   = new TransactionDAO(_context);
            string         accTranID = generateAccTranID();

            DTO.Transaction tranDTO = new DTO.Transaction(accTranID, userName, tran.DeliveryPhone, tran.DeliveryAddress, 0);
            tranDAO.Insert(tranDTO);
            double total = 0;
            List <DTO.Accessory> list = new List <DTO.Accessory>();

            foreach (var item in shoppingCart)
            {
                string             id         = item.Key;
                DTO.Accessory      dtoSession = item.Value;
                DTO.Accessory      accDTO     = accDAO.FindByPrimaryKey(id);
                DTO.TransactionRel tranRelDTO = new DTO.TransactionRel(id, accTranID, accDTO.AccName, accDTO.AccCatId, accDTO.Brand, accDTO.Description, accDTO.Image, accDTO.Price, accDTO.SalePercent, accDTO.StartSellingDate, dtoSession.Quantity, accDTO.AccCatName);
                TransactionRelDAO  tranRelDAO = new TransactionRelDAO(_context);
                tranRelDAO.Insert(tranRelDTO);
                accDTO.AvailableQuantity -= dtoSession.Quantity;
                total += dtoSession.Quantity * accDTO.Price * (1 - accDTO.SalePercent);
                list.Add(accDTO);
            }
            accDAO.Update(list);
            total = (double)Math.Round(total * 100) / 100;
            tranDTO.TotalPrice = total;
            tranDAO.UpdateTotalPrice(total, accTranID);
            shoppingCart.Clear();
            HttpContext.Session.SetCollectionAsJson("cart", shoppingCart);
            return(RedirectToAction("UserDetails", "Principal"));
        }