Double Entry Accounting with Invoicing in Rails

You may remember a little post I made ages ago about Double Entry Accounting in Rails, in which I went over what one needed in order to make a simple double entry accounting system within the confines of the Ruby on Rails web development framework.

After a few requests by readers, I have decided to write the sequel. Double Entry Accounting with Invoicing in Rails.

To recap…

Last time I identified the following core tables you needed in your database:

  • Account (has many Postings)
  • Asset Type (e.g. £, $, monkeys)
  • Batch (has many Journals, links them into a group of transactions – though not really needed)
  • Journal (has many Postings, links them into a transaction)
  • Posting (associates with Account, Journal, and Asset Type)

Though I failed to elaborate on what you really needed to store in these tables. In truth, it can be as complicated or as simple as you’d like. No need for Batches? Then don’t include them.

As long as you at least store amounts in Posting’s, and practice the art of double entry accounting, you should be fine.

The next major point of note was how does one calculate the balances in the accounts? Well it turned out the solution was rather simple:

credit_amount = postings.sum(:value, :conditions => "amount < 0")
debit_amount = postings.sum(:value, :conditions => "amount > 0")

Coupled in with the balances were the amounts in the Postings, which had to be flipped depending on the type of account they resided in:

def real_amount
  return [:asset, :expense].include?(self.account.account_type) ? self.value : -self.value

def real_amount=(val)
  self.value = [:asset, :expense].include?(self.account.account_type) ? val : -val

In simpler terms: if an account is an asset or expense account, a positive value = a positive amount. Otherwise, a positive value = a negative amount. Vice versa.

So how do we factor invoicing into this?

There are two ways in which invoicing can be linked into a double entry accounting system:

  1. Link the payment of each invoice to transactions in the system
  2. Link the amount owed by each client for each invoice to transactions in the system

Realistically though, #1 is a fairly stupid idea as in the real world, clients don’t always pay on time. Or with the correct amount or order. Not only that, they can also pay off multiple invoices at once!

Which brings us to #2 – linking the amount owed by each client for each invoice into the system.

How do we do this? Well to summarise:

  • Have a revenue account which tracks how much you make.
  • Have a checking account which tracks what is in the bank.
  • Make an asset account for each client.
  • When you make out an invoice, record a transaction which transfers the total amount of the invoice from your revenue account into the client account.
  • Link any transaction(s) generated by the invoice to the invoice (so you can revert them later if required)
  • When the client pays, record a transaction which transfers the paid amount from the client account into your checking account.

Which might look like this in the system:

Account Debit Credit
Checking Accounts
Bank 100 0
Invoice Accounts
Microsoft 50 0
Apple 50 0
Revenue Accounts
Revenue 0 200


  • For tax purposes, your revenue account will at least sum up to the total amount in all of your invoices.
  • If the client’s account has a positive balance, they owe you money for any one of your invoices.
  • If the client’s account has a negative balance, they’ve obviously overpaid you. So go party!!

… which more or less concludes this article. Happy accounting!