{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:open-utility-stack:bill:1",
  "title": "bill@1",
  "description": "An issued bill for one contract account: every sector's sections, each line rounded once, the VAT groups, the totals and what was prepaid. A profile of TMF678 Customer Bill; the EN 16931 invoice is attached as a document. Anyone holding it recomputes the totals from the lines and the digest from the document (common/digest.md) without the issuer's code: the bill that proves itself. Personal data (names, addresses, bank details) is not in it but in the attached invoice, behind the account reference (G7).",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "profile",
    "number",
    "account",
    "creditor",
    "currency",
    "period",
    "issue_date",
    "due_date",
    "sections",
    "vat_groups",
    "totals"
  ],
  "properties": {
    "profile": {
      "const": "bill@1"
    },
    "number": {
      "description": "The invoice number (EN 16931 BT-1; TMF678 billNo): a series and a gap-free sequence within it.",
      "$ref": "#/$defs/Number"
    },
    "previous": {
      "description": "The number of the previous bill in the series, so a gap is visible.",
      "$ref": "#/$defs/Number"
    },
    "account": {
      "description": "The contract account (TMF678 billingAccount).",
      "type": "string",
      "pattern": "\\S",
      "maxLength": 128
    },
    "creditor": {
      "description": "The issuing party: the tenant in the event `source` (CE-1).",
      "type": "string",
      "pattern": "\\S",
      "maxLength": 128
    },
    "currency": {
      "type": "string",
      "pattern": "^[A-Z]{3}$"
    },
    "period": {
      "description": "The billing period (TMF678 billingPeriod), both ends inclusive.",
      "$ref": "urn:open-utility-stack:common:time:1#/$defs/DatePeriod"
    },
    "issue_date": {
      "description": "EN 16931 BT-2; TMF678 billDate.",
      "$ref": "urn:open-utility-stack:common:time:1#/$defs/Date"
    },
    "due_date": {
      "description": "EN 16931 BT-9; TMF678 paymentDueDate.",
      "$ref": "urn:open-utility-stack:common:time:1#/$defs/Date"
    },
    "sections": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/Section"
      },
      "description": "In the order the bill shows them. Array order is part of the digest (`common/digest.md`)."
    },
    "vat_groups": {
      "description": "EN 16931 BG-23, one per category and rate (TMF678 taxItem), in the order the bill's lines first use them. Array order is part of the digest.",
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/VatGroup"
      }
    },
    "payments": {
      "description": "Payments already received and applied (Abschläge, transfers), in the order they were applied. Array order is part of the digest.",
      "type": "array",
      "items": {
        "$ref": "#/$defs/Payment"
      }
    },
    "totals": {
      "$ref": "#/$defs/Totals"
    },
    "documents": {
      "description": "The rendered bill: the EN 16931 invoice (XRechnung or ZUGFeRD) and the PDF the customer reads (TMF678 billDocument).",
      "type": "array",
      "items": {
        "$ref": "#/$defs/Document"
      }
    },
    "extensions": {
      "type": "object",
      "propertyNames": {
        "pattern": "^[a-z][a-z0-9-]*(\\.[a-z][a-z0-9-]*)*$"
      },
      "additionalProperties": {
        "type": "object"
      }
    }
  },
  "$defs": {
    "Number": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "series",
        "sequence"
      ],
      "properties": {
        "series": {
          "type": "string",
          "pattern": "^[A-Za-z0-9][A-Za-z0-9-]*$",
          "maxLength": 32
        },
        "sequence": {
          "type": "integer",
          "minimum": 1,
          "maximum": 999999999
        }
      }
    },
    "Section": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "key",
        "producer",
        "title",
        "net",
        "lines"
      ],
      "properties": {
        "key": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9-]*$",
          "maxLength": 32,
          "description": "Unique within the bill: one section per key. A producer with two contracts on one account names two keys."
        },
        "producer": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9-]*$",
          "maxLength": 32
        },
        "title": {
          "type": "string",
          "pattern": "\\S",
          "maxLength": 80
        },
        "section_digest": {
          "description": "Digest of the producer's bill-section@1 document, placed unchanged.",
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        },
        "net": {
          "description": "The sum of the section's line nets.",
          "$ref": "#/$defs/Money2"
        },
        "lines": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/Line"
          },
          "description": "In the order the section shows them."
        }
      }
    },
    "Line": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "charge",
        "description",
        "net",
        "tax_category",
        "tax_rate"
      ],
      "properties": {
        "charge": {
          "description": "The rated-charge@1 `client_reference` the line bills.",
          "$ref": "urn:open-utility-stack:common:reference:1#/$defs/ClientReference"
        },
        "charge_digest": {
          "description": "Digest of that rated-charge@1 document, so the line binds the exact charge.",
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        },
        "corrects": {
          "description": "The charge this line corrects, from the rated charge.",
          "$ref": "urn:open-utility-stack:common:reference:1#/$defs/ClientReference"
        },
        "description": {
          "type": "string",
          "pattern": "\\S",
          "maxLength": 200
        },
        "quantity": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "value",
            "unit"
          ],
          "properties": {
            "value": {
              "type": "string",
              "pattern": "^-?(0|[1-9][0-9]*)(\\.[0-9]+)?$"
            },
            "unit": {
              "type": "string",
              "pattern": "^[A-Z0-9]{2,3}$"
            }
          }
        },
        "unit_price": {
          "$ref": "urn:open-utility-stack:common:money:1#/$defs/Money"
        },
        "net": {
          "description": "The charge's net amount rounded once, half away from zero, to the cent (EN 16931 BT-131).",
          "$ref": "#/$defs/Money2"
        },
        "tax_category": {
          "enum": [
            "S",
            "Z",
            "AE",
            "E",
            "O"
          ]
        },
        "tax_rate": {
          "$ref": "urn:open-utility-stack:common:money:1#/$defs/Rate"
        }
      }
    },
    "VatGroup": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "category",
        "rate",
        "base",
        "tax"
      ],
      "properties": {
        "category": {
          "enum": [
            "S",
            "Z",
            "AE",
            "E",
            "O"
          ]
        },
        "rate": {
          "$ref": "urn:open-utility-stack:common:money:1#/$defs/Rate"
        },
        "base": {
          "description": "EN 16931 BT-116: the sum of the group's line nets.",
          "$ref": "#/$defs/Money2"
        },
        "tax": {
          "description": "EN 16931 BT-117: base × rate, rounded once per group, half away from zero.",
          "$ref": "#/$defs/Money2"
        },
        "exemption": {
          "type": "object",
          "additionalProperties": false,
          "minProperties": 1,
          "properties": {
            "text": {
              "type": "string",
              "pattern": "\\S",
              "maxLength": 200
            },
            "code": {
              "type": "string",
              "pattern": "^VATEX-[A-Z0-9-]+$"
            }
          }
        }
      }
    },
    "Payment": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "date",
        "description",
        "amount"
      ],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "\\S",
          "maxLength": 128
        },
        "date": {
          "$ref": "urn:open-utility-stack:common:time:1#/$defs/Date"
        },
        "description": {
          "type": "string",
          "pattern": "\\S",
          "maxLength": 200
        },
        "amount": {
          "description": "Gross amount received.",
          "$ref": "#/$defs/Money2"
        }
      }
    },
    "Totals": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "net",
        "vat",
        "gross",
        "prepaid",
        "payable"
      ],
      "properties": {
        "net": {
          "description": "EN 16931 BT-109; TMF678 taxExcludedAmount.",
          "$ref": "#/$defs/Money2"
        },
        "vat": {
          "description": "EN 16931 BT-110.",
          "$ref": "#/$defs/Money2"
        },
        "gross": {
          "description": "EN 16931 BT-112; TMF678 taxIncludedAmount.",
          "$ref": "#/$defs/Money2"
        },
        "prepaid": {
          "description": "EN 16931 BT-113.",
          "$ref": "#/$defs/Money2"
        },
        "payable": {
          "description": "EN 16931 BT-115; TMF678 amountDue. Negative is a credit (Guthaben).",
          "$ref": "#/$defs/Money2"
        }
      }
    },
    "Document": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "kind",
        "media_type",
        "digest"
      ],
      "properties": {
        "kind": {
          "enum": [
            "xrechnung-cii",
            "xrechnung-ubl",
            "zugferd",
            "pdf"
          ]
        },
        "media_type": {
          "type": "string",
          "pattern": "^[a-z]+/[a-z0-9.+-]+$"
        },
        "digest": {
          "description": "SHA-256 of the document's bytes.",
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        },
        "uri": {
          "type": "string",
          "format": "uri"
        }
      }
    },
    "Money2": {
      "description": "Money to the cent: a bill's amounts are rounded once, and only here.",
      "allOf": [
        {
          "$ref": "urn:open-utility-stack:common:money:1#/$defs/Money"
        },
        {
          "properties": {
            "scale": {
              "const": 2
            }
          }
        }
      ]
    }
  }
}
