{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:open-utility-stack:rated-charge:1",
  "title": "rated-charge@1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "profile",
    "client_reference",
    "source_reference",
    "product",
    "section",
    "service_period",
    "net_amount",
    "tax_category",
    "tax_rate",
    "description",
    "rating_evidence_digest"
  ],
  "properties": {
    "profile": {
      "const": "rated-charge@1"
    },
    "client_reference": {
      "$ref": "urn:open-utility-stack:common:reference:1#/$defs/ClientReference",
      "description": "The line's identity within producer and tenant; replaying the same source yields the same reference."
    },
    "source_reference": {
      "type": "string",
      "pattern": "\\S",
      "description": "The contract, supply point or dispatch the line was rated for: the emitter's own id."
    },
    "account": {
      "description": "The customer's contract account (konto's account reference), where the producer knows it. Otherwise the account system resolves it from `source_reference`.",
      "type": "string",
      "pattern": "\\S",
      "maxLength": 128
    },
    "corrects": {
      "description": "The `client_reference` of the line this one corrects. The corrected line stays as it was; the bill shows this line as its correction.",
      "$ref": "urn:open-utility-stack:common:reference:1#/$defs/ClientReference"
    },
    "product": {
      "type": "string",
      "pattern": "\\S"
    },
    "section": {
      "type": "string",
      "pattern": "\\S"
    },
    "service_period": {
      "$ref": "urn:open-utility-stack:common:time:1#/$defs/DatePeriod",
      "description": "The dates the line covers, both ends inclusive, with their zone."
    },
    "net_amount": {
      "$ref": "urn:open-utility-stack:common:money:1#/$defs/Money",
      "description": "Signed net amount at full precision; negative is a credit. Rounded once, on the bill."
    },
    "quantity": {
      "description": "The rated quantity, where the line has one: 312.4 kWh, 1 month.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "value",
        "unit"
      ],
      "properties": {
        "value": {
          "type": "string",
          "pattern": "^-?(0|[1-9][0-9]*)(\\.[0-9]+)?$"
        },
        "unit": {
          "description": "UN/ECE Recommendation 20 code, as EN 16931 BT-130 uses: KWH, MTQ (m³), MON, DAY, C62 (one).",
          "type": "string",
          "pattern": "^[A-Z0-9]{2,3}$"
        }
      }
    },
    "unit_price": {
      "description": "Net price per one `quantity.unit`, at full precision: 0.3120 EUR per KWH. Never negative (EN 16931 BR-27): a credit has a negative quantity.",
      "allOf": [
        {
          "$ref": "urn:open-utility-stack:common:money:1#/$defs/Money"
        },
        {
          "properties": {
            "value": {
              "not": {
                "pattern": "^-"
              }
            }
          }
        }
      ]
    },
    "tax_category": {
      "enum": [
        "S",
        "Z",
        "AE",
        "E",
        "O"
      ]
    },
    "tax_rate": {
      "$ref": "urn:open-utility-stack:common:money:1#/$defs/Rate"
    },
    "tax_exemption": {
      "description": "Why no VAT is charged: EN 16931 BT-120 (text) and BT-121 (code, the VATEX list). Required for categories E, AE and O (BR-E-10, BR-AE-10, BR-O-10).",
      "type": "object",
      "additionalProperties": false,
      "minProperties": 1,
      "properties": {
        "text": {
          "type": "string",
          "pattern": "\\S",
          "maxLength": 200
        },
        "code": {
          "type": "string",
          "pattern": "^VATEX-[A-Z0-9-]+$",
          "maxLength": 32
        }
      }
    },
    "description": {
      "type": "string",
      "pattern": "\\S"
    },
    "rating_evidence_digest": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "extensions": {
      "type": "object",
      "additionalProperties": {
        "type": "object"
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "tax_category": {
            "enum": [
              "Z",
              "AE",
              "E",
              "O"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "tax_rate": {
            "pattern": "^0(\\.0+)?$"
          }
        }
      },
      "else": {
        "properties": {
          "tax_rate": {
            "not": {
              "pattern": "^0(\\.0+)?$"
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "tax_category": {
            "enum": [
              "E",
              "AE",
              "O"
            ]
          }
        },
        "required": [
          "tax_category"
        ]
      },
      "then": {
        "required": [
          "tax_exemption"
        ]
      },
      "else": {
        "not": {
          "required": [
            "tax_exemption"
          ]
        }
      }
    },
    {
      "dependentRequired": {
        "quantity": [
          "unit_price"
        ],
        "unit_price": [
          "quantity"
        ]
      }
    }
  ],
  "description": "One rated line for one settlement period, before invoice rounding. A profile of TMF635 Usage v5 (RatedProductUsage). The rating engine emits it; the account system (konto) bills it. A line is never edited: a re-rating is a new line that `corrects` the old one."
}
