The Scientific & General Agencies is a lab chemicals distributor in Jaipur. They sell lab chemicals and equipment by catalogue number, and the whole business runs on one chain. A lead becomes a quotation, then an order, then one or more dispatches, an invoice for each dispatch, and a payment. I rebuilt the ERP they run that chain on, on my own, through Horizon Beam, in seven weeks.
Most of an ERP is screens and forms, and most of the scope was exactly that: CRM and leads, orders, procurement, inventory, accounting, tickets and reports. This post is about a part nobody sees, which is making sure a number means the same thing everywhere it appears.
The rule the business needed
An invoice total has to equal its lines. That sounds too obvious to write down until you follow the number. The same total has to reach the GST report and the Tally export their accountant imports. Tax splits into CGST and SGST, or IGST, depending on where the goods go. An order ships in parts, and each dispatch gets an invoice that bills only that dispatch.
Every one of those is a place where a total can drift away from the lines it came from. A rounding done one way on the order and another way on the invoice, or a discount applied to the lines on one screen and to the total on another. Once a wrong total is stored, every report that reads it is wrong too, and someone has to find it by hand.
So the first constraint I wrote down was that the money has to reconcile, everywhere, every time.
Whole paise and one module
Every amount in the system is an integer number of paise. Never a float, and never rupees with decimals. Floats are fine for a lot of things and wrong for money, because tiny rounding errors pile up across lines, discounts and tax, and the pile ends up on a document someone signs.
All the arithmetic on money goes through one module. The quotation, the order, the invoice, the credit note, the vendor PO and every export read their totals from the same code, so none of them can round in its own way. Grand total and amount due are derived from the lines. Nobody types them in.
Every write goes through a guard like this one, which refuses anything that isn't a whole number of paise:
export type Paise = number;
export function assertPaise(value: number, label = 'amount'): Paise {
if (!Number.isFinite(value)) throw new Error(`${label} is not finite: ${value}`);
if (!Number.isInteger(value)) throw new Error(`${label} must be integer paise, got ${value}`);
if (!Number.isSafeInteger(value)) throw new Error(`${label} exceeds safe integer range: ${value}`);
return value;
}Turning paise into rupees happens only at the edge, when a page or a PDF shows the number. Storage never sees a rupee amount.
Making MongoDB behave like an accounting database
My first spec asked for PostgreSQL. The client wanted to stay on MongoDB Atlas, so I kept MongoDB and made it behave like an accounting database instead. The ledger, stock allocation and document numbering run inside transactions on a replica set, so a save either lands whole or doesn't land.
Before every save, the totals are recomputed from the lines. If what's about to be stored doesn't match what the lines add up to, the save is rejected with an error that names both numbers. The same check sits in the save hooks of the documents themselves, so a total that slips past one layer meets it again at the next. There's also a scheduled job written to recheck every financial document, which switches on with a paid hosting plan.
Order status follows the same idea. It's worked out from the line quantities on every change, what was ordered, dispatched and invoiced, so two screens can't show different answers about the same order. Nobody sets it by hand, so nobody can forget to.
Where the same idea shows up again
Corrections follow a similar rule. GST doesn't allow editing an issued tax invoice, so only drafts can be edited and everything else goes through a credit note. I wrote that up in GST invoices you can't edit. Costing uses weighted average cost, still waiting on the client's final yes, and it's read from one function so FIFO stays reachable. The reasons are in weighted average cost vs FIFO.
If I had to pull one thing out of this build for anyone writing finance features, it's this: decide where a number is allowed to be created, and make every other place read it. Here that was one module for the arithmetic and one rule at save time. Everything else, the PDFs, the GST report, the Tally export, only ever reads.
The lab distributor ERP case study has the full scope, including what's still open. If your business runs on a chain like this one, the ERP and CRM service is where I'd start.