# 创建假期发放记录

向飞书人事休假系统写入假期发放记录。对应假勤管理-休假管理-[发放记录](https://example.feishu.cn/people/workforce-management/manage/leave/leave_admin/granting_record)的创建或者导入功能

**注意事项**：仅飞书人事企业版可用

## 请求

基本 | &nbsp;
---|---
HTTP URL | https://open.feishu.cn/open-apis/corehr/v1/leave_granting_records
HTTP Method | POST
接口频率限制 | [1000 次/分钟、50 次/秒](https://open.feishu.cn/document/ukTMukTMukTM/uUzN04SN3QjL1cDN)
支持的应用类型 | Custom App、Store App
权限要求<br>**调用该 API 所需的权限。开启其中任意一项权限即可调用**<br>开启任一权限即可 | 更新假期授予记录(corehr:leave_granting_record:write)<br>更新核心人事信息(corehr:corehr)
字段权限要求 | **注意事项**：该接口返回体中存在下列敏感字段，仅当开启对应的权限后才会返回；如果无需获取这些字段，则不建议申请<br>获取用户 user ID(contact:user.employee_id:readonly)

### 请求头

名称 | 类型 | 必填 | 描述
---|---|---|---
Authorization | string | 是 | `tenant_access_token`<br>**值格式**："Bearer `access_token`"<br>**示例值**："Bearer t-7f1bcd13fc57d46bac21793a18e560"<br>[了解更多：如何选择与获取 access token](https://open.feishu.cn/document/uAjLw4CM/ugTN1YjL4UTN24CO1UjN/trouble-shooting/how-to-choose-which-type-of-token-to-use)
Content-Type | string | 是 | **固定值**："application/json; charset=utf-8"

### 查询参数

名称 | 类型 | 必填 | 描述
---|---|---|---
user_id_type | string | 否 | 用户 ID 类型<br>**示例值**：open_id<br>**可选值有**：<br>- open_id：标识一个用户在某个应用中的身份。同一个用户在不同应用中的 Open ID 不同。[了解更多：如何获取 Open ID](https://open.feishu.cn/document/uAjLw4CM/ugTN1YjL4UTN24CO1UjN/trouble-shooting/how-to-obtain-openid)<br>- union_id：标识一个用户在某个应用开发商下的身份。同一用户在同一开发商下的应用中的 Union ID 是相同的，在不同开发商下的应用中的 Union ID 是不同的。通过 Union ID，应用开发商可以把同个用户在多个应用中的身份关联起来。[了解更多：如何获取 Union ID？](https://open.feishu.cn/document/uAjLw4CM/ugTN1YjL4UTN24CO1UjN/trouble-shooting/how-to-obtain-union-id)<br>- user_id：标识一个用户在某个租户内的身份。同一个用户在租户 A 和租户 B 内的 User ID 是不同的。在同一个租户内，一个用户的 User ID 在所有应用（包括商店应用）中都保持一致。User ID 主要用于在不同的应用间打通用户数据。[了解更多：如何获取 User ID？](https://open.feishu.cn/document/uAjLw4CM/ugTN1YjL4UTN24CO1UjN/trouble-shooting/how-to-obtain-user-id)<br>- people_corehr_id：以飞书人事的ID来识别用户<br>**默认值**：`open_id`<br>**当值为 `user_id`，字段权限要求**：<br>获取用户 user ID(contact:user.employee_id:readonly)

### 请求体

名称 | 类型 | 必填 | 描述
---|---|---|---
leave_type_id | string | 是 | 假期类型 ID，枚举值可通过[获取假期类型列表](https://open.larkoffice.com/document/server-docs/corehr-v1/leave/leave_types)接口获取（若假期类型下存在假期子类，此处仅支持传入假期子类的 ID）<br>**示例值**："7111688079785723436"
employment_id | string | 是 | 员工 ID，飞书人事的雇员id。对应user_id_type<br>**示例值**："6982509313466189342"
granting_quantity | string | 是 | 发放数量（小数位数不能超过6位，授予数量范围为-9999~9999）<br>**示例值**："0.5"
granting_unit | int | 是 | 发放时长单位<br>可选值有：<br>- 1: 天<br>- 2: 小时<br>**示例值**：1
effective_date | string | 是 | 生效时间，格式为yyyy-MM-dd<br>**示例值**："2022-01-01"
expiration_date | string | 否 | 失效时间，格式为yyyy-MM-dd<br>**示例值**："2022-01-01"
section_type | int | 否 | 是否参与折算（1不参与折算，2参与折算）。默认不折算<br>**示例值**：1
reason | i18n\[\] | 是 | 发放原因
lang | string | 是 | 名称信息的语言<br>**示例值**："zh_CN"
value | string | 是 | 名称信息的内容<br>**示例值**："张三"
external_id | string | 否 | 自定义外部 ID，可用于避免数据重复写入（不能超过 64 字符）。如果重复录入，不会创建新纪录、也不会更新原始记录<br>**示例值**："111"

### 请求体示例
```json
{
    "leave_type_id": "7111688079785723436",
    "employment_id": "6982509313466189342",
    "granting_quantity": "0.5",
    "granting_unit": 1,
    "effective_date": "2022-01-01",
    "expiration_date": "2022-01-01",
    "section_type": 1,
    "reason": [
        {
            "lang": "zh_CN",
            "value": "张三"
        }
    ],
    "external_id": "111"
}
```

## 响应

### 响应体

名称 | 类型 | 描述
---|---|---
code | int | 错误码，非 0 表示失败
msg | string | 错误描述
data | \- | \-
leave_granting_record | leave_granting_record | 假期发放记录
id | string | 假期发放记录 ID，可用与[删除假期发放记录](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/corehr-v1/leave_granting_record/delete)和[修改发放记录](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/attendance-v1/leave_accrual_record/patch)
employment_id | string | 员工 ID，对应user_id_type
leave_type_id | string | 假期类型 ID
granting_quantity | string | 发放数量
granting_unit | int | 发放时长单位<br>可选值有：<br>- 1: 天<br>- 2: 小时
effective_date | string | 生效时间，格式为yyyy-MM-dd
expiration_date | string | 失效时间（根据休假规则自动计算），格式为yyyy-MM-dd
granted_by | int | 发放来源<br>可选值有：<br>- 1: 系统发放<br>- 2: 手动发放<br>- 3: 外部系统发放
reason | i18n\[\] | 发放原因
lang | string | 名称信息的语言
value | string | 名称信息的内容
created_at | string | 发放记录的创建时间，格式为毫秒级时间戳
created_by | string | 发放记录的创建人，值为创建人的员工 ID，对应user_id_type
updated_at | string | 发放记录的更新时间，格式为毫秒级时间戳
updated_by | string | 发放记录的更新人，值为更新人的员工 ID，对应user_id_type
section_type | int | 是否参与折算（1不参与折算，2参与折算）

### 响应体示例
```json
{
    "code": 0,
    "msg": "success",
    "data": {
        "leave_granting_record": {
            "id": "6893014062142064135",
            "employment_id": "6893014062142064135",
            "leave_type_id": "6893014062142064135",
            "granting_quantity": "0.5",
            "granting_unit": 1,
            "effective_date": "2022-01-01",
            "expiration_date": "2022-01-01",
            "granted_by": 3,
            "reason": [
                {
                    "lang": "zh_CN",
                    "value": "张三"
                }
            ],
            "created_at": "1608725989000",
            "created_by": "646465654545",
            "updated_at": "1608725989000",
            "updated_by": "646465654545",
            "section_type": 1
        }
    }
}
```

### 错误码

HTTP状态码 | 错误码 | 描述 | 排查建议
---|---|---|---
400 | 1160001 | No tenant ID | 租户ID为空
400 | 1160002 | Invalid granting unit | 授予单位出错
400 | 1160003 | Invalid effective date | 日期格式必须是类似“2020-01-01”
400 | 1160004 | Invalid granting quantity | 必须是能解析成数字的字符串，例如“2.5”
400 | 1160005 | Accessed data object not found | 检查leaveTypeID是否正确
400 | 1160006 | Employment not found | 检查employmentID是否正确
400 | 1160007 | Leave plan version not found | 假期计划版本数据不存在，请检查该类型的假期对应的计划配置是否创建
400 | 1160008 | Error occurred while checking if the employee is eligible for the vacation plan | 员工不适用于假期计划版本，请检查该假期计划适用人员范围是否和员工信息匹配
400 | 1160009 | Accrual rule not found | 检查该假期计划版本是否正确创建，是否存在有效的发放规则
400 | 1160010 | Granting record already exists | response里会带上已存在的发放记录的信息，用户可以将其取出，不需要再重试请求
500 | 1160011 | Error occurred when getting employment information | 获取员工信息失败
500 | 1160012 | An exception occurs in the database | 数据库异常，请联系 [技术支持](https://applink.feishu.cn/TLJpeNdW)
500 | 1160013 | Error occurred while checking if the employee is eligible for the vacation plan. | 检查员工是否符合假期计划适用范围时发生错误
500 | 1160014 | Error occurred when calculate accrual record | 计算授予计划错误
400 | 1160024 | There is a subclass for the leave type, but the subclass ID has not been passed | 如果假期类型存在子类，那么leaveTypeID必须传子类ID
400 | 1160025 | The granting quantity range is from -9999 to 9999 | 额度范围为-9999～9999
400 | 1160026 | The number of decimal places of the granted quantity cannot exceed 6 | 发放数量最多6位小数
400 | 1160027 | The length of the granting reason cannot exceed 3000 | 发放原因长度最多3000
500 | 1160028 | There is an error in the unit conversion configuration in the granting rule | 检查假期计划版本的单位转换规则是否配置正确
400 | 1160029 | The leave type has been deactivated | 不能往已停用的假期类型写发放记录
400 | 1160030 | The length of the granted unique ID cannot exceed 64 | 授予唯一ID长度不能超过64
500 | 1169999 | Unknown error | 未知错误，请联系 [技术支持](https://applink.feishu.cn/TLJpeNdW)
500 | 1160015 | Internal error | 内部错误，请联系 [技术支持](https://applink.feishu.cn/TLJpeNdW)
400 | 1160016 | Invalid param | 对照接口文档的入参排查，是否漏填参数、格式错误等（例如数值参数传了字母、日期格式错误等）
400 | 1160017 | User not found | 未找到用户信息
500 | 1160018 | Invalid leave balance calculate Conf | 无效的假期余额计算配置
500 | 1160019 | The calculation result of leave balance is empty | 假期余额计算为空
400 | 1160020 | When calculating the leave balance, there is no leave plan version that matches the employee | 没有与员工匹配的假期计划版本
400 | 1160021 | For the leave type that is not granted according to the cycle, balance calculation is not supported | 不按周期授予的假期类型，不支持余额计算
400 | 1160022 | The data of the leave plan version is invalid | 假期计划版本数据不合法
500 | 1160023 | Error occurred when calculating the leave balance | 假期余额计算出错
400 | 1160031 | This granting record cannot be edited or deleted | 该授予记录不可编辑或删除
400 | 1160032 | The expiration time of this granting record is invalid | 该授予记录过期时间不合法
400 | 1160033 | The effective date is after the granting date | 生效日期在发放日期之后
400 | 1160034 | The expiration date format is incorrect | 失效日期格式错误
400 | 1160035 | No permission to access the employee data | 无权访问该员工数据

