{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://comercioia.cl/ucp/schemas/tax_document/2026-10-16.json",
  "name": "cl.comercioia.shopping.tax_document",
  "version": "2026-10-16",
  "title": "Chilean Tax Document (boleta / factura)",
  "description": "OPEN PROPOSAL, ready to implement and open for comment. Not yet stable: fields may change before the first stable release. Lets the buyer choose a boleta or a factura at checkout (according to the operation and the buyer's tax status, subject to the legal requirements of the document) and returns the SII electronic tax documents (DTE) issued for the order. Amounts are integer CLP (CLP has no minor unit). Version 2026-10-16 adds the references a business buyer requires on its factura (purchase order, HES, contract, dispatch guide) and the payer identification and payment method required on a boleta for a sale covered by SII Res. Ex. 44/2025 whose value exceeds the 135 UF threshold; it only adds optional fields to 2026-10-15. Spec: https://comercioia.cl/spec/#tax-document",
  "requires": {
    "protocol": { "min": "2026-08-25" },
    "capabilities": {
      "dev.ucp.shopping.checkout": { "min": "2026-08-25" },
      "dev.ucp.shopping.order": { "min": "2026-08-25" }
    }
  },
  "$defs": {
    "rut": {
      "type": "string",
      "pattern": "^[0-9]{1,8}-[0-9Kk]$",
      "description": "Chilean taxpayer ID (RUT) without dots, with a hyphen and the modulo 11 check digit, e.g. 76123456-0. Receivers MUST validate the check digit. The generic RUT 66666666-6 is used only in the cases the SII format allows, not in every boleta.",
      "examples": ["76123456-0", "66666666-6"]
    },
    "document_type": {
      "type": "integer",
      "description": "SII DTE type code: 33 factura electrónica, 34 factura exenta, 39 boleta electrónica, 41 boleta exenta, 52 guía de despacho, 56 nota de débito, 61 nota de crédito.",
      "examples": [39, 33, 41, 34]
    },
    "receiver": {
      "type": "object",
      "description": "Receiver of the document: the buying company on a factura, or the payer (who pays, who may differ from the buyer) on a boleta. Required for a factura, with rut, razon_social, giro, direccion and comuna. Required on a boleta for a sale covered by SII Res. Ex. 44/2025 whose value exceeds the 135 UF threshold, with at least the payer's rut and razon_social; on other boletas it is optional. Lengths follow the SII DTE and boleta formats.",
      "required": ["rut", "razon_social"],
      "properties": {
        "rut": {
          "$ref": "#/$defs/rut",
          "description": "The receiver's real RUT (SII RUTRecep). MUST NOT be the generic 66666666-6."
        },
        "razon_social": {
          "type": "string",
          "maxLength": 100,
          "description": "Legal name of a company or, for a person, their full name (nombres y apellidos). SII RznSocRecep."
        },
        "giro": { "type": "string", "maxLength": 40, "description": "Receiver's line of business as registered with the SII. Factura only." },
        "direccion": { "type": "string", "maxLength": 70 },
        "comuna": { "type": "string", "maxLength": 20 },
        "ciudad": { "type": "string", "maxLength": 20 },
        "email": { "type": "string", "format": "email", "description": "Address that receives the DTE." }
      }
    },
    "reference": {
      "type": "object",
      "description": "A document the tax document must reference, as a line of its SII reference zone (Referencia: TpoDocRef, FolioRef, FchRef, RazonRef). Up to 40 per document.",
      "required": ["type", "number"],
      "properties": {
        "type": {
          "type": "string",
          "pattern": "^([0-9]{1,3}|[A-Za-z]{1,3})$",
          "description": "SII TpoDocRef, as a string. A numeric value MUST be a code listed in the SII Formato DTE, e.g. 801 orden de compra, 802 nota de pedido, 803 contrato, 52 guía de despacho electrónica, 33 factura electrónica. An alphabetic value of up to 3 letters is not validated by the SII and may name a non-tax document outside that list, e.g. HES (hoja de entrada de servicios), which some buyers require. Buyers differ: others ask for the HES number under 801 or 802. Businesses MUST send the code the buyer asked for, unchanged.",
          "examples": ["801", "802", "803", "HES", "52"]
        },
        "number": {
          "type": "string",
          "minLength": 1,
          "maxLength": 18,
          "description": "SII FolioRef: the referenced document's number, exactly as the buyer issued it, with no added prefixes or characters. Alphanumeric only for non-tax documents (800 codes or alphabetic types); the folio of a tax document, such as a guía de despacho, is digits only.",
          "examples": ["4500195544", "OC-2026-0457", "1001266280"]
        },
        "date": {
          "type": "string",
          "format": "date",
          "description": "SII FchRef: date of the referenced document, YYYY-MM-DD. Mandatory on a factura; the boleta format has no reference date."
        },
        "reason": {
          "type": "string",
          "maxLength": 90,
          "description": "SII RazonRef: free text printed with the reference."
        }
      }
    },
    "payment_method": {
      "type": "integer",
      "description": "SII MedioPago of a boleta (Formato Boletas Electrónicas v4.2): 1 efectivo, 2 pago electrónico, 3 transferencia electrónica, 4 cheque, 5 otro. Mandatory and printed on a boleta for a sale covered by SII Res. Ex. 44/2025 whose value exceeds the 135 UF threshold. Businesses MUST treat unknown values as 5.",
      "examples": [2, 3, 1, 4, 5]
    },
    "tax_document_request": {
      "type": "object",
      "description": "The buyer's choice of tax document. When absent, the business issues a boleta.",
      "properties": {
        "document_choice": {
          "type": "string",
          "description": "'boleta' (default) or 'factura', according to the operation and the buyer's tax status. The choice at checkout is subject to the legal requirements of the document. Businesses MUST treat unknown values as 'boleta' and say so in messages[].",
          "examples": ["boleta", "factura"]
        },
        "receiver": { "$ref": "#/$defs/receiver" },
        "references": {
          "type": "array",
          "maxItems": 40,
          "items": { "$ref": "#/$defs/reference" },
          "description": "References the buyer requires on its document, typically a factura: purchase order, HES, contract or dispatch guide. Businesses MUST either print them or reject the checkout with a message; they MUST NOT silently drop them."
        },
        "payment_method": {
          "$ref": "#/$defs/payment_method",
          "description": "SII MedioPago the business will print on the boleta. Businesses derive it from the payment handler used and SHOULD echo it in responses when the boleta is for a sale covered by SII Res. Ex. 44/2025 whose value exceeds the 135 UF threshold; a platform MAY send it only when the buyer pays outside the negotiated payment handlers, e.g. cash on delivery."
        }
      },
      "if": {
        "properties": { "document_choice": { "const": "factura" } },
        "required": ["document_choice"]
      },
      "then": {
        "required": ["receiver"],
        "properties": {
          "receiver": { "required": ["rut", "razon_social", "giro", "direccion", "comuna"] },
          "references": { "items": { "required": ["date"] } }
        }
      }
    },
    "issued_document": {
      "type": "object",
      "description": "An SII electronic tax document issued for the order.",
      "required": ["type", "folio", "issuer_rut", "issued_at", "total"],
      "properties": {
        "type": { "$ref": "#/$defs/document_type" },
        "folio": { "type": "integer", "minimum": 1 },
        "issuer_rut": { "$ref": "#/$defs/rut" },
        "receiver_rut": {
          "$ref": "#/$defs/rut",
          "description": "The generic RUT 66666666-6 is used in the cases the SII format allows, for non-periodic, non-residential sales or services when the RUT is unknown and no applicable internal identifier exists. It is not a universal rule for every boleta. It is never used on a boleta for a sale covered by SII Res. Ex. 44/2025 whose value exceeds the 135 UF threshold."
        },
        "issued_at": { "type": "string", "format": "date-time" },
        "total": { "type": "integer", "minimum": 0, "description": "Total in integer CLP, VAT included." },
        "net": { "type": "integer", "minimum": 0 },
        "iva": { "type": "integer", "minimum": 0 },
        "exempt": { "type": "integer", "minimum": 0 },
        "payment_method": {
          "$ref": "#/$defs/payment_method",
          "description": "SII MedioPago printed on the boleta. Present on every boleta for a sale covered by SII Res. Ex. 44/2025 whose value exceeds the 135 UF threshold."
        },
        "references": {
          "type": "array",
          "maxItems": 40,
          "items": { "$ref": "#/$defs/reference" },
          "description": "References as printed in the issued document, in line order (NroLinRef)."
        },
        "issue_trigger": {
          "type": "string",
          "description": "Event that triggered issuance. In sales of goods, issuance is tied to their actual or symbolic delivery (DL 825 art. 55); for services, the boleta is issued when the remuneration is received or made available, and the factura follows the rules of its tax period. payment_confirmed can be an operational option but does not replace those rules. The business must configure the applicable event.",
          "examples": ["payment_confirmed", "dispatched", "delivered"]
        },
        "sii_track_id": { "type": "string", "description": "SII submission tracking ID." },
        "sii_status": {
          "type": "string",
          "description": "Latest known SII status of the document.",
          "examples": ["pending", "accepted", "accepted_with_objections", "rejected"]
        },
        "representation_url": {
          "type": "string",
          "format": "uri",
          "description": "https link to a printable representation (PDF or HTML) the buyer can keep."
        }
      }
    },
    "dev.ucp.shopping.checkout": {
      "title": "Checkout with Chilean tax document",
      "description": "Checkout extended with the buyer's boleta or factura choice.",
      "allOf": [
        { "$ref": "https://ucp.dev/2026-08-25/schemas/shopping/checkout.json" },
        {
          "type": "object",
          "properties": {
            "tax_document": {
              "$ref": "#/$defs/tax_document_request",
              "ucp_request": { "create": "optional", "update": "optional", "complete": "optional" }
            }
          }
        }
      ]
    },
    "dev.ucp.shopping.order": {
      "title": "Order with Chilean tax documents",
      "description": "Order extended with the SII documents issued for it.",
      "allOf": [
        { "$ref": "https://ucp.dev/2026-08-25/schemas/shopping/order.json" },
        {
          "type": "object",
          "properties": {
            "tax_documents": {
              "type": "array",
              "items": { "$ref": "#/$defs/issued_document" },
              "description": "SII documents issued for this order, in issue order."
            }
          }
        }
      ]
    }
  }
}
