{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://hupe1980.github.io/mako/schemas/events/de.mako.billing.charge.rated.json",
  "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": "#/$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": "#/$defs/ClientReference"
    },
    "product": {
      "type": "string",
      "pattern": "\\S"
    },
    "section": {
      "type": "string",
      "pattern": "\\S"
    },
    "service_period": {
      "$ref": "#/$defs/DatePeriod",
      "description": "The dates the line covers, both ends inclusive, with their zone."
    },
    "net_amount": {
      "$ref": "#/$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": "#/$defs/Money"
        },
        {
          "properties": {
            "value": {
              "not": {
                "pattern": "^-"
              }
            }
          }
        }
      ]
    },
    "tax_category": {
      "enum": [
        "S",
        "Z",
        "AE",
        "E",
        "O"
      ]
    },
    "tax_rate": {
      "$ref": "#/$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.",
  "$defs": {
    "ClientReference": {
      "description": "Opaque, chosen by the sender, compared byte for byte; 1 to 128 printable ASCII characters, no space at either end. Its identity is (authenticated sender, value).",
      "type": "string",
      "minLength": 1,
      "maxLength": 128,
      "pattern": "^[!-~]([ -~]*[!-~])?$"
    },
    "DatePeriod": {
      "description": "Business dates, both ends inclusive: a billing period, a service period.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "from",
        "to",
        "zone"
      ],
      "properties": {
        "from": {
          "$ref": "#/$defs/Date"
        },
        "to": {
          "$ref": "#/$defs/Date"
        },
        "zone": {
          "$ref": "#/$defs/Zone"
        }
      }
    },
    "Date": {
      "description": "A civil date, YYYY-MM-DD.",
      "type": "string",
      "format": "date"
    },
    "Zone": {
      "description": "An IANA time-zone name. The stack's business zone is Europe/Berlin.",
      "type": "string",
      "pattern": "^[A-Za-z][A-Za-z0-9_+-]*(/[A-Za-z0-9_+-]+)*$",
      "maxLength": 64
    },
    "Money": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "currency",
        "value",
        "scale"
      ],
      "properties": {
        "currency": {
          "description": "ISO 4217 alphabetic code.",
          "type": "string",
          "pattern": "^[A-Z]{3}$"
        },
        "value": {
          "description": "Signed decimal string, no exponent, no leading zeros, no plus sign.",
          "type": "string",
          "pattern": "^-?(0|[1-9][0-9]*)(\\.[0-9]+)?$"
        },
        "scale": {
          "description": "The number of fractional digits in `value`.",
          "type": "integer",
          "minimum": 0,
          "maximum": 28
        }
      }
    },
    "Rate": {
      "description": "A fraction as a decimal string, such as a VAT rate of 0.19.",
      "type": "string",
      "pattern": "^(0(\\.[0-9]+)?|1(\\.0+)?)$"
    }
  },
  "x-dataschema": "urn:open-utility-stack:rated-charge:1"
}
