TEIF (Tunisian Electronic Invoice Format) is the XML format every El Fatoora invoice must use. A TEIF file has a header that identifies sender and receiver, a body with the invoice data, and signatures: the issuer's first, then TTN's after registration. The structure is fixed by an XSD schema and an implementation guide that TTN provides.
This article walks through the structure as described in the published schema and its implementation guide. Versions change: work from the XSD and guide version TTN gives you, and treat the codes below as examples to check against its referentials.
The overall structure
A skeleton of a signed invoice, simplified, looks like this:
- <TEIF version='...'>
- <InvoiceHeader> sender and receiver identifiers </InvoiceHeader>
- <InvoiceBody>
- <Bgm> document type and invoice number </Bgm>
- <Dtm> dates: issue date, service period </Dtm>
- <PartnerSection> seller, buyer, with tax IDs and addresses </PartnerSection>
- <PytSection> payment terms and means </PytSection>
- <LinSection> one Lin per invoice line </LinSection>
- <InvoiceMoa> invoice-level amounts </InvoiceMoa>
- <InvoiceTax> tax totals per type and rate </InvoiceTax>
- </InvoiceBody>
- <RefTtnVal> added by TTN at registration </RefTtnVal>
- <ds:Signature> issuer signature, then TTN signature </ds:Signature>
- </TEIF>
Header and document identification
InvoiceHeader carries MessageSenderIdentifier and the receiver's identifier. For a Tunisian company, the identifier is the tax ID (matricule fiscal), and the identifier type is given by a code attribute. The schema checks the matricule pattern, so a badly formatted tax ID fails before any business rule runs.
Bgm holds the document type, as a code from the referential, and the document number. Invoice, credit note and fee note are different types. Use the right one: a credit note is not a negative invoice.
Dates, parties and payment
- Dtm / DateText: each date carries a functionCode (for example I-31 for the invoice date) and an explicit format attribute such as ddMMyy. The value must match the format exactly.
- PartnerSection / PartnerDetails: one block per party, with a function code (seller, buyer and others), a Nad block containing PartnerIdentifier, name and address.
- PytSection: payment terms, conditions and means, each from a referential list.
Lines, amounts and taxes
Each Lin has an item identifier and description, a quantity with its unit, unit price, line amounts and the taxes on the line. Invoice totals sit in InvoiceMoa as amount details, each with an amount type code. In the referentials, codes such as I-176, I-177, I-178 and I-180 distinguish totals like amount before tax, tax base, tax amount and total including tax. Tax types also have codes, with VAT among them (I-1602 in the referential).
Amounts in dinars are written with three decimals. The schema does not force your totals to add up, but your accountant and the tax authority will expect them to: line totals, tax per rate and invoice totals must reconcile to the millime.
Mandatory or optional
The XSD marks which elements are required. In practice, an invoice that passes the schema still needs the content the law requires on any invoice. Treat these as mandatory in your generator:
- Document type and unique number.
- Issue date, and the service period where relevant.
- Seller and buyer identification with tax IDs for business customers, legal names and addresses.
- Each line: description, quantity, unit price, amount before tax, VAT rate.
- Totals before tax, per VAT rate, other taxes such as stamp duty where applicable, and total due.
- The issuer signature.
Optional elements (references to orders or contracts, extra free text, bank details) are useful to the customer. Add them when your data is reliable, not before.
Mapping from a typical ERP invoice
- Invoice number and type: Bgm.
- Invoice date and due date: Dtm, with the right function codes.
- Company and customer records: PartnerSection. Tax IDs must be stored as structured fields, not typed into the address.
- Payment terms: PytSection, mapped from your ERP's term codes to the referential.
- Invoice lines: one Lin each. Discounts and line taxes go in the line.
- Tax summary: InvoiceTax, one entry per tax type and rate.
- Totals: InvoiceMoa.
Keep the mapping in one place in code, with a test fixture per invoice case. When the spec version changes, you change one mapper.
Validation errors and their fixes
- Schema error on the tax ID: the matricule does not match the pattern. Fix the customer record, then regenerate.
- Invalid code value: a code outside the referential. Check your mapping tables against the current annex.
- Date format error: the value does not match the format attribute. Format dates from a date object, never from a string.
- Element order error: XSD sequences are ordered. Generate from the schema order.
- Signature error: the file changed after signing, or the wrong canonicalisation was used. Sign last, send the exact bytes.
- Totals mismatch flagged downstream: rounding per line versus per total. Pick the rule your accountant validates and apply it everywhere.
For the submission side, see our guide to the TTN El Fatoora API. If you need the generator built into your platform, see web development; for Odoo, see Odoo ERP.
FAQ
Is TEIF based on UBL?
No. TEIF is a Tunisian schema with its own element names and referentials. A UBL generator will not produce valid TEIF.
Where do I get the XSD?
From TTN, with the technical documentation provided on enrolment. Use that version, not a copy found online.
Can I validate before signing?
Yes, and you should. Validate the unsigned XML against the schema, then sign.
Singleton Solutions writes TEIF generators with tests for each invoice case. Tell us what your invoices look like.