Beispiel #1
0
        public IActionResult MustSignUp(CheckOutMustSignUpViewModel vm)
        {
            if (ModelState.IsValid)
            {
                _context.Users.Add(vm.User);
                _context.SaveChanges();

                AnoCart anocart = _context.AnoCarts.Where(ac => ac.Id == vm.AnoCartId).FirstOrDefault();

                Cart cart   = new Cart();
                int  userid = _context.Users.Where(u => u.PhoneNumber == vm.User.PhoneNumber).FirstOrDefault().Id;

                cart.User_Id       = userid;
                cart.CartStatus_Id = anocart.CartStatus_Id;
                cart.TotalQuantity = anocart.TotalQuantity;
                cart.TotalPrice    = anocart.TotalPrice;
                cart.DateCreate    = anocart.DateCreate;
                cart.DateModify    = anocart.DateModify;

                _context.Carts.Add(cart);
                _context.SaveChanges();

                int cartid = _context.Carts.Where(c => c.User_Id == userid).FirstOrDefault().Id;

                if (_context.AnoCartDetails.Where(ac => ac.Cart_Id == anocart.Id).FirstOrDefault() != null)
                {
                    List <AnoCartDetail> anodetails = _context.AnoCartDetails.Where(ac => ac.Cart_Id == anocart.Id).ToList();
                    foreach (AnoCartDetail detail in anodetails)
                    {
                        CartDetail cdt = new CartDetail()
                        {
                            Cart_Id     = cartid,
                            Product_Id  = detail.Product_Id,
                            PriceSingle = detail.PriceSingle,
                            PriceTotal  = detail.PriceTotal,
                            Quantity    = detail.Quantity,
                            DateCreate  = detail.DateCreate,
                            DateModify  = detail.DateModify
                        };
                        _context.CartDetails.Add(cdt);
                        _context.SaveChanges();
                    }
                }
                return(RedirectToAction("AlreadySignUp", "Checkout", new { userid = userid }));
            }
            else
            {
                return(View(
                           new CheckOutMustSignUpViewModel()
                {
                    User = vm.User,
                    AnoCartId = vm.AnoCartId,
                    ProductTypes = _context.ProductTypes.ToList(),
                    productBrands = _context.ProductBrands.ToList()
                }
                           ));
            }
        }
Beispiel #2
0
        public void ClearAnoCart(int id)
        {
            AnoCart cart = _context.AnoCarts.Where(c => c.Id == id).FirstOrDefault();
            List <AnoCartDetail> details = _context.AnoCartDetails.Where(cd => cd.Cart_Id == id).ToList();

            if (details != null)
            {
                foreach (AnoCartDetail detail in details)
                {
                    _context.AnoCartDetails.Remove(detail);
                    _context.SaveChanges();
                }
            }
            _context.SaveChanges();
        }
Beispiel #3
0
        public async Task <IActionResult> DeleteItem(int id)
        {
            List <CartItemViewModel> cartItem = new List <CartItemViewModel>();

            //đã đăng nhập
            if (HttpContext.Session.GetInt32("IdTaiKhoan").HasValue)
            {
                CartDetail cartDetail = await(from c in _context.Carts
                                              join cd in _context.CartDetails
                                              on c.Id equals cd.Cart_Id
                                              where cd.Id == id & c.User_Id == HttpContext.Session.GetInt32("IdTaiKhoan").Value
                                              select cd).SingleOrDefaultAsync();
                if (cartDetail != null)
                {
                    if (cartDetail.Quantity >= 1)
                    {
                        Cart cart = await(from c in _context.Carts
                                          where c.User_Id == HttpContext.Session.GetInt32("IdTaiKhoan").Value
                                          select c).SingleOrDefaultAsync();
                        cart.TotalQuantity -= cartDetail.Quantity;
                        cart.TotalPrice    -= cartDetail.PriceTotal;
                        _context.Carts.Update(cart);
                        _context.SaveChanges();

                        _context.CartDetails.Remove(cartDetail);
                        _context.SaveChanges();
                    }
                    cartItem = await(from d in _context.CartDetails
                                     join c in _context.Carts
                                     on d.Cart_Id equals c.Id
                                     join p in _context.Products
                                     on d.Product_Id equals p.Id
                                     where c.User_Id == HttpContext.Session.GetInt32("IdTaiKhoan") & d.Quantity > 0
                                     select new CartItemViewModel
                    {
                        Id         = d.Id,
                        Name       = p.Name,
                        Quantity   = d.Quantity,
                        Price      = d.PriceSingle,
                        TotalPrice = d.PriceTotal
                    }).ToListAsync();
                    return(PartialView("Users/_CartItemPartial", cartItem));
                }
                return(PartialView("Users/_CartItemPartial", cartItem));
            }
            //chưa đăng nhập
            else
            {
                AnoCartDetail anoCartDetail = await(from c in _context.AnoCarts
                                                    join cd in _context.AnoCartDetails
                                                    on c.Id equals cd.Cart_Id
                                                    where cd.Id == id & c.Id == HttpContext.Session.GetInt32("IdCart").Value
                                                    select cd).SingleOrDefaultAsync();
                if (anoCartDetail != null)
                {
                    if (anoCartDetail.Quantity >= 1)
                    {
                        AnoCart anoCart = await(from c in _context.AnoCarts
                                                where c.Id == HttpContext.Session.GetInt32("IdCart").Value
                                                select c).SingleOrDefaultAsync();
                        anoCart.TotalQuantity -= anoCartDetail.Quantity;
                        anoCart.TotalPrice    -= anoCartDetail.PriceTotal;
                        _context.AnoCarts.Update(anoCart);
                        _context.SaveChanges();

                        _context.AnoCartDetails.Remove(anoCartDetail);
                        _context.SaveChanges();
                    }
                    cartItem = await(from d in _context.AnoCartDetails
                                     join c in _context.AnoCarts
                                     on d.Cart_Id equals c.Id
                                     join p in _context.Products
                                     on d.Product_Id equals p.Id
                                     where c.Id == HttpContext.Session.GetInt32("IdCart") & d.Quantity > 0
                                     select new CartItemViewModel
                    {
                        Id         = d.Id,
                        Name       = p.Name,
                        Quantity   = d.Quantity,
                        Price      = d.PriceSingle,
                        TotalPrice = d.PriceTotal
                    }).ToListAsync();
                    return(PartialView("Users/_CartItemPartial", cartItem));
                }
                return(PartialView("Users/_CartItemPartial", cartItem));
            }
        }
Beispiel #4
0
        // nhận từ ajax?
        public async Task <int> AddItem(int id)
        {
            AnoCartDetail anoCartDetail;
            AnoCart       anoCart;

            Cart       cart;
            CartDetail cartDetail;
            Product    product = await(from p in _context.Products
                                       where p.Id == id
                                       select p).FirstOrDefaultAsync();

            if (product == null)
            {
                return(-1);
            }

            //tai khoan chua dang nhap
            if (HttpContext.Session.GetInt32("IdTaiKhoan") == null)
            {
                //khởi tạo cart lần đầu
                if (HttpContext.Session.GetInt32("IdCart") == null)
                {
                    anoCart = new AnoCart
                    {
                        CartStatus_Id = 2,
                        TotalPrice    = product.Price - (int)(product.Price * product.Saleoff) / 100,
                        TotalQuantity = 1,
                        DateCreate    = DateTime.Now,
                        DateModify    = DateTime.Now
                    };

                    //thêm cart vào database
                    _context.AnoCarts.Add(anoCart);
                    _context.SaveChanges();

                    //lấy và set id cart vừa tạo vào session
                    int idCart = await(from a in _context.AnoCarts select a.Id).MaxAsync();
                    HttpContext.Session.SetInt32("IdCart", idCart);

                    //tạo mới anoCartDetail
                    anoCartDetail = new AnoCartDetail
                    {
                        Cart_Id     = HttpContext.Session.GetInt32("IdCart").Value,
                        Product_Id  = id,
                        Quantity    = 1,
                        PriceTotal  = product.Price - (int)(product.Price * product.Saleoff) / 100,
                        PriceSingle = product.Price - (int)(product.Price * product.Saleoff) / 100,
                        DateCreate  = DateTime.Now,
                        DateModify  = DateTime.Now
                    };
                    _context.AnoCartDetails.Add(anoCartDetail);
                    _context.SaveChanges();

                    return(1);
                }

                //thêm sản phẩm theo id khi cart đã được tạo
                else
                {
                    //lấy anoCartDetail theo id sản phẩm và id cart
                    anoCartDetail = await(from d in _context.AnoCartDetails
                                          where d.Product_Id == id & d.Cart_Id == HttpContext.Session.GetInt32("IdCart")
                                          select d).SingleOrDefaultAsync();

                    //nếu chưa có anoCartDetail theo sản phẩm và cart
                    //tạo mới
                    if (anoCartDetail == null)
                    {
                        anoCartDetail = new AnoCartDetail
                        {
                            Cart_Id     = HttpContext.Session.GetInt32("IdCart").Value,
                            Product_Id  = product.Id,
                            Quantity    = 1,
                            PriceTotal  = product.Price - (int)(product.Price * product.Saleoff) / 100,
                            PriceSingle = product.Price - (int)(product.Price * product.Saleoff) / 100,
                            DateCreate  = DateTime.Now,
                            DateModify  = DateTime.Now
                        };

                        //thêm anoCartDrtail vào database
                        _context.AnoCartDetails.Add(anoCartDetail);
                        _context.SaveChanges();

                        //cập nhật anoCart
                        anoCart                = await(from c in _context.AnoCarts where c.Id == HttpContext.Session.GetInt32("IdCart") select c).SingleOrDefaultAsync();
                        anoCart.TotalPrice    += (product.Price - (int)(product.Price * product.Saleoff) / 100);
                        anoCart.TotalQuantity += 1;
                        _context.AnoCarts.Update(anoCart);
                        _context.SaveChanges();

                        return(1);
                    }
                    //đã có anoCartDetail tang so luong, gia tien (anoCart, anoCartDetails)
                    else
                    {
                        //Cap nhat anoCartDetails
                        anoCartDetail = await(from d in _context.AnoCartDetails
                                              where d.Product_Id == id & d.Cart_Id == HttpContext.Session.GetInt32("IdCart")
                                              select d).SingleOrDefaultAsync();
                        anoCartDetail.Quantity  += 1;
                        anoCartDetail.PriceTotal = anoCartDetail.Quantity * anoCartDetail.PriceSingle;

                        //cap nhat anoCart
                        anoCart                = await(from c in _context.AnoCarts where c.Id == HttpContext.Session.GetInt32("IdCart") select c).SingleOrDefaultAsync();
                        anoCart.TotalPrice    += (product.Price - (int)(product.Price * product.Saleoff) / 100);
                        anoCart.TotalQuantity += 1;
                        _context.AnoCarts.Update(anoCart);
                        _context.SaveChanges();

                        return(1);
                    }
                }
            }

            //tai khoan da dang nhap
            else
            {
                cart = await(from c in _context.Carts
                             where c.User_Id == HttpContext.Session.GetInt32("IdTaiKhoan")
                             select c).SingleOrDefaultAsync();

                //tai khoan nay chua co cart , thi tao moi
                if (cart == null)
                {
                    //them moi cart
                    cart = new Cart
                    {
                        CartStatus_Id = 2,
                        User_Id       = HttpContext.Session.GetInt32("IdTaiKhoan").Value,
                        TotalPrice    = product.Price - (int)(product.Price * product.Saleoff) / 100,
                        TotalQuantity = 1,
                        DateCreate    = DateTime.Now,
                        DateModify    = DateTime.Now
                    };
                    _context.Carts.Add(cart);
                    _context.SaveChanges();

                    //them moi cartDetail
                    cartDetail = new CartDetail
                    {
                        Cart_Id     = cart.Id,
                        Product_Id  = id,
                        Quantity    = 1,
                        PriceTotal  = product.Price - (int)(product.Price * product.Saleoff) / 100,
                        PriceSingle = product.Price - (int)(product.Price * product.Saleoff) / 100,
                        DateCreate  = DateTime.Now,
                        DateModify  = DateTime.Now
                    };
                    _context.CartDetails.Add(cartDetail);
                    _context.SaveChanges();

                    return(1);
                }
                //tai khoan nay da tao cart
                else if (cart != null)
                {
                    //lấy cartDetail theo id sản phẩm và id cart
                    cartDetail = await(from d in _context.CartDetails
                                       where d.Product_Id == id & d.Cart_Id == cart.Id
                                       select d).SingleOrDefaultAsync();

                    //Chua co cartDetail them moi
                    if (cartDetail == null)
                    {
                        cartDetail = new CartDetail
                        {
                            Cart_Id     = cart.Id,
                            Product_Id  = product.Id,
                            Quantity    = 1,
                            PriceTotal  = product.Price - (int)(product.Price * product.Saleoff) / 100,
                            PriceSingle = product.Price - (int)(product.Price * product.Saleoff) / 100,
                            DateCreate  = DateTime.Now,
                            DateModify  = DateTime.Now
                        };

                        //thêm cartDrtail vào database
                        _context.CartDetails.Add(cartDetail);
                        _context.SaveChanges();

                        //cập nhật Cart
                        cart                = await(from c in _context.Carts where c.Id == cart.Id select c).SingleOrDefaultAsync();
                        cart.TotalPrice    += (product.Price - (int)(product.Price * product.Saleoff) / 100);
                        cart.TotalQuantity += 1;
                        _context.Carts.Update(cart);
                        _context.SaveChanges();

                        return(1);
                    }

                    //đã có cartDetail tang so luong, gia tien (cart, cartDetails)
                    else
                    {
                        //Cap nhat CartDetails
                        cartDetail = await(from d in _context.CartDetails
                                           where d.Product_Id == id & d.Cart_Id == cart.Id
                                           select d).SingleOrDefaultAsync();
                        cartDetail.Quantity  += 1;
                        cartDetail.PriceTotal = cartDetail.Quantity * cartDetail.PriceSingle;

                        //cap nhat Cart
                        cart                = await(from c in _context.Carts where c.Id == cart.Id select c).SingleOrDefaultAsync();
                        cart.TotalPrice    += (product.Price - (int)(product.Price * product.Saleoff) / 100);
                        cart.TotalQuantity += 1;
                        _context.Carts.Update(cart);
                        _context.SaveChanges();

                        return(1);
                    }
                }
            }
            return(0);
        }