# Canonical invoice model `einvoice.model.Invoice` represents invoices and credit notes in a standalone Python package. It requires no Django setup or database. Nested objects live in `einvoice.model.terms`. Pydantic provides typed construction, nested JSON reading and serialization without maintaining a separate codec. It also rejects unknown keys so a misspelled business term cannot silently disappear. ```python from pathlib import Path from einvoice.model import Invoice invoice = Invoice.from_json( Path("einvoice/model/tests/fixtures/b2b_invoice.json").read_bytes() ) assert invoice.seller.name == "Furnizor Exemplu SRL" assert Invoice.from_json(invoice.to_json()) == invoice ``` Construct objects with descriptive Python names, or read the same fields using their aliases: `Seller(name="Example", postal_address=...)` corresponds to `{"BT-27": "Example", "BG-5": ...}`. Fields' Pydantic aliases and generated JSON Schema expose the BT/BG tags. Seller, buyer and delivery addresses have separate types because their similar attributes have different business identifiers. The wire representation uses nested groups such as `BG-4` (seller), `BG-22` (totals) and `BG-25` (lines). Repeated terms/groups are JSON arrays, including empty arrays when absent; optional scalar terms/groups are omitted by `to_json()`. Amounts, prices, quantities and percentages use `Decimal` and serialize as JSON strings to preserve decimal precision and trailing zeroes. Dates serialize as `YYYY-MM-DD`. Codes are strings, preserving leading zeroes. Identifiers with optional schemes use an object under their BT key, for example `"BT-34": {"value": "facturi@example.invalid", "scheme_id": "EM"}`. `scheme_version_id` is available for item classifications. Attached documents use `"BT-125": {"content": "RXhlbXBsdQ==", "mime_code": "text/plain", "filename": "exemplu.txt"}` with base64 content. These supplementary component keys belong to the containing business term; they do not introduce new BTs. `model_version` defaults to `EN 16931-1:2017`. This describes the semantic model target and is separate from the CIUS specification identifier in `BG-2.BT-24`. Unsupported versions are rejected. Adding the 2026 revision requires explicit version support and reviewed mappings; changing the label alone is insufficient. The package covers core document metadata and references, notes, seller/buyer parties, payee, tax representative, addresses and contacts, delivery and periods, payment instructions, document and line allowances/charges, totals, VAT breakdown, supporting documents, lines, prices, item identifiers and attributes. Official EN 16931 and Romanian sources are linked in the model module's docstring. This is a data contract, not a compliance validator. It checks types and shape, but does not enforce CIUS/EN rules, calculate totals, validate identifiers or code lists, infer credit-note signs, or implement CII conversion, persistence or ANAF calls. The separate `einvoice.ubl.parse_ubl` binding reads UBL 2.1 Invoice and CreditNote documents into the model, preserving supplied signs, decimal precision, repeated groups and identifier schemes. `einvoice.ubl.invoice_to_ubl` renders the model to UBL 2.1 XML, selecting CreditNote for type code `381` and Invoice otherwise. It preserves the supplied specification identifier (`BG-2.BT-24`) and signs; it does not calculate missing amounts or validate compliance. UBL extensions and syntax-only elements are outside the semantic model, so regenerated XML is not a byte-for-byte round trip. ```python from einvoice.ubl import invoice_to_ubl, parse_ubl invoice = parse_ubl(Path("invoices/samples/valid.xml").read_bytes()) assert Invoice.from_json(invoice.to_json()) == invoice xml = invoice_to_ubl(invoice) ``` Serialization raises `einvoice.ubl.UBLSerializeError`, with `rule_id` and `explanation`, for unsupported bindings, XML-incompatible text, or output-budget violations. The serializer applies a conservative 256 KiB construction budget, including decimal expansion and repeated text. Credit notes with a due date require payment instructions, and credit-note project references are unsupported. A VAT total is required with a VAT breakdown; an accounting currency is required with an accounting-currency VAT total. Gross price requires a price discount, and a price base-quantity unit requires a base quantity. For syntax-required fields without a supplied semantic value, serialization uses `NA` for a missing purchase-order ID when a sales-order reference exists, and for a card's network ID. For validated conversion over HTTP, see the API reference served at `/api/docs/`. `Buyer.b2c_identifier_kind` recognises a CNP or the 13-zero consumer identifier for Romanian buyers, using the legal registration and party identifiers' purpose and scheme without changing the versioned BT JSON shape. See the [validator runtime](../README.md#validator-runtime) for identifier classification, offline-check limitations and the demo's bounded subprocess. Both original JSON fixtures are synthetic historical 2024 examples, not authority-approved invoices or current tax guidance. One exercises a B2B invoice with multiple lines and VAT categories; the other exercises a credit note and its preceding-invoice reference. Run its database-independent tests with: ```bash python -m unittest discover -s einvoice/model/tests -v ``` The same tests are discovered by the existing `python manage.py test` CI step.