Tech Blog :: Track Freshbooks Expenses in Google Docs with PHP and XML


Mar 24 '11 10:34am

Track Freshbooks Expenses in Google Docs with PHP and XML

I've been trying to automate as much of my financial forecasting as possible, with coding up front that will last a while. My primary tools are Freshbooks (for expense and invoice tracking) and Google Docs for spreadsheets. I wrote yesterday about pulling data from one spreadsheet into another using importRange. Last night I took it several steps further, pulling expenses from the Freshbooks API into XML, then XML to GDocs, and automating tax calculations based on expense category.

1. Freshbooks Expenses to XML

Building on an existing freshbooks-php library, I wrote a PHP script called freshbooks_expenses_xml. (Link goes to GitHub.)

To get it set up, create a keys.php file, and put the whole package on your server somewhere. Play with the parameters described in the readme to get different XML output.

2. XML to Google Docs

In cell A1 of a clean spreadsheet, enter this function:
=importXML("http://your-site.com/freshbooks-expenses/expenses.php?date_from=2011-01-01&date_to=2011-12-31&headers=1&", "//expenses/*").
GDocs will fetch the data and populate the spreadsheet. (Note: I had some trouble making the headers consistent with the columns, and worked around it; you might want to do the same by omitting headers=1 in the function and putting in your own.)

3. Making useful tax calculations with the data

For estimated quarterly taxes (as an LLC), I need to know my revenue (calculated in another spreadsheet, not yet but possibly soon also pulled automatically from Freshbooks) minus my business expenses. As I learned doing taxes for 2010, not all expense categories are equal: Meals & Entertainment, for example, is generally deducted at 50%, while others are 100%. This is easy to do with custom GDocs functions. Next to my expenses (pulled automatically), I have a column for Month, a column for Quarter (using a custom function), and a column for Deduction, using the amount and the category. (To write a custom function, go to Tools > Scripts > Script Editor.)

Finally, in my income sheet, I use sumif() on the range in the other [expenses] sheet with the calculated deductions for that quarter, times my expected tax rate, and I know how much quarterly taxes to pay!

(Update: A revised version of this post now appears on the FreshBooks Developer Blog.)