{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kaikei.keydrop.net/schema/0.1/entity.schema.json",
  "title": "事業者と設定の履歴",
  "description": "entity.json（02-entity.md）。履歴の期間が重ならないことは Validator が検証する",
  "type": "object",
  "required": [
    "type",
    "name",
    "consumptionTax"
  ],
  "properties": {
    "type": {
      "enum": [
        "individual",
        "corporation"
      ]
    },
    "name": {
      "$ref": "common.schema.json#/$defs/name"
    },
    "nameKana": {
      "type": "string"
    },
    "corporateNumber": {
      "type": "string",
      "pattern": "^\\d{13}$"
    },
    "startedOn": {
      "$ref": "common.schema.json#/$defs/date"
    },
    "invoiceRegistrations": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "number",
          "validFrom"
        ],
        "properties": {
          "number": {
            "$ref": "common.schema.json#/$defs/invoiceRegistrationNumber"
          },
          "validFrom": {
            "$ref": "common.schema.json#/$defs/date"
          },
          "validTo": {
            "$ref": "common.schema.json#/$defs/date"
          },
          "extensions": {
            "$ref": "common.schema.json#/$defs/extensions"
          }
        },
        "additionalProperties": false
      }
    },
    "consumptionTax": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/consumptionTaxSetting"
      }
    },
    "incomeTax": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "validFrom",
          "filing"
        ],
        "properties": {
          "validFrom": {
            "$ref": "common.schema.json#/$defs/date"
          },
          "validTo": {
            "$ref": "common.schema.json#/$defs/date"
          },
          "filing": {
            "enum": [
              "blue",
              "white"
            ]
          },
          "returnForms": {
            "type": "array",
            "items": {
              "enum": [
                "general",
                "real_estate",
                "agriculture"
              ]
            },
            "uniqueItems": true,
            "minItems": 1
          },
          "specialDeduction": {
            "$ref": "common.schema.json#/$defs/amount"
          },
          "extensions": {
            "$ref": "common.schema.json#/$defs/extensions"
          }
        },
        "additionalProperties": false
      }
    },
    "foreignCurrency": {
      "type": "object",
      "description": "外貨の換算の規則（07-currency.md）。rateType が ttm / tts / ttb / custom の行に使う",
      "properties": {
        "rounding": {
          "enum": [
            "round",
            "floor",
            "ceil"
          ],
          "default": "round",
          "description": "外貨 × レート の 1 円未満の端数処理（四捨五入 / 切り捨て / 切り上げ）"
        },
        "tolerance": {
          "type": "integer",
          "minimum": 0,
          "default": 1,
          "description": "検証 C1 で許す、円の金額と 外貨 × レート（端数処理後）の差（円）"
        }
      },
      "additionalProperties": false
    },
    "corporateProfile": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "validFrom"
        ],
        "properties": {
          "validFrom": {
            "$ref": "common.schema.json#/$defs/date"
          },
          "validTo": {
            "$ref": "common.schema.json#/$defs/date"
          },
          "capital": {
            "$ref": "common.schema.json#/$defs/amount"
          },
          "smeStatus": {
            "enum": [
              "sme",
              "large"
            ]
          },
          "extensions": {
            "$ref": "common.schema.json#/$defs/extensions"
          },
          "perCapitaAnnual": {
            "$ref": "common.schema.json#/$defs/amount",
            "description": "法人住民税の均等割の年額（道府県民税と市町村民税の合計）。省略時は税マスタの最低額"
          }
        },
        "additionalProperties": false
      }
    },
    "extensions": {
      "$ref": "common.schema.json#/$defs/extensions"
    },
    "recordRetentionYears": {
      "type": "integer",
      "minimum": 7,
      "maximum": 10,
      "description": "帳簿・証憑の保存年数（通常 7 年。法人で欠損金がある場合などは 10 年）"
    },
    "changeLog": {
      "type": "array",
      "description": "事業者の情報を変更した記録（任意）",
      "items": {
        "type": "object",
        "required": [
          "at",
          "summary"
        ],
        "properties": {
          "at": {
            "$ref": "common.schema.json#/$defs/datetime"
          },
          "summary": {
            "type": "string"
          },
          "by": {
            "type": "string"
          }
        },
        "additionalProperties": false
      }
    }
  },
  "additionalProperties": false,
  "allOf": [
    {
      "if": {
        "properties": {
          "type": {
            "const": "corporation"
          }
        }
      },
      "then": {
        "not": {
          "required": [
            "incomeTax"
          ]
        }
      },
      "else": {
        "not": {
          "anyOf": [
            {
              "required": [
                "corporateProfile"
              ]
            },
            {
              "required": [
                "corporateNumber"
              ]
            }
          ]
        }
      }
    }
  ],
  "$defs": {
    "consumptionTaxSetting": {
      "type": "object",
      "required": [
        "validFrom",
        "taxableStatus"
      ],
      "properties": {
        "validFrom": {
          "$ref": "common.schema.json#/$defs/date"
        },
        "validTo": {
          "$ref": "common.schema.json#/$defs/date"
        },
        "taxableStatus": {
          "enum": [
            "exempt",
            "taxable"
          ]
        },
        "filingMethod": {
          "type": "string",
          "description": "general | simplified | 税マスタの filingSpecials の id（twenty_percent・thirty_percent など）",
          "pattern": "^[a-z][a-z0-9_]*$"
        },
        "simplifiedDefaultBusinessType": {
          "type": "integer",
          "minimum": 1,
          "maximum": 6
        },
        "deductionMethod": {
          "enum": [
            null,
            "full",
            "itemized",
            "proportional"
          ]
        },
        "accountingMethod": {
          "enum": [
            "tax_included",
            "tax_excluded",
            "mixed"
          ]
        },
        "mixedDetail": {
          "type": "object",
          "properties": {
            "fixedAssets": {
              "enum": [
                "tax_included",
                "tax_excluded"
              ]
            },
            "inventory": {
              "enum": [
                "tax_included",
                "tax_excluded"
              ]
            },
            "expenses": {
              "enum": [
                "tax_included",
                "tax_excluded"
              ]
            }
          },
          "required": [
            "fixedAssets",
            "inventory",
            "expenses"
          ],
          "additionalProperties": false
        },
        "salesTaxCalculation": {
          "enum": [
            "proportional",
            "stacked"
          ]
        },
        "purchaseTaxCalculation": {
          "enum": [
            "proportional",
            "stacked"
          ]
        },
        "rounding": {
          "enum": [
            "floor",
            "round",
            "ceil"
          ]
        },
        "taxPeriodShortening": {
          "enum": [
            "none",
            "quarterly",
            "monthly"
          ]
        },
        "basePeriodTaxableSales": {
          "$ref": "common.schema.json#/$defs/amount"
        },
        "extensions": {
          "$ref": "common.schema.json#/$defs/extensions"
        }
      },
      "additionalProperties": false,
      "allOf": [
        {
          "if": {
            "properties": {
              "taxableStatus": {
                "const": "taxable"
              }
            }
          },
          "then": {
            "required": [
              "filingMethod",
              "accountingMethod"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "accountingMethod": {
                "const": "mixed"
              }
            },
            "required": [
              "accountingMethod"
            ]
          },
          "then": {
            "required": [
              "mixedDetail"
            ]
          }
        }
      ]
    }
  }
}
