{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kaikei.keydrop.net/schema/0.1/subledgers.schema.json",
  "title": "補助簿",
  "description": "subledgers/fixed-assets.json・inventory.json・settlements.json（09-subledgers.md）。ファイルごとに $defs の該当する定義で検証する",
  "$defs": {
    "fixedAssetsFile": {
      "type": "object",
      "required": ["assets"],
      "properties": {
        "assets": { "type": "array", "items": { "$ref": "#/$defs/asset" } },
        "extensions": { "$ref": "common.schema.json#/$defs/extensions" }
      },
      "additionalProperties": false
    },
    "asset": {
      "type": "object",
      "required": ["id", "name", "account", "acquiredOn", "acquisitionCost", "method"],
      "properties": {
        "id": { "$ref": "common.schema.json#/$defs/ulid" },
        "name": { "type": "string", "minLength": 1 },
        "managementNumber": { "type": "string" },
        "account": { "$ref": "common.schema.json#/$defs/accountCode" },
        "quantity": { "type": "number", "exclusiveMinimum": 0 },
        "unit": { "type": "string" },
        "area": { "$ref": "common.schema.json#/$defs/decimal" },
        "location": { "type": "string" },
        "acquiredOn": { "$ref": "common.schema.json#/$defs/date" },
        "inServiceOn": { "$ref": "common.schema.json#/$defs/date" },
        "acquisitionCost": { "$ref": "common.schema.json#/$defs/amount" },
        "method": {
          "enum": [
            "straight_line", "declining_200", "declining_250",
            "old_straight_line", "old_declining",
            "lump_sum_3y", "small_amount_immediate",
            "equal", "voluntary", "none"
          ]
        },
        "usefulLifeYears": { "type": "integer", "minimum": 1, "maximum": 100 },
        "residualValue": { "$ref": "common.schema.json#/$defs/amount" },
        "businessRatio": { "$ref": "common.schema.json#/$defs/ratio" },
        "rentalRatio": { "$ref": "common.schema.json#/$defs/ratio" },
        "department": { "oneOf": [{ "$ref": "common.schema.json#/$defs/identifier" }, { "type": "null" }] },
        "acquisitionEntry": { "$ref": "common.schema.json#/$defs/ulid" },
        "evidence": { "type": "array", "items": { "$ref": "common.schema.json#/$defs/ulid" }, "uniqueItems": true },
        "revisedAcquisitionCost": { "$ref": "common.schema.json#/$defs/amount" },
        "specialDepreciation": {
          "type": "object",
          "required": ["type", "amount"],
          "properties": {
            "type": { "type": "string" },
            "amount": { "$ref": "common.schema.json#/$defs/amount" }
          },
          "additionalProperties": false
        },
        "compressionAmount": { "$ref": "common.schema.json#/$defs/amount" },
        "leaseType": { "enum": ["finance_transfer", "finance_non_transfer", "operating"] },
        "propertyTaxTarget": { "type": "boolean" },
        "smallAmountSpecial": { "type": "boolean" },
        "opening": {
          "oneOf": [
            { "type": "null" },
            {
              "type": "object",
              "required": ["period", "accumulated"],
              "properties": {
                "period": { "$ref": "common.schema.json#/$defs/identifier" },
                "accumulated": { "$ref": "common.schema.json#/$defs/amount" }
              },
              "additionalProperties": false
            }
          ]
        },
        "disposal": {
          "oneOf": [
            { "type": "null" },
            {
              "type": "object",
              "required": ["date", "type"],
              "properties": {
                "date": { "$ref": "common.schema.json#/$defs/date" },
                "type": { "enum": ["sold", "retired", "transferred", "lost"] },
                "proceeds": { "$ref": "common.schema.json#/$defs/amount" },
                "entry": { "$ref": "common.schema.json#/$defs/ulid" }
              },
              "additionalProperties": false
            }
          ]
        },
        "extensions": { "$ref": "common.schema.json#/$defs/extensions" }
      },
      "additionalProperties": false,
      "allOf": [
        {
          "if": { "properties": { "method": { "enum": ["straight_line", "declining_200", "declining_250", "old_straight_line", "old_declining", "equal"] } } },
          "then": { "required": ["usefulLifeYears", "inServiceOn"] }
        }
      ]
    },
    "inventoryFile": {
      "type": "object",
      "required": ["counts"],
      "properties": {
        "counts": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["period", "date", "total"],
            "properties": {
              "period": { "$ref": "common.schema.json#/$defs/identifier" },
              "date": { "$ref": "common.schema.json#/$defs/date" },
              "method": { "enum": ["last_purchase", "cost", "lower_of_cost_or_market", "moving_average", "weighted_average", "fifo", "specific", "retail"] },
              "items": {
                "type": "array",
                "items": {
                  "type": "object",
                  "required": ["name", "amount"],
                  "properties": {
                    "name": { "type": "string" },
                    "quantity": { "type": "number", "minimum": 0 },
                    "unitCost": { "$ref": "common.schema.json#/$defs/amount" },
                    "amount": { "$ref": "common.schema.json#/$defs/amount" }
                  },
                  "additionalProperties": false
                }
              },
              "total": { "$ref": "common.schema.json#/$defs/amount" },
              "entry": { "$ref": "common.schema.json#/$defs/ulid" }
            },
            "additionalProperties": false
          }
        },
        "extensions": { "$ref": "common.schema.json#/$defs/extensions" }
      },
      "additionalProperties": false
    },
    "settlementsFile": {
      "type": "object",
      "required": ["items"],
      "properties": {
        "items": { "type": "array", "items": { "$ref": "#/$defs/settlementItem" } },
        "extensions": { "$ref": "common.schema.json#/$defs/extensions" }
      },
      "additionalProperties": false
    },
    "settlementItem": {
      "type": "object",
      "description": "請求・支払の単位（科目と消込キー）ごとの補足情報。金額と状態は仕訳から計算する",
      "required": ["account", "key"],
      "properties": {
        "account": { "$ref": "common.schema.json#/$defs/accountCode" },
        "key": { "type": "string", "minLength": 1 },
        "lines": {
          "type": "array",
          "description": "行に settlement.key のない仕訳を、この請求に結び付ける（確定した仕訳を書き換えずに、消込キーを後から付ける）",
          "items": {
            "type": "object",
            "required": ["entry", "line"],
            "properties": {
              "entry": { "$ref": "common.schema.json#/$defs/ulid" },
              "line": { "type": "integer", "minimum": 0 }
            },
            "additionalProperties": false
          }
        },
        "partner": { "$ref": "common.schema.json#/$defs/identifier" },
        "partnerName": { "type": "string", "minLength": 1 },
        "title": { "type": "string", "minLength": 1 },
        "documentNumber": { "type": "string", "minLength": 1 },
        "issuedOn": { "$ref": "common.schema.json#/$defs/date" },
        "dueDate": { "$ref": "common.schema.json#/$defs/date" },
        "expectedDate": { "$ref": "common.schema.json#/$defs/date", "description": "入金・支払の見込み日（期日と違うときだけ）" },
        "settlementAccount": { "$ref": "common.schema.json#/$defs/identifier", "description": "入金される（支払に使う）見込みの決済口座の ID" },
        "memo": { "type": "string" },
        "extensions": { "$ref": "common.schema.json#/$defs/extensions" }
      },
      "additionalProperties": false
    }
  }
}
