An e-invoicing data model separates the mutable commercial record from the immutable compliance record. Store a snapshot of parties and lines as they were at issuance, the generated XML and its hash, each submission attempt and acknowledgement with raw payloads, the derived status, and pointers to archived artefacts. Enforce immutability on submitted documents at the database level and model credit notes as new documents referencing the original.
Key facts at a glance
- Separate commercial tables (editable) from compliance tables (append-only).
- Store the XML and SHA-256 per document version.
- One row per acknowledgement, never overwrite status history.
- Snapshot party data at issuance; master data changes later must not alter past documents.
- Index by TIN, document number, issue date, status for reconciliation queries.
Core tables
| Table | Key columns | Notes |
|---|---|---|
| einvoice_document | id, tenant_tin, number, type_code (380/381), issue_date, currency, references_document_id, status, xml_hash | One row per issued document; immutable after submission |
| einvoice_party_snapshot | document_id, role (seller/buyer), tin, trn, legal_name, address_json, country | Frozen copy of master data at issuance |
| einvoice_line | document_id, line_no, description, qty, unit_code, unit_price, line_net, vat_category, vat_rate, reason_code | As generated; totals derived |
| einvoice_allowance_charge | document_id, line_no (nullable), is_charge, amount, base, percent, reason, vat_category, vat_rate | Line-level when line_no set |
| einvoice_vat_breakdown | document_id, category, rate, taxable_amount, vat_amount, reason_code | One per category/rate |
| einvoice_xml | document_id, version, xml_blob_or_pointer, sha256, generated_at, ruleset_version | Keep every version submitted |
| einvoice_submission | document_id, attempt_no, provider_code, provider_ref, sent_at, http_status, request_hash | One per attempt |
| einvoice_ack | document_id, kind (provider_accepted/rejected/delivered/reported/mlr), received_at, payload_json | Append-only |
Immutability
Once a document has a provider_accepted acknowledgement, updates and deletes on its rows should be impossible, not merely discouraged. Use database triggers that raise on UPDATE/DELETE when the status is submitted, or move submitted rows into partitions with no write grants for the application role. Corrections create new documents of type 381 with references_document_id set.
Derived status and reporting
Compute the document's current state from the acknowledgement history rather than storing it as the only truth; keep a denormalised status column for indexing but rebuild it from einvoice_ack nightly to catch drift. Reconciliation views join documents, breakdowns and the latest acknowledgement per kind to produce period totals by VAT category that can be compared to the provider's report and to the VAT return.
Multi-entity and retention
Carry tenant_tin on every compliance table so queries, exports and access control are per legal entity. Partition by issue year to make retention management and archival straightforward, and store large XML blobs in object storage with the pointer and hash in the database rather than in the row itself.
Where Zaini Developers fits in
This schema is close to what FORMA uses in production. We adapt it to your ERP's existing tables so the compliance record is complete without duplicating your commercial data.
Book a free consultation →Frequently asked questions
Should I store the XML in the database or in files?
Store the hash and metadata in the database and the XML in versioned object storage; small deployments can keep both in the database.
How do I handle a document rejected by the provider?
Keep the rejected version in einvoice_xml, record the rejection acknowledgement, fix the source and generate a new version under the same document number if the number was never accepted.
Do I need to snapshot the buyer's address?
Yes. If the customer moves next year, last year's documents must still show the address used at the time.
What about inbound invoices?
Mirror the structure with a direction flag; inbound documents also need acknowledgements, matching status and archive pointers.
