{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kaikei.keydrop.net/schema/0.1/journal-month.schema.json",
  "title": "仕訳（暦月ごとのファイル）",
  "description": "journals/YYYY/YYYY-MM.json（05-journals.md）。貸借一致などの業務ルールは Validator が検証する",
  "type": "object",
  "required": [
    "month",
    "entries"
  ],
  "properties": {
    "month": {
      "$ref": "common.schema.json#/$defs/yearMonth"
    },
    "entries": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/entry"
      }
    },
    "extensions": {
      "$ref": "common.schema.json#/$defs/extensions"
    }
  },
  "additionalProperties": false,
  "$defs": {
    "entry": {
      "type": "object",
      "required": [
        "id",
        "date",
        "description",
        "status",
        "lines"
      ],
      "properties": {
        "id": {
          "$ref": "common.schema.json#/$defs/ulid"
        },
        "date": {
          "$ref": "common.schema.json#/$defs/date"
        },
        "number": {
          "type": "string",
          "maxLength": 32
        },
        "description": {
          "type": "string",
          "minLength": 1,
          "maxLength": 500
        },
        "voucherType": {
          "enum": [
            "transfer",
            "receipt",
            "payment"
          ]
        },
        "adjustment": {
          "type": "boolean"
        },
        "adjustmentScope": {
          "enum": [
            "final",
            "interim"
          ]
        },
        "status": {
          "enum": [
            "draft",
            "confirmed"
          ]
        },
        "partner": {
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "department": {
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "lines": {
          "type": "array",
          "minItems": 2,
          "items": {
            "$ref": "#/$defs/line"
          }
        },
        "evidence": {
          "type": "array",
          "items": {
            "$ref": "common.schema.json#/$defs/ulid"
          },
          "uniqueItems": true
        },
        "tags": {
          "$ref": "#/$defs/tagList"
        },
        "memo": {
          "type": "string",
          "maxLength": 2000
        },
        "origin": {
          "$ref": "common.schema.json#/$defs/origin"
        },
        "reverses": {
          "oneOf": [
            {
              "$ref": "common.schema.json#/$defs/ulid"
            },
            {
              "type": "null"
            }
          ]
        },
        "corrects": {
          "description": "訂正後の仕訳のとき、訂正した元の仕訳の id（元の仕訳は取消仕訳で取り消す）",
          "oneOf": [
            {
              "$ref": "common.schema.json#/$defs/ulid"
            },
            {
              "type": "null"
            }
          ]
        },
        "createdAt": {
          "$ref": "common.schema.json#/$defs/datetime"
        },
        "confirmedAt": {
          "$ref": "common.schema.json#/$defs/datetime"
        },
        "extensions": {
          "$ref": "common.schema.json#/$defs/extensions"
        },
        "partnerName": {
          "type": "string",
          "maxLength": 255,
          "description": "取引先マスタにない相手の名前（検索用）"
        },
        "servicePeriod": {
          "$ref": "common.schema.json#/$defs/period",
          "description": "役務の対象期間（〇月分・前払・未払の按分）"
        },
        "createdBy": {
          "type": "string",
          "maxLength": 128
        },
        "confirmedBy": {
          "type": "string",
          "maxLength": 128
        }
      },
      "additionalProperties": false,
      "dependentSchemas": {
        "adjustmentScope": {
          "required": [
            "adjustment"
          ],
          "properties": {
            "adjustment": {
              "const": true
            }
          }
        }
      }
    },
    "line": {
      "type": "object",
      "required": [
        "side",
        "account",
        "amount"
      ],
      "properties": {
        "side": {
          "$ref": "common.schema.json#/$defs/side"
        },
        "account": {
          "$ref": "common.schema.json#/$defs/accountCode"
        },
        "subAccount": {
          "$ref": "common.schema.json#/$defs/subAccountCode"
        },
        "amount": {
          "$ref": "common.schema.json#/$defs/amount"
        },
        "tax": {
          "$ref": "common.schema.json#/$defs/tax"
        },
        "currency": {
          "$ref": "common.schema.json#/$defs/currency"
        },
        "partner": {
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "department": {
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "tags": {
          "$ref": "#/$defs/tagList"
        },
        "description": {
          "type": "string",
          "maxLength": 500
        },
        "allocation": {
          "$ref": "common.schema.json#/$defs/allocation"
        },
        "settlement": {
          "$ref": "common.schema.json#/$defs/settlement"
        },
        "withholding": {
          "$ref": "common.schema.json#/$defs/withholding"
        },
        "extensions": {
          "$ref": "common.schema.json#/$defs/extensions"
        },
        "servicePeriod": {
          "$ref": "common.schema.json#/$defs/period"
        }
      },
      "additionalProperties": false
    },
    "tagList": {
      "type": "array",
      "items": {
        "$ref": "common.schema.json#/$defs/identifier"
      },
      "uniqueItems": true
    }
  }
}
