Checking supplier documents before the ERP write
In practice, quality certificates and invoices arrive from suppliers as PDF files or scans; in this run they are synthetic records. A model reads them, and the values go into the ERP: batch numbers, quantities, test results, amounts. A reading that is right on 98.3% of fields can still put wrong records into the system of record. This page describes a check that runs before every ERP write, and what it did in that run.
1. What was run
One command generates 12 weeks of synthetic data for 3 sites and 12 suppliers: purchase orders, delivery notes with batch and heat numbers, 479 quality certificates and 392 invoices, some of them marked as scans. A simulated model reads each document. Its errors are injected and logged, so the right value of every field is known and each result can be scored against it. Each decision is then logged, and each passed record is written into a simulated ERP while timeouts, outages and crashes are injected.
2. Flow
- DocumentCertificate or invoice, with a text layer or marked as a scan.
- ReadingThe model fills a fixed schema and cites where it read each value.
- RulesSchema, format, value found at its cited source, cross-check against ERP data, invoice matched to deliveries, a second check on invoice totals.
- PersonConfirms the critical fields. Only after 20 confirmations in a row with nothing corrected can a supplier's records skip human confirmation on a document type; scanned records never do, and an alert sends that supplier's or site's records of that type back to human confirmation.
- ERP writeEvery decision is logged first. Only an explicit pass is written, through a journal and with an idempotency key.
A record that fails a step is not written. It is held with the reason and an owner, for example the buyer, incoming quality or accounts payable.
3. Results
| Wrong records written to the ERP | Without the check (as read) | With the check (rules, then a person) |
|---|---|---|
| This run, 871 records | 134 | 0 (144 held, each with a reason and an owner) |
| 39 runs on other random data: median (lowest to highest) | 149 (120 to 185) | 1 (0 to 3), none in a critical field |
Main run: seed 17, 12 weeks, 3 sites, 12 suppliers, 871 documents (479 certificates, 392 invoices). After the rules: 727 passed (130 written without human confirmation, 597 with critical fields confirmed by a person) and 144 held, each with the reason and an owner.
As read: 98.3% of 9,102 fields were read correctly, yet 134 records would be written wrong: 91 certificates, and 43 invoices approved by a plain invoice-to-order match (18 booked against the wrong delivery, 17 with a misread critical field, 8 billed before the goods arrived).
With the check: of the 148 records that, by the known truth, must be held or corrected (not the same set as the 134 above, which also depends on the plain invoice-to-order match), the rules hold 138 and the person confirming critical fields corrects 10. Known wrong invoice matches among the 338 invoices passed: 0 (over the 39 runs: 0 in every run). Also held: 6 correct records (false holds), waiting for a person to release them.
4. One record from the run
Certificate C-S03-0577 from supplier S03, for order 4500010280. The format check passes. The cross-check against the delivery notes of the order fails, so the record is held for incoming quality. The reason, as the check wrote it:
erp: batch 48213 is not on a delivery of order 4500010280 (delivered: 48231); it is on no delivery note
5. Drift alerts
One supplier changes its certificate template in week 7: its rejection rate raises an alert after 7 days. Certificates at one site switch language in week 9: alert after 14 days. False alerts in 12 weeks: 0. An alert sends that supplier's or site's records of that document type back to human confirmation.
Over the 39 other runs (seeds 1 to 40 except 17): the template alert came after a median of 7 days (39 in the worst run), the language alert after a median of 3 days (21 in the worst run); false alerts 0 to 4 per run (median 0).
6. Controls and how each is tested
| Control | Protects against | How it is tested |
|---|---|---|
| Fail-closed | A record written by default after an error, a missing field or an unclear result. | Tests inject an exception into each check function and raise twelve exception types, built-in and custom; they also feed in an unusable check result, remove each critical field, give unparseable values and an unknown document type. In every case the record is held, and only an explicit pass is written. A keyboard interrupt is not caught: it stops the run and is never turned into a pass. A record whose check raises an exception is held and the run goes on; a decision that cannot be logged stops the run before its ERP write. Over the full run, no held decision reaches the ERP.Tests: 19 (test_fail_closed.py 17, test_write_path.py 2) |
| Cross-check against ERP data | A value that is well formed but wrong, such as a batch number with two digits swapped or a value taken from the wrong table row. | The example above and every wrong-row case pass the format check and are held by the cross-check. Every defect injected into a document with a text layer is held, except a day and month swap (see limits), and so is every document seeded with a conflict against the ERP data, such as a supplier typo, a duplicate invoice or a price change.Tests: 13 (test_prototype.py) |
| Each value traced to its cited source | A value the model reports that is not at the place in the document it cites, such as a heat number read as the batch number. | A value that is not what its cited table cell says fails the trace, and so does a value cited from the heat column as a batch number; in the run every heat number taken as a batch number is flagged with a reason that names the cause. Numbers written in several formats, and ambiguous dates (both readings are kept), are tested.Tests: 4 (test_prototype.py) |
| Second channel on invoice totals | A misread amount that the main check lets through. | The total read is compared with the total recomputed from the lines (quantity x unit price). A difference of more than 0.01 (one cent) holds the invoice even when the main check passed it, so per-line rounding that the main check tolerates is caught. On seed 17 it flags exactly the invoices with a misread total, unit price or quantity, 21 in the run and all held, and no correct invoice.Tests: 4 (test_fail_closed.py) |
| Invoice matched to received, uninvoiced deliveries | Paying for the wrong delivery, paying twice, paying before the goods arrive. | A partial delivery invoiced out of order is matched to the right delivery, where a plain invoice-to-order match books the oldest one; a delivery already invoiced, the same invoice number twice, billing before receipt and a total that is not the sum of its lines are held; two deliveries that fit equally go to the buyer.Tests: 9 (test_prototype.py) |
| A person on critical fields | Misreads the rules cannot see, such as an in-spec test value misread on a scan. | Short scripted runs check the rule record by record: a supplier's records on a document type skip human confirmation after 20 confirmations in a row with nothing corrected, and not before. A correction, or a wrong value found in the weekly hand check, ends the skip and starts the count again; a record held by the rules neither counts nor resets it. A scan still goes to the person, even when the supplier's other records skip human confirmation. Over the whole main run, no scanned record skips it, and an independent replay of the rule on the run's decisions finds every skip allowed.Tests: 9 (test_prototype.py) |
| Rejection rate per supplier and site, with an alert | A silent change at the source, such as a new certificate template or a new language. | After an alert, no record of that supplier or site on that document type skips human confirmation until the alert window ends, even after 20 confirmations with nothing corrected inside the window. A supplier alert also starts the count of that supplier and document type again; a site alert does not. The hold reasons name the cause. In the run: alerts after 7 and 14 days, 0 false alerts.Tests: 6 (test_prototype.py) |
| Idempotent ERP writes | Duplicate ERP rows from retries and timeouts. | The key is the SHA-256 of the normalised record, so equivalent forms of the same record give one key. The same write delivered 3 times gives one ERP row, against an ERP that enforces the key and against one that only allows a lookup. Against the lookup-only ERP, 3 restarts give one row, a retry after a timeout that came after the ERP had committed gives one row, and timeouts before commit are retried with growing waits. A randomised run of faults and crashes ends with no duplicate and no lost record. In the full run the simulated ERP, which does not deduplicate by itself, timed out 29 times before and 17 times after commit: 727 rows for 727 passed records, 0 duplicates.Tests: 9 (test_erp_writer.py 7, test_write_path.py 2) |
| Journal before the write | Records lost or written twice if the process stops between the decision and the write. | The intent is written to a journal and flushed to disk before the ERP is called. Tests simulate a crash before the journal entry, between the decision and the write, and between the ERP commit and the done mark: after the restart each record is written once. The full run had 3 simulated crashes, one at each of these points, and lost nothing and wrote nothing twice; a run with 10 simulated crashes did the same.Tests: 6 (test_erp_writer.py 4, test_write_path.py 2) |
| Source document hash, from receipt to the ERP row | Losing track of which document a written value came from, or a document changed after receipt. | The SHA-256 of each source document record, taken at receipt, is stored with its decision and reaches its ERP row. Over the full run all 871 logged decisions and all 727 ERP rows carry their document's hash. A document changed after receipt is held.Tests: 4 (test_fail_closed.py 2, test_write_path.py 1, test_erp_writer.py 1) |
| Chained decision log with HMAC tags | A decision changed, removed, reordered or added after the fact. | Each row carries the SHA-256 of the previous row and an HMAC-SHA256 tag, both recomputed independently in a test. HMAC-SHA256 is a keyed hash, not a public-key signature: whoever can verify the log also holds the key. The verifier finds a modified, a deleted, a moved and an inserted row, and a wrong key; rows cut off the end are found against the anchor (row count and last hash). The key is never written to the log, a key file inside the output folder is refused, and the key is in no output file of the run. All 871 decisions of the full run are in the log in order, and the log verifies against its anchor.Tests: 13 (test_ledger.py 10, test_write_path.py 3) |
| Reconciliation after the run | A difference between what was decided and what is in the ERP that no single step would see. | After the full run the ERP rows are checked against the log, and the journal is checked for anything still pending or waiting for a person: no problem found. Tests then break a copy of the run, and each case is reported: a changed log row, a log cut at the end, a log rewritten with the key during the run (found against the decisions the run holds in memory), a duplicate, missing or changed ERP row, an ERP row with no decision behind it, and a source hash that differs from receipt. An ERP that refuses every write, or never answers, shows up in the reconciliation report, and its records go to a person queue.Tests: 10 (test_write_path.py) |
| Reproducible run | Results that cannot be repeated and checked. | The same seed gives the same data and the same fault plan, the three layers of the score add up, and the decision log carries no clock time, so the same input gives identical bytes. Seed 17, run again with the other seeds, gives the numbers of the main run and is left out of the 39 other runs.Tests: 6 (test_prototype.py 2, test_write_path.py 2, test_ledger.py 1, test_facts.py 1) |
| The check never reads the answer key | Results that look good only because the check could see the truth. | A test reads the source of the check: no reference to the truth files, no file access, and only three standard modules imported.Tests: 4 (test_prototype.py) |
| Standard library, no network | Hidden dependencies or network calls. | A test parses every Python file of the prototype and fails on any import from outside the standard library and on any network module.Tests: 1 (test_facts.py) |
Test suite on the run that produced these numbers: 269 of 269 pass. Python standard library only, no network modules. The write path does not change the scores: they are the same with and without it. The numbers of the run on this page come from its facts file, which is written only when every test passes, the test log adds up, the decision log verifies with its key and the ERP holds no extra row. Test names and code on request.
7. Limits
- All data is synthetic and the model is simulated. The as-read count (134) follows from error rates I chose; what counts is the same measure on a labelled set of real documents.
- The rules were written knowing the defect types, so a high catch rate here is expected.
- The person is simulated and never misses. Real people do, and the weekly hand-checked sample is what measures it. In this run the sample checked 144 records (12 a week) and found 0 wrong, as expected with a simulated person. With real people, 0 wrong in 144 would only show that fewer than about 2.1% of written records are wrong (one-sided, 95% confidence); showing a lower rate needs a larger sample.
- Scans have no text layer, so only the cited region is checked: a test value misread as another value inside the specification passes the rules, and only the person catches it. That is why a scan never skips human confirmation.
- Issue date is not a critical field here: a day and month swap that stays inside the date window passes.
- The ERP is simulated: no real ERP or MES interface, one part per order, one line per invoice. Nothing is written to any real system. No RPA and no production-line data feed.
- The decision log is chained and tagged with HMAC-SHA256, a keyed hash rather than a signature, and it is not encrypted: anyone who holds the key can rewrite it. Rows cut off the end leave a shorter chain that still verifies; they are found only against the anchor (row count and last hash), which this run keeps next to the log. In production the anchor would be kept where the log writer cannot change it.
- Against an ERP without its own idempotency key, the lookup before each retry is safe only if a request that timed out cannot still commit later: a late commit would give a second row, which only an ERP that enforces the key on its side would refuse. Late commits are not tested.
- Faults, crashes and retry waits are simulated inside one process from a fixed seed; waits are logged, not actually waited out. Real process kills and power loss are not tested.
- The source hash is that of the synthetic document record, not of a received file.
- The second channel covers invoice totals only.
- Encryption at rest and in transit is left to the infrastructure the check runs on (TLS, encrypted disks, a key store). In production the HMAC key of the decision log would sit in that key store, not in a file. No certification is claimed.
- The alert thresholds and the 20 confirmations are placeholders, to be set on real volumes.