Most apps do not start with a ledger. They start with an orders table and a balance column, and that works until the first refund, the first partial payment, or the first time two requests update the same balance at once. Then the numbers drift, and nobody can say why.

We have built money features for school fees, cooperative deposits, payouts and our own office accounts. These are the rules we now follow every time. None of them are original; Square, Modern Treasury and Martin Fowler have written about each of them in depth (links below).

1. Model events as balanced journal entries

Two tables: a journal entry (the event: when, what, why, who) and its lines (account, debit, credit). An entry is only valid when its debits equal its credits. Check it in application code, and if your database allows it, again with a constraint or a check at commit time.

journal_entry(id, date, description, source_type, source_id, created_by, created_at)
journal_line(id, entry_id, account_id, debit, credit, party_id)

2. Store money as integers in the smallest unit

৳1,234.50 is 123450 poisha. Floating point cannot represent most decimal amounts exactly, and the errors are tiny until they are not. If you need several currencies, store the currency code on every line and never add amounts in different currencies together.

3. Never update or delete a posted entry

A ledger is append-only. If an entry was wrong, post a reversing entry with the same lines swapped, then post the correct one. You keep the full story: what was believed, when it was corrected, and by whom. Square's Books service is built entirely on this idea of immutability.

4. Balances are derived, not stored as truth

An account's balance is the sum of its lines. Caching it is fine for speed, but the cache must always be rebuildable from the lines. If the two ever disagree, the lines win.

5. Make every write idempotent

Payment webhooks arrive twice. Mobile apps retry. Give every money-moving request an idempotency key, store it with the entry, and refuse a second entry with the same key. This one rule prevents most double-charges we have ever seen.

6. Link every entry to what caused it

Store the source (invoice, payment, payout) on the entry. Months later, when someone asks "why is this ৳3,000 here?", the answer is one click away. It also lets you check that every paid invoice has exactly one payment entry.

7. Keep an audit trail that cannot be quietly changed

Record who did what and when, outside the ledger itself. In our own office app every command is written to an audit log where each row includes a hash of the previous one, so a changed or deleted row breaks the chain and is noticed.

A worked example

A client pays an invoice of ৳1,00,000; the bank deducts ৳150. In poisha:

EntryAccountDebitCredit
#1042bank9985000
#1042bank_fees15000
#1042receivable10000000

Debits 10,000,000, credits 10,000,000. If the webhook fires again with the same idempotency key, nothing is written. If the client later disputes the fee, a new entry explains it; #1042 never changes.

When to use an existing tool

For a company's own accounts, an accounting product (Odoo, or a plain-text tool such as Beancount) is usually the right answer. Build your own ledger when money movement is your product: wallets, marketplaces, payouts, member deposits. In that case, these seven rules are the foundation.

Tricky cases, and what we would do

Two requests withdraw from the same wallet at the same moment

Lock the account row (or use a serializable transaction) while you check the balance and write the entry. Check, then write, inside one transaction. Otherwise both see ৳500 and both spend it.

A webhook says “paid”, but the bank later reverses it

Never edit the first entry. Post a reversal with a link to the gateway’s reversal reference. The customer’s history then shows both events, which is exactly what support needs.

You need to change how a fee is calculated

Old entries keep the old fee. Version the rule (store which rule produced each line) and apply the new one from a date. Rewriting history to match the new rule is how ledgers stop matching bank statements.

Building a finance system? Talk to us

At MHD Soft we build finance and accounting software with two kinds of people in the room: developers, and people who have done the accounts themselves, in agencies, factories, NGOs and shops. Their field experience decides how a screen should work; their feedback goes back into every release. That is how we keep systems correct (double-entry underneath, VAT and AIT handled, bKash and bank payments matched) while keeping the price within reach of a growing Bangladeshi business.

If you have a case that does not fit your current software, tell us about it. Odd cases are where we learn the most.

References and further reading

  1. Books: an immutable double-entry accounting database service — Square
  2. Accounting for developers, part I — Modern Treasury
  3. Accounting Patterns — Martin Fowler
  4. Double-entry accounting for software engineers — Balanced
  5. Plain Text Accounting