/// <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));
            }