public void AllUsersStores()
 {
     InventoryRequest request = new InventoryRequest();
     request.StoreNumber = 998; // A store that exists but is not in the user's store list
     request.StoreNumberType = StoreNumberType.AllUsersStores;
     User user = this.DomainContext.Users.Single(u => u.Username == "jmclachl");
     var stores = request.GetStoreNumbersToFind(this.DomainContext, user);
     Assert.AreEqual(3, stores.Count());
     Assert.IsFalse(stores.Contains(request.StoreNumber));
 }
 public void AllStoresWithStoreThatDoesntExist()
 {
     InventoryRequest request = new InventoryRequest();
     request.StoreNumber = 999; // A store that doesn't exist
     request.StoreNumberType = StoreNumberType.AllStores;
     User user = this.DomainContext.Users.Single(u => u.Username == "jmclachl");
     var stores = request.GetStoreNumbersToFind(this.DomainContext, user);
     Assert.AreEqual(5, stores.Count());
     Assert.IsFalse(stores.Contains(request.StoreNumber));
 }
 public void SingleStore()
 {
     InventoryRequest request = new InventoryRequest();
     request.StoreNumber = 45; // A store that in the user's list
     User user = this.DomainContext.Users.Single(u => u.Username == "jmclachl");
     var stores = request.GetStoreNumbersToFind(this.DomainContext, user);
     Assert.AreEqual(1, stores.Count());
     Assert.IsTrue(stores.Contains(request.StoreNumber));
 }
 public void TestRequest()
 {
     InventoryRequest request = new InventoryRequest();
     request.ProductId = 99;
     request.ProductIdType = ProductIdType.AllUsersProducts;
     request.StoreNumber = 45;
     request.StoreNumberType = StoreNumberType.AllUsersStores;
     User user = this.DomainContext.Users.Single(u => u.Username == "jmclachl");
     var products = request.GetProductIdsToFind(user);
     Assert.AreEqual(3, products.Count());
     var stores = request.GetStoreNumbersToFind(this.DomainContext, user);
     Assert.AreEqual(3, stores.Count());
 }
        public List<StoreWithInventory> GetInventoriesWithRequest(InventoryRequest request)
        {
            List<StoreWithInventory> result = new List<StoreWithInventory>();
            // The result dictionary - key is the store ID, value is the store
            Dictionary<int, StoreWithInventory> resultStores = new Dictionary<int, StoreWithInventory>();

            using (traceManager.StartTrace("General"))
            {
                var userEntity = this.GetUserEntity();
                var productsToFind = request.GetProductIdsToFind(userEntity);

                // Get the results from the LCBO website
                List<LcboInventoryData2> lcboInventory = this.lcboService.RetrieveMatches(productsToFind);

                // Detect new and changed stores
                this.DetectNewAndChangedStores(lcboService, lcboInventory);

                // Get the store Ids
                var storeids = request.GetStoreNumbersToFind(this.domainContext, userEntity);

                //TODO: filter by store/product selection

                Dictionary<int, ProductListItem> productDictionary = new Dictionary<int, ProductListItem>();

                // The LCBO results filtered by the stores in the request
                var lcboInventoriesFiltered = from l in lcboInventory where storeids.Contains(l.StoreNumber) select l;

                foreach (var lcboInventoryItem in lcboInventoriesFiltered)
                {
                    StoreWithInventory currentStoreWithInventory;
                    if (resultStores.ContainsKey(lcboInventoryItem.StoreNumber))
                    {
                        // The current store is in the result dictionary, so set currentStore to it.
                        currentStoreWithInventory = resultStores[lcboInventoryItem.StoreNumber];
                    }
                    else
                    {
                        // The current store is not in the result dictionary.
                        // Set currentStore to be a new store
                        var Store = this.domainContext.Stores.Single(s => s.Id == lcboInventoryItem.StoreNumber);
                        var store = new InvStore()
                        {
                            Address = Store.Address,
                            City = Store.City,
                            Hours = Store.Hours,
                            Latitude = Store.Latitude,
                            Longitude = Store.Longitude,
                            StoreNumber = Store.Id
                        };

                        currentStoreWithInventory = new StoreWithInventory() { Store = store };
                        resultStores.Add(lcboInventoryItem.StoreNumber, currentStoreWithInventory);
                    }

                    // Inventory.CreateInventory(lcboInventoryItem.StoreNumber, lcboInventoryItem.ProductId);
                    // Set its level to that of the lcbo result value
                    ProductListItem productsVm = null;
                    if (productDictionary.ContainsKey(lcboInventoryItem.ProductId))
                    {
                        // Current product ID is in the dictionary, so set currentProduct to it
                        productsVm = productDictionary[lcboInventoryItem.ProductId];
                    }
                    else
                    {
                        // Current product ID is not in the dictionary
                        var product = this.domainContext.Products.Where(p => p.Id == lcboInventoryItem.ProductId).Include("Substance").Include("Container").First();
                        productsVm = new ProductListItem() { Name = product.Name };
                        productDictionary.Add(lcboInventoryItem.ProductId, productsVm);
                    }

                    // Create an inventory item
                    InventoryInfo currentInventory = new InventoryInfo
                    {
                        NumberInStock = lcboInventoryItem.NumberInStock,
                        Product = productsVm
                    };

                    // Set the current inventory's product to a new product based on the current product
                    // Attach the inventory item to the current store.  So we have Store.Inventory.Product and Store.Inventory.InventoryLevel.
                    currentStoreWithInventory.Inventory.Add(currentInventory);
                }

            }

            result = new List<StoreWithInventory>(resultStores.Values);

            return result;
        }