{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.yomanai.ai/developers/entry-webhook.v1.schema.json",
  "title": "Yomanai entry webhook, delivery body, version 1",
  "description": "The body of every POST Yomanai makes to a company's webhook endpoint: one approved journal entry, carrying what Yomanai's file export for the company's accounting system would carry plus the identifiers a webhook needs. Prose: https://www.yomanai.ai/developers/entry-webhook. Within version 1 Yomanai may add properties and may add values to company.ledger; consumers ignore what they do not know.",
  "type": "object",
  "required": ["webhook_version", "delivery_id", "sent_at", "company", "document", "supplier", "supplier_cards", "entry"],
  "properties": {
    "webhook_version": { "const": 1, "description": "Also sent as the Yomanai-Webhook-Version header." },
    "delivery_id": { "$ref": "#/$defs/uuid", "description": "Unique per delivery; equals the Idempotency-Key header. Retries reuse it, a resend after a fix mints a new one." },
    "sent_at": { "type": "string", "format": "date-time", "description": "When this request was built." },
    "company": {
      "type": "object",
      "required": ["id", "name", "registration_number", "ledger"],
      "properties": {
        "id": { "$ref": "#/$defs/uuid", "description": "The company in Yomanai. Stable." },
        "name": { "type": "string" },
        "registration_number": { "type": ["string", "null"], "description": "The company's VAT registration number." },
        "ledger": { "type": "string", "description": "The accounting system the values are shaped for; decides the vocabulary of entry_type_code, currency_code and account keys. Known: hashavshevet, rivhit, priority. Extensible.", "examples": ["hashavshevet", "rivhit", "priority"] }
      }
    },
    "document": {
      "type": "object",
      "required": ["id", "url", "number", "allocation_number"],
      "properties": {
        "id": { "$ref": "#/$defs/uuid", "description": "The source document. Stable across resends; with entry.leg it identifies the entry." },
        "url": { "type": "string", "format": "uri", "description": "Opens the document in Yomanai for a signed-in user; the link the file export writes." },
        "number": { "type": ["string", "null"], "description": "The document number as printed, in full." },
        "allocation_number": { "type": ["string", "null"], "description": "מספר הקצאה." }
      }
    },
    "supplier": {
      "oneOf": [{ "$ref": "#/$defs/supplier_card" }, { "type": "null" }],
      "description": "The supplier card the entry is booked against, as the export ships it; null for an entry that carries no supplier card (a typed entry without one)."
    },
    "supplier_cards": {
      "type": "array",
      "items": { "$ref": "#/$defs/supplier_card" },
      "description": "Every supplier card the entry's lines address, as the export ships them, the supplier's own first; create or update each before booking the entry. Empty when the entry addresses none."
    },
    "entry": {
      "type": "object",
      "required": ["leg", "entry_type_code", "date", "value_date", "reference_1", "reference_2", "document_number", "description", "currency", "currency_code", "books_currency", "exchange_rate", "amount_total", "cost_center", "registration_number", "lines"],
      "properties": {
        "leg": { "enum": ["expense", "payment"] },
        "entry_type_code": { "type": "string", "description": "סוג תנועה in the company's ledger vocabulary, frozen at approval." },
        "date": { "$ref": "#/$defs/date", "description": "The accounting date." },
        "value_date": { "oneOf": [{ "$ref": "#/$defs/date" }, { "type": "null" }] },
        "reference_1": { "type": "string", "description": "What the company configured for the ledger's first reference field; may be empty." },
        "reference_2": { "type": "string", "description": "What the company configured for the ledger's second reference field; may be empty." },
        "document_number": { "type": "string", "description": "The document number as the ledger's note field receives it: shortened to the length the company configured, whole when none is configured. document.number stays the printed number." },
        "description": { "type": "string" },
        "currency": { "$ref": "#/$defs/iso_currency" },
        "currency_code": { "type": ["string", "null"], "description": "The same currency in the ledger's own code table, exactly as the file export writes it in the currency field; null when the file leaves it blank." },
        "books_currency": { "$ref": "#/$defs/iso_currency", "description": "The unit of every amount." },
        "exchange_rate": { "oneOf": [{ "$ref": "#/$defs/rate" }, { "type": "null" }] },
        "amount_total": { "$ref": "#/$defs/money", "description": "The document total in the document currency. Negative for a credit note." },
        "cost_center": { "type": ["string", "null"], "description": "קוד תמחיר; expense entries only." },
        "registration_number": { "type": ["string", "null"], "description": "The counterparty's registration number the entry reports (the file's עוסק מורשה field): the number on the document, which may differ from the card's own." },
        "lines": {
          "type": "array",
          "minItems": 2,
          "items": {
            "type": "object",
            "required": ["n", "side", "role", "account_key", "amount", "amount_foreign", "description", "record", "entry_type_code"],
            "properties": {
              "n": { "type": "integer", "minimum": 1 },
              "side": { "enum": ["debit", "credit"] },
              "role": { "enum": ["supplier", "expense", "vat", "payment", "tax_deduction", null], "description": "What the file export encodes by column position." },
              "account_key": { "type": "string" },
              "amount": { "$ref": "#/$defs/money", "description": "In the books currency." },
              "amount_foreign": { "oneOf": [{ "$ref": "#/$defs/money" }, { "type": "null" }], "description": "In the document currency, when it differs from the books currency; null where the file export leaves the foreign amount blank." },
              "description": { "type": ["string", "null"], "description": "A per-line description the bookkeeper wrote; fall back to entry.description." },
              "record": { "type": "integer", "minimum": 0, "description": "Lines sharing a value form one ledger record in Yomanai's file export." },
              "entry_type_code": { "type": ["string", "null"], "description": "The record's own code when it differs from the entry's." }
            }
          }
        }
      }
    }
  },
  "$defs": {
    "uuid": { "type": "string", "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" },
    "date": { "type": "string", "format": "date", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" },
    "iso_currency": { "type": "string", "pattern": "^[A-Z]{3}$" },
    "money": { "type": "string", "pattern": "^-?[0-9]+\\.[0-9]{2}$", "description": "A decimal string with exactly two fraction digits." },
    "rate": { "type": "string", "pattern": "^[0-9]+(\\.[0-9]{1,4})?$", "description": "A decimal string with up to four fraction digits." },
    "supplier_card": {
      "type": "object",
      "required": ["id", "name", "account_key", "registration_number", "withholding_rate"],
      "properties": {
        "id": { "$ref": "#/$defs/uuid", "description": "The supplier card in Yomanai. Stable." },
        "name": { "type": "string", "description": "The card's name." },
        "account_key": { "type": "string", "description": "The card's account key in the company's accounting system, as the lines carry it." },
        "registration_number": { "type": ["string", "null"], "description": "The card's own VAT registration number." },
        "withholding_rate": { "oneOf": [{ "$ref": "#/$defs/rate" }, { "type": "null" }], "description": "ניכוי מס במקור as a fraction (\"0.05\" = 5 %); null when unknown to Yomanai." }
      }
    }
  }
}
