Skip to content

Instantly share code, notes, and snippets.

@mdornseif
Created March 30, 2011 09:39
Show Gist options
  • Save mdornseif/894134 to your computer and use it in GitHub Desktop.
Save mdornseif/894134 to your computer and use it in GitHub Desktop.
Find Invoices to get a Payment reminder
def get_invoices(datum):
"""Returns a dict if invoices generated before `datum` and unpayed."""
# Create your consumer with the proper key/secret.
consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
client = oauth.Client(consumer, token=oauth.Token(key=CONSUMER_KEY, secret=CONSUMER_SECRET))
client.set_signature_method(XeroOAuthSignatureMethod_RSA_SHA1())
urlbase = 'https://api.xero.com/api.xro/2.0/Invoices'
datum = datum.strftime('%Y-%m-%dT00:00:00')
query = 'Date<=DateTime.Parse("%s")&&Status=="AUTHORISED"&&AmountPaid==0' % datum
url = "%s?%s&order=DueDate" % (urlbase, urllib.urlencode({'where': query}))
resp, content = client.request(url, "GET")
xml = ET.fromstring(content)
invoices = {}
for i in xml.getiterator('Invoice'):
ref = i.findtext('Reference')
if ref and (ref.startswith('WL') or ref.startswith('SF')):
invoice = {}
for attr in '''Reference Date DueDate Status LineAmountTypes SubTotal TotalTax Total UpdatedDateUTC
CurrencyCode InvoiceID InvoiceNumber AmountDue AmountPaid'''.split():
invoice[attr] = i.find(attr).text.strip()
invoices[ref.strip()] = invoice
return invoices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment