private static ProductPrice GetItemPrice(RequestContext requestContext, string itemId, string inventDimId, string unitOfMeasure, string customerAcctNumber) { SalesTransaction salesTransaction = new SalesTransaction { Id = Guid.NewGuid().ToString(), CustomerId = customerAcctNumber, }; SalesLine salesLine = new SalesLine() { LineId = Guid.NewGuid().ToString("N"), ItemId = itemId, InventoryDimensionId = inventDimId, SalesOrderUnitOfMeasure = unitOfMeasure, Quantity = 1m, }; salesTransaction.SalesLines.Add(salesLine); GetIndependentPriceDiscountServiceRequest priceRequest = new GetIndependentPriceDiscountServiceRequest(salesTransaction); GetPriceServiceResponse pricingServiceResponse = requestContext.Execute <GetPriceServiceResponse>(priceRequest); SalesLine resultLine = pricingServiceResponse.Transaction.SalesLines[0]; ProductPrice productPrice = GetProductPrice( resultLine.ItemId, resultLine.InventoryDimensionId, resultLine.BasePrice, resultLine.AgreementPrice, resultLine.AdjustedPrice, requestContext.GetChannelConfiguration().Currency); return(productPrice); }
/// <summary> /// Executes the workflow for a get price check for a product. /// </summary> /// <param name="request">The request.</param> /// <returns>The response.</returns> protected override PriceCheckResponse Process(PriceCheckRequest request) { ThrowIf.Null(request, "request"); ItemBarcode itemBarcode = null; if (string.IsNullOrEmpty(request.Barcode) && string.IsNullOrEmpty(request.ItemId)) { throw new DataValidationException(DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_ItemIdBarcodeMissing, "Either an item identifier or barcode is required."); } if (string.IsNullOrEmpty(request.ItemId)) { GetProductBarcodeDataRequest dataRequest = new GetProductBarcodeDataRequest(request.Barcode); itemBarcode = this.Context.Runtime.Execute <GetProductBarcodeDataResponse>(dataRequest, this.Context).Barcode; } SalesTransaction salesTransaction = new SalesTransaction() { Id = Guid.NewGuid().ToString(), CustomerId = request.CustomerAccountNumber, }; SalesLine salesLine = new SalesLine() { ItemId = request.ItemId, InventoryDimensionId = request.InventoryDimensionId ?? itemBarcode.InventoryDimensionId, SalesOrderUnitOfMeasure = request.UnitOfMeasureSymbol ?? itemBarcode.UnitId, Quantity = 1m, LineId = Guid.NewGuid().ToString() }; salesTransaction.SalesLines.Add(salesLine); GetIndependentPriceDiscountServiceRequest priceRequest = new GetIndependentPriceDiscountServiceRequest(salesTransaction); GetPriceServiceResponse pricingServiceResponse = this.Context.Execute <GetPriceServiceResponse>(priceRequest); SalesLine resultLine = pricingServiceResponse.Transaction.SalesLines[0]; ProductPrice productPrice = GetProductPrice( resultLine.ItemId, resultLine.InventoryDimensionId, resultLine.BasePrice, resultLine.TotalAmount, this.Context.GetChannelConfiguration().Currency); var productPrices = new List <ProductPrice> { productPrice }; return(new PriceCheckResponse(productPrices.AsPagedResult())); }
/// <summary> /// Calculates price and discount independently for each item. /// </summary> /// <param name="request">The pricing request.</param> /// <returns>The response object.</returns> private static GetPriceServiceResponse CalculateIndependentPriceAndDiscount(GetIndependentPriceDiscountServiceRequest request) { PricingDataServiceManager pricingDataManager = new PricingDataServiceManager(request.RequestContext); DateTimeOffset today = request.RequestContext.GetNowInChannelTimeZone(); Customer customer = Discount.GetCustomer(request.RequestContext, request.Transaction.CustomerId); PE.PriceContext priceContext = PE.PriceContextHelper.CreatePriceContext( request.RequestContext, pricingDataManager, request.Transaction, new ChannelCurrencyOperations(request.RequestContext), today, request.Transaction.CustomerId, customer != null ? customer.PriceGroup : string.Empty, customer != null ? customer.LineDiscountGroup : string.Empty, customer != null ? customer.MultilineDiscountGroup : string.Empty, customer != null ? customer.TotalDiscountGroup : string.Empty, request.Transaction.IsTaxIncludedInPrice, PricingCalculationMode.Independent, DiscountCalculationMode.CalculateOffer, request.CalculateForNewSalesLinesOnly, request.NewSalesLineIdSet); PE.PricingEngine.CalculatePricesForSalesLines(request.Transaction.PriceCalculableSalesLines, priceContext, pricingDataManager); PE.PricingEngine.CalculateDiscountsForLines(pricingDataManager, request.Transaction, true, priceContext); return(new GetPriceServiceResponse(request.Transaction)); }
/// <summary> /// Executes the workflow to retrieve the prices and calculated discount amount for the given product identifiers. /// </summary> /// <param name="request">The request.</param> /// <returns>The response.</returns> protected override GetIndependentProductPriceDiscountResponse Process(GetIndependentProductPriceDiscountRequest request) { ThrowIf.Null(request, "request"); ProductSearchCriteria productSearchCriteria = new ProductSearchCriteria( request.Context.ChannelId.GetValueOrDefault(), request.Context.CatalogId.GetValueOrDefault()) { DataLevel = CommerceEntityDataLevel.Standard, Ids = request.ProductIds.ToList() }; ProductSearchResultContainer productSearchResult = request.RequestContext.Runtime.Execute <ProductSearchServiceResponse>( new ProductSearchServiceRequest(productSearchCriteria, request.QueryResultSettings), request.RequestContext).ProductSearchResult; List <ProductPrice> productPrices = new List <ProductPrice>(request.ProductIds.Count()); // Create sales line for every product id there is in the request. List <SalesLine> salesLines = new List <SalesLine>(request.ProductIds.Count()); foreach (var product in productSearchResult.Results) { if (product.IsMasterProduct) { foreach (var variant in product.GetVariants()) { if (request.ProductIds.Contains(variant.DistinctProductVariantId)) { salesLines.Add(new SalesLine { ItemId = product.ItemId, Variant = variant, InventoryDimensionId = variant.InventoryDimensionId, SalesOrderUnitOfMeasure = product.Rules.DefaultUnitOfMeasure, LineId = System.Guid.NewGuid().ToString("N"), Quantity = 1, ProductId = variant.DistinctProductVariantId, CatalogId = request.Context.CatalogId.GetValueOrDefault() }); } } } else { salesLines.Add(new SalesLine { ItemId = product.ItemId, SalesOrderUnitOfMeasure = product.Rules.DefaultUnitOfMeasure, LineId = System.Guid.NewGuid().ToString("N"), Quantity = 1, ProductId = product.RecordId, CatalogId = request.Context.CatalogId.GetValueOrDefault() }); } } // Set the catalog ids on the sales lines. if (request.Context.CatalogId != null) { if (request.Context.CatalogId.Value > 0) { // If a specific catalogId is set on the context, add it to the catalogIds on the sales lines. foreach (var sl in salesLines) { sl.CatalogIds.Add(request.Context.CatalogId.Value); } } else { GetProductCatalogAssociationsDataRequest productCatalogAssociationRequest = new GetProductCatalogAssociationsDataRequest(salesLines.Select(p => p.ProductId)) { QueryResultSettings = QueryResultSettings.AllRecords }; ReadOnlyCollection <ProductCatalogAssociation> productCatalogs = request.RequestContext.Runtime.Execute <GetProductCatalogAssociationsDataResponse>( productCatalogAssociationRequest, request.RequestContext).CatalogAssociations; // If catalogId is 0, add all independent catalogs to the catalogIds on the sales lines. foreach (var sl in salesLines) { sl.CatalogIds.UnionWith(productCatalogs.Where(pc => pc.ProductRecordId == sl.ProductId).Select(pc => pc.CatalogRecordId)); } } } Collection <SalesAffiliationLoyaltyTier> affiliations = null; if (request.AffiliationLoyaltyTiers != null) { affiliations = new Collection <SalesAffiliationLoyaltyTier>((from alt in request.AffiliationLoyaltyTiers select new SalesAffiliationLoyaltyTier { AffiliationId = alt.AffiliationId, AffiliationType = alt.AffiliationType, LoyaltyTierId = alt.LoyaltyTierId, ReasonCodeLines = alt.ReasonCodeLines, CustomerId = alt.CustomerId, ChannelId = request.Context.ChannelId.GetValueOrDefault() }).ToList()); } SalesTransaction transaction = new SalesTransaction { SalesLines = new Collection <SalesLine>(salesLines), CustomerId = request.CustomerAccountNumber, AffiliationLoyaltyTierLines = affiliations }; // Calculate prices and discounts for sales lines GetIndependentPriceDiscountServiceRequest itemPriceDiscountServiceRequest = new GetIndependentPriceDiscountServiceRequest(transaction); GetPriceServiceResponse itemPriceServiceResponse = this.Context.Execute <GetPriceServiceResponse>(itemPriceDiscountServiceRequest); Dictionary <long, SalesLine> salesLineDictionary = itemPriceServiceResponse.Transaction.SalesLines.ToDictionary(sl => sl.ProductId); foreach (long productId in request.ProductIds) { SalesLine salesLine; if (!salesLineDictionary.TryGetValue(productId, out salesLine)) { salesLine = new SalesLine(); } ProductPrice productPrice = new ProductPrice { UnitOfMeasure = salesLine.SalesOrderUnitOfMeasure, ItemId = salesLine.ItemId, InventoryDimensionId = salesLine.InventoryDimensionId, BasePrice = salesLine.BasePrice, TradeAgreementPrice = salesLine.AgreementPrice, AdjustedPrice = salesLine.AdjustedPrice, DiscountAmount = salesLine.DiscountAmount, ProductId = productId, ChannelId = request.Context.ChannelId.GetValueOrDefault(), CatalogId = request.Context.CatalogId.GetValueOrDefault() }; productPrices.Add(productPrice); } return(new GetIndependentProductPriceDiscountResponse(productPrices.AsPagedResult())); }