/// <summary> /// Validates the items. /// </summary> private void ValidateItems() { var orderForms = CurrentOrderGroup.OrderForms.ToArray(); var lineItems = orderForms.SelectMany(x => x.LineItems.ToArray()); var validLineItems = lineItems.Where(x => !String.IsNullOrEmpty(x.CatalogItemId) && !x.CatalogItemId.StartsWith("@")).ToArray(); string catalogId = null; // get the store and compare if (validLineItems.Any()) { var store = new StoreClient(StoreRepository, CustomerSessionService, CacheRepository).GetStoreById(CurrentOrderGroup.StoreId); catalogId = store.Catalog; } if (!String.IsNullOrEmpty(catalogId)) { var catalogHelper = new CatalogClient(CatalogRepository, null, CustomerSessionService, CacheRepository, InventoryRepository); var catalogItemIds = (from i in validLineItems select i.CatalogItemId).ToArray(); var items = catalogHelper.GetItems(catalogItemIds); foreach (var lineItem in validLineItems) { var item = items.SingleOrDefault(i => i.ItemId.Equals(lineItem.CatalogItemId, StringComparison.OrdinalIgnoreCase)); if (item != null && catalogHelper.GetItemAvailability(item.ItemId, lineItem.FulfillmentCenterId).IsAvailable) { //Inventory Info if (item.TrackInventory) { var inventory = catalogHelper.GetItemInventory(lineItem.CatalogItemId, lineItem.FulfillmentCenterId); PopulateInventoryInfo(inventory, lineItem); } //Variation Info PopulateVariationInfo(item, lineItem); } else // remove item from the list { RemoveLineItem(lineItem); } } } else // since no catalog is associated with a store, remove all items { RemoveAllLineItems(); } }
private void AdjustStockItemQuantity(LineItem lineItem) { if (String.IsNullOrEmpty(lineItem.CatalogItemId) || lineItem.CatalogItemId.StartsWith("@")) { return; } var catalogHelper = new CatalogClient(CatalogRepository, null, CustomerSessionService, CacheRepository, InventoryRepository); var item = catalogHelper.GetItem(lineItem.CatalogItemId); if (item != null && item.TrackInventory) { var repo = InventoryRepository; var inventory = catalogHelper.GetItemInventory(lineItem.CatalogItemId, lineItem.FulfillmentCenterId, false); if (inventory != null) { if (AdjustStockInventoryQuantity(lineItem, inventory)) { repo.UnitOfWork.Commit(); } else { throw new InvalidWorkflowException(string.Format("Failed to adjust inventory for lineItem {0}", lineItem.LineItemId)); } } } }