/// <summary> /// For all sales lines on the transaction, retrieve the product rec id if it's not already set. /// </summary> /// <param name="pricingDataManager">Provides data access to the calculation.</param> /// <param name="priceContext">Price context.</param> /// <param name="salesLines">Sales lines.</param> private static void PopulateProductIds(IPricingDataAccessor pricingDataManager, PriceContext priceContext, IEnumerable <SalesLine> salesLines) { var itemVariantIds = new HashSet <ItemVariantInventoryDimension>(); foreach (var line in salesLines) { if ((line.Variant == null || line.Variant.DistinctProductVariantId == 0) && !string.IsNullOrWhiteSpace(line.InventoryDimensionId)) { var itemVariantId = new ItemVariantInventoryDimension(line.ItemId, line.InventoryDimensionId); itemVariantIds.Add(itemVariantId); } } // We make a single database call to retrieve all variant identifiers that we need // and create a map using the ItemVariantInventoryDimension as its key. var variantsMap = new Dictionary <ItemVariantInventoryDimension, ProductVariant>(); if (itemVariantIds.Any()) { variantsMap = ((IEnumerable <ProductVariant>)pricingDataManager.GetVariants(itemVariantIds)).ToDictionary(key => new ItemVariantInventoryDimension(key.ItemId, key.InventoryDimensionId)); } // Consider calculable lines only. Ignore voided or return-by-receipt lines. foreach (var line in salesLines) { if (line.MasterProductId == 0) { Item item = PriceContextHelper.GetItem(priceContext, line.ItemId); line.MasterProductId = (item != null) ? item.Product : 0L; if (item != null && string.IsNullOrWhiteSpace(line.OriginalSalesOrderUnitOfMeasure)) { line.OriginalSalesOrderUnitOfMeasure = item.SalesUnitOfMeasure; } } if ((line.Variant == null || line.Variant.DistinctProductVariantId == 0) && !string.IsNullOrWhiteSpace(line.InventoryDimensionId)) { ProductVariant variant; var itemVariant = new ItemVariantInventoryDimension(line.ItemId, line.InventoryDimensionId); if (variantsMap.TryGetValue(itemVariant, out variant)) { line.Variant = variant; } } if (line.ProductId == 0) { line.ProductId = line.Variant != null ? line.Variant.DistinctProductVariantId : line.MasterProductId; } } }
private static int GetMaxLinesPerItem(IEnumerable <SalesLine> salesLines) { Dictionary <ItemVariantInventoryDimension, int> linesByItem = new Dictionary <ItemVariantInventoryDimension, int>(); foreach (SalesLine salesLine in salesLines) { int linesPerItem; ItemVariantInventoryDimension item = salesLine.GetItemVariantInventoryDimension(); if (!linesByItem.TryGetValue(item, out linesPerItem)) { linesByItem.Add(item, linesPerItem); } linesByItem[item] = linesPerItem + 1; } return(linesByItem.Values.Max()); }
private static void SetSalesLineInventoryForShipping(RequestContext context, IEnumerable <SalesLine> salesLines, IEnumerable <ItemAvailability> itemAvailabilities, Dictionary <string, decimal> salesLineInventoryQuantities) { ILookup <ItemVariantInventoryDimension, ItemAvailability> itemAvailabilityLookup = itemAvailabilities.ToLookup(itemAvailability => itemAvailability.GetItem()); ILookup <ItemVariantInventoryDimension, SalesLine> salesLineLookup = salesLines.ToLookup(salesLine => salesLine.GetItemVariantInventoryDimension()); foreach (IGrouping <ItemVariantInventoryDimension, SalesLine> grouping in salesLineLookup) { ItemVariantInventoryDimension item = grouping.Key; SortedSet <ItemAvailability> itemAvailabilitySortedSet = new SortedSet <ItemAvailability>(itemAvailabilityLookup[item], new ItemAvailabilityByQuantityDescendingComparer()); foreach (SalesLine salesLine in grouping.OrderByDescending(salesLine => salesLineInventoryQuantities[salesLine.LineId])) { ItemAvailability itemAvailability = itemAvailabilitySortedSet.FirstOrDefault(); if (SetSalesInventory(context, salesLine, itemAvailability, salesLineInventoryQuantities)) { // Remove and add to force sort the set if available quantity is reduced. itemAvailabilitySortedSet.Remove(itemAvailability); itemAvailabilitySortedSet.Add(itemAvailability); } } } }
private static Dictionary <ItemVariantInventoryDimension, ItemQuantity> GetSalesLineItemQuantities(IEnumerable <SalesLine> salesLines, Dictionary <string, decimal> salesLineInventoryQuantities) { Dictionary <ItemVariantInventoryDimension, ItemQuantity> itemQuantities = new Dictionary <ItemVariantInventoryDimension, ItemQuantity>(); foreach (SalesLine salesLine in salesLines) { ItemQuantity itemQuantity; ItemVariantInventoryDimension item = salesLine.GetItemVariantInventoryDimension(); if (!itemQuantities.TryGetValue(item, out itemQuantity)) { itemQuantity = new ItemQuantity() { ItemId = item.ItemId, VariantInventoryDimensionId = item.VariantInventoryDimensionId, }; itemQuantities.Add(item, itemQuantity); } itemQuantity.Quantity += salesLineInventoryQuantities[salesLine.LineId]; } return(itemQuantities); }