1.前端薪酬公式配置:

DYNAMIC('云函数名','云函数返回要计算薪酬项目字段key值')

示列:

奖金:DYNAMIC('salary_item_scale','bonus_salary')

基本工资:DYNAMIC('salary_item_scale','basic_salary')

2.云函数

薪酬项目取值云函数
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/1/14 9:53
# @Author  : Aries
# @Site    : 
# @File    : salary_item_scale.py
# @Software: PyCharm
import datetime
import logging
import time

import dateutil

import errors
from apps.salary.formula.formula import BaseSalaryFormulaObject
from core.extend.dynamic_plugin.customer_util import CustomerUtil
from core.ds.plugins import BaseModelPlugIn
from apps.time.service_base_dynamic import LeaveStandardCalcDynamicBase
from core.extend.sync_outer.services_base import BaseSyncOuterService


class salary_item_scale(BaseSalaryFormulaObject):
    """
    多个薪酬项目的话都要计算出来,薪酬计算从缓存取数据,不然会报错。
    薪酬计算从薪级薪档变更明细取薪酬项目数据:基本工资取本月,奖金应去上个月。
    取数模型:SalaryScaleDetail
    """

    def execute(self, field):
        _cache_key = 'salary_item_scale_zcl'  # 缓存key值自定义唯一key值
        _cache = self.executor.context.get(_cache_key)
        if _cache is None:#缓存为空计算
            employee_ids = [_p.get('employee_id') for _p in self.context['profile_list']]
            logging.info("salary_item_scale|field|{}".format(field))
            if not self.context['month']:
                raise errors.DATA_RULE_ERROR.description("薪酬期间不能为空")
            # 你的取数逻辑
            salary_month = "{}-01".format(self.context["month"])
            logging.info("salary_item_scale|salary_month|{}|".format(salary_month))
            bonus_month = (datetime.datetime.strptime(salary_month, "%Y-%m-%d") + dateutil.relativedelta.relativedelta(
                months=-1)).strftime("%Y-%m-%d")
            logging.info("salary_item_scale|bonus_month|{}|".format(bonus_month))
            self.executor.context[_cache_key] = _cache

            result = {}
            # 基本工资 职等名称
            salary_data_list = self.get_SalaryScaleDetail_data(employee_ids=employee_ids, month=salary_month)
            for item in salary_data_list:
                result[item.get("employee_id")] = {
                    "basic_salary": item.get("base_salary"),
                    "job_grade": item.get("job_grade").get("name")
                }
            # 奖金
            bonus_data_list = self.get_SalaryScaleDetail_data(employee_ids, month=bonus_month)
            for item in bonus_data_list:
                result.get(item.get("employee_id"),{"basic_salary":0,"job_grade":""})["bonus_salary"]=item.get("bonus_salary")
            logging.info("salary_item_scale|result|{}|".format(result))
            _cache = result
            self.executor.context[_cache_key] = _cache
        _result = _cache.get(self.context['profile']['employee_id'], {}).get(field, 0) or 0
        logging.info("salary_item_scale|_result|{}|".format(_result))
        return _result

    def get_SalaryScaleDetail_data(self, employee_ids, month):
        """
        获取 SalaryScaleDetail 数据
        :param employee_ids:
        :param month:
        :return:
        """
        logging.info("salary_item_scale|data|{}|{}".format(employee_ids, month))
        return CustomerUtil.call_open_api(
            name="hcm.model.list",
            param={
                "model": "SalaryScaleDetail",
                "page_index": 1,
                "page_size": 9999,
                "filter_dict": {
                    "employee_id": employee_ids,
                    "effect_date": {"lte": month},
                    "end_date": {"gt": month}
                },
                "extra_property": {
                    "state": "search_zcl",
                    "sorts": [{"key": "effect_date", "type": "desc"}],
                    "fields": [
                        {"field": ["employee_id"], "key": "employee_id"},
                        {"field": ["bonus_salary"], "key": "bonus_salary"},
                        {"field": ["base_salary"], "key": "base_salary"},
                        {"field": ["annual_salary"], "key": "annual_salary"},
                        {"field": ["job_grade", "name"], "key": "job_grade_name"}
                    ]
                }
            }
        )["list"]
  • 无标签