{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Račun rich canonical intake",
  "description": "Closed first delta. Graph uniqueness, class/role references, calendar/period/page checks and exact source read-back are enforced by the bills owner. Rich canonicalization is SHA-256 of lossless canonical bill JSON (JCS-compatible for safe integer pages; older wide page values retain exact decimal fingerprints), associated with the immutable canonical_format; format mismatch conflicts independently of the digest.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "format",
    "bill"
  ],
  "properties": {
    "format": {
      "const": "racun-canonical-rich-1"
    },
    "bill": {
      "$ref": "#/$defs/bill"
    }
  },
  "$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"
        },
        "endpoints": {
          "type": "string",
          "enum": [
            "inclusive",
            "exclusive",
            "start_inclusive_end_exclusive"
          ]
        }
      }
    },
    "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"
      }
    },
    "GraphRef": {
      "type": "string",
      "pattern": "^urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
    },
    "BillIdentifier": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "scheme",
        "value"
      ],
      "properties": {
        "scheme": {
          "$ref": "#/$defs/text"
        },
        "value": {
          "$ref": "#/$defs/text"
        }
      }
    },
    "BillSnapshotParty": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "roles"
      ],
      "properties": {
        "id": {
          "$ref": "#/$defs/GraphRef"
        },
        "roles": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "enum": [
              "issuer",
              "supplier",
              "consolidator",
              "biller",
              "service_operator",
              "network_operator",
              "metering_operator",
              "creditor",
              "payee",
              "debtor",
              "account_holder",
              "payer",
              "tax_authority"
            ]
          }
        },
        "display_name": {
          "$ref": "#/$defs/text"
        },
        "identifiers": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/BillIdentifier"
          }
        }
      }
    },
    "BillProviderAccount": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id"
      ],
      "properties": {
        "id": {
          "$ref": "#/$defs/GraphRef"
        },
        "identifiers": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/BillIdentifier"
          }
        },
        "account_holder_party_ref": {
          "$ref": "#/$defs/GraphRef"
        }
      }
    },
    "BillContract": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "type"
      ],
      "properties": {
        "id": {
          "$ref": "#/$defs/GraphRef"
        },
        "identifiers": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/BillIdentifier"
          }
        },
        "type": {
          "$ref": "#/$defs/text"
        },
        "provider_account_ref": {
          "$ref": "#/$defs/GraphRef"
        }
      }
    },
    "BillBillingPoint": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "type"
      ],
      "properties": {
        "id": {
          "$ref": "#/$defs/GraphRef"
        },
        "identifiers": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/BillIdentifier"
          }
        },
        "type": {
          "$ref": "#/$defs/text"
        },
        "contract_ref": {
          "$ref": "#/$defs/GraphRef"
        }
      }
    },
    "BillDevice": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "type"
      ],
      "properties": {
        "id": {
          "$ref": "#/$defs/GraphRef"
        },
        "identifiers": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/BillIdentifier"
          }
        },
        "type": {
          "$ref": "#/$defs/text"
        },
        "billing_point_ref": {
          "$ref": "#/$defs/GraphRef"
        }
      }
    },
    "BillServiceComponent": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "service_type"
      ],
      "properties": {
        "id": {
          "$ref": "#/$defs/GraphRef"
        },
        "service_type": {
          "$ref": "#/$defs/text"
        },
        "provider_account_ref": {
          "$ref": "#/$defs/GraphRef"
        },
        "contract_ref": {
          "$ref": "#/$defs/GraphRef"
        },
        "billing_point_ref": {
          "$ref": "#/$defs/GraphRef"
        },
        "operator_party_ref": {
          "$ref": "#/$defs/GraphRef"
        },
        "totals": {
          "$ref": "#/$defs/BillServiceTotals"
        }
      }
    },
    "BillCompleteness": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "level"
      ],
      "properties": {
        "level": {
          "type": "string",
          "enum": [
            "complete",
            "partial",
            "payment_only",
            "summary_only",
            "unknown"
          ]
        },
        "pages_received": {
          "type": "integer",
          "minimum": 1
        },
        "pages_expected": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "BillSourceDates": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "service_performed_on": {
          "$ref": "#/$defs/date"
        },
        "meter_read_on": {
          "$ref": "#/$defs/date"
        },
        "tax_point_on": {
          "$ref": "#/$defs/date"
        }
      },
      "minProperties": 1
    },
    "bill": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "source_document_id",
        "revision",
        "revision_kind",
        "document_number",
        "currency",
        "customer",
        "obligation",
        "completeness"
      ],
      "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"
            },
            "provider_account_ref": {
              "$ref": "#/$defs/GraphRef"
            }
          }
        },
        "lines": {
          "description": "detail items, only when the source sends them.",
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "description",
              "semantic_class"
            ],
            "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"
              },
              "semantic_class": {
                "type": "string",
                "enum": [
                  "charge",
                  "tax",
                  "discount",
                  "credit",
                  "fee",
                  "interest",
                  "rate",
                  "measurement",
                  "calculation_factor",
                  "informational"
                ]
              },
              "service_component_ref": {
                "$ref": "#/$defs/GraphRef"
              }
            }
          }
        },
        "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"
              },
              "service_component_ref": {
                "$ref": "#/$defs/GraphRef"
              },
              "device_ref": {
                "$ref": "#/$defs/GraphRef"
              }
            }
          }
        },
        "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,
          "properties": {
            "net": {
              "$ref": "#/$defs/money"
            },
            "vat": {
              "$ref": "#/$defs/money"
            },
            "gross": {
              "$ref": "#/$defs/money"
            }
          },
          "minProperties": 1
        },
        "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), 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"
            },
            "payee_party_ref": {
              "$ref": "#/$defs/GraphRef"
            }
          }
        },
        "document_type": {
          "type": "string",
          "enum": [
            "invoice",
            "bill",
            "reminder",
            "payment_notice",
            "payment_slip",
            "statement",
            "credit_note",
            "debit_note",
            "correction",
            "cancellation",
            "receipt"
          ]
        },
        "completeness": {
          "$ref": "#/$defs/BillCompleteness"
        },
        "language": {
          "type": "string",
          "minLength": 1,
          "description": "Source-supplied BCP 47 language tag, validated by intake."
        },
        "dates": {
          "$ref": "#/$defs/BillSourceDates"
        },
        "parties": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/BillSnapshotParty"
          }
        },
        "provider_accounts": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/BillProviderAccount"
          }
        },
        "contracts": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/BillContract"
          }
        },
        "billing_points": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/BillBillingPoint"
          }
        },
        "devices": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/BillDevice"
          }
        },
        "service_components": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/BillServiceComponent"
          }
        },
        "relationships": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/BillRelationship"
          }
        },
        "adjustments": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/Adjustment"
          }
        },
        "balance_snapshot": {
          "$ref": "#/$defs/BalanceSnapshot"
        },
        "payment_instructions": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/transfer_instruction"
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "currency": {
                "const": "BAM"
              }
            }
          },
          "then": {
            "not": {
              "anyOf": [
                {
                  "required": [
                    "payment_instruction"
                  ]
                },
                {
                  "required": [
                    "payment_instructions"
                  ]
                }
              ]
            }
          }
        },
        {
          "if": {
            "properties": {
              "completeness": {
                "properties": {
                  "level": {
                    "const": "payment_only"
                  }
                }
              }
            }
          },
          "then": {
            "not": {
              "anyOf": [
                {
                  "required": [
                    "totals"
                  ]
                },
                {
                  "required": [
                    "lines"
                  ]
                },
                {
                  "required": [
                    "vat_summary"
                  ]
                },
                {
                  "required": [
                    "measurements"
                  ]
                },
                {
                  "required": [
                    "service_components"
                  ]
                }
              ]
            }
          }
        },
        {
          "not": {
            "required": [
              "payment_instruction",
              "payment_instructions"
            ]
          }
        }
      ]
    },
    "BillRelationship": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "target_source_document_id"
      ],
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "corrects",
            "supersedes",
            "cancels",
            "duplicate_of"
          ]
        },
        "target_source_document_id": {
          "$ref": "#/$defs/text"
        },
        "target_revision": {
          "type": "integer",
          "minimum": 1,
          "maximum": 2147483647
        }
      }
    },
    "Adjustment": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "amount",
        "included_in_amount_due",
        "source_fact_id"
      ],
      "properties": {
        "type": {
          "const": "prior_balance"
        },
        "amount": {
          "$ref": "#/$defs/money"
        },
        "included_in_amount_due": {
          "type": "boolean"
        },
        "source_fact_id": {
          "$ref": "#/$defs/text"
        }
      },
      "description": "Source explanation, no new obligation; source_fact_id is unique only within this snapshot adjustment array."
    },
    "BalanceSnapshot": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "as_of",
        "prior_due",
        "included_in_amount_due",
        "source_fact_id"
      ],
      "properties": {
        "as_of": {
          "$ref": "#/$defs/date"
        },
        "prior_due": {
          "$ref": "#/$defs/money"
        },
        "included_in_amount_due": {
          "type": "boolean"
        },
        "source_fact_id": {
          "$ref": "#/$defs/text"
        }
      },
      "description": "Same source_fact_id as an adjustment requires equal amount and included boolean. Owner enforces this within the snapshot only."
    },
    "transfer_instruction": {
      "type": "object",
      "additionalProperties": false,
      "description": "Alternative for the entire obligation in EUR. Lowercase urn:uuid IDs share the existing Local ID namespace of this version, unique across graph classes and instructions, dates are inclusive and ordered. Partial payment is source information only. Bills owner checks equality with currency and amount_due.",
      "required": [
        "id",
        "type",
        "currency",
        "amount",
        "destination",
        "reference",
        "partial_payment"
      ],
      "properties": {
        "payee": {
          "description": "detail (primatelj), 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"
            }
          }
        },
        "payee_party_ref": {
          "$ref": "#/$defs/GraphRef"
        },
        "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"
        },
        "id": {
          "$ref": "#/$defs/GraphRef"
        },
        "type": {
          "const": "credit_transfer"
        },
        "currency": {
          "const": "EUR"
        },
        "amount": {
          "$ref": "#/$defs/money"
        },
        "destination": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "scheme",
            "value"
          ],
          "properties": {
            "scheme": {
              "const": "HR:IBAN"
            },
            "value": {
              "description": "HUB3 field 10: Croatian IBAN. The mod-97 check (ISO 13616) runs in code.",
              "type": "string",
              "pattern": "^HR[0-9]{19}$"
            }
          }
        },
        "reference": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "scheme",
            "value"
          ],
          "properties": {
            "scheme": {
              "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"
              ]
            },
            "value": {
              "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}$"
            }
          }
        },
        "preferred": {
          "type": "boolean"
        },
        "valid_from": {
          "$ref": "#/$defs/date"
        },
        "valid_until": {
          "$ref": "#/$defs/date"
        },
        "partial_payment": {
          "type": "string",
          "enum": [
            "allowed",
            "forbidden",
            "unspecified"
          ]
        }
      }
    },
    "BillServiceTotals": {
      "type": "object",
      "additionalProperties": false,
      "minProperties": 1,
      "description": "Source-only net/tax/gross in the document currency; absent is not zero. No calculation or payment authority.",
      "properties": {
        "net": {
          "$ref": "#/$defs/money"
        },
        "tax": {
          "$ref": "#/$defs/money"
        },
        "gross": {
          "$ref": "#/$defs/money"
        }
      }
    }
  }
}
