couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Metin Akat <>
Subject FIFO/LIFO accountancy
Date Wed, 03 Feb 2010 15:29:26 GMT

I am writing an accounting application in CouchDB..
Here is the problem I'm addressing:

I'll give a greatly simplified example, so that it's easier to track
what I'm talking about.

consider this:

1. We buy 5 apples, 2 dollars each for 10 dollars. We put them (or to
be more exact, their value) inside an account, which represents our
warehouse. The best way to do this in CouchDB is to record a DEBIT
operation to that warehouse. (Besides, a CREDIT operation takes place
in another account, but I'll skip that part for the sake of

{ "action": "DEBIT", "account" : "warehouse", "value": 10, "asset":
"apples", "qty": 5}

2. We buy another 5 apples, but this time 3 dollars each, for 15 dollars.

{ "action": "DEBIT", "account" : "warehouse", "value": 15, "asset":
"apples", "qty": 5}

3. We write a map/reduce view that calculates the balance of the
account. It will show that we have 10 apples at the cost of 25

4. Lets sell 6 apples to a customer, 5 dollars each. I won't go into
much detail on why this is required by accountancy standards but if we
use FIFO here, we must issue 2 transactions. One removing 5 apples at
2 dollars and another, removing 1 apple at 3 dollars. The profit is
recorded in another account where it's 5x3 = 15 for the first
transaction and 2x1=2 for the second transaction.

So, we need some way to figure out which apples we sell first. (how
much value to remove from the warehouse account). In order to do this
we need to know how much have we sold till the moment. And this needs
to be fault tolerant.
For example, if somebody (at some other cash register in the store)
sold 3 apples before our deal, our transactions would be quite a bit
different. What if this happened one milisecond before we sell our

Me and my colleagues are thinking of lots of different solutions (some
better and others worse), but I won't describe them here, as I'd like
to see some "fresh brain's" solution. Have you ever encountered such a
problem with couchdb?

View raw message