{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Račun canonical bill version",
  "description": "One version of one provider bill in Račun's own contract. It is not a ZGH or Ubillty format and not a transport envelope. The provider is not a field: it comes from the authenticated channel or the import. identity is provider + source_document_id + revision; the document holds no source meta-fields, so all of it is fingerprint content.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "source_document_id",
    "revision",
    "revision_kind",
    "document_number",
    "issue_date",
    "currency",
    "customer",
    "totals",
    "obligation"
  ],
  "properties": {
    "source_document_id": {
      "description": "identity: the provider's ID for this document, unchanged across its revisions.",
      "$ref": "#/$defs/text"
    },
    "revision": {
      "description": "identity: version number of this document.",
      "type": "integer",
      "minimum": 1,
      "maximum": 2147483647
    },
    "revision_kind": {
      "description": "Revisions: correction, replacement and cancellation each arrive as a new revision. The bill view shows the history and the Storniran status.",
      "enum": [
        "original",
        "correction",
        "replacement",
        "cancellation"
      ]
    },
    "document_number": {
      "description": "list, detail and search: the bill number printed on this version.",
      "$ref": "#/$defs/text"
    },
    "issue_date": {
      "description": "list, detail and year filter.",
      "$ref": "#/$defs/date"
    },
    "period": {
      "description": "list and detail: service period, when the bill has one. statistics never read it.",
      "$ref": "#/$defs/period"
    },
    "currency": {
      "description": "currency of every amount in this version, scale 2. Intake checks it against the currency the instance's market file binds (HR: EUR, BA: BAM); a payment_instruction is accepted only where that file declares the HR bank instruction format, so BA bank instructions are not implemented.",
      "enum": [
        "EUR",
        "BAM"
      ]
    },
    "customer": {
      "description": "provider customer/account routing key used by the current read-right predicate. This object is not a protocol Party or a typed-identifier graph, and the number alone never proves access.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "provider_account"
      ],
      "properties": {
        "provider_account": {
          "description": "Account or customer number as the provider prints it.",
          "$ref": "#/$defs/text"
        }
      }
    },
    "lines": {
      "description": "detail items, only when the source sends them.",
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "description",
          "net"
        ],
        "properties": {
          "description": {
            "description": "service or charge.",
            "$ref": "#/$defs/text"
          },
          "quantity": {
            "description": "consumption or quantity, a decimal string.",
            "type": "string",
            "pattern": "^(0|[1-9][0-9]*)(\\.[0-9]+)?$"
          },
          "unit": {
            "description": "unit of the quantity, for example m3.",
            "$ref": "#/$defs/text"
          },
          "net": {
            "description": "line amount before VAT.",
            "$ref": "#/$defs/money"
          }
        }
      }
    },
    "measurements": {
      "description": "statistics (Potrošnja): consumption the provider states explicitly, only when the source sends it. A line's quantity and unit are what a charge was computed on (several lines may repeat one reading), so lines are never read as consumption and a service is never derived from a line description. Statistics attribute each measurement whole to the month of its own period's end; a period of several months is never spread.",
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "service",
          "unit",
          "quantity",
          "period"
        ],
        "properties": {
          "service": {
            "description": "the provider's own label for the measured service, for example Toplinska energija. Compared exactly; Račun keeps no list of services.",
            "$ref": "#/$defs/text"
          },
          "unit": {
            "description": "the provider's unit, for example kWh or m3. Compared exactly and never converted.",
            "$ref": "#/$defs/text"
          },
          "quantity": {
            "description": "consumption measured in the period, a decimal string.",
            "type": "string",
            "pattern": "^(0|[1-9][0-9]*)(\\.[0-9]+)?$"
          },
          "period": {
            "description": "the measurement period as the provider states it; it may differ from the document period. The start must be on or before the end (checked by intake).",
            "$ref": "#/$defs/period"
          }
        }
      }
    },
    "vat_summary": {
      "description": "detail VAT recap, only when the bill has one. Amounts outside the scope of VAT have no entry; a zero rate is 0.00.",
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "rate",
          "base",
          "vat"
        ],
        "properties": {
          "rate": {
            "description": "VAT rate as a fraction: 0.25 is 25 %.",
            "type": "string",
            "pattern": "^0\\.[0-9]{2}$"
          },
          "base": {
            "$ref": "#/$defs/money"
          },
          "vat": {
            "$ref": "#/$defs/money"
          }
        }
      }
    },
    "totals": {
      "description": "document totals as printed. They are separate from the obligation and are never used to compute the amount due.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "net",
        "vat",
        "gross"
      ],
      "properties": {
        "net": {
          "$ref": "#/$defs/money"
        },
        "vat": {
          "$ref": "#/$defs/money"
        },
        "gross": {
          "$ref": "#/$defs/money"
        }
      }
    },
    "obligation": {
      "description": "what the customer owes for this version, separate from the totals.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "amount_due"
      ],
      "properties": {
        "amount_due": {
          "description": "Amount due as the provider states it. Payment initiation and HUB3 field 3 take the payment amount from here.",
          "$ref": "#/$defs/money"
        },
        "due_date": {
          "description": "due date, used for the Kasni status.",
          "$ref": "#/$defs/date"
        }
      }
    },
    "payment_instruction": {
      "description": "Bank transfer details: bank transfer data for the obligation, stored with this bill version. Provider configuration may supply part of it only through an explicit adapter rule applied before canonicalization; a later configuration change never alters an existing version. The amount and currency come from obligation.amount_due and currency. HUB3 field numbers are positions in HUB's PDF417 2D barcode format, version 6. Optional fields are used when the source provides them.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "iban",
        "model",
        "reference"
      ],
      "properties": {
        "payee": {
          "description": "detail (primatelj), intent payee and HUB3 fields 7 to 9, when the source provides them. HUB does not require payee data for a national order.",
          "type": "object",
          "additionalProperties": false,
          "minProperties": 1,
          "properties": {
            "name": {
              "$ref": "#/$defs/text"
            },
            "street": {
              "description": "Street and number.",
              "$ref": "#/$defs/text"
            },
            "postcode": {
              "$ref": "#/$defs/text"
            },
            "city": {
              "$ref": "#/$defs/text"
            }
          }
        },
        "iban": {
          "description": "HUB3 field 10: Croatian IBAN. The mod-97 check (ISO 13616) runs in code.",
          "type": "string",
          "pattern": "^HR[0-9]{19}$"
        },
        "model": {
          "description": "HUB3 field 11: reference model. The current profile implements HR01 and HR05; other models, HR99 included, are not implemented, which does not make them invalid.",
          "enum": [
            "HR01",
            "HR05"
          ]
        },
        "reference": {
          "description": "HUB3 field 12: for HR01 and HR05, one to three groups of at most 12 digits separated by hyphens, at most 22 characters (HUB reference models). The check digits run in code.",
          "type": "string",
          "maxLength": 22,
          "pattern": "^[0-9]{1,12}(-[0-9]{1,12}){0,2}$"
        },
        "purpose_code": {
          "description": "HUB3 field 13: ISO 20022 purpose code, when the source provides it. HUB says it is not mandatory.",
          "type": "string",
          "pattern": "^[A-Z]{4}$"
        },
        "description": {
          "description": "detail and HUB3 field 14: payment description, when the source provides it.",
          "$ref": "#/$defs/text"
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "currency": {
            "const": "BAM"
          }
        }
      },
      "then": {
        "not": {
          "required": [
            "payment_instruction"
          ]
        }
      }
    }
  ],
  "$defs": {
    "text": {
      "type": "string",
      "minLength": 1
    },
    "period": {
      "description": "A calendar interval whose start is on or before its end (checked by intake).",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "start",
        "end"
      ],
      "properties": {
        "start": {
          "$ref": "#/$defs/date"
        },
        "end": {
          "$ref": "#/$defs/date"
        }
      }
    },
    "date": {
      "description": "a calendar date. Validators that treat format as an annotation still need a calendar check in code.",
      "type": "string",
      "format": "date",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$"
    },
    "money": {
      "description": "a decimal string with EUR/BAM's two decimals, never a JSON number. Zero and negative are allowed. Zero is written 0.00, never -0.00: PostgreSQL numeric has no negative zero, and a stored version must read back exactly.",
      "type": "string",
      "pattern": "^-?(0|[1-9][0-9]*)\\.[0-9]{2}$",
      "not": {
        "const": "-0.00"
      }
    }
  }
}
