//
        // GET: /Inventory/Create
        public ActionResult TakeInventory()
        {
            InventoryContext db = new InventoryContext();
            TakeInventoryVM vm = new TakeInventoryVM();
            Inventory takeInventory = new Inventory();

            //Determine if an inventory exists. Create one if it doesn't.
            if (db.Inventories.All(x => x.Completed)) {

                takeInventory.Date = DateTime.Now;

                db.Inventories.Add(takeInventory);
                db.SaveChanges();

                vm.InventoryId = takeInventory.Id;
            }

            else {
                var inventories = db.Inventories.Select(x => x).ToList();
                var inv = inventories.LastOrDefault();
                vm.InventoryId = inv.Id;
            }

            //Get list of tests to display on view
            var testsToInventory = (from x in db.Tests
                                   where x.Products.Count() > 0
                                   select x).OrderBy(x => x.Abbreviation).ToList();

            var invItems = (from i in db.InventoryItems
                            where i.InventoryId == vm.InventoryId
                            select i);

            var regularlyOrderedProducts = (from p in db.Products
                                           where p.RegularlyOrdered == true
                                           select p).ToList();

            foreach (var item in testsToInventory) {
                TakeInventoryVM.Test test = new TakeInventoryVM.Test();
                foreach (var p in item.Products) {
                    if (p.RegularlyOrdered == true) {
                        test.Id = item.Id;
                        test.TestName = item.Abbreviation;
                    }
                }
                vm.Tests.Add(test);
            }
            foreach (var item in vm.Tests) {
                //Determine how to set IsComplete property
                var invItemsForTest = (from i in invItems
                                       where i.Product.TestId == item.Id
                                       select i).ToList();

                var regOrderedByTest = (from p in regularlyOrderedProducts
                                        where p.TestId == item.Id
                                        select p).ToList();

                //For Test.IsComplete to be 'true' there must be the same number of InventoryItems as there are RegularlyOrderedProducts
                //    and their 'IsComplete' properties must all be set to 'true'
                if (invItemsForTest.Count() == regOrderedByTest.Count() && invItemsForTest.All(x => x.IsInventoried)) {
                    item.IsComplete = true;
                }
                else {
                    item.IsComplete = false;
                }
            }
            return View(vm);
        }
        //
        // GET: /Inventory/
        public ActionResult Index()
        {
            InventoryContext db = new InventoryContext();
            TakeInventoryVM vm = new TakeInventoryVM();
            vm.Inventories = db.Inventories
                .Include(i => i.InventoryItems)
                .OrderByDescending(x => x.Date)
                .ToList();

            //Check for any currently open inventories and set VM property as such
            //(Necessary to display proper link in index.cshtml
            if (db.Inventories.All(x => x.Completed))
            {
                vm.AllInventoriesComplete = true;
            }
            else
            {
                vm.AllInventoriesComplete = false;
            }

            return View(vm);
        }