In September 2026 a customer sent us a puzzled message. One purchase invoice had two payment vouchers for exactly the same amount, created 1.38 seconds apart. The supplier had been paid once. The cash box had been charged twice.
Someone had clicked "pay" twice, quickly, on the web. That alone should never cost money, so we traced the payment flow. It was five separate steps run from the app: read how much remains on the invoice, take the money out of the cash box, mark the invoice as paid, save the payment voucher, then update the supplier statement and the journal. Both clicks read the same "remaining" amount before either reached the step that marked the invoice paid. So both took money out of the cash box. Only one got through the next step.
The strangest part was why the steps were in that order. Two months earlier we had fixed the opposite bug: invoices marked "paid" while no money had left the cash box. The fix was to move money first. Fixing one direction had opened the other.
The short answer
Stop double payments by putting the whole payment into one database function with a request ID, and by adding a database rule that refuses any payment that would push the total paid above the invoice total. Reordering the steps only chooses which half can fail. A locked pay button helps the user, but only a rule inside the database protects you from every screen, device and double click.
Why reordering steps is never the real fix
A purchase invoice is the mirror of a sales invoice. You are the customer now. It has a supplier, lines with cost prices, discounts, tax, a total, the amount paid now and the amount you still owe. One purchase touches a lot: stock goes up, new cost layers open at the purchase cost, the supplier's balance rises if anything is unpaid, a cash box or bank goes down if you paid, and a journal entry is posted.
Now look at the payment of an existing invoice when it is a chain of separate calls.
In July 2026 a customer had a large purchase invoice showing "paid", while the supplier statement showed it fully owed, the journal entry said "on credit", and no money had left any cash box. Paying an existing invoice was four separate calls. The first marked the invoice as paid. The second saved the payment voucher. The third tried to take the money from the cash box and failed, because the box was empty. The fourth never ran. The first two were already saved. The user saw an error message, but the contradiction stayed in the data. The same pattern came back four times, through four different screens, before we closed it everywhere.
Our first fix reordered the steps: take the money first, mark the invoice paid only if that worked. It stopped "paid without money". It also created the double-click bug, because two clicks could now both take money before either one marked the invoice. The mobile app had a lock on the pay button. The web did not.
The real fix had three parts. A database trigger (a rule that runs automatically inside the database) now refuses any cash movement that would make the total paid exceed the invoice total, from any screen. The web button locks while a payment is running. And the duplicate vouchers already in the data were canceled, returning the money to the affected cash boxes.
The pattern: one function, one request ID
Sales invoices and stock vouchers in KashBoss carry a request ID, a unique tag created when the user presses the button. If the same request arrives twice, the database recognizes it and refuses the second. Any action that moves money should carry one. This is the prompt we use:
Keep the supplier statement honest
Every supplier has a statement: a running list of invoices, payments and returns, with the balance after each line. Pick a sign convention, write it down, and make every screen, import and repair follow it. In KashBoss a purchase or opening balance increases the balance (we owe more), and a payment or return decreases it.
Our statement bugs were all different ways for the detail and the total to drift apart:
- Written twice. Where a warehouse keeper confirms receipt and an accountant approves the money, a purchase was written to the supplier statement at both steps. The journal had a uniqueness rule, so it refused the second entry. The statement had no such rule and took both. If you protect the journal, protect the detailed records the same way.
- The wrong sign. An import from a customer's old system recorded opening balances on the wrong side. The balance column looked right, but the running balance was inverted for 35 suppliers. The app was fine; the import was wrong.
- Payments that did not come back. Canceling a payment restored the balance from the statement line it had created, but payments made through the edit screen changed the balance directly and left no line. Cancellation found nothing to reverse.
- A balance stuck in memory. The owner saw one balance in the suppliers list and a different one on the supplier's own page. The list had loaded once and never refreshed, because the live-update channel can drop silently. Lists now expire after 45 seconds, and opening a page refreshes its row.
To catch all of these at once, we ask for a read-only check:
Discounts, landed cost and edits
Suppliers give discounts per line and per invoice, and sometimes free bonus items. Where each lands is an owner decision, not a coding detail. Ours: the invoice discount goes to its own account and does not touch product costs; a line discount lowers that product's cost only; bonus items enter stock at zero cost. The AI explained the trade-off in plain words, I decided, and the decision was written down so nobody "fixes" it back.
The price on a supplier's invoice is rarely the full cost. Shipping and customs also have to be paid before goods are on the shelf, and adding them is called landed cost. We spread extra costs across products by value, so a product worth 30 percent of the invoice takes 30 percent of the extra cost. Skip it and your goods look cheaper than they are and your profit looks better than it is.
Finally, editing a purchase must not rewrite history. Our old edit deleted and recreated stock movements at the moment of the edit, so goods bought in July appeared to arrive in September and the history showed negative stock for six weeks. Edits now keep the invoice's own date. And an invoice whose goods have already been sold cannot be canceled; the right path is a purchase return.
Key takeaways
- A purchase touches stock, cost, the supplier, the cash and the books. Write them all in one transaction.
- Reordering steps chooses which half can fail. Only one database function fixes it.
- Protect supplier statements with the same uniqueness rules as journal entries.
- Accounting choices such as discount treatment belong to the owner. Explain the trade-off, decide, write it down.
Chapter 15 of the book has the complete checklist and the full story of the discount contract.
Frequently asked questions
How do I prevent a user from paying the same invoice twice?
Do it in three layers. Lock the pay button while the request runs, send a unique request ID so the database refuses duplicates, and add a database rule that blocks total payments above the invoice total. The database rule is the one that works whatever screen or device the click comes from.
What is a request ID, or idempotency key?
It is a unique tag created when the user presses a button and sent with the request. If the same tag arrives again, because of a double click or a retry after a network drop, the database recognizes it and does not repeat the action.
What is landed cost and do I need it?
Landed cost is the supplier price plus shipping, customs and transport, spread across the products you received. You need it whenever those extra costs are real, otherwise your stock value and profit reports are better than reality.