Exemplo n.º 1
0
        public ActionResult DeleteItemCart(int?MaSP)
        {
            if (!MaSP.HasValue)
            {
                return(RedirectToAction("ShowCart"));
            }
            if (Session["Cart"] == null)
            {
                return(RedirectToAction("ShowCart"));
            }

            SanPham sanPham = DB.SanPhams.SingleOrDefault(p => p.MaSP == MaSP);

            if (sanPham == null)
            {
                return(RedirectToAction("ShowCart"));
            }
            List <ItemCartViewModel> itemCarts = GetCart();

            ItemCartViewModel updateItemCart = itemCarts.Find(p => p.MaSP == MaSP);

            if (updateItemCart == null)
            {
                return(RedirectToAction("ShowCart"));
            }
            itemCarts.Remove(updateItemCart);
            return(RedirectToAction("ShowCart"));
        }
Exemplo n.º 2
0
        public ActionResult AddToCart(int?MaSP, string strUrl)
        {
            SanPham findSP = DB.SanPhams.SingleOrDefault(p => p.MaSP == MaSP);

            if (findSP == null)
            {
                Response.StatusCode = 404;
                return(null);
            }
            List <ItemCartViewModel> itemCarts    = GetCart();
            ItemCartViewModel        findItemCart = itemCarts.SingleOrDefault(p => p.MaSP == MaSP);

            if (findItemCart != null)
            {
                if (findSP.SoLuongTon <= findItemCart.SoLuong)
                {
                    return(Content("Sản phẩm tồn Không đủ !"));
                }
                findItemCart.SoLuong++;
                findItemCart.ThanhTien = findItemCart.DonGia * findItemCart.SoLuong;
                return(Redirect(strUrl));
            }

            ItemCartViewModel itemCart = new ItemCartViewModel(MaSP.Value);

            itemCarts.Add(itemCart);
            return(Redirect(strUrl));
        }
Exemplo n.º 3
0
 public IActionResult OnGetLoadCartLayout()
 {
     if (_userManager.GetUserAsync(HttpContext.User).Result != null)
     {
         var cart = _cartRepository.GetCartByCustomerId(_userManager.GetUserAsync(HttpContext.User).Result.Id);
         if (cart != null)
         {
             ItemInCarts = new List <ItemCartViewModel>();
             var items = cart.CartDetails.Where(cd => cd.IsDeleted == false).ToList();
             if (items.Count > 0)
             {
                 foreach (var item in items)
                 {
                     if (item.Item.Quantity > 0)
                     {
                         var itemCartViewModel = new ItemCartViewModel
                         {
                             ItemId = item.ItemId,
                             //Image = $"/images/client/ProductImages/{item.Item.ProductImages?.FirstOrDefault()?.Name}",
                             Image = (item.Item.ProductImages.Count() > 0) ?
                                     $"/images/client/ProductImages/{item.Item.ProductImages?.FirstOrDefault()?.Name}" : $"/images/client/ProductImages/no-image.jpg",
                             Price       = item.Item.Price,
                             ProductName = item.Item.Name,
                             Quantity    = item.Quantity,
                         };
                         ItemInCarts.Add(itemCartViewModel);
                     }
                 }
             }
         }
     }
     return(new OkObjectResult(ItemInCarts));
 }
Exemplo n.º 4
0
        public ActionResult UpdateCart(ItemCartViewModel itemCart)
        {
            if (Session["Cart"] == null)
            {
                return(RedirectToAction("ShowCart"));
            }

            SanPham sanPham = DB.SanPhams.SingleOrDefault(p => p.MaSP == itemCart.MaSP);

            if (sanPham == null)
            {
                return(RedirectToAction("ShowCart"));
            }
            if (itemCart.SoLuong < 1 || !IsNumber(itemCart.SoLuong.ToString()))
            {
                return(RedirectToAction("DeleteItemCart", new { MaSP = itemCart.MaSP }));
            }
            if (sanPham.SoLuongTon < itemCart.SoLuong)
            {
                return(Content("Số lượng sản phẩm tồn không đủ !"));
            }

            List <ItemCartViewModel> itemCarts = GetCart();

            ItemCartViewModel updateItemCart = itemCarts.Find(p => p.MaSP == itemCart.MaSP);

            updateItemCart.SoLuong   = itemCart.SoLuong;
            updateItemCart.ThanhTien = itemCart.SoLuong * updateItemCart.DonGia;
            return(RedirectToAction("ShowCart"));
        }
