{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://taips.tap.rsvp/schemas/data-structures/invoice.json",
  "title": "Invoice",
  "description": "Invoice object for detailed payment information per TAIP-16",
  "type": "object",
  "required": ["id", "issueDate", "currencyCode", "lineItems", "total"],
  "properties": {
    "id": {
      "type": "string",
      "description": "Unique identifier for the invoice",
      "examples": ["INV001", "2025-001"]
    },
    "issueDate": {
      "type": "string",
      "format": "date",
      "description": "Date the invoice was issued (ISO 8601 date format YYYY-MM-DD)",
      "examples": ["2025-04-20"]
    },
    "currencyCode": {
      "type": "string",
      "pattern": "^[A-Z]{3}$",
      "description": "ISO 4217 currency code for invoice amounts",
      "examples": ["USD", "EUR", "GBP"]
    },
    "lineItems": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/lineItem"
      },
      "minItems": 1,
      "description": "Individual items being invoiced"
    },
    "taxTotal": {
      "$ref": "#/$defs/taxTotal",
      "description": "Aggregate tax information"
    },
    "total": {
      "type": "number",
      "minimum": 0,
      "description": "Total amount of the invoice, including taxes"
    },
    "subTotal": {
      "type": "number",
      "minimum": 0,
      "description": "Sum of line totals before taxes"
    },
    "dueDate": {
      "type": "string",
      "format": "date",
      "description": "Payment due date (ISO 8601 date format YYYY-MM-DD)",
      "examples": ["2025-05-20"]
    },
    "note": {
      "type": "string",
      "description": "Additional notes or terms for the invoice"
    },
    "paymentTerms": {
      "type": "string",
      "description": "Terms of payment",
      "examples": ["Net 30", "Due on receipt", "2/10 net 30"]
    },
    "accountingCost": {
      "type": "string",
      "description": "Buyer's accounting code for routing costs to specific accounts"
    },
    "orderReference": {
      "$ref": "#/$defs/orderReference",
      "description": "Information about a related order"
    },
    "additionalDocumentReference": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/documentReference"
      },
      "description": "References to additional documents"
    }
  },
  "$defs": {
    "lineItem": {
      "type": "object",
      "required": ["id", "description", "quantity", "unitPrice", "lineTotal"],
      "properties": {
        "id": {
          "type": "string",
          "description": "Unique identifier for the line item within the invoice"
        },
        "description": {
          "type": "string",
          "description": "Description of the item or service"
        },
        "name": {
          "type": "string",
          "description": "Product name (based on schema.org/Product)"
        },
        "image": {
          "type": "string",
          "format": "uri",
          "description": "URL to an image of the product (based on schema.org/Product)"
        },
        "url": {
          "type": "string",
          "format": "uri",
          "description": "URL to the product page (based on schema.org/Product)"
        },
        "quantity": {
          "type": "number",
          "minimum": 0,
          "description": "Quantity of the item"
        },
        "unitCode": {
          "type": "string",
          "description": "Unit of measure using UBL Unit of Measure Code standard",
          "examples": ["KGM", "LTR", "PCE", "EA", "HUR"]
        },
        "unitPrice": {
          "type": "number",
          "minimum": 0,
          "description": "Price per unit"
        },
        "lineTotal": {
          "type": "number",
          "minimum": 0,
          "description": "Total amount for this line item (typically quantity × unitPrice)"
        },
        "taxCategory": {
          "$ref": "#/$defs/taxCategory",
          "description": "Tax information for the line item"
        }
      }
    },
    "taxCategory": {
      "type": "object",
      "required": ["id", "percent", "taxScheme"],
      "properties": {
        "id": {
          "type": "string",
          "description": "Tax category code according to UN/ECE 5305 Tax Category Code list",
          "examples": ["S", "Z", "E"]
        },
        "percent": {
          "type": "number",
          "minimum": 0,
          "description": "Tax rate percentage"
        },
        "taxScheme": {
          "type": "string",
          "description": "Tax scheme according to UN/ECE 5305 Tax Type Code list",
          "examples": ["VAT", "GST", "SAL"]
        }
      }
    },
    "taxTotal": {
      "type": "object",
      "required": ["taxAmount"],
      "properties": {
        "taxAmount": {
          "type": "number",
          "minimum": 0,
          "description": "Total tax amount for the invoice"
        },
        "taxSubtotal": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/taxSubtotal"
          },
          "description": "Tax breakdown by category"
        }
      }
    },
    "taxSubtotal": {
      "type": "object",
      "required": ["taxableAmount", "taxAmount", "taxCategory"],
      "properties": {
        "taxableAmount": {
          "type": "number",
          "minimum": 0,
          "description": "Amount subject to this tax"
        },
        "taxAmount": {
          "type": "number",
          "minimum": 0,
          "description": "Tax amount for this category"
        },
        "taxCategory": {
          "$ref": "#/$defs/taxCategory"
        }
      }
    },
    "orderReference": {
      "type": "object",
      "required": ["id"],
      "properties": {
        "id": {
          "type": "string",
          "description": "Order identifier"
        },
        "issueDate": {
          "type": "string",
          "format": "date",
          "description": "Date when the order was issued (ISO 8601 date format)"
        }
      }
    },
    "documentReference": {
      "type": "object",
      "required": ["id"],
      "properties": {
        "id": {
          "type": "string",
          "description": "Document identifier"
        },
        "documentType": {
          "type": "string",
          "description": "Type of document using UBL Document Type Code standard",
          "examples": ["Invoice", "CreditNote", "DebitNote", "Order", "Contract", "Timesheet"]
        },
        "url": {
          "type": "string",
          "format": "uri",
          "description": "URL where the document can be accessed"
        }
      }
    }
  },
  "additionalProperties": false
}