public ActionResult AddProduct(AddProductView view) { var user = db.Users.Where(x => x.UserName == User.Identity.Name).FirstOrDefault(); if (ModelState.IsValid) { var orderDetailTemp = db.OrderDetailTemps.Where(o => o.UserName == User.Identity.Name && o.ProductID == view.ProductID).FirstOrDefault(); if (orderDetailTemp == null) { var product = db.Products.Find(view.ProductID); orderDetailTemp = new OrderDetailTemp { Description = product.Description, Price = product.Price, ProductID = product.ProductID, Quantity = view.Quantity, ImpuestoRate = product.Impuesto.Rate, UserName = User.Identity.Name, }; db.OrderDetailTemps.Add(orderDetailTemp); } else { orderDetailTemp.Quantity += view.Quantity; db.Entry(orderDetailTemp).State = EntityState.Modified; } db.SaveChanges(); return(RedirectToAction("Create")); } ViewBag.ProductID = new SelectList(comboHelper.GetProducts(user.CompanyID), "ProductID", "Description"); return(PartialView(view)); }
public async Task AddItemToOrderAsync(AddItemViewModel model, string userName, bool isReSeller) { var user = await _userHelper.GetUserByEmailAsync(userName); if (user == null) { return; } var product = await _context.Products.FindAsync(model.ProductId); if (product == null) { return; } float newPrice; if (isReSeller) { newPrice = product.Price * 0.8f; } else { newPrice = product.Price; } var orderDetailTemp = await _context.OrderDetailsTemp .Where(odt => odt.User == user && odt.Product == product) .FirstOrDefaultAsync(); if (orderDetailTemp == null) { orderDetailTemp = new OrderDetailTemp { Price = newPrice, Product = product, Quantity = model.Quantity, User = user }; _context.OrderDetailsTemp.Add(orderDetailTemp); } else { orderDetailTemp.Quantity += model.Quantity; _context.OrderDetailsTemp.Update(orderDetailTemp); } await _context.SaveChangesAsync(); }
// Método para agregar un item en una orden public async Task AddItemToOrderAsync(AddItemViewModel model, string userName) { // Se carga la entidad User mediante el método que valida y trae el usuario que se a logueado var user = await this.userHelper.GetUserByEmailAsync(userName); if (user == null) { return; } // Buscamos el ProductId en la entidad Products var product = await this.context.Products.FindAsync(model.ProductId); if (product == null) { return; } // Buscamos si ya existe el producto en la orden temporal del usuario var orderDetailTemp = await this.context.OrderDetailTemps .Where(odt => odt.User == user && odt.Product == product) .FirstOrDefaultAsync(); if (orderDetailTemp == null) { // Si no existe se crea un nuevo objeto OrderDetailTemp orderDetailTemp = new OrderDetailTemp { Price = product.Price, Product = product, Quantity = model.Quantity, User = user, }; // Se adiciona el item al contexto this.context.OrderDetailTemps.Add(orderDetailTemp); } else { // Si existe se adiciona la cantidad al item orderDetailTemp.Quantity += model.Quantity; // Se actualiza el cambio en el contexto this.context.OrderDetailTemps.Update(orderDetailTemp); } // Guardo los cambios del contexto a la base de datos await this.context.SaveChangesAsync(); }
//este metodo es para adicionar un producto a la tabla temporal public async Task AddItemToOrderAsync(AddItemViewModel model, string userName) {// el usuario existe? var user = await this.userHelper.GetUserByEmailAsync(userName); if (user == null) { return; } var product = await this.context.Products.FindAsync(model.ProductId); if (product == null) { return; } //si este usuario ya pidio el producto ... no saque otra linea con el mismo producto. // solo que aumente la cantidad en var orderDetailTemp = await this.context.OrderDetailTemps .Where(odt => odt.User == user && odt.Product == product) .FirstOrDefaultAsync(); if (orderDetailTemp == null) { orderDetailTemp = new OrderDetailTemp { Price = product.Price, Product = product, Quantity = model.Quantity, User = user, }; this.context.OrderDetailTemps.Add(orderDetailTemp); } else { orderDetailTemp.Quantity += model.Quantity; this.context.OrderDetailTemps.Update(orderDetailTemp); } await this.context.SaveChangesAsync(); }
public async Task AddItemToOrderAsync( AddItemViewModel _model, string _userName) { var user = await this.iUserHelper.GetUserByEmailAsync(_userName); if (user == null) { return; } var product = await this.context.Products.FindAsync(_model.ProductId); if (product == null) { return; } var orderDetailTemp = await this.context.OrderDetailTemps .Where(odt => odt.User == user && odt.Product == product) .FirstOrDefaultAsync(); if (orderDetailTemp == null) { orderDetailTemp = new OrderDetailTemp { Price = product.Price, Product = product, Quantity = _model.Quantity, User = user, }; this.context.OrderDetailTemps.Add(orderDetailTemp); } else { orderDetailTemp.Quantity += _model.Quantity; this.context.OrderDetailTemps.Update(orderDetailTemp); } await this.context.SaveChangesAsync(); }
public async Task AddItemToOrderAsync(AddItemViewModel model, string userName) { var user = await _userHelper.GetUserByEmailAsync(userName); if (user == null) { return; } var flight = await _context.Flights.FindAsync(model.FlightID); if (flight == null) { return; } var orderDetailTemp = await _context.OrderDetailTemps .Where(odt => odt.User == user && odt.Flight == flight) .FirstOrDefaultAsync(); if (orderDetailTemp == null) { orderDetailTemp = new OrderDetailTemp { Price = flight.Price, Flight = flight, Quantity = model.Quantity, User = user, }; _context.OrderDetailTemps.Add(orderDetailTemp); } else { orderDetailTemp.Quantity += model.Quantity; _context.OrderDetailTemps.Update(orderDetailTemp); } await _context.SaveChangesAsync(); }
public async Task AddItemToOrderAsync(AddItemViewModel model, string userName) { var user = await _userHelper.GetUserByEmailAsync(userName); if (user == null) { return; } var item = await _context.Items.FindAsync(model.ItemId); if (item == null) { return; } var orderDetailTemp = await _context.OrderDetailsTemp .Where(odt => odt.User == user && odt.Item == item) .FirstOrDefaultAsync(); if (orderDetailTemp == null) { orderDetailTemp = new OrderDetailTemp { Price = item.Price, Item = item, Quantity = model.Quantity, User = user, }; _context.OrderDetailsTemp.Add(orderDetailTemp); } else { orderDetailTemp.Quantity += model.Quantity; _context.OrderDetailsTemp.Update(orderDetailTemp); } await _context.SaveChangesAsync(); }
public ActionResult Index() { ViewBag.TopHotALL = _productService.ListTopHotALL(); ViewBag.productCategory = _productCategoryService.ListAll(); var list = new List <CartItem>(); try { HttpCookie Cookie = HttpContext.Request.Cookies[ListCart.CartCookie]; // lấy cookie if (Cookie != null) // check cookies có value nếu cookie not null { string ValueCookie = Server.UrlDecode(Cookie.Value); //Decode dịch ngược mã các ký tự đặc biệt tham khảo var cart = JsonConvert.DeserializeObject <List <CartItem> >(ValueCookie); // convert json to list object if (cart != null) { list = (List <CartItem>)cart; } int kk = 0; while (1 != 2) { kk++; HttpCookie cooki = HttpContext.Request.Cookies[kk.ToString()];// lấy cookie if (cooki != null) { string valueCookie = Server.UrlDecode(cooki.Value); //Decode dịch ngược mã các ký tự đặc biệt tham khảo var cart2 = JsonConvert.DeserializeObject <List <CartItem> >(valueCookie); // convert json to list object if (cart2 != null) { var list2 = (List <CartItem>)cart2; list.AddRange(list2); } } else { break; } } } } catch { } //////// // if (User.Identity.IsAuthenticated && list.Count != 0) { foreach (var lst in list) { var orderDetailTemp = _orderDetailTempService.ListAllByUserID(User.Identity.GetUserId()); var product = _productService.GetById(lst.Product.Id); if (orderDetailTemp == null) { OrderDetailTemp temp = new OrderDetailTemp(); temp.Id = Guid.NewGuid(); temp.UserID = User.Identity.GetUserId(); _orderDetailTempService.Create(temp); orderDetailTemp = _orderDetailTempService.ListAllByUserID(User.Identity.GetUserId()); } if (orderDetailTemp.CartContent == null) { var item = new CartItem(); item.Product = product; item.Quatity = lst.Quatity; var list2 = new List <CartItem>(); list2.Add(item); string jsonItem = JsonConvert.SerializeObject(list2, Formatting.Indented); var value = Server.UrlEncode(jsonItem); //Encode dịch mã các ký tự đặc biệt, từ string orderDetailTemp.CartContent = value; // cookie mới đè lên cookie cũ } else { string value = Server.UrlDecode(orderDetailTemp.CartContent); //Decode dịch ngược mã các ký tự đặc biệt tham khảo var cart = JsonConvert.DeserializeObject <List <CartItem> >(value); // convert json to list object var list2 = (List <CartItem>)cart; if (list2.Exists(x => x.Product.Id == product.Id)) { foreach (var item in list2) { if (item.Product.Id == product.Id) { item.Quatity += lst.Quatity; } } } else { var item = new CartItem(); item.Product = product; item.Quatity = lst.Quatity; list2.Add(item); } string jsonItem = JsonConvert.SerializeObject(list2, Formatting.Indented); OrderDetailTemp temp = new OrderDetailTemp(); temp.Id = _orderDetailTempService.ListAllByUserID(User.Identity.GetUserId()).Id; temp.UserID = User.Identity.GetUserId(); temp.CartContent = jsonItem; _orderDetailTempService.Update(temp); } } /////Xóa cooki /// int i = -1; while (1 != 2) { i++; HttpCookie cooki = HttpContext.Request.Cookies[i.ToString()];// lấy cookie if (cooki == null) { break; } //cooki.Expires = DateTime.Now.AddDays(-100); //HttpContext.Response.Cookies.Add(cooki); HttpContext.Response.Cookies[i.ToString()].Expires = DateTime.Now.AddDays(-1); } } return(View()); }
public ActionResult AddItem(Guid id, int quantity) { if (User.Identity.IsAuthenticated == false) { var product = _productService.GetById(id); int k = -1; var list3 = new List <CartItem>(); while (1 != 2) { k++; HttpCookie cooki = HttpContext.Request.Cookies[k.ToString()];// lấy cookie// convert json to list object if (cooki != null) { string ValueCookie = Server.UrlDecode(cooki.Value);//Decode dịch ngược mã các ký tự đặc biệt var cart = JsonConvert.DeserializeObject <List <CartItem> >(ValueCookie); var list = (List <CartItem>)cart.ToList(); if (list.Exists(x => x.Product.Id == id)) { foreach (var item in list) { if (item.Product.Id == product.Id) { item.Quatity += quantity; } list3.Add(item); } string jsonitem = JsonConvert.SerializeObject(list3, Formatting.Indented); //HttpCookie cookie = new HttpCookie(ListCart.CartCookie);// create cooki.Value = Server.UrlEncode(jsonitem); string b = cooki.Value; HttpContext.Response.Cookies[k.ToString()].Value = b; HttpContext.Response.Cookies[k.ToString()].Expires = DateTime.Now.AddHours(15); return(RedirectToAction("Index")); //int f = 0; //while (1 != 2) //{ // f++; // HttpCookie cookie = HttpContext.Request.Cookies[f.ToString()]; // if (cookie == null) // { // HttpCookie Cooki2 = new HttpCookie(f.ToString());// create // Cooki2.Expires = DateTime.Now.AddHours(15); // Cooki2.Value = Server.UrlEncode(jsonitem);//Encode dịch mã các ký tự đặc biệt, từ string // HttpContext.Response.Cookies.Add(Cooki2);// cookie mới đè lên cookie cũ // break; // } //} //return RedirectToAction("Index"); } } else { break; } } HttpCookie Cookie = HttpContext.Request.Cookies[ListCart.CartCookie];// lấy cookie if (Cookie != null) { string ValueCookie = Server.UrlDecode(Cookie.Value); //Decode dịch ngược mã các ký tự đặc biệt var cart = JsonConvert.DeserializeObject <List <CartItem> >(ValueCookie); // convert json to list object if (cart != null) { var list = (List <CartItem>)cart; var list2 = new List <CartItem>(); if (list.Exists(x => x.Product == product)) { foreach (var item in list) { if (item.Product == product) { item.Quatity += quantity; } } } else { var item = new CartItem(); item.Product = product; item.Quatity = quantity; list2.Add(item); } string jsonItem = JsonConvert.SerializeObject(list2, Formatting.Indented); //HttpCookie cookie = new HttpCookie(ListCart.CartCookie);// create int i = 0; while (1 != 2) { i++; HttpCookie cooki = HttpContext.Request.Cookies[i.ToString()]; if (cooki == null) { HttpCookie Cooki2 = new HttpCookie(i.ToString());// create Cooki2.Expires = DateTime.Now.AddHours(15); Cooki2.Value = Server.UrlEncode(jsonItem); //Encode dịch mã các ký tự đặc biệt, từ string HttpContext.Response.Cookies.Add(Cooki2); // cookie mới đè lên cookie cũ break; } } //cookie.Expires.AddDays(1); //cookie.Value = Server.UrlEncode(jsonItem);//Encode dịch mã các ký tự đặc biệt, từ string //HttpContext.Response.Cookies.Add(cookie);// cookie mới đè lên cookie cũ } } else { //Tạo mới CartItem var item = new CartItem(); item.Product = product; item.Quatity = quantity; var list = new List <CartItem>(); list.Add(item); string jsonItem = JsonConvert.SerializeObject(list, Formatting.Indented); HttpCookie cookie = new HttpCookie(ListCart.CartCookie);// create cookie.Expires = DateTime.Now.AddHours(15); cookie.Value = Server.UrlEncode(jsonItem); //Encode dịch mã các ký tự đặc biệt, từ string HttpContext.Response.Cookies.Add(cookie); // cookie mới đè lên cookie cũ } } else { var orderDetailTemp = _orderDetailTempService.ListAllByUserID(User.Identity.GetUserId()); var product = _productService.GetById(id); if (orderDetailTemp == null) { OrderDetailTemp temp = new OrderDetailTemp(); temp.Id = Guid.NewGuid(); temp.UserID = User.Identity.GetUserId(); _orderDetailTempService.Create(temp); orderDetailTemp = _orderDetailTempService.ListAllByUserID(User.Identity.GetUserId()); } if (orderDetailTemp.CartContent == null) { var item = new CartItem(); item.Product = product; item.Quatity = quantity; var list = new List <CartItem>(); list.Add(item); string jsonItem = JsonConvert.SerializeObject(list, Formatting.Indented); var value = Server.UrlEncode(jsonItem); //Encode dịch mã các ký tự đặc biệt, từ string orderDetailTemp.CartContent = value; // cookie mới đè lên cookie cũ } else { string value = Server.UrlDecode(orderDetailTemp.CartContent); //Decode dịch ngược mã các ký tự đặc biệt tham khảo var cart = JsonConvert.DeserializeObject <List <CartItem> >(value); // convert json to list object var list = (List <CartItem>)cart; if (list.Exists(x => x.Product.Id == product.Id)) { foreach (var item in list) { if (item.Product.Id == product.Id) { item.Quatity += quantity; } } } else { var item = new CartItem(); item.Product = product; item.Quatity = quantity; list.Add(item); } string jsonItem = JsonConvert.SerializeObject(list, Formatting.Indented); OrderDetailTemp temp = new OrderDetailTemp(); temp.Id = _orderDetailTempService.ListAllByUserID(User.Identity.GetUserId()).Id; temp.UserID = User.Identity.GetUserId(); temp.CartContent = jsonItem; _orderDetailTempService.Update(temp); } //string Value = Server.UrlDecode(orderDetailTemp.CartContent);//Decode dịch ngược mã các ký tự đặc biệt tham khảo //var cart = JsonConvert.DeserializeObject<List<CartItem>>(Value);// convert json to list object //list = (List<CartItem>)cart; } return(RedirectToAction("Index")); }
public JsonResult Update(string cartModel) { if (User.Identity.IsAuthenticated == false) { var jsonCart = new JavaScriptSerializer().Deserialize <List <CartItem> >(cartModel); int i = -1; while (1 != 2) { i++; HttpCookie cooki = HttpContext.Request.Cookies[i.ToString()];// lấy cookie if (cooki == null) { break; } string valueCookie = Server.UrlDecode(cooki.Value); //Decode dịch ngược mã các ký tự đặc biệt tham khảo var cart2 = JsonConvert.DeserializeObject <List <CartItem> >(valueCookie); // convert json to list object var list2 = (List <CartItem>)cart2; foreach (var item in list2) { var jsonItem = jsonCart.SingleOrDefault(x => x.Product.Id == item.Product.Id); if (jsonItem != null) { item.Quatity = jsonItem.Quatity; } } string jsonitem = JsonConvert.SerializeObject(list2, Formatting.Indented); cooki.Value = Server.UrlEncode(jsonitem); string b = cooki.Value; HttpContext.Response.Cookies[i.ToString()].Value = b; HttpContext.Response.Cookies[i.ToString()].Expires = DateTime.Now.AddHours(15); } //var cart = (List<CartItem>)Session[ListCart.CartSession]; //foreach (var item in cart) //{ // var jsonItem = jsonCart.SingleOrDefault(x => x.Product.Id == item.Product.Id); // if(jsonItem!=null) // { // item.Quatity = jsonItem.Quatity; // } //} //Session[ListCart.CartSession] = cart; } else { var jsonCart = new JavaScriptSerializer().Deserialize <List <CartItem> >(cartModel); try { var orderDetailTemp = _orderDetailTempService.ListAllByUserID(User.Identity.GetUserId()); string Value = Server.UrlDecode(orderDetailTemp.CartContent); //Decode dịch ngược mã các ký tự đặc biệt tham khảo var cart = JsonConvert.DeserializeObject <List <CartItem> >(Value); // convert json to list object var list = (List <CartItem>)cart; foreach (var item in list) { var jsonItem = jsonCart.SingleOrDefault(x => x.Product.Id == item.Product.Id); if (jsonItem != null) { item.Quatity = jsonItem.Quatity; } } string jsonitem = JsonConvert.SerializeObject(list, Formatting.Indented); OrderDetailTemp temp = new OrderDetailTemp(); temp.Id = _orderDetailTempService.ListAllByUserID(User.Identity.GetUserId()).Id; temp.UserID = User.Identity.GetUserId(); temp.CartContent = jsonitem; _orderDetailTempService.Update(temp); } catch { } } return(Json(new { status = true })); }
public JsonResult Delete(Guid id) { if (User.Identity.IsAuthenticated == false) { int i = -1; int thu = 0; while (1 != 2) { i++; HttpCookie cooki = HttpContext.Request.Cookies[i.ToString()];// lấy cookie if (cooki != null) { string valueCookie = Server.UrlDecode(cooki.Value); //Decode dịch ngược mã các ký tự đặc biệt tham khảo var cart2 = JsonConvert.DeserializeObject <List <CartItem> >(valueCookie); // convert json to list object var list2 = (List <CartItem>)cart2; if (cart2 != null) { HttpCookie cookiAfter = HttpContext.Request.Cookies[(i + 1).ToString()];// lấy cookie if (list2.Exists(x => x.Product.Id == id)) { if (cookiAfter == null) { HttpContext.Response.Cookies[i.ToString()].Expires = DateTime.Now.AddDays(-1); break; } else { thu = 3; } } if (thu == 3) { if (cookiAfter == null) { HttpContext.Response.Cookies[i.ToString()].Expires = DateTime.Now.AddDays(-1); break; } //cooki.Value = cookiAfter.Value; //HttpContext.Response.Cookies.Add(cooki); var a = cooki.Value; string b = cookiAfter.Value; HttpContext.Response.Cookies[i.ToString()].Value = b; HttpContext.Response.Cookies[i.ToString()].Expires = DateTime.Now.AddHours(15); } } } else { break; } } } else { try { var orderDetailTemp = _orderDetailTempService.ListAllByUserID(User.Identity.GetUserId()); string Value = Server.UrlDecode(orderDetailTemp.CartContent); //Decode dịch ngược mã các ký tự đặc biệt tham khảo var cart = JsonConvert.DeserializeObject <List <CartItem> >(Value); // convert json to list object var list = (List <CartItem>)cart; if (list.Exists(x => x.Product.Id == id)) { foreach (var item in list) { if (item.Product.Id == id) { list.Remove(item); break; } } } string jsonItem = JsonConvert.SerializeObject(list, Formatting.Indented); OrderDetailTemp temp = new OrderDetailTemp(); temp.Id = _orderDetailTempService.ListAllByUserID(User.Identity.GetUserId()).Id; temp.UserID = User.Identity.GetUserId(); temp.CartContent = jsonItem; _orderDetailTempService.Update(temp); } catch { } } return(Json(new { status = true })); }