Ejemplo n.º 1
0
        public static IEnumerable<Item> GetItems(GetItemsRequest request)
        {
            // If we don't have what we need to make this call, stop here.
            if (request.Configuration == null)
                throw new InvalidRequestException("ExigoService.GetItems() requires an OrderConfiguration.");

            if (request.Configuration.CategoryID == 0 && request.CategoryID == null && request.ItemCodes.Length == 0)
                throw new InvalidRequestException("ExigoService.GetItems() requires either a CategoryID or a collection of item codes."); ;

            // Set some defaults
            if (request.CategoryID == null && request.ItemCodes.Length == 0)
            {
                request.CategoryID = request.Configuration.CategoryID;
            }

            // Create the contexts we will use
            var context = Exigo.OData();

            // Determine how many categories we need to pull based on the levels. Currently designed to go one level deep.
            var categoryIDs = new List<int>();
            if (request.CategoryID != null)
            {
                categoryIDs.Add((int)request.CategoryID);

                if (request.IncludeChildCategories)
                {
                    // Get the child categories
                    var ids = context.WebCategories
                        .Where(c => c.WebID == 1)
                        .Where(c => c.ParentID == (int)request.CategoryID)
                        .Select(c => new
                        {
                            c.WebCategoryID
                        }).ToList();

                    categoryIDs.AddRange(ids.Select(c => c.WebCategoryID));
                }
            }

            // If we requested a specific category, get the item codes in the category
            if (categoryIDs.Count > 0)
            {
                var categoryItemCodes = context.WebCategoryItems
                    .Where(c => c.WebID == 1)
                    .Where(categoryIDs.ToOrExpression<WebCategoryItem, int>("WebCategoryID"))
                    .Select(c => new
                    {
                        c.Item.ItemCode
                    }).ToList();

                var existingItemCodes = request.ItemCodes.ToList();
                existingItemCodes.AddRange(categoryItemCodes.Select(c => c.ItemCode).Distinct().ToList());
                request.ItemCodes = existingItemCodes.ToArray();
            }

            // If we don't have any items, stop here.
            if (request.ItemCodes.Length == 0) yield break;

            // Get the data
            var query = context.ItemWarehousePrices.Expand("Item")
                    .Where(c => c.WarehouseID == request.Configuration.WarehouseID)
                    .Where(c => c.PriceTypeID == request.Configuration.PriceTypeID)
                    .Where(c => c.CurrencyCode == request.Configuration.CurrencyCode);

            if (request.ItemCodes != null && request.ItemCodes.Count() > 0)
            {
                query = query.Where(request.ItemCodes.ToList().ToOrExpression<ItemWarehousePrice, string>("Item.ItemCode"));
            }

            var odataItems = query.ToList();
            var items = odataItems.Select(c => (ExigoService.Item)c).ToList();

            // Populate the group members and dynamic kits
            PopulateGroupMembers(items, request.Configuration);
            PopulateDynamicKitMembers(items);

            // Return the data
            foreach (var item in items)
            {
                yield return item;
            }
        }