private void UpdateShoppingList(Inventory inventory) { JourListDMContainer dm = new JourListDMContainer(); var inv = dm.Inventories.Single(z=>z.Id == inventory.Id); bool needmore, needlist; // deal with shopping list if necessary needmore = inv.OnHand < inv.RestockThreshold; needlist = inv.ShoppingList == null; if (needmore) { if (needlist) { // Createlist ShoppingList list = new ShoppingList(); do { list.Id = Guid.NewGuid(); } while (dm.ShoppingLists.Count(z => z.Id == list.Id) > 0); dm.AddToShoppingLists(list); inv.ShoppingList = list; } var log = inv.InventoryLogs.Where(z => z.UnitId == inv.Unit.Id); if (log.Count() > 0) { double sze = (from a in log group a by a.Size into s orderby s.Count() descending select s).First().First().Size; inv.ShoppingList.SizeNeeded = sze; inv.ShoppingList.QuantityNeeded = Math.Round((inv.RequiredQuantity - inv.OnHand) / sze, MidpointRounding.AwayFromZero); } else { inv.ShoppingList.SizeNeeded = (inv.RequiredQuantity - inv.OnHand); inv.ShoppingList.QuantityNeeded = 1; } } else if (!needlist) // Remove the item from the shoppinglist dm.ShoppingLists.DeleteObject(dm.Inventories.First(z => z.ShoppingList.Id == inv.ShoppingList.Id).ShoppingList); dm.SaveChanges(); }