/// <summary>
            /// Executes the workflow to fetch the invoice and convert to a cart.
            /// </summary>
            /// <param name="request">Instance of <see cref="RecallSalesInvoiceRequest"/>.</param>
            /// <returns>Instance of <see cref="RecallSalesInvoiceResponse"/>.</returns>
            protected override RecallSalesInvoiceResponse Process(RecallSalesInvoiceRequest request)
            {
                ThrowIf.Null(request, "request");

                // Get the invoices
                var realtimeRequest = new GetInvoiceRealtimeRequest(
                    string.Empty,
                    request.InvoiceId);

                GetInvoiceRealtimeResponse realtimeResponse = this.Context.Execute <GetInvoiceRealtimeResponse>(realtimeRequest);

                if (realtimeResponse.Order == null)
                {
                    throw new DataValidationException(DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_ObjectNotFound, string.Format("The sales invoice: {0} is not found.", request.InvoiceId));
                }

                SalesTransaction transaction = realtimeResponse.Order;

                // Update warehouse since the order is being returned in this store
                transaction.InventoryLocationId = this.Context.GetOrgUnit().InventoryLocationId;
                transaction.StoreId             = this.Context.GetOrgUnit().OrgUnitNumber;

                foreach (SalesLine salesLine in transaction.ActiveSalesLines)
                {
                    salesLine.InventoryLocationId = transaction.InventoryLocationId;
                    salesLine.FulfillmentStoreId  = transaction.StoreId;
                }

                // update the transaction id in sales order if the request contains transaction Id.
                if (!string.IsNullOrWhiteSpace(request.TransactionId))
                {
                    realtimeResponse.Order.Id = request.TransactionId;
                }

                Cart cart = CustomerOrderWorkflowHelper.SaveTransactionAndConvertToCart(this.Context, realtimeResponse.Order);

                return(new RecallSalesInvoiceResponse(cart));
            }
Ejemplo n.º 2
0
            /// <summary>
            /// Get invoices filtering by the request.
            /// </summary>
            /// <param name="request">The request containing the sales or invoice id.</param>
            /// <returns>The response containing the invoices.</returns>
            private static GetInvoiceRealtimeResponse GetInvoices(GetInvoiceRealtimeRequest request)
            {
                if (request == null)
                {
                    throw new ArgumentNullException("request");
                }

                if (string.IsNullOrWhiteSpace(request.SalesId) && string.IsNullOrWhiteSpace(request.InvoiceId) && string.IsNullOrWhiteSpace(request.CustomerAccountNumber))
                {
                    throw new ArgumentException("CustomerAccount, SalesId and InvoiceId must be set in request", "request");
                }

                // Calls TS API
                ReadOnlyCollection <object> transactionResponse;

                if (!string.IsNullOrWhiteSpace(request.CustomerAccountNumber))
                {
                    // if a Customer Account was specified, then get all invoices for the given customer.
                    transactionResponse = new TransactionServiceClient(request.RequestContext).GetSalesInvoices(request.CustomerAccountNumber);
                }
                else
                {
                    // Else, get the specific invoice(s) for the given sales/invoice id.
                    transactionResponse = new TransactionServiceClient(request.RequestContext).GetSalesInvoices(request.SalesId, request.InvoiceId);
                }

                SalesInvoice[] invoices = null;
                SalesOrder     order    = null;

                if (!string.IsNullOrWhiteSpace(request.CustomerAccountNumber))
                {
                    // If CustomerAccount was specified, parse the list of invoices for that customer.
                    try
                    {
                        invoices = InvoiceHelper.GetInvoicesFromArray(transactionResponse);
                    }
                    catch (XmlException ex)
                    {
                        RetailLogger.Log.CrtTransactionServiceInvoiceXmlDocumentCreationFailure(request.CustomerAccountNumber, request.SalesId, request.InvoiceId, ex);
                    }
                }
                else if (string.IsNullOrWhiteSpace(request.InvoiceId))
                {
                    // If a sales order id was specified, parse the list of invoices from that sales order.
                    try
                    {
                        invoices = InvoiceHelper.GetInvoicesFromXml(transactionResponse[0].ToString());
                    }
                    catch (XmlException ex)
                    {
                        RetailLogger.Log.CrtTransactionServiceInvoiceXmlDocumentCreationFailure(request.CustomerAccountNumber, request.SalesId, request.InvoiceId, ex);
                    }
                }
                else
                {
                    // Otherwise, parse the invoice details from the single given invoice id.
                    try
                    {
                        order = InvoiceHelper.GetSalesOrderFromXml(transactionResponse[0].ToString(), request.RequestContext);
                    }
                    catch (XmlException ex)
                    {
                        RetailLogger.Log.CrtTransactionServiceInvoiceXmlDocumentCreationFailure(request.CustomerAccountNumber, request.SalesId, request.InvoiceId, ex);
                    }

                    // Check that the channel currency code is the same as the recalled order
                    if (order != null && !string.IsNullOrWhiteSpace(order.CurrencyCode) && !request.RequestContext.GetChannelConfiguration().Currency.Equals(order.CurrencyCode, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new DataValidationException(
                                  DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_CurrencyChannelOrderMismatch,
                                  string.Format("Channel currency = {0} doesn't match sales order currency = {1}", request.RequestContext.GetChannelConfiguration().Currency, order.CurrencyCode));
                    }
                }

                return(new GetInvoiceRealtimeResponse(invoices.AsPagedResult(), order));
            }