public OrderController(WildeRoverMgmtAppContext context, UserManager <User> userManager) { _userManager = userManager; _context = context; //Get latest OrderSummary (TODO: change to also query for user as well) var current = (from o in _context.OrderLog.Include("OrderList.Item") orderby o.Date descending select o).FirstOrDefault(); //If there is no uncompleted order if (current == null || current.Completed) { //Create new OrderSummary current = new OrderSummary(); current.Completed = false; current.Date = DateTime.Now; _context.OrderLog.Add(current); _context.SaveChanges(); current = (from o in _context.OrderLog.Include("OrderList.Item") orderby o.Date descending select o).FirstOrDefault(); } _order = current; }
//Create InventoryAreaLogs associated with new Inventory Summary private void CreateInventoryAreaLogs(InventorySummary summary) { var areas = from a in _context.InventoryAreas.Include("ItemSlots.WildeRoverItem") select a; foreach (var area in areas.ToList()) { //Create area inventory log InventoryAreaInventoryLog log = new InventoryAreaInventoryLog(); log.InventorySummary = summary; log.InventorySummaryId = summary.InventorySummaryId; log.InventoryArea = area; log.InventoryAreaid = area.InventoryAreaId; log.Date = DateTime.Now; //Add to context and retrieve Id _context.InventoryAreaLogs.Add(log); _context.SaveChanges(); log = _context.InventoryAreaLogs.Last(); //populate the Inventory var items = from i in area.ItemSlots orderby i.Slot ascending select i.WildeRoverItem; foreach (var item in items) { ItemCount temp = new ItemCount(); temp.InventoryAreaInventoryLog = log; temp.InventoryAreaInventoryLogId = log.InventoryAreaInventoryLogId; temp.Item = item; temp.WildeRoverItemId = item.WildeRoverItemId; _context.ItemCounts.Add(temp); //Add ItemCount to area log log.Inventory.Add(temp); } //Update log _context.InventoryAreaLogs.Update(log); //Add area inventory log to summary summary.InventoryAreaLogs.Add(log); } //Update summary _context.InventoryLog.Update(summary); //_context.SaveChanges(); }
public InventoryController(WildeRoverMgmtAppContext context, UserManager <User> userManager) { _userManager = userManager; _context = context; //Get latest inventory summary if it exists var current = (from i in _context.InventoryLog.Include("InventoryAreaLogs.Inventory.Item") orderby i.Date descending select i).FirstOrDefault(); //No inventory log created for the day if (current == null || current.Submitted) { _summary = new InventorySummary(); _summary.Date = DateTime.Now; _summary.Submitted = false; _context.InventoryLog.Add(_summary); _context.SaveChanges(); _summary = (from i in _context.InventoryLog.Include("InventoryAreaLogs.Inventory.Item") orderby i.Date descending select i).FirstOrDefault(); _context.InventoryLog.Update(_summary); CreateInventoryAreaLogs(_summary); _context.SaveChanges(); } else //Log exists { _summary = current; } }