{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kaikei.keydrop.net/schema/0.1/accounts.schema.json",
  "title": "勘定科目",
  "description": "masters/accounts.json と標準の科目セット（04-accounts.md）",
  "type": "object",
  "required": ["accounts"],
  "properties": {
    "basedOn": {
      "type": "string",
      "pattern": "^olj-std-[a-z0-9-]+@\\d{4}\\.\\d+$",
      "description": "もとにした標準の科目セットと版"
    },
    "standardSet": {
      "type": "object",
      "description": "標準の科目セットのファイルだけが持つ",
      "required": ["id", "version", "entityType", "returnForm"],
      "properties": {
        "id": { "type": "string", "pattern": "^olj-std-[a-z0-9-]+$" },
        "version": { "type": "string", "pattern": "^\\d{4}\\.\\d+$" },
        "entityType": { "enum": ["individual", "corporation"] },
        "returnForm": { "enum": ["general", "real_estate", "agriculture", "corporate"] },
        "name": { "$ref": "common.schema.json#/$defs/name" },
        "note": { "type": "string" }
      },
      "additionalProperties": false
    },
    "accounts": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/account" }
    },
    "extensions": { "$ref": "common.schema.json#/$defs/extensions" }
  },
  "additionalProperties": false,
  "$defs": {
    "account": {
      "type": "object",
      "required": ["code", "name", "category", "group", "normalSide", "report"],
      "properties": {
        "code": { "$ref": "common.schema.json#/$defs/accountCode" },
        "name": { "$ref": "common.schema.json#/$defs/name" },
        "searchKey": { "type": "string", "maxLength": 32 },
        "category": { "enum": ["asset", "liability", "equity", "revenue", "expense"] },
        "group": { "$ref": "#/$defs/group" },
        "normalSide": { "$ref": "common.schema.json#/$defs/side" },
        "role": { "oneOf": [{ "$ref": "#/$defs/role" }, { "type": "null" }] },
        "report": { "$ref": "#/$defs/report" },
        "defaultTax": { "oneOf": [{ "$ref": "common.schema.json#/$defs/taxDefault" }, { "type": "null" }] },
        "subAccounts": {
          "type": "array",
          "items": { "$ref": "#/$defs/subAccount" }
        },
        "requiresSubAccount": { "type": "boolean" },
        "currency": { "oneOf": [{ "$ref": "common.schema.json#/$defs/currencyCode" }, { "type": "null" }] },
        "custom": { "type": "boolean" },
        "hidden": { "type": "boolean" },
        "order": { "type": "integer" },
        "validFrom": { "$ref": "common.schema.json#/$defs/date" },
        "validTo": { "$ref": "common.schema.json#/$defs/date" },
        "extensions": { "$ref": "common.schema.json#/$defs/extensions" }
      },
      "additionalProperties": false,
      "allOf": [
        {
          "if": { "properties": { "category": { "enum": ["asset", "liability", "equity"] } } },
          "then": { "properties": { "group": { "enum": ["current_assets", "fixed_assets", "deferred_assets", "current_liabilities", "long_term_liabilities", "equity"] } } },
          "else": { "properties": { "group": { "enum": ["sales", "cost_of_sales", "sga", "non_operating_income", "non_operating_expenses", "extraordinary_income", "extraordinary_losses", "income_taxes", "manufacturing_cost"] } } }
        }
      ]
    },
    "subAccount": {
      "type": "object",
      "required": ["code", "name"],
      "properties": {
        "code": { "$ref": "common.schema.json#/$defs/subAccountCode" },
        "name": { "$ref": "common.schema.json#/$defs/name" },
        "searchKey": { "type": "string", "maxLength": 32 },
        "defaultTax": { "$ref": "common.schema.json#/$defs/taxDefault" },
        "defaultAllocation": { "$ref": "common.schema.json#/$defs/allocation" },
        "currency": { "$ref": "common.schema.json#/$defs/currencyCode" },
        "partner": { "$ref": "common.schema.json#/$defs/identifier" },
        "hidden": { "type": "boolean" },
        "validFrom": { "$ref": "common.schema.json#/$defs/date" },
        "validTo": { "$ref": "common.schema.json#/$defs/date" },
        "extensions": { "$ref": "common.schema.json#/$defs/extensions" }
      },
      "additionalProperties": false
    },
    "report": {
      "type": "object",
      "minProperties": 1,
      "properties": {
        "blueReturn": { "$ref": "#/$defs/blueReturnLine" },
        "realEstate": { "type": "string", "description": "不動産所得用の欄の ID（標準の科目セットで定義）" },
        "agriculture": { "type": "string", "description": "農業所得用の欄の ID（標準の科目セットで定義）" },
        "corporate": { "type": "string", "minLength": 1, "description": "法人の決算書の表示科目" },
        "etaxCode": { "type": "string", "pattern": "^\\d{2}[A-F]\\d{6}(-\\d+)?$", "description": "e-Tax の財務諸表 CSV の勘定科目コード" }
      },
      "additionalProperties": false
    },
    "blueReturnLine": {
      "description": "青色申告決算書（一般用）の欄の ID。balance_sheet は貸借対照表だけに出る科目",
      "enum": [
        "balance_sheet",
        "sales", "opening_inventory", "purchases", "closing_inventory",
        "taxes_dues", "packing_freight", "utilities", "travel", "communication",
        "advertising", "entertainment", "insurance", "repairs", "supplies",
        "depreciation", "welfare", "wages", "outsourcing", "interest_discount",
        "rent", "bad_debts",
        "blank_1", "blank_2", "blank_3", "blank_4", "blank_5", "blank_6",
        "miscellaneous", "family_employee_salary",
        "bad_debt_allowance_reversal", "bad_debt_allowance"
      ]
    },
    "group": {
      "enum": [
        "current_assets", "fixed_assets", "deferred_assets",
        "current_liabilities", "long_term_liabilities", "equity",
        "sales", "cost_of_sales", "sga",
        "non_operating_income", "non_operating_expenses",
        "extraordinary_income", "extraordinary_losses",
        "income_taxes", "manufacturing_cost"
      ]
    },
    "role": {
      "enum": [
        "cash", "bank", "receivable", "payable",
        "input_tax", "output_tax", "consumption_tax_payable",
        "owner_draw", "owner_contribution", "owner_capital", "retained_earnings",
        "opening_inventory", "closing_inventory",
        "accumulated_depreciation", "depreciation",
        "fx_gain", "fx_loss", "suspense"
      ]
    }
  }
}
