/// <summary>
 /// Deprecated Method for adding a new object to the Inventories EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToInventories(Inventory inventory)
 {
     base.AddObject("Inventories", inventory);
 }
        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();
        }
 /// <summary>
 /// Create a new Inventory object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 public static Inventory CreateInventory(global::System.Int64 id)
 {
     Inventory inventory = new Inventory();
     inventory.Id = id;
     return inventory;
 }
        /// <summary>
        /// Add an inventory item given an item model if it doesn't exist
        /// Reactivates it if deactivated
        /// </summary>
        /// <param name="model"></param>
        /// <returns>The manipulated/created inventory entry</returns>
        private Inventory AddToInventory(ItemModel model)
        {
            JourListDMContainer dm = new JourListDMContainer();
            var member = dm.Members.SingleOrDefault(z => z.Name == User.Identity.Name);

            if (member == null) return null;

            // If inventory already exists but is inactive, reactivate
            // If it exists and is active, then just return the inventory item, do nothing
            Inventory inv;
            if ((inv = member.Inventories.FirstOrDefault(z=>z.Item.Id == model.Id)) != null)
            {
                if (inv.Active == false)
                    inv.Active = true;
                else return inv;
            }

            // Add an inventory listing if it doesn't exist.
            else
            {
                inv = new Inventory();
                inv.Item = dm.Items.Single(z=>z.Id == model.Id);
                inv.RestockThreshold = model.RestockThreshold;
                inv.OnHand = model.OnHand;
                inv.RequiredQuantity = model.RequiredQuantity;
                inv.Unit = dm.Units.Single(z => z.Id == model.UnitId);
                inv.Member = member;
                dm.AddToInventories(inv);
            }

            dm.SaveChanges();

            return inv;
        }