In September 2026 we found crumbs in a customer's warehouse. Not real crumbs. Mathematical ones.
The customer sold tea by the carton and by the single pack, sixty packs to a carton. KashBoss stored stock in cartons, so selling one pack meant deducting one sixtieth of a carton. The database kept three decimal places, and fractions like that do not fit. Take a simpler carton of twelve: one piece is 0.08333 of a carton, with threes going on forever, and it was saved as 0.083. Sell twelve single pieces and you deduct 0.996 cartons, not one. A phantom 0.004 of a carton stays behind. At that customer, 719 of 794 lines sold by the piece carried a tiny rounding difference.
The crumbs looked harmless until they broke something. When a warehouse transfer had to split its quantity across several of these tiny leftover batches, one piece rounded down to 0.000, broke a rule in the database, and the whole transfer failed. We tried rounding, more decimal places and sweeping the crumbs away. None of it was the real fix.
The short answer
Store every quantity in the smallest unit you sell, such as pieces or grams, and convert to the bigger unit (cartons, kilos) only when you display it. Multiplication is always exact, while division like 1 divided by 12 never ends, so storing the big unit guarantees rounding crumbs. Back that with a movement ledger, where every stock change is a row, so the stored stock can always be checked against the sum of its movements.
Stock is a ledger, not a number
Beginners store stock as one number per product: "40 cartons". It works until someone asks "why is it 40?" and nobody can answer.
The better model is a movement ledger, like a bank statement. Every change is a row: purchase +50, sale -8, transfer out -2, count adjustment +1. Current stock is the sum of the rows. You may keep the current number for speed, but then you have an invariant, a rule that must always be true: the stored stock in each warehouse equals the sum of its movements. If they ever disagree, something wrote one without the other.
Each warehouse or branch has its own stock of each product, and every check must happen at that level. A sale from Warehouse A must check Warehouse A, not the company total.
Store small, show big
When the crumbs appeared we first treated the symptoms. We taught transfers to skip cost layers too small to matter, and on September 1, 2026 we widened 33 quantity columns from three decimals to six. The phantom leftover dropped from more than a pack per carton to less than a hundredth of one. Better, but not zero, because 1 divided by 12 has no exact decimal however many places you keep.
The real fix was to invert the unit: store pieces, show cartons. We built a database function that converts one product to the new model. It touches eleven tables, among them stock, movements, batches, invoice lines, prices and barcodes. We tested it on 462 real products inside a transaction that undoes itself, and every value matched to the cent. New two-unit products now use the new model, while existing ones keep theirs until converted on purpose.
Display is where it gets dangerous. Showing "493 shrink-packs" instead of "5,916 bottles" needs helper functions that convert to the big unit. Those helpers must never be used inside the cart's arithmetic, or the invoice gets multiplied by the unit factor, which is 1,000 for grams and kilos. We added an automatic test that fails if a display helper ever appears in the cashier's calculation code.
In September 2026 I opened a product and asked: what is this "5 tons"? It was a cheese sold by the kilo and stored in grams, and its low-stock alert had been set to 5,000 grams. The product screen filled in the alert box before it filled in the "units per kilo" box, and the conversion helper read the boxes live. While filling, the factor was 1, so 5,000 grams displayed as "5000 kilos". At saving time the factor was 1,000, so the alert was saved as five million grams. The same trap applied to the stock quantity box. The AI had introduced it a few days earlier while adding big-unit display. Luckily every product in the new model still had zero stock, so no real quantity was damaged.
The lesson: any calculation that runs while a form is being filled must read units from the saved product, never from boxes on the screen that may not be filled yet. Order of filling is not a guarantee.
A related disaster happened in July 2026, when a customer changed the base unit of 48 products from piece to carton and the stored quantities were not divided. Overnight, stock across seven branches was inflated four to twenty-four times. We repaired it with a full backup, a dry run that undid itself, and one transaction that rebuilt each product's stock by replaying its movements. Today, changing the unit of an existing product asks for confirmation first. This is the prompt that starts the whole conversion:
Variants, batches and transfers carry their own rules
A shoe comes in sizes and colors. Each combination is a variant with its own stock, barcode and often cost. The parent product is only a template, and its stock is the sum of its variants. We agreed four decisions before building: a generic attribute model, stock per variant per branch, cost per variant, and a parent with no stock number of its own.
The next day, testing showed the module everyone forgot. A coffee product with variants showed 20 in the warehouse view and "system: 10" on a new stock count, because the stock count module still read the parent's old number. No count had been confirmed, so nothing was damaged, and the fix shipped the same day. The rule: when you add a new dimension to stock, list every module that reads or writes stock and tick them off one by one.
Cost has to travel too. Food and medicine come in batches with expiry dates, and each batch is also a cost layer with its own purchase cost. In August 2026 we found that our transfer function deducted cost layers from the source warehouse every time, but created the matching layer at the destination only when the batch had an expiry date or a batch number. Most batches had neither, so for almost every transfer the quantity arrived and the cost evaporated. The fix was in the database function itself: the destination layer is now always created.
Counts and negative stock
A confirmed stock count taught us not to update two related numbers with two different rules. Ours set the warehouse stock to the counted number (an absolute value) but changed the company total by the difference from the number recorded when the sheet was opened. Anything sold during the count made the two diverge permanently, and cashiers kept selling because the guard checked the warehouse. Use the difference in both places, or recompute both from the ledger.
On negative stock, decide once and enforce everywhere. We closed four doors: a guard that checked the company total instead of the warehouse, manual quantity changes that gave no visible refusal, offline selling that checked only at upload, and canceling purchases whose goods were already sold. A guard without a visible message is worse than none, because people trust the word "saved".
Key takeaways
- Treat stock like a bank statement: every change is a row, and the balance must equal the sum.
- Store the smallest unit, show the biggest. Division creates crumbs; multiplication never does.
- Adding variants, batches or units touches every module. Make the list before building.
- Cost must travel with goods in transfers, or profit reports lose their meaning.
- Every refusal needs a visible message.
Chapter 16 of the book includes the movement-ledger scan we ran across the whole database and its checklist.
Frequently asked questions
Should I store stock in cartons or pieces?
Store the smallest unit you sell, usually pieces, and display cartons by multiplying and dividing only on screen. Stored cartons force division, and fractions such as one twelfth cannot be saved exactly, so tiny errors pile up.
What is a stock movement ledger?
It is a list where every stock change is its own row: purchases, sales, transfers and count adjustments. Current stock is the sum of the rows, so you can always explain why a number is what it is and detect when the stored number has drifted.
How do I stop my app from selling stock I do not have?
Check stock at the warehouse level inside the database, not only on the screen, and make every refusal visible to the user. Cover every path: online, offline, manual changes and purchase cancellations.