// blog
The Bug That Overwrote Invoices: What Reading Old Code Turns Up
Virtual Salesman 3 saved the company file first, then the document, then raised the document counter in memory without marking the company as changed. Close the program and the new counter was lost, so the next document got the same number and replaced the earlier file. The author’s own sample data shows it. In the web version the database hands out the number and refuses a second document with the same one.

When we moved Virtual Salesman 3, a Delphi program from 2006, to the web, we read every line that saves something. One of them could lose a customer’s invoice without a word.
What happened
When you saved a document, the program did three things, in this order:
- Saved the company file, which holds the counters: the last quotation, order and invoice number.
- Saved the document under its number, replacing any file with that name.
- Raised the counter, in memory only, without marking the company as changed.
So unless you changed something else before closing the program, the raised counter was never written. The next time, the program offered the same number again, and saving that document replaced the earlier file.
How we know
The author’s own sample data is in exactly that state. The company file says the last order was 108 and the last invoice 108. But order 109 and invoice 109 are in the folder. The next order would have been number 109, and it would have replaced the one already there.
What the web version does
- The next number is worked out inside the same database transaction that saves the document, from the counter and from the highest number already used.
- A unique key in the database refuses a second document with the same number, whatever the code does.
- The importer reads the old counters, sees that 109 exists, and continues from 110. It lists that repair in its report.
What to look for in your own old software
- Numbers that sometimes repeat: invoices, work orders, case numbers. Ask whoever uses it every day.
- Anything saved in more than one step, where closing the program in between leaves half a change.
- Files named after a number in a folder, with no database to refuse a duplicate.
- “It happens sometimes” problems nobody can reproduce. They’re usually an order of operations like this one.
Reading the code is the surest way to find these. It’s also the part of a conversion that pays for itself.
The full case study shows the rest of what we found, and the web version lets you try it. Running on old software yourself? Tell us about it.
































































































































