{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kaikei.keydrop.net/schema/0.1/masters.schema.json",
  "title": "その他のマスタ",
  "description": "masters/partners.json・departments.json・tags.json・rules.json・payment-accounts.json・people.json（08-masters.md）。ファイルごとに $defs の該当する定義で検証する",
  "$defs": {
    "partnersFile": {
      "type": "object",
      "required": [
        "partners"
      ],
      "properties": {
        "partners": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/partner"
          }
        },
        "extensions": {
          "$ref": "common.schema.json#/$defs/extensions"
        }
      },
      "additionalProperties": false
    },
    "partner": {
      "type": "object",
      "required": [
        "id",
        "name"
      ],
      "properties": {
        "id": {
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "name": {
          "$ref": "common.schema.json#/$defs/name"
        },
        "nameKana": {
          "type": "string"
        },
        "shortName": {
          "type": "string"
        },
        "aliases": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "uniqueItems": true,
          "description": "明細・証憑での別の表記（銀行の振込名義のカナ・カードの利用明細の店名など）。取引先の特定に使う"
        },
        "paymentLinks": {
          "type": "array",
          "description": "取引先と、自社の決済口座（銀行口座・カード）の結び付け。どの口座で入金・支払があるかと、その口座の明細に出る取引先の表記（振込名義など）",
          "items": {
            "type": "object",
            "required": [
              "paymentAccount"
            ],
            "properties": {
              "paymentAccount": {
                "$ref": "common.schema.json#/$defs/identifier",
                "description": "自社の決済口座の ID（masters/payment-accounts.json）"
              },
              "direction": {
                "enum": [
                  "in",
                  "out"
                ],
                "description": "in: この口座に入金される / out: この口座から支払う。省略は両方"
              },
              "names": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                },
                "uniqueItems": true,
                "description": "その口座の明細に出る、この取引先の表記（振込名義のカナなど）"
              },
              "extensions": {
                "$ref": "common.schema.json#/$defs/extensions"
              }
            },
            "additionalProperties": false
          }
        },
        "corporateNumber": {
          "type": "string",
          "pattern": "^\\d{13}$"
        },
        "invoiceRegistrations": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "number"
            ],
            "properties": {
              "number": {
                "$ref": "common.schema.json#/$defs/invoiceRegistrationNumber"
              },
              "validFrom": {
                "$ref": "common.schema.json#/$defs/date"
              },
              "validTo": {
                "$ref": "common.schema.json#/$defs/date"
              },
              "checkedAt": {
                "$ref": "common.schema.json#/$defs/date"
              },
              "extensions": {
                "$ref": "common.schema.json#/$defs/extensions"
              }
            },
            "additionalProperties": false
          }
        },
        "country": {
          "type": "string",
          "pattern": "^[A-Z]{2}$"
        },
        "roles": {
          "type": "array",
          "items": {
            "enum": [
              "customer",
              "supplier"
            ]
          },
          "uniqueItems": true
        },
        "defaultAccount": {
          "$ref": "common.schema.json#/$defs/accountCode"
        },
        "paymentTerms": {
          "type": "object",
          "properties": {
            "closingDay": {
              "oneOf": [
                {
                  "const": "end"
                },
                {
                  "type": "integer",
                  "minimum": 1,
                  "maximum": 28
                }
              ]
            },
            "paymentMonthOffset": {
              "type": "integer",
              "minimum": 0,
              "maximum": 12
            },
            "paymentDay": {
              "oneOf": [
                {
                  "const": "end"
                },
                {
                  "type": "integer",
                  "minimum": 1,
                  "maximum": 28
                }
              ]
            },
            "extensions": {
              "$ref": "common.schema.json#/$defs/extensions"
            }
          },
          "additionalProperties": false
        },
        "validFrom": {
          "$ref": "common.schema.json#/$defs/date"
        },
        "validTo": {
          "$ref": "common.schema.json#/$defs/date"
        },
        "extensions": {
          "$ref": "common.schema.json#/$defs/extensions"
        },
        "defaultTax": {
          "$ref": "common.schema.json#/$defs/taxDefault"
        },
        "invoiceCheck": {
          "type": "object",
          "required": [
            "status",
            "checkedAt"
          ],
          "properties": {
            "status": {
              "enum": [
                "registered",
                "not_registered",
                "revoked",
                "unknown"
              ]
            },
            "checkedAt": {
              "$ref": "common.schema.json#/$defs/date"
            }
          },
          "additionalProperties": false,
          "description": "直近に公表サイトで確認した結果（登録していない相手も記録できる）"
        }
      },
      "additionalProperties": false
    },
    "departmentsFile": {
      "type": "object",
      "required": [
        "departments"
      ],
      "properties": {
        "departments": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "id",
              "name"
            ],
            "properties": {
              "id": {
                "$ref": "common.schema.json#/$defs/identifier"
              },
              "name": {
                "$ref": "common.schema.json#/$defs/name"
              },
              "parent": {
                "oneOf": [
                  {
                    "$ref": "common.schema.json#/$defs/identifier"
                  },
                  {
                    "type": "null"
                  }
                ]
              },
              "validFrom": {
                "$ref": "common.schema.json#/$defs/date"
              },
              "validTo": {
                "$ref": "common.schema.json#/$defs/date"
              },
              "extensions": {
                "$ref": "common.schema.json#/$defs/extensions"
              }
            },
            "additionalProperties": false
          }
        },
        "extensions": {
          "$ref": "common.schema.json#/$defs/extensions"
        }
      },
      "additionalProperties": false
    },
    "tagsFile": {
      "type": "object",
      "required": [
        "tagGroups",
        "tags"
      ],
      "properties": {
        "tagGroups": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "id",
              "name",
              "exclusive"
            ],
            "properties": {
              "id": {
                "$ref": "common.schema.json#/$defs/identifier"
              },
              "name": {
                "$ref": "common.schema.json#/$defs/name"
              },
              "exclusive": {
                "type": "boolean"
              },
              "extensions": {
                "$ref": "common.schema.json#/$defs/extensions"
              }
            },
            "additionalProperties": false
          }
        },
        "tags": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "id",
              "group",
              "name"
            ],
            "properties": {
              "id": {
                "$ref": "common.schema.json#/$defs/identifier"
              },
              "group": {
                "$ref": "common.schema.json#/$defs/identifier"
              },
              "name": {
                "$ref": "common.schema.json#/$defs/name"
              },
              "validFrom": {
                "$ref": "common.schema.json#/$defs/date"
              },
              "validTo": {
                "$ref": "common.schema.json#/$defs/date"
              },
              "extensions": {
                "$ref": "common.schema.json#/$defs/extensions"
              },
              "parent": {
                "oneOf": [
                  {
                    "$ref": "common.schema.json#/$defs/identifier"
                  },
                  {
                    "type": "null"
                  }
                ]
              }
            },
            "additionalProperties": false
          }
        },
        "extensions": {
          "$ref": "common.schema.json#/$defs/extensions"
        }
      },
      "additionalProperties": false
    },
    "rulesFile": {
      "type": "object",
      "required": [
        "rules"
      ],
      "properties": {
        "rules": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/rule"
          }
        },
        "extensions": {
          "$ref": "common.schema.json#/$defs/extensions"
        },
        "recurring": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/recurring"
          }
        }
      },
      "additionalProperties": false
    },
    "rule": {
      "type": "object",
      "required": [
        "id",
        "priority",
        "match",
        "action"
      ],
      "properties": {
        "id": {
          "$ref": "common.schema.json#/$defs/ulid"
        },
        "priority": {
          "type": "integer"
        },
        "match": {
          "type": "object",
          "minProperties": 1,
          "properties": {
            "source": {
              "type": "array",
              "items": {
                "type": "string",
                "pattern": "^[a-z][a-z0-9_]*$",
                "description": "bank_csv | card_csv | receipt | invoice | manual | ad_platform_csv など"
              },
              "uniqueItems": true
            },
            "account": {
              "$ref": "common.schema.json#/$defs/accountCode",
              "description": "取り込み元の口座の科目"
            },
            "subAccount": {
              "$ref": "common.schema.json#/$defs/subAccountCode"
            },
            "text": {
              "type": "object",
              "minProperties": 1,
              "maxProperties": 1,
              "properties": {
                "contains": {
                  "type": "string"
                },
                "startsWith": {
                  "type": "string"
                },
                "endsWith": {
                  "type": "string"
                },
                "equals": {
                  "type": "string"
                },
                "regex": {
                  "type": "string"
                }
              },
              "additionalProperties": false
            },
            "amount": {
              "type": "object",
              "properties": {
                "min": {
                  "$ref": "common.schema.json#/$defs/amount"
                },
                "max": {
                  "$ref": "common.schema.json#/$defs/amount"
                }
              },
              "additionalProperties": false
            },
            "direction": {
              "enum": [
                "in",
                "out"
              ]
            }
          },
          "additionalProperties": false
        },
        "action": {
          "type": "object",
          "required": [
            "account"
          ],
          "properties": {
            "account": {
              "$ref": "common.schema.json#/$defs/accountCode"
            },
            "subAccount": {
              "$ref": "common.schema.json#/$defs/subAccountCode"
            },
            "partner": {
              "$ref": "common.schema.json#/$defs/identifier"
            },
            "department": {
              "$ref": "common.schema.json#/$defs/identifier"
            },
            "tags": {
              "type": "array",
              "items": {
                "$ref": "common.schema.json#/$defs/identifier"
              },
              "uniqueItems": true
            },
            "tax": {
              "$ref": "common.schema.json#/$defs/taxDefault"
            },
            "description": {
              "type": "string"
            },
            "allocation": {
              "$ref": "common.schema.json#/$defs/allocation"
            },
            "ignore": {
              "type": "boolean",
              "description": "true なら仕訳を作らない（取り込みの対象外）"
            }
          },
          "additionalProperties": false
        },
        "learnedFrom": {
          "enum": [
            "user",
            "user_approval"
          ]
        },
        "hits": {
          "type": "integer",
          "minimum": 0
        },
        "lastUsedAt": {
          "$ref": "common.schema.json#/$defs/date"
        },
        "active": {
          "type": "boolean"
        },
        "extensions": {
          "$ref": "common.schema.json#/$defs/extensions"
        }
      },
      "additionalProperties": false
    },
    "recurring": {
      "type": "object",
      "required": [
        "id",
        "name",
        "schedule",
        "template"
      ],
      "properties": {
        "id": {
          "$ref": "common.schema.json#/$defs/ulid"
        },
        "name": {
          "type": "string"
        },
        "schedule": {
          "type": "object",
          "required": [
            "frequency"
          ],
          "properties": {
            "frequency": {
              "enum": [
                "monthly",
                "yearly"
              ]
            },
            "day": {
              "oneOf": [
                {
                  "const": "end"
                },
                {
                  "type": "integer",
                  "minimum": 1,
                  "maximum": 28
                }
              ]
            },
            "month": {
              "type": "integer",
              "minimum": 1,
              "maximum": 12
            },
            "startOn": {
              "$ref": "common.schema.json#/$defs/date"
            },
            "endOn": {
              "$ref": "common.schema.json#/$defs/date"
            }
          },
          "additionalProperties": false
        },
        "template": {
          "type": "object",
          "required": [
            "description",
            "lines"
          ],
          "properties": {
            "description": {
              "type": "string"
            },
            "partner": {
              "$ref": "common.schema.json#/$defs/identifier"
            },
            "lines": {
              "type": "array",
              "minItems": 2,
              "items": {
                "$ref": "journal-month.schema.json#/$defs/line"
              }
            }
          },
          "additionalProperties": false
        },
        "active": {
          "type": "boolean"
        },
        "extensions": {
          "$ref": "common.schema.json#/$defs/extensions"
        }
      },
      "additionalProperties": false
    },
    "paymentAccountsFile": {
      "type": "object",
      "required": [
        "paymentAccounts"
      ],
      "properties": {
        "paymentAccounts": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/paymentAccount"
          }
        },
        "extensions": {
          "$ref": "common.schema.json#/$defs/extensions"
        }
      },
      "additionalProperties": false
    },
    "paymentAccount": {
      "type": "object",
      "description": "決済口座（銀行口座・現金・クレジットカード・決済サービス・電子マネー・借入・立替）。残高を持つ勘定科目と補助科目に 1 対 1 で結び付ける（08-masters.md）",
      "required": [
        "id",
        "name",
        "kind",
        "account"
      ],
      "properties": {
        "id": {
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "name": {
          "$ref": "common.schema.json#/$defs/name"
        },
        "kind": {
          "enum": [
            "bank_account",
            "cash",
            "credit_card",
            "payment_service",
            "e_money",
            "loan",
            "reimbursement",
            "other"
          ]
        },
        "account": {
          "$ref": "common.schema.json#/$defs/accountCode"
        },
        "subAccount": {
          "type": "string",
          "minLength": 1
        },
        "partner": {
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "person": {
          "$ref": "common.schema.json#/$defs/identifier",
          "description": "この残高の相手の人（masters/people.json の ID。立替の決済口座で使う）"
        },
        "maskedNumber": {
          "type": "string",
          "pattern": "^\\d{1,4}$",
          "description": "見分けるための番号の下 4 桁まで。カード番号・口座番号の全桁を入れてはならない"
        },
        "terms": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "validFrom": {
                "$ref": "common.schema.json#/$defs/date"
              },
              "validTo": {
                "$ref": "common.schema.json#/$defs/date"
              },
              "closingDay": {
                "oneOf": [
                  {
                    "const": "end"
                  },
                  {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 28
                  }
                ]
              },
              "paymentMonthOffset": {
                "type": "integer",
                "minimum": 0,
                "maximum": 12
              },
              "paymentDay": {
                "oneOf": [
                  {
                    "const": "end"
                  },
                  {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 28
                  }
                ]
              },
              "extensions": {
                "$ref": "common.schema.json#/$defs/extensions"
              }
            },
            "additionalProperties": false
          },
          "description": "締めと決済の条件の履歴"
        },
        "settlementAccount": {
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "validFrom": {
          "$ref": "common.schema.json#/$defs/date"
        },
        "validTo": {
          "$ref": "common.schema.json#/$defs/date"
        },
        "extensions": {
          "$ref": "common.schema.json#/$defs/extensions"
        }
      },
      "additionalProperties": false
    },
    "peopleFile": {
      "type": "object",
      "required": [
        "people"
      ],
      "properties": {
        "people": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/person"
          }
        },
        "extensions": {
          "$ref": "common.schema.json#/$defs/extensions"
        }
      },
      "additionalProperties": false
    },
    "person": {
      "type": "object",
      "description": "人（従業員・役員など、立替の精算の相手）。住所・個人番号・給与の額・口座番号は入れない（08-masters.md）",
      "required": [
        "id",
        "name"
      ],
      "properties": {
        "id": {
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "name": {
          "$ref": "common.schema.json#/$defs/name"
        },
        "nameKana": {
          "type": "string"
        },
        "kind": {
          "enum": [
            "employee",
            "officer",
            "family",
            "other"
          ]
        },
        "department": {
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "validFrom": {
          "$ref": "common.schema.json#/$defs/date"
        },
        "validTo": {
          "$ref": "common.schema.json#/$defs/date"
        },
        "extensions": {
          "$ref": "common.schema.json#/$defs/extensions"
        }
      },
      "additionalProperties": false
    }
  }
}
