Mustafa Nevfel

Accounting systems

Invoices, ledgers and money arithmetic: correctness that has to hold for years, not just for the demo.

  1. 8 min read

    An invoice is a snapshot, not a calculation

    Why recomputing invoice totals from current rules breaks, and how to freeze integer minor-unit amounts, rule versions and document numbers in PostgreSQL.

Snippets

  1. Snippet · SQL

    Gapless invoice numbering in PostgreSQL without a sequence

    Increment a counter row with UPDATE ... RETURNING inside the issuing transaction: the row lock orders issuers and a rollback gives the number back.

  2. Snippet · SQL

    Make issued invoices immutable in PostgreSQL

    A BEFORE UPDATE OR DELETE trigger catches mistakes, privileges enforce it, and corrections are linked credit notes with a derived adjusted total.

  3. Snippet · TypeScript

    Store money as integer minor units and round tax per line or document

    Do the arithmetic in bigint minor units with an explicit tie rule. Five 1.10 lines at 8% tax to 0.45 per line or 0.44 per document, so store the scope.