{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kaikei.keydrop.net/schema/0.1/common.schema.json",
  "title": "OLJ 共通の定義",
  "$defs": {
    "date": {
      "type": "string",
      "pattern": "^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])$",
      "description": "YYYY-MM-DD（日本時間の暦日）"
    },
    "datetime": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?[+-]\\d{2}:\\d{2}$",
      "description": "オフセット付きの日時"
    },
    "yearMonth": {
      "type": "string",
      "pattern": "^\\d{4}-(0[1-9]|1[0-2])$"
    },
    "ulid": {
      "type": "string",
      "pattern": "^[0-9A-HJKMNP-TV-Z]{26}$"
    },
    "identifier": {
      "type": "string",
      "pattern": "^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$",
      "description": "取引先・部門・タグ・会計期間などの ID"
    },
    "accountCode": {
      "type": "string",
      "pattern": "^\\d{4}$"
    },
    "subAccountCode": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2,}$"
    },
    "amount": {
      "type": "integer",
      "minimum": 0,
      "description": "最小通貨単位の整数（0 以上）"
    },
    "decimal": {
      "type": "string",
      "pattern": "^-?(0|[1-9]\\d*)(\\.\\d+)?$",
      "description": "10 進数の文字列"
    },
    "ratio": {
      "type": "string",
      "pattern": "^(0(\\.\\d+)?|1(\\.0+)?)$",
      "description": "0 以上 1 以下の割合（10 進数の文字列）"
    },
    "rate": {
      "type": "string",
      "pattern": "^0\\.\\d+$",
      "description": "税率（例: \"0.10\"）"
    },
    "sha256": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "invoiceRegistrationNumber": {
      "type": "string",
      "pattern": "^T\\d{13}$"
    },
    "currencyCode": {
      "type": "string",
      "pattern": "^[A-Z]{3}$"
    },
    "side": {
      "enum": [
        "debit",
        "credit"
      ]
    },
    "name": {
      "type": "object",
      "required": [
        "ja"
      ],
      "properties": {
        "ja": {
          "type": "string",
          "minLength": 1
        }
      },
      "additionalProperties": {
        "type": "string"
      },
      "description": "言語コードをキーにした表示名"
    },
    "extensions": {
      "type": "object",
      "propertyNames": {
        "pattern": "^x-[a-z0-9]+(-[a-z0-9]+)*$"
      },
      "description": "ベンダーの拡張。キーは x-<ベンダー名>-<名前>"
    },
    "validity": {
      "type": "object",
      "properties": {
        "validFrom": {
          "$ref": "#/$defs/date"
        },
        "validTo": {
          "$ref": "#/$defs/date"
        }
      }
    },
    "tax": {
      "type": "object",
      "description": "行の消費税の情報（06-tax.md）",
      "required": [
        "category"
      ],
      "properties": {
        "category": {
          "$ref": "#/$defs/taxCategory"
        },
        "rate": {
          "$ref": "#/$defs/rate"
        },
        "reduced": {
          "type": "boolean"
        },
        "amount": {
          "$ref": "#/$defs/amount"
        },
        "input": {
          "enum": [
            "inclusive",
            "exclusive"
          ]
        },
        "adjustment": {
          "enum": [
            null,
            "return",
            "bad_debt",
            "bad_debt_recovered"
          ]
        },
        "invoice": {
          "$ref": "#/$defs/invoice"
        },
        "businessType": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 1,
          "maximum": 6
        },
        "purpose": {
          "enum": [
            null,
            "taxable_sales",
            "non_taxable_sales",
            "common"
          ]
        },
        "import": {
          "type": "object",
          "properties": {
            "nationalTax": {
              "$ref": "#/$defs/amount"
            },
            "localTax": {
              "$ref": "#/$defs/amount"
            },
            "customsValue": {
              "$ref": "#/$defs/amount"
            },
            "declarationNumber": {
              "type": "string"
            }
          },
          "additionalProperties": false
        },
        "extensions": {
          "$ref": "#/$defs/extensions"
        },
        "digitalService": {
          "enum": [
            null,
            "business",
            "consumer"
          ],
          "description": "電気通信利用役務の区分（国外事業者からの仕入のみ）"
        }
      },
      "additionalProperties": false,
      "allOf": [
        {
          "if": {
            "properties": {
              "category": {
                "enum": [
                  "sales_taxable",
                  "purchase_taxable",
                  "import_tax",
                  "reverse_charge"
                ]
              }
            }
          },
          "then": {
            "required": [
              "rate",
              "amount"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "category": {
                "enum": [
                  "import_tax"
                ]
              }
            }
          },
          "else": {
            "not": {
              "required": [
                "import"
              ]
            }
          }
        },
        {
          "if": {
            "properties": {
              "category": {
                "const": "reverse_charge"
              }
            }
          },
          "then": {
            "properties": {
              "amount": {
                "const": 0
              }
            }
          }
        }
      ]
    },
    "taxCategory": {
      "enum": [
        "sales_taxable",
        "sales_zero_rated",
        "sales_non_taxable",
        "sales_non_taxable_export",
        "sales_non_taxable_securities",
        "sales_out_of_scope",
        "purchase_taxable",
        "purchase_non_taxable",
        "purchase_out_of_scope",
        "import_tax",
        "reverse_charge"
      ]
    },
    "invoice": {
      "type": "object",
      "required": [
        "type"
      ],
      "properties": {
        "type": {
          "enum": [
            "qualified",
            "non_qualified",
            "books_only",
            "small_amount"
          ]
        },
        "registrationNumber": {
          "$ref": "#/$defs/invoiceRegistrationNumber"
        },
        "booksOnlyReason": {
          "type": [
            "string",
            "null"
          ],
          "description": "税マスタの booksOnlyReasons の id"
        }
      },
      "additionalProperties": false,
      "if": {
        "properties": {
          "type": {
            "const": "books_only"
          }
        }
      },
      "then": {
        "required": [
          "booksOnlyReason"
        ],
        "properties": {
          "booksOnlyReason": {
            "type": "string"
          }
        }
      }
    },
    "taxDefault": {
      "type": "object",
      "description": "入力のときの tax の既定値（集計には使わない）",
      "properties": {
        "category": {
          "$ref": "#/$defs/taxCategory"
        },
        "rate": {
          "$ref": "#/$defs/rate"
        },
        "reduced": {
          "type": "boolean"
        },
        "businessType": {
          "type": "integer",
          "minimum": 1,
          "maximum": 6
        },
        "purpose": {
          "enum": [
            "taxable_sales",
            "non_taxable_sales",
            "common"
          ]
        },
        "invoice": {
          "type": "object",
          "properties": {
            "type": {
              "enum": [
                "qualified",
                "non_qualified",
                "books_only",
                "small_amount"
              ]
            }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    },
    "currency": {
      "type": "object",
      "description": "行の外貨の情報（07-currency.md）",
      "required": [
        "code",
        "minorUnit",
        "amount",
        "rateType"
      ],
      "properties": {
        "code": {
          "$ref": "#/$defs/currencyCode"
        },
        "minorUnit": {
          "type": "integer",
          "minimum": 0,
          "maximum": 4
        },
        "amount": {
          "$ref": "#/$defs/amount"
        },
        "rate": {
          "$ref": "#/$defs/decimal"
        },
        "rateType": {
          "enum": [
            "ttm",
            "tts",
            "ttb",
            "actual",
            "card_statement",
            "custom"
          ]
        },
        "rateDate": {
          "$ref": "#/$defs/date"
        },
        "rateSource": {
          "type": "string"
        },
        "purpose": {
          "enum": [
            "revaluation",
            "fx_settlement"
          ]
        }
      },
      "additionalProperties": false,
      "allOf": [
        {
          "if": {
            "properties": {
              "rateType": {
                "enum": [
                  "ttm",
                  "tts",
                  "ttb",
                  "custom"
                ]
              }
            }
          },
          "then": {
            "required": [
              "rate"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "amount": {
                "const": 0
              }
            }
          },
          "then": {
            "required": [
              "purpose"
            ],
            "properties": {
              "purpose": {
                "const": "revaluation"
              }
            }
          }
        }
      ]
    },
    "settlement": {
      "type": "object",
      "required": [
        "key"
      ],
      "properties": {
        "key": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "dueDate": {
          "$ref": "#/$defs/date"
        },
        "type": {
          "enum": [
            "fee",
            "discount",
            "offset",
            "bad_debt",
            "applied",
            "refund",
            "transfer",
            "other"
          ],
          "description": "入金・支払以外で請求の残高を動かす行の種類（振込手数料の負担・値引・相殺・貸倒・ほかの請求への充当・返金・前受金などへの振替）。省略は、請求（通常の側）か、入金・支払（反対側）"
        }
      },
      "additionalProperties": false
    },
    "withholding": {
      "type": "object",
      "required": [
        "kind"
      ],
      "properties": {
        "kind": {
          "enum": [
            "salary",
            "retirement",
            "reward",
            "interest",
            "dividend",
            "other"
          ]
        },
        "baseAmount": {
          "$ref": "#/$defs/amount"
        }
      },
      "additionalProperties": false
    },
    "allocation": {
      "type": "object",
      "required": [
        "businessRatio"
      ],
      "properties": {
        "businessRatio": {
          "$ref": "#/$defs/ratio"
        }
      },
      "additionalProperties": false
    },
    "origin": {
      "type": "object",
      "required": [
        "by"
      ],
      "properties": {
        "by": {
          "enum": [
            "human",
            "rule",
            "history",
            "llm",
            "import",
            "system"
          ]
        },
        "model": {
          "type": "string"
        },
        "confidence": {
          "$ref": "#/$defs/ratio"
        },
        "source": {
          "type": "string"
        },
        "system": {
          "type": "string",
          "description": "取り込み元のシステム名（例: yayoi, moneyforward, freee, accountbook, bank_csv）"
        },
        "externalId": {
          "type": "string",
          "description": "取り込み元での ID。再取り込みの重複判定に使う"
        }
      },
      "additionalProperties": false
    },
    "period": {
      "type": "object",
      "required": [
        "from",
        "to"
      ],
      "properties": {
        "from": {
          "$ref": "common.schema.json#/$defs/date"
        },
        "to": {
          "$ref": "common.schema.json#/$defs/date"
        }
      },
      "additionalProperties": false
    }
  }
}
