/// <summary>
            /// Gets the delivery options that are applicable to entire SalesTransaction i.e., common for all the sales lines.
            /// </summary>
            /// <param name="request">The request.</param>
            /// <returns>The matching delivery options.</returns>
            private static GetOrderDeliveryOptionsServiceResponse GetOrderDeliveryOptions(GetOrderDeliveryOptionsServiceRequest request)
            {
                if (request.SalesTransaction == null)
                {
                    throw new NotSupportedException("Sales transaction on the request cannot be null.");
                }

                if (request.SalesTransaction.ShippingAddress == null)
                {
                    throw new NotSupportedException("The shipping address on the sales transaction cannot be null if order level delivery options are being fetched.");
                }

                // Consider active lines only. Ignore voided lines.
                var salesLines = request.SalesTransaction.ActiveSalesLines;

                foreach (SalesLine salesLine in salesLines)
                {
                    salesLine.ShippingAddress = request.SalesTransaction.ShippingAddress;
                }

                var dataServiceRequest = new GetLineDeliveryOptionsDataRequest(salesLines);

                dataServiceRequest.QueryResultSettings = QueryResultSettings.AllRecords;
                var dataServiceResponse = request.RequestContext.Execute <EntityDataServiceResponse <SalesLineDeliveryOption> >(dataServiceRequest);

                IEnumerable <DeliveryOption> deliveryOptions = null;

                if (dataServiceResponse != null)
                {
                    var salesLineDeliveryOptions = dataServiceResponse.PagedEntityCollection.Results;
                    deliveryOptions = GetCommonDeliveryOptions(salesLineDeliveryOptions);
                }

                // Raise notification if no common delivery options were found.
                if (deliveryOptions == null || !deliveryOptions.Any())
                {
                    var notification = new EmptyOrderDeliveryOptionSetNotification(request.SalesTransaction.Id);
                    request.RequestContext.Notify(notification);
                }

                return(new GetOrderDeliveryOptionsServiceResponse(deliveryOptions.AsPagedResult()));
            }
Exemple #2
0
            /// <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);
            }