public ActionResult AutoOrder(Order orderInput) { //orderInput.Date = DateTime.Now; //var inventory = from item in db.Inventories // where item.Id == 1 // select item; //db.Orders.Add(orderInput); List<OrderItem> orderItems = new List<OrderItem>(); Order order = new Order(); foreach (var item in orderInput.OrderItems) { if (item.Quantity > 0) { orderItems.Add(item); } } order.Date = DateTime.Now; order.OrderItems = orderItems; db.Orders.Add(order); db.SaveChanges(); return RedirectToAction("Index"); }
// // GET: /Order/AutoOrder public ActionResult AutoOrder() { //AutoOrderVM vm = new AutoOrderVM(); Order order = new Order(); order.OrderItems = new List<OrderItem>(); Inventory inventory = db.Inventories .Select(x => x) .ToList() .LastOrDefault(); inventory.InventoryItems = ( from invItem in inventory.InventoryItems join product in db.Products on invItem.ProductId equals product.Id select invItem).OrderBy(x => x.Product.Test.Abbreviation).ToList(); foreach (var item in inventory.InventoryItems) { OrderItem orderItem = new OrderItem { ProductId = item.ProductId, Quantity = item.Product.MaxInStock - item.Product.UnitsInStock, Product = item.Product, Order = order, OrderId = 0 }; if (orderItem.Quantity > 0) { order.OrderItems.Add(orderItem); } } foreach (var item in order.OrderItems) { order.TotalPrice = order.TotalPrice + item.Product.PricePerUnit * item.Quantity; } return View(order); }