Context: C#, Xero.Api
I'm having an interesting time with Invoices. I have written a bit of C# code to accept a query string and a filename from the command line. I run the query against Xero's endpoint and store the results in the file. So far so good.
However, I've found the I get anomalous results in certain circumstances.
I'm using the following C# code:
private static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Syntax:\n\tXero \"<querystring>\" <outputspec>");
Environment.Exit(1);
}
var query = args[0];
var fspec = args[1];
Console.WriteLine($"{query}\n{fspec}");
// Private Application Sample
var xeroCoreApi = new XeroCoreApi("https://api.xero.com",
new PrivateAuthenticator(<not shown>),
new Consumer(<not shown>, <not shown>),
null,
new DefaultMapper(),
new DefaultMapper());
var org = xeroCoreApi.Organisation;
var user = new ApiUser { Name = Environment.MachineName };
var invoices = xeroCoreApi
.Invoices
.Where(query)
.Find();
var total = invoices.Count();
var page = 2;
StringBuilder sb = new StringBuilder();
sb.Append("<Invoices>");
if (invoices.Any())
{
foreach (Invoice I in invoices)
{
GatherInvoiceDetails(sb, I);
}
total = invoices.Count();
while (invoices.Count() == 100)
{
invoices = xeroCoreApi.Invoices.Page(page++).Find();
if (invoices.Any())
{
total += invoices.Count();
foreach (Invoice I in invoices)
{
GatherInvoiceDetails(sb, I);
}
}
}
}
sb.AppendLine("</Invoices>");
Console.WriteLine($"pages: {page-1}. invoices: {total}");
System.IO.File.WriteAllText(fspec, sb.ToString());
}
I have a batch file containing the following:
@echo off
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == ""AUTHORISED""" \temp\authorised.xml
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == ""DELETED""" \temp\deleted.xml
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == ""DRAFT""" \temp\draft.xml
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == ""PAID""" \temp\paid.xml
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == ""VOIDED""" \temp\voided.xml
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14)" \temp\all.xml
Running the batch gives:
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == "AUTHORISED"
\temp\authorised.xml
pages: 1. invoices: 32
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == "DELETED"
\temp\deleted.xml
pages: 1. invoices: 0
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == "DRAFT"
\temp\draft.xml
pages: 1. invoices: 2
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == "PAID"
\temp\paid.xml
pages: 34. invoices: 3339
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == "VOIDED"
\temp\voided.xml
pages: 1. invoices: 26
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14)
\temp\all.xml
pages: 34. invoices: 3339
Note that the "all.xml" output stores 3339 invoices. The "paid.xml" also stores 3339 invoices. And if you do the arithmetic, you'll notice that the voided, paid, draft, deleted and authorised add up to a number greater than 3339.
This is a bit weird. Any ideas?
P.S.
It's also quite weird that, in all.xml, there are items in there with a DueDate outside the requested range, e.g.
<DueDate>7/22/2016 12:00:00 AM</DueDate>
P.P.S.
Maybe I just need to bypass the Xero filtering and do my own, viz
var everything = new System.Collections.Generic.List<Xero.Api.Core.Model.Invoice>();
...
foreach (Invoice I in invoices)
{
everything.Add(I);
}
...
IEnumerable<Invoice> some = everything
.Where(i => i.DueDate >= new DateTime(2017, 08, 01)
&& i.DueDate < new DateTime(2017, 09, 13)
&& i.Status == Api.Core.Model.Status.InvoiceStatus.Paid);