public async Task<ActionResult> Create(Order order) { if (ModelState.IsValid) { order.OrderDate = DateTime.Now; var cart = ShoppingCart.GetCart(this.HttpContext); order.OrderDetails = new List<OrderDetail>(); order.OrderTotal = 0; foreach (var c in cart.GetCartItems()) { var art = db.Article.Where(a => a.ArticleId == c.Article.ArticleId).Single(); if (art.ArticleInStock - c.Count < 0) { return View("~/Views/ShoppingCart/FailedOrder.cshtml"); } order.OrderDetails.Add(new OrderDetail() { ArticleId = c.ArticleId, OrderId = order.OrderId, Quantity = c.Count, UnitPrice = c.Article.ArticlePrice }); order.OrderTotal += c.Article.ArticlePrice * c.Count; //OrderTotal ex VAT, else adjust here. art.ArticleInStock -= c.Count; } db.Order.Add(order); ShoppingCart.GetCart(this.HttpContext).EmptyCart(); await db.SaveChangesAsync(); return RedirectToAction("Details", "Orders", new { id = order.OrderId }); } return View(order); }
public async Task<ActionResult> Edit(Order order) { if (ModelState.IsValid) { db.Entry(order).State = EntityState.Modified; await db.SaveChangesAsync(); return RedirectToAction("Index"); } return View(order); }
public Order CreateOrder(Order order) { decimal orderTotal = 0; order.OrderDetails = new List<OrderDetail>(); var cartItems = GetCartItems(); foreach (var article in cartItems) { var orderDetail = new OrderDetail { ArticleId = article.ArticleId, OrderId = order.OrderId, UnitPrice = article.Article.ArticlePrice, Quantity = article.Count }; orderTotal += (article.Count * article.Article.ArticlePrice); order.OrderDetails.Add(orderDetail); DB.OrderDetails.Add(orderDetail); var art = DB.Article.Where(a => a.ArticleId == article.Article.ArticleId).Single(); art.ArticleInStock -= article.Count; if (art.ArticleInStock < 0) art.ArticleInStock = 0; } // Set the order's total to the orderTotal count order.OrderTotal = orderTotal; DB.SaveChanges(); EmptyCart(); // Return the OrderId as the confirmation number return order; }