/// <summary> /// Gets the delivery options applicable for each sales line level. /// </summary> /// <param name="request">The request.</param> /// <returns>The matching delivery options.</returns> private static GetLineDeliveryOptionsServiceResponse GetLineDeliveryOptions(GetLineDeliveryOptionsServiceRequest request) { if (request.SalesLines.IsNullOrEmpty()) { throw new NotSupportedException("A non-empty set of sales lines must be provided for computing line level delivery options"); } if (request.SalesLines.Where(sl => sl.ShippingAddress == null).Any()) { throw new NotSupportedException("The shipping address should be set on each requested line when fetching line level delivery options."); } var dataServiceRequest = new GetLineDeliveryOptionsDataRequest(request.SalesLines); dataServiceRequest.QueryResultSettings = request.QueryResultSettings; var dataServiceResponse = request.RequestContext.Execute <EntityDataServiceResponse <SalesLineDeliveryOption> >(dataServiceRequest); var deliveryOptions = dataServiceResponse.PagedEntityCollection; // Group all lines identifiers without an associated delivery option. var salesLinesWithoutDeliveryOption = new Collection <string>(); foreach (var saleLineDeliveryOption in deliveryOptions.Results) { if (!saleLineDeliveryOption.DeliveryOptions.Any()) { salesLinesWithoutDeliveryOption.Add(saleLineDeliveryOption.SalesLineId); } } if (salesLinesWithoutDeliveryOption.Any()) { // Raise notification of an anomaly. EmptyLineDeliveryOptionSetNotification notification = new EmptyLineDeliveryOptionSetNotification(salesLinesWithoutDeliveryOption); request.RequestContext.Notify(notification); } return(new GetLineDeliveryOptionsServiceResponse(deliveryOptions)); }
/// <summary> /// Executes the workflow to fetch line level delivery options for given cart. /// </summary> /// <param name="request">Instance of <see cref="GetDeliveryOptionsRequest"/>.</param> /// <returns>Instance of <see cref="GetDeliveryOptionsResponse"/>.</returns> protected override GetDeliveryOptionsResponse Process(GetDeliveryOptionsRequest request) { ThrowIf.Null(request, "request"); ValidateRequest(request); // Get the Sales Transaction SalesTransaction salesTransaction = CartWorkflowHelper.LoadSalesTransaction(this.Context, request.CartId); if (salesTransaction == null) { throw new CartValidationException(DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_CartNotFound, request.CartId); } GetDeliveryOptionsRequestHandler.ResetPreviousShippingAddressOnTransaction(salesTransaction); Collection <SalesLine> requestedSalesLines = null; // Used if line level delivery options were requested. if (!request.FetchDeliveryOptionsForLines) { salesTransaction.ShippingAddress = request.HeaderShippingAddress; } else { requestedSalesLines = new Collection <SalesLine>(); Dictionary <string, LineShippingAddress> shippingAddressByLineId = request.LineShippingAddresses.ToDictionary(lsa => lsa.LineId); foreach (var salesLine in salesTransaction.ActiveSalesLines) { LineShippingAddress lineShippingAddress; if (shippingAddressByLineId.TryGetValue(salesLine.LineId, out lineShippingAddress)) { salesLine.ShippingAddress = lineShippingAddress.ShippingAddress; requestedSalesLines.Add(salesLine); } } } // Validate and resolve addresses. ShippingHelper.ValidateAndResolveAddresses(this.Context, salesTransaction); // Get the delivery options. GetDeliveryOptionsResponse response; if (!request.FetchDeliveryOptionsForLines) { // Get the delivery options that are common to all the cart lines. var serviceRequest = new GetOrderDeliveryOptionsServiceRequest(salesTransaction); serviceRequest.QueryResultSettings = request.QueryResultSettings; var serviceResponse = this.Context.Execute <GetOrderDeliveryOptionsServiceResponse>(serviceRequest); response = new GetDeliveryOptionsResponse(serviceResponse.DeliveryOptions); } else { // Get the delivery options for each line. var serviceRequest = new GetLineDeliveryOptionsServiceRequest(requestedSalesLines); serviceRequest.QueryResultSettings = request.QueryResultSettings; var serviceResponse = this.Context.Execute <GetLineDeliveryOptionsServiceResponse>(serviceRequest); response = new GetDeliveryOptionsResponse(serviceResponse.LineDeliveryOptions); } return(response); }