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.HasValue && !request.ItemCodes.Any())
            {
                throw new InvalidRequestException("ExigoService.GetItems() requires either a CategoryID or a collection of item codes.");
            }
            ;

            #region Categories
            // Set some defaults
            var topCategoryID = request.CategoryID ?? request.Configuration.CategoryID;

            var categoryIDs = new List <int>()
            {
                topCategoryID
            };

            // Get all category ids underneath the request's category id
            if (request.IncludeAllChildCategories)
            {
                var childCategoryIDs = GetChildCategoryIDs(topCategoryID);
                categoryIDs.AddRange(childCategoryIDs);
            }
            // if no categories, stop here (this should never happen. included for consistency)
            if (!categoryIDs.Any())
            {
                yield break;
            }
            #endregion

            #region Item Codes
            // If we requested specific categories, get the item codes in the categories
            var itemCodes    = new List <string>();
            var hasCartItems = request.ShoppingCartItems.Any();

            // if we have cart items, use those first
            if (hasCartItems)
            {
                var parentItems = request.ShoppingCartItems
                                  .ToList();
                var parentItemCodes = parentItems
                                      .Select(i => i.ItemCode)
                                      .Distinct()
                                      .ToList();
                itemCodes.AddRange(parentItemCodes);
            }
            // get and/or filter itemcodes
            else
            {
                var availableItemCodes = ExigoDAL.GetWebCategoryItemCodes(categoryIDs);
                if (request.ItemCodes.Any())
                {
                    // if we have no cart items, decide whether to restrict or not.
                    if (request.IgnoreCategoryRestrictions)
                    {
                        // If we're ignoring category restrictions, get the item codes requested only.
                        // (This should only be done for [dynamic/static] kit children. Items must still be available in the requested warehouse)
                        itemCodes.AddRange(request.ItemCodes);
                    }
                    else
                    {
                        // Default behavior: filter request item codes by the item codes available in the selected categories
                        var filteredItemCodes = availableItemCodes.Where(c => request.ItemCodes.Any(rc => rc == c));
                        itemCodes.AddRange(filteredItemCodes);
                    }
                }
                else
                {
                    // if no item codes are requested, add all available item codes
                    itemCodes.AddRange(availableItemCodes);
                }
            }

            // If we don't have any item codes, stop here. (This will happen if request.ItemCodes are not available in the categories requested)
            if (!itemCodes.Any())
            {
                yield break;
            }
            #endregion

            #region Items
            // get the item information
            var priceTypeID = (request.PriceTypeID.HasValue && request.PriceTypeID.Value > 0) ? request.PriceTypeID.Value : request.Configuration.PriceTypeID;
            var items       = GetItemInformation(new GetItemInformationRequest(request)
            {
                PriceTypeID = priceTypeID,
                ItemCodes   = itemCodes
            });
            // If we don't have any items, stop here.
            if (!items.Any())
            {
                yield break;
            }
            #endregion

            #region Additional Details
            // Populate the group members and dynamic kits
            PopulateAdditionalItemData(new PopulateAdditionalItemDataRequest(request)
            {
                Items          = items,
                WebCategoryIDs = categoryIDs
            });
            #endregion

            // if we provided shopping cart items, return our items relative to our shoppingCartItems.
            // (Note: This process will duplicate items if there were both order and autoorder items of the same code. This is necessary and expected.)
            if (hasCartItems)
            {
                items = MergeShoppingCartItemDetailsWith <Item>(request.ShoppingCartItems, items).ToList();
            }

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