public IActionResult CodCheckout(string id, bool?usePoint = false) { try { var giohangthanhtoanthanhcong = _context.CartTable.Find(id).IsCOD = true; _context.SaveChanges(); var totalPrice = _context.CartDetailTable.Where(x => x.CartID == id).Include(x => x.Product).Sum(x => x.Product.ProductPrice * x.Quantity); var user = _userManager.Users.Include(x => x.DeliveryInfo).SingleAsync(x => x.Id == _cookieServices.ReadCookie(HttpContext, "CART_INFORMATION")).Result; var pointLeft = user.Point; if (usePoint == true && user.Point > 0) { if (user.Point <= totalPrice) { pointLeft = 0; } else { pointLeft = (user.Point - Convert.ToInt32(totalPrice)); } } return(Redirect(Url.Action(nameof(CheckoutSuccess), "Home", new { id = id, userid = user.Id, pointLeft = pointLeft }, protocol: Request.Scheme))); } catch (HttpException e) { return(RedirectToAction(nameof(CheckoutFail))); } }
internal static bool ConfirmDeleted(this DoAnTMDT_Entities _context, HttpContext httpContext, CookieServices _cookieServices, string orderID) { string cookie = _cookieServices.ReadCookie(httpContext, "CART_INFORMATION"); if (cookie != null) { try { var cartDetailsInDB = _context.CartDetailTable.Include(x => x.Product).Where(x => x.CartID == orderID).ToList(); foreach (var cartDetail in cartDetailsInDB) { _context.RemoveFromCart(httpContext, cartDetail.ProductID, cartDetail.Size, (byte)cartDetail.Quantity); } _context.Remove(_context.CartTable.Find(orderID)); _context.SaveChanges(); return(true); } catch (Exception e) { return(false); } } return(false); }
internal static bool ConfirmOrder(this DoAnTMDT_Entities _context, UserManager <ApplicationUser> _userManager, HttpContext httpContext, CookieServices _cookieServices, string orderID) { string cookie = _cookieServices.ReadCookie(httpContext, "CART_INFORMATION"); if (cookie != null) { try { var cartInDB = _context.CartTable.Find(orderID); double pointConverted; if (cartInDB.IsCOD) { cartInDB.IsPayed = true; cartInDB.PayDate = DateTime.Now; pointConverted = _context.CartDetailTable.Where(x => x.CartID == orderID).Include(x => x.Product).Sum(x => x.Product.ProductPrice * x.Quantity) / 20; } cartInDB.IsCompleted = true; //Tính điểm xu, nếu thanh toán = Paypal thì dc giảm 20% => tổng giá nhân cho (1*0.2) //Xu được tính theo tỉ lệ tổng giá chia 20, khi sử dụng 1 xu tương đương 1$ pointConverted = (_context.CartDetailTable.Where(x => x.CartID == orderID).Include(x => x.Product).Sum(x => x.Product.ProductPrice * x.Quantity) * (1 - 0.2) / 20); var user = _userManager.FindByNameAsync(httpContext.User.Identity.Name).Result; user.Point += Convert.ToInt32(pointConverted); _userManager.UpdateAsync(user); _context.SaveChanges(); return(true); } catch (Exception) { return(false); } } return(false); }
internal static bool RemoveFromCart(this DoAnTMDT_Entities _context, HttpContext httpContext, int itemID, string size, byte quantity = 1) { Product bienkiemtraxemcosanphamtrongdb = _context.ProductTable.Find(itemID); if (bienkiemtraxemcosanphamtrongdb != null) { string cookieKey = _cookieServices.ReadCookie(httpContext, "CART_INFORMATION"); string guidKey = Guid.NewGuid().ToString(); if (string.IsNullOrEmpty(cookieKey)) { cookieKey = httpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); _cookieServices.AddCookie(httpContext, "CART_INFORMATION", cookieKey); } var bienkiemtraxemcodonhangchuathanhtoancocungmadonhang = _context.CartDetailTable.Include(x => x.Cart).Where(x => x.CartID == x.Cart.CartID && !x.Cart.IsPayed); if (bienkiemtraxemcodonhangchuathanhtoancocungmadonhang.Count() > 0) { var bienkiemtraxemsanphamdodacotronggiohangchua = bienkiemtraxemcodonhangchuathanhtoancocungmadonhang.Where(x => x.ProductID == itemID && x.Size == size).FirstOrDefault(); if (bienkiemtraxemsanphamdodacotronggiohangchua != null) { if (size == "small") { bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity += quantity; } else if (size == "large") { bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity += quantity; } else { bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity += quantity; } bienkiemtraxemsanphamdodacotronggiohangchua.Quantity -= quantity; if (bienkiemtraxemsanphamdodacotronggiohangchua.Quantity == 0) { var chitietdonhangsexoa = _context.CartDetailTable.Where(x => x.ProductID == itemID && x.Size == size).FirstOrDefault(); if (_context.CartTable.Find(chitietdonhangsexoa.CartID) != null) { _context.Remove(chitietdonhangsexoa); } } _context.SaveChanges(); return(true); } else { return(false); } } } return(false); }
public IActionResult EditProduct(int id, Product cate) { try { // TODO: Add update logic here //if (cate.ImageUpload != null) //{ // string filename = Path.GetFileNameWithoutExtension(cate.ImageUpload.FileName); // string extension = Path.GetExtension(cate.ImageUpload.FileName); // filename = filename + extension; // cate.Images = "~/images/" + filename; // cate.ImageUpload.SaveAs(Path.Combine(Server.MapPath("~/images/"), filename)); //} cate = _context.ProductTable.Where(s => s.ProductID == id).FirstOrDefault(); TryUpdateModelAsync(cate); _context.SaveChanges(); return(RedirectToAction("Product")); } catch { return(View()); } }
internal static bool AddToCart(this DoAnTMDT_Entities _context, HttpContext httpContext, int itemID, string size, byte quantity = 1) { Product bienkiemtraxemcosanphamtrongdb = _context.ProductTable.Find(itemID); if (bienkiemtraxemcosanphamtrongdb != null) { string cookieKey = _cookieServices.ReadCookie(httpContext, "CART_INFORMATION"); if (string.IsNullOrEmpty(cookieKey)) { cookieKey = httpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); _cookieServices.AddCookie(httpContext, "CART_INFORMATION", cookieKey); } var bienkiemtraxemcodonhangchuathanhtoancocungmadonhang = _context.CartDetailTable.Include(x => x.Cart).Where(x => x.Cart.UserID == cookieKey && x.Cart.IsDisplay); if (bienkiemtraxemcodonhangchuathanhtoancocungmadonhang.Count() > 0) { var bienkiemtraxemsanphamdodacotronggiohangchua = bienkiemtraxemcodonhangchuathanhtoancocungmadonhang.Where(x => x.ProductID == itemID && x.Size == size).FirstOrDefault(); if (bienkiemtraxemsanphamdodacotronggiohangchua != null) { byte calculatedQuantity = 0; if (size == "small") { if (bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity; bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity -= quantity; } } else if (size == "large") { if (bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity; bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity -= quantity; } } else { if (bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity; bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity -= quantity; } } if (calculatedQuantity > 0) { bienkiemtraxemsanphamdodacotronggiohangchua.Quantity += calculatedQuantity; } else { bienkiemtraxemsanphamdodacotronggiohangchua.Quantity += quantity; } } else { byte calculatedQuantity = 0; bienkiemtraxemcodonhangchuathanhtoancocungmadonhang.First().Cart.OderDate = DateTime.Now; if (size == "small") { if (bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity; bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity -= quantity; } } else if (size == "large") { if (bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity; bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity -= quantity; } } else { if (bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity; bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity -= quantity; } } if (calculatedQuantity > 0) { _context.CartDetailTable.Add(new CartDetail { Product = bienkiemtraxemcosanphamtrongdb, Quantity = calculatedQuantity, Cart = bienkiemtraxemcodonhangchuathanhtoancocungmadonhang.First().Cart, Size = size, CartID = bienkiemtraxemcodonhangchuathanhtoancocungmadonhang.First().CartID }); } else { _context.CartDetailTable.Add(new CartDetail { Product = bienkiemtraxemcosanphamtrongdb, Quantity = quantity, Cart = bienkiemtraxemcodonhangchuathanhtoancocungmadonhang.First().Cart, Size = size, CartID = bienkiemtraxemcodonhangchuathanhtoancocungmadonhang.First().CartID }); } } _context.SaveChanges(); return(true); } else { string guidKey = Guid.NewGuid().ToString(); Cart cartDetail = new Cart { CartID = guidKey, UserID = cookieKey, OderDate = DateTime.Now }; CartDetail detail; byte calculatedQuantity = 0; if (size == "small") { if (bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity; bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity -= quantity; } } else if (size == "large") { if (bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity; bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity -= quantity; } } else { if (bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity; bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity -= quantity; } } if (calculatedQuantity > 0) { detail = new CartDetail { Product = bienkiemtraxemcosanphamtrongdb, Quantity = calculatedQuantity, Size = size, CartID = guidKey, Cart = cartDetail }; } else { detail = new CartDetail { Product = bienkiemtraxemcosanphamtrongdb, Quantity = quantity, Size = size, CartID = guidKey, Cart = cartDetail }; } _context.CartDetailTable.Add(detail); _context.SaveChanges(); return(true); } } return(false); }