{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://temlavo.com/schemas/invoice.v1.json",
  "title": "Temlavo invoice.v1 data schema",
  "description": "Public structural and wire schema for the invoice object under document.data. The executable API contract remains authoritative for runtime refinements.",
  "type": "object",
  "properties": {
    "vendor": {
      "description": "The seller that issued the invoice.",
      "$ref": "#/$defs/InvoiceParty"
    },
    "customer": {
      "description": "The buyer the invoice is addressed to.",
      "$ref": "#/$defs/InvoiceParty"
    },
    "invoice": {
      "description": "The invoice header: its number, dates, purchase order, currency and payment terms.",
      "title": "Invoice details",
      "$ref": "#/$defs/InvoiceHeader"
    },
    "line_items": {
      "maxItems": 500,
      "type": "array",
      "items": { "$ref": "#/$defs/LineItem" },
      "description": "The invoice's billed rows."
    },
    "line_items_status": {
      "description": "How completely the rows were extracted. It is an AI assessment: `complete` is not proof that every source row was captured.",
      "title": "Line-item extraction",
      "$ref": "#/$defs/LineItemsStatus"
    },
    "totals": {
      "description": "Invoice-level amounts.",
      "$ref": "#/$defs/InvoiceTotals"
    },
    "taxes": {
      "maxItems": 500,
      "type": "array",
      "items": { "$ref": "#/$defs/TaxComponent" },
      "description": "The invoice-level tax breakdown, one entry per tax component.",
      "title": "Tax breakdown"
    }
  },
  "required": [
    "vendor",
    "customer",
    "invoice",
    "line_items",
    "line_items_status",
    "totals",
    "taxes"
  ],
  "additionalProperties": false,
  "if": {
    "properties": { "line_items_status": { "const": "unavailable" } },
    "required": ["line_items_status"]
  },
  "then": {
    "properties": { "line_items": { "type": "array", "maxItems": 0 } }
  },
  "$defs": {
    "Address": {
      "type": "object",
      "properties": {
        "line1": {
          "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }],
          "description": "First address line.",
          "title": "Address line 1"
        },
        "line2": {
          "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }],
          "description": "Second address line.",
          "title": "Address line 2"
        },
        "city": {
          "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }],
          "description": "City or town."
        },
        "region": {
          "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }],
          "description": "State, province or region."
        },
        "postal_code": {
          "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }],
          "description": "Postal or ZIP code.",
          "title": "Postal code"
        },
        "country_code": {
          "anyOf": [{ "$ref": "#/$defs/CountryCode" }, { "type": "null" }],
          "description": "ISO 3166-1 alpha-2 code, such as `US`, when unambiguous.",
          "title": "Country"
        }
      },
      "required": [
        "line1",
        "line2",
        "city",
        "region",
        "postal_code",
        "country_code"
      ],
      "additionalProperties": false
    },
    "CountryCode": { "type": "string", "pattern": "^[A-Z]{2}$" },
    "InvoiceParty": {
      "type": "object",
      "properties": {
        "name": {
          "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }],
          "description": "The name as printed, without trademark marks or trailing commas, semicolons and colons."
        },
        "tax_id": {
          "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }],
          "description": "The tax ID. A printed scheme label such as `VAT No:` is removed when the identifier after it is clear.",
          "title": "Tax ID"
        },
        "address": {
          "anyOf": [{ "$ref": "#/$defs/Address" }, { "type": "null" }],
          "description": "The postal address, or null when none is printed or none of its parts can be read."
        }
      },
      "required": ["name", "tax_id", "address"],
      "additionalProperties": false
    },
    "InvoiceDate": {
      "type": "string",
      "format": "date",
      "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))$"
    },
    "CurrencyCode": {
      "type": "string",
      "enum": [
        "ADP",
        "AED",
        "AFA",
        "AFN",
        "ALK",
        "ALL",
        "AMD",
        "ANG",
        "AOA",
        "AOK",
        "AON",
        "AOR",
        "ARA",
        "ARP",
        "ARS",
        "ARY",
        "ATS",
        "AUD",
        "AWG",
        "AYM",
        "AZM",
        "AZN",
        "BAD",
        "BAM",
        "BBD",
        "BDT",
        "BEC",
        "BEF",
        "BEL",
        "BGJ",
        "BGK",
        "BGL",
        "BGN",
        "BHD",
        "BIF",
        "BMD",
        "BND",
        "BOB",
        "BOP",
        "BOV",
        "BRB",
        "BRC",
        "BRE",
        "BRL",
        "BRN",
        "BRR",
        "BSD",
        "BTN",
        "BUK",
        "BWP",
        "BYB",
        "BYN",
        "BYR",
        "BZD",
        "CAD",
        "CDF",
        "CHC",
        "CHE",
        "CHF",
        "CHW",
        "CLF",
        "CLP",
        "CNY",
        "COP",
        "COU",
        "CRC",
        "CSD",
        "CSJ",
        "CSK",
        "CUC",
        "CUP",
        "CVE",
        "CYP",
        "CZK",
        "DDM",
        "DEM",
        "DJF",
        "DKK",
        "DOP",
        "DZD",
        "ECS",
        "ECV",
        "EEK",
        "EGP",
        "ERN",
        "ESA",
        "ESB",
        "ESP",
        "ETB",
        "EUR",
        "FIM",
        "FJD",
        "FKP",
        "FRF",
        "GBP",
        "GEK",
        "GEL",
        "GHC",
        "GHP",
        "GHS",
        "GIP",
        "GMD",
        "GNE",
        "GNF",
        "GNS",
        "GQE",
        "GRD",
        "GTQ",
        "GWE",
        "GWP",
        "GYD",
        "HKD",
        "HNL",
        "HRD",
        "HRK",
        "HTG",
        "HUF",
        "IDR",
        "IEP",
        "ILP",
        "ILR",
        "ILS",
        "INR",
        "IQD",
        "IRR",
        "ISJ",
        "ISK",
        "ITL",
        "JMD",
        "JOD",
        "JPY",
        "KES",
        "KGS",
        "KHR",
        "KMF",
        "KPW",
        "KRW",
        "KWD",
        "KYD",
        "KZT",
        "LAJ",
        "LAK",
        "LBP",
        "LKR",
        "LRD",
        "LSL",
        "LSM",
        "LTL",
        "LTT",
        "LUC",
        "LUF",
        "LUL",
        "LVL",
        "LVR",
        "LYD",
        "MAD",
        "MDL",
        "MGA",
        "MGF",
        "MKD",
        "MLF",
        "MMK",
        "MNT",
        "MOP",
        "MRO",
        "MRU",
        "MTL",
        "MTP",
        "MUR",
        "MVQ",
        "MVR",
        "MWK",
        "MXN",
        "MXP",
        "MXV",
        "MYR",
        "MZE",
        "MZM",
        "MZN",
        "NAD",
        "NGN",
        "NIC",
        "NIO",
        "NLG",
        "NOK",
        "NPR",
        "NZD",
        "OMR",
        "PAB",
        "PEH",
        "PEI",
        "PEN",
        "PES",
        "PGK",
        "PHP",
        "PKR",
        "PLN",
        "PLZ",
        "PTE",
        "PYG",
        "QAR",
        "RHD",
        "ROK",
        "ROL",
        "RON",
        "RSD",
        "RUB",
        "RUR",
        "RWF",
        "SAR",
        "SBD",
        "SCR",
        "SDD",
        "SDG",
        "SDP",
        "SEK",
        "SGD",
        "SHP",
        "SIT",
        "SKK",
        "SLE",
        "SLL",
        "SOS",
        "SRD",
        "SRG",
        "SSP",
        "STD",
        "STN",
        "SUR",
        "SVC",
        "SYP",
        "SZL",
        "THB",
        "TJR",
        "TJS",
        "TMM",
        "TMT",
        "TND",
        "TOP",
        "TPE",
        "TRL",
        "TRY",
        "TTD",
        "TWD",
        "TZS",
        "UAH",
        "UAK",
        "UGS",
        "UGW",
        "UGX",
        "USD",
        "USN",
        "USS",
        "UYI",
        "UYN",
        "UYP",
        "UYU",
        "UYW",
        "UZS",
        "VEB",
        "VED",
        "VEF",
        "VES",
        "VNC",
        "VND",
        "VUV",
        "WST",
        "XAD",
        "XAF",
        "XAG",
        "XAU",
        "XBA",
        "XBB",
        "XBC",
        "XBD",
        "XCD",
        "XCG",
        "XDR",
        "XEU",
        "XFO",
        "XFU",
        "XOF",
        "XPD",
        "XPF",
        "XPT",
        "XRE",
        "XSU",
        "XTS",
        "XUA",
        "XXX",
        "YDD",
        "YER",
        "YUD",
        "YUM",
        "YUN",
        "ZAL",
        "ZAR",
        "ZMK",
        "ZMW",
        "ZRN",
        "ZRZ",
        "ZWC",
        "ZWD",
        "ZWG",
        "ZWL",
        "ZWN",
        "ZWR"
      ]
    },
    "InvoiceHeader": {
      "type": "object",
      "properties": {
        "number": {
          "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }],
          "description": "The invoice number. A leading `#` or `№` is removed; leading zeros stay.",
          "title": "Invoice number"
        },
        "issue_date": {
          "anyOf": [{ "$ref": "#/$defs/InvoiceDate" }, { "type": "null" }],
          "description": "The issue date. Null when the printed date is ambiguous."
        },
        "due_date": {
          "anyOf": [{ "$ref": "#/$defs/InvoiceDate" }, { "type": "null" }],
          "description": "The due date. Null when the printed date is ambiguous."
        },
        "purchase_order_number": {
          "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }],
          "description": "The purchase order the invoice itself states or references, with a leading `#` or `№` removed. A separate purchase order page in the same upload never supplies it.",
          "title": "PO number"
        },
        "currency": {
          "anyOf": [{ "$ref": "#/$defs/CurrencyCode" }, { "type": "null" }],
          "description": "The invoice currency: as printed, or inferred from the invoice's context. Null when the context is insufficient or contradictory."
        },
        "payment_terms": {
          "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }],
          "description": "Payment terms as printed, such as `Net 30`."
        }
      },
      "required": [
        "number",
        "issue_date",
        "due_date",
        "purchase_order_number",
        "currency",
        "payment_terms"
      ],
      "additionalProperties": false
    },
    "LineItem": {
      "type": "object",
      "properties": {
        "description": {
          "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }],
          "description": "The row's description, with wrapped lines merged."
        },
        "product_code": {
          "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }],
          "description": "SKU, part or product code."
        },
        "quantity": {
          "anyOf": [
            { "$ref": "#/$defs/SignedDecimalString" },
            { "type": "null" }
          ],
          "description": "The row's quantity."
        },
        "unit_of_measure": {
          "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }],
          "description": "The unit, such as `each` or `hours`."
        },
        "unit_price": {
          "anyOf": [{ "$ref": "#/$defs/Money" }, { "type": "null" }],
          "description": "The quoted amount per unit. It is never copied into or calculated from `line_total`."
        },
        "discount": {
          "anyOf": [{ "$ref": "#/$defs/NonNegativeMoney" }, { "type": "null" }],
          "description": "The row's reduction."
        },
        "tax": {
          "anyOf": [{ "$ref": "#/$defs/LineTax" }, { "type": "null" }],
          "description": "The row's tax, or null when the invoice shows no line tax. Several distinct line taxes without a printed aggregate leave it unknown; invoice-level breakdowns are in `taxes`."
        },
        "line_total": {
          "anyOf": [{ "$ref": "#/$defs/Money" }, { "type": "null" }],
          "description": "The row's extended amount as printed, never calculated. Credits and reversals keep their sign."
        }
      },
      "required": [
        "description",
        "product_code",
        "quantity",
        "unit_of_measure",
        "unit_price",
        "discount",
        "tax",
        "line_total"
      ],
      "additionalProperties": false
    },
    "SignedDecimalString": {
      "type": "string",
      "pattern": "^[+-]?\\d+(?:\\.\\d+)?$"
    },
    "Money": {
      "type": "object",
      "properties": {
        "amount": {
          "description": "The amount as a decimal string, with its sign.",
          "$ref": "#/$defs/SignedDecimalString"
        },
        "currency": {
          "description": "The amount's ISO 4217 currency code.",
          "$ref": "#/$defs/CurrencyCode"
        }
      },
      "required": ["amount", "currency"],
      "additionalProperties": false
    },
    "NonNegativeMoney": {
      "type": "object",
      "properties": {
        "amount": {
          "description": "The amount as a decimal string, never negative.",
          "$ref": "#/$defs/NonNegativeDecimalString"
        },
        "currency": {
          "description": "The amount's ISO 4217 currency code.",
          "$ref": "#/$defs/CurrencyCode"
        }
      },
      "required": ["amount", "currency"],
      "additionalProperties": false
    },
    "NonNegativeDecimalString": {
      "type": "string",
      "pattern": "^\\+?\\d+(?:\\.\\d+)?$"
    },
    "LineTax": {
      "type": "object",
      "properties": {
        "label": {
          "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }],
          "description": "The tax label as printed."
        },
        "rate": {
          "anyOf": [
            { "$ref": "#/$defs/NonNegativeDecimalString" },
            { "type": "null" }
          ],
          "description": "As a decimal fraction: 8.25% is `0.0825`."
        },
        "amount": {
          "anyOf": [{ "$ref": "#/$defs/Money" }, { "type": "null" }],
          "description": "The row's tax amount."
        }
      },
      "required": ["label", "rate", "amount"],
      "additionalProperties": false
    },
    "LineItemsStatus": {
      "type": "string",
      "enum": ["complete", "partial", "unavailable"],
      "description": "AI assessment of line-item extraction. Complete is not independent proof that every source item was captured.\n\n- `complete`: The AI assessed the rows as complete. It is not proof that every source row was captured.\n- `partial`: Some rows were extracted and are kept; others are missing.\n- `unavailable`: Extraction yielded no rows and `line_items` is empty. It doesn't mean the invoice has none."
    },
    "InvoiceTotals": {
      "type": "object",
      "properties": {
        "subtotal": {
          "anyOf": [{ "$ref": "#/$defs/Money" }, { "type": "null" }],
          "description": "The invoice charge base before invoice-level adjustments."
        },
        "discount": {
          "anyOf": [{ "$ref": "#/$defs/NonNegativeMoney" }, { "type": "null" }],
          "description": "The invoice-level reduction, even when the invoice prints it with a minus sign."
        },
        "shipping": {
          "anyOf": [{ "$ref": "#/$defs/Money" }, { "type": "null" }],
          "description": "Shipping charges."
        },
        "other_charges": {
          "anyOf": [{ "$ref": "#/$defs/Money" }, { "type": "null" }],
          "description": "The signed net of other invoice-level adjustments, when their meaning is explicit."
        },
        "tax": {
          "anyOf": [{ "$ref": "#/$defs/Money" }, { "type": "null" }],
          "description": "Invoice-level tax. Its breakdown is in `taxes`."
        },
        "total": {
          "anyOf": [{ "$ref": "#/$defs/Money" }, { "type": "null" }],
          "description": "The invoice total after reductions, shipping, other charges and tax, before payments. When the invoice prints no total, it is the amount that its subtotal and adjustments, and its payable plus what was deducted from it, both give exactly; with nothing deducted, that is the payable."
        },
        "amount_due": {
          "anyOf": [{ "$ref": "#/$defs/Money" }, { "type": "null" }],
          "description": "The amount currently payable. Payments, deposits, credits and prior balances can make it differ from `total`."
        }
      },
      "required": [
        "subtotal",
        "discount",
        "shipping",
        "other_charges",
        "tax",
        "total",
        "amount_due"
      ],
      "additionalProperties": false
    },
    "TaxComponent": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "enum": ["sales_tax", "vat", "gst", "other", "unknown"],
          "description": "The tax category. It is never null: a real tax is kept even when its category can't be read.\n\n- `sales_tax`: Identified as sales tax.\n- `vat`: Identified as VAT.\n- `gst`: Identified as GST.\n- `other`: A clearly identified category other than sales tax, VAT or GST.\n- `unknown`: A real tax whose category can't be safely classified."
        },
        "label": {
          "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }],
          "description": "The tax label as printed."
        },
        "rate": {
          "anyOf": [
            { "$ref": "#/$defs/NonNegativeDecimalString" },
            { "type": "null" }
          ],
          "description": "As a decimal fraction: 8.25% is `0.0825`."
        },
        "taxable_amount": {
          "anyOf": [{ "$ref": "#/$defs/Money" }, { "type": "null" }],
          "description": "The amount the tax applies to."
        },
        "amount": {
          "anyOf": [{ "$ref": "#/$defs/Money" }, { "type": "null" }],
          "description": "The tax amount. Null when none is printed, even when tax is known not to apply; it is never turned into zero."
        },
        "jurisdiction": {
          "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }],
          "description": "The taxing jurisdiction, when printed."
        }
      },
      "required": [
        "type",
        "label",
        "rate",
        "taxable_amount",
        "amount",
        "jurisdiction"
      ],
      "additionalProperties": false
    }
  }
}