Exemplo n.º 5
0
        private void DecreaseStockInWholesale(ItemCartViewModel itemCart)
        {
            var productInDatabase = _myContex.Products.Find(itemCart.ProductId);
            int currentstock      = productInDatabase.Stock - itemCart.Quantity;

            productInDatabase.Stock = currentstock;
            _myContex.SaveChanges();
        }
        public async Task <IActionResult> AddProductCart(ItemCartViewModel itemCart)
        {
            var response = await _salesBffService.AddProductCart(itemCart);

            if (HasErrorsInResponse(response))
            {
                return(View("Index", await _salesBffService.GetCart()));
            }

            return(RedirectToAction("Index"));
        }
        public async Task <ResponseErrorResult> UpdateProductCart(Guid productId, ItemCartViewModel itemCart)
        {
            var itemContent = GetContent(itemCart);

            var response = await _httpClient.PutAsync($"/sales/cart/product/{itemCart.ProductId}", itemContent);

            if (!CheckErrorsResponse(response))
            {
                return(await DeserializeObjectResponse <ResponseErrorResult>(response));
            }

            return(ReturnOk());
        }
        public async Task <ResponseErrorResult> AddProductCart(ItemCartViewModel itemCart)
        {
            var itemContent = GetContent(itemCart);

            var response = await _httpClient.PostAsync("/sales/cart/product", itemContent);

            if (!CheckErrorsResponse(response))
            {
                return(await DeserializeObjectResponse <ResponseErrorResult>(response));
            }

            return(ReturnOk());
        }
        public async Task <IActionResult> UpdateProductCart(Guid productId, int quantity)
        {
            var item = new ItemCartViewModel {
                ProductId = productId, Quantity = quantity
            };
            var response = await _salesBffService.UpdateProductCart(productId, item);

            if (HasErrorsInResponse(response))
            {
                return(View("Index", await _salesBffService.GetCart()));
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 10
0
        // adding next product to current cart or increase quantity
        public int AddItemToExistCart(CustomerEntity loggedUser, ItemCartViewModel itemCart)
        {
            // finding last order ID
            int orderId = _myContex.Orders.OrderByDescending(s => s.OrderId)
                          .Where(o => o.CustomerId == loggedUser.CustomerId)
                          .Where(o => o.StateOrder == "cart")
                          .FirstOrDefault().OrderId;

            // checking it is this item in cart if yes increase quantity if no, add new position OrderDetail
            OrderDetailEntity orderDetailExist = _myContex.OrderDetails.Where(a => a.OrderId == orderId)
                                                 .Where(a => a.ProductSymbol == itemCart.ProductSymbol)
                                                 .FirstOrDefault();

            if (orderDetailExist == null)
            {
                // create object with order details and save in database
                OrderDetailEntity orderDetail = new OrderDetailEntity()
                {
                    ProductId               = itemCart.ProductId,
                    ProductSymbol           = itemCart.ProductSymbol,
                    ProductName             = itemCart.ProductName,
                    OrderId                 = orderId,
                    Quantity                = itemCart.Quantity,
                    CurrentStockInWholesale = GetCurrentStockInWholesale(itemCart.ProductId),
                    Price = itemCart.Price,
                    Value = itemCart.Quantity * itemCart.Price,
                };
                _myContex.OrderDetails.Add(orderDetail);
                _myContex.SaveChanges();

                // decrease current item stock in database
                DecreaseStockInWholesale(itemCart);
            }
            else
            {
                orderDetailExist.Quantity += itemCart.Quantity;
                _myContex.SaveChanges();

                // decrease current item stock in database
                DecreaseStockInWholesale(itemCart);
            }



            // receive items in cart
            var itemsInCart = _myContex.OrderDetails.Where(c => c.OrderId == orderId).ToList();

            return(orderId);
        }
Exemplo n.º 11
0
        public IActionResult OnGetLoadTmpCart()
        {
            var cart = _cartRepository.GetCartByCustomerId(_userManager.GetUserAsync(HttpContext.User).Result.Id);

            if (cart != null)
            {
                ItemInCarts = new List <ItemCartViewModel>();
                var items = cart.CartDetails.Where(cd => cd.IsDeleted == false).ToList();
                if (items.Count > 0)
                {
                    foreach (var item in items)
                    {
                        if (item.Item.Quantity > 0)
                        {
                            var itemCartViewModel = new ItemCartViewModel
                            {
                                ItemId = item.ItemId,
                                Image  = (item.Item.ProductImages.Count() > 0) ?
                                         $"/images/client/ProductImages/{item.Item.ProductImages?.FirstOrDefault()?.Name}" : $"/images/client/ProductImages/no-image.jpg",
                                Price       = item.Item.Price,
                                ProductName = item.Item.Name,
                                //Quantity = item.Quantity,
                            };
                            if (item.Item.Quantity > item.Quantity)
                            {
                                itemCartViewModel.Quantity = item.Quantity;
                            }
                            else
                            {
                                itemCartViewModel.Quantity = item.Item.Quantity;
                            }
                            ItemInCarts.Add(itemCartViewModel);
                        }
                    }
                }
            }
            if (TempData.Get <List <ItemCartViewModel> >(CommonConstants.ItemsCheckout) != null)
            {
                TempData.Set <List <ItemCartViewModel> >(CommonConstants.ItemsCheckout, null);
            }

            TempData.Set("ItemsCheckout", ItemInCarts);
            TempData.Keep();
            return(new OkObjectResult(ItemInCarts));
        }
Exemplo n.º 12
0
        public IActionResult OnGetLoadCart()
        {
            var user = _userManager.GetUserAsync(HttpContext.User).Result;
            var cart = _cartRepository.GetCartByCustomerId(user.Id);

            if (cart != null)
            {
                ItemInCarts = new List <ItemCartViewModel>();
                var items = cart.CartDetails.Where(cd => cd.IsDeleted == false).ToList();
                if (items.Count > 0)
                {
                    foreach (var item in items)
                    {
                        var itemCartViewModel = new ItemCartViewModel
                        {
                            ItemId = item.ItemId,
                            Image  = (item.Item.ProductImages.Count() > 0) ?
                                     $"/images/client/ProductImages/{item.Item.ProductImages?.FirstOrDefault()?.Name}" : $"/images/client/ProductImages/no-image.jpg",
                            Price       = item.Item.Price,
                            ProductName = item.Item.Name,
                            MaxQuantity = item.Item.Quantity,
                            BrandName   = item.Item.BrandName
                        };
                        if (item.Quantity < item.Item.Quantity || item.Item.Quantity <= 0)
                        {
                            itemCartViewModel.Quantity = item.Quantity;
                        }
                        else
                        {
                            itemCartViewModel.Quantity = item.Item.Quantity;
                        }

                        if (item.Item.Quantity > 0 && item.Quantity > item.Item.Quantity)
                        {
                            item.Quantity = item.Item.Quantity;
                            _cartDetailRepository.Update(item);
                        }
                        ItemInCarts.Add(itemCartViewModel);
                    }
                }
            }
            return(new OkObjectResult(ItemInCarts));
        }
Exemplo n.º 13
0
        public ActionResult AddItemToCart(ItemCartViewModel itemCart)
        {
            string         userLogged = _userManager.GetUserName(HttpContext.User);
            CustomerEntity loggedUser = _appService.GetLoggedCustomer(userLogged);

            int orderId;

            if (String.IsNullOrEmpty(Request.Cookies["cartStatus"]))
            {
                orderId = _cartService.CreateNewCartOrder(loggedUser, itemCart);
            }
            else
            {
                orderId = _cartService.AddItemToExistCart(loggedUser, itemCart);
            }

            Response.Cookies.Append("cartStatus", "tempCart");
            Response.Cookies.Append("orderId", orderId.ToString());

            return(RedirectToAction("Cart", orderId));
        }
Exemplo n.º 14
0
        // creating new cart and saving that to database with state "cart"
        public int CreateNewCartOrder(CustomerEntity loggedUser, ItemCartViewModel itemCart)
        {
            OrderEntity order = new OrderEntity()
            {
                CustomerId  = loggedUser.CustomerId,
                StateOrder  = "cart",
                DateOrder   = DateTime.Now,
                StatusOrder = "W realizacji",
            };

            _myContex.Orders.Add(order);
            _myContex.SaveChanges();

            int orderId = _myContex.Orders.OrderByDescending(s => s.OrderId)
                          .Where(o => o.CustomerId == loggedUser.CustomerId)
                          .Where(o => o.StateOrder == "cart")
                          .FirstOrDefault().OrderId;

            // create object with order details and save in database
            OrderDetailEntity orderDetail = new OrderDetailEntity()
            {
                ProductId               = itemCart.ProductId,
                ProductSymbol           = itemCart.ProductSymbol,
                ProductName             = itemCart.ProductName,
                OrderId                 = orderId,
                Quantity                = itemCart.Quantity,
                CurrentStockInWholesale = GetCurrentStockInWholesale(itemCart.ProductId),
                Price = itemCart.Price,
                Value = itemCart.Quantity * itemCart.Price,
            };

            _myContex.OrderDetails.Add(orderDetail);
            _myContex.SaveChanges();

            // decrease current item stock in database
            DecreaseStockInWholesale(itemCart);

            return(orderId);
        }
Exemplo n.º 15
0
        public ActionResult EditCart(int?MaSP)
        {
            SanPham findSP = DB.SanPhams.SingleOrDefault(p => p.MaSP == MaSP);

            if (findSP == null)
            {
                Response.StatusCode = 404;
                return(null);
            }
            if (Session["Cart"] == null)
            {
                return(RedirectToAction("ShowCart"));
            }
            List <ItemCartViewModel> itemCarts = Session["Cart"] as List <ItemCartViewModel>;
            ItemCartViewModel        itemCart  = itemCarts.SingleOrDefault(p => p.MaSP == MaSP);

            if (itemCart == null)
            {
                return(RedirectToAction("ShowCart"));
            }
            ViewBag.Cart = itemCarts;
            return(View(itemCart));
        }