diff --git a/yuthon_hr_employee/models/yuthon_hr_department.py b/yuthon_hr_employee/models/yuthon_hr_department.py index 6924d4ca1..661b74e42 100644 --- a/yuthon_hr_employee/models/yuthon_hr_department.py +++ b/yuthon_hr_employee/models/yuthon_hr_department.py @@ -4,10 +4,23 @@ from pypinyin import pinyin,lazy_pinyin from odoo.osv import expression from odoo.exceptions import ValidationError, UserError +# 作为顶级根节点、在下属部门显示名中不带出的上级部门(只显示本部门,不拼上级) +# 精确匹配:与部门名完全相同才算根级 +ROOT_DEPARTMENTS = {'集团本部', '客户服务中心'} +# 前缀匹配:以这些字样开头的部门都算根级(覆盖 天河粮储/天河物管/天河投资 等公司) +ROOT_DEPARTMENT_PREFIXES = ('天河',) + + +def _is_root_department(department): + """判断上级部门是否属于根级(根级不拼进下级显示名)。""" + name = department.name or '' + return name in ROOT_DEPARTMENTS or name.startswith(ROOT_DEPARTMENT_PREFIXES) + class YuthonHrDepartment(models.Model): _inherit = "hr.department" - _rec_name = 'name' + # 部门显示名只带一级上级("上级 / 本部门"),最多一层;无上级则只显示本部门 + _rec_name = 'hierarchical_name' _order = 'department_code asc' name = fields.Char(string="部门名称", tracking=True, required=True) @@ -25,6 +38,7 @@ class YuthonHrDepartment(models.Model): company_id = fields.Many2one('res.company', string="公司", index=True, default=lambda self: self.env.company, store=True) pinyin_name = fields.Char(string="拼音名称", compute='_compute_pinyin', index=True, store=True) is_company = fields.Boolean(string="本公司", default=True, compute='_compute_is_company', search='_search_part_of_company') + hierarchical_name = fields.Char(string="层级名称", compute='_compute_hierarchical_name', store=True, index=True) @api.depends_context('uid', 'company') @api.depends('company_id') @@ -86,6 +100,36 @@ class YuthonHrDepartment(models.Model): if employee.name: employee.pinyin_name = ''.join(lazy_pinyin(employee.name)) + @api.depends('name', 'parent_id.name') + def _compute_hierarchical_name(self): + for department in self: + parent = department.parent_id + # 上级为空 或 上级属于根级部门(集团本部/天河系公司等)→ 只显示本部门 + if parent and not _is_root_department(parent): + department.hierarchical_name = '%s(%s)' % (department.name, parent.name) + else: + department.hierarchical_name = department.name + + def action_recompute_hierarchical_name(self): + """手动重算所有部门的层级显示名(改了 ROOT_DEPARTMENTS 规则后,store 字段旧值不会自动重算,用此动作触发)。 + + 可在 设置→技术→动作→服务器动作 找到「重算部门层级显示名」并运行, + 或直接在该模型的列表/表单「动作」菜单中点击。 + """ + all_depts = self.sudo().search([]) + all_depts._compute_hierarchical_name() + self.env.cr.commit() + return { + 'type': 'ir.actions.client', + 'tag': 'display_notification', + 'params': { + 'title': '部门层级名重算完成', + 'message': '已重算 %d 个部门' % len(all_depts), + 'type': 'success', + 'sticky': False, + } + } + @api.model def _name_search(self, name, domain=None, operator='ilike', limit=None, order=None): domain = domain or [] diff --git a/yuthon_hr_employee/views/yuthon_hr_department_views.xml b/yuthon_hr_employee/views/yuthon_hr_department_views.xml index 824a51649..a72bae1d6 100644 --- a/yuthon_hr_employee/views/yuthon_hr_department_views.xml +++ b/yuthon_hr_employee/views/yuthon_hr_department_views.xml @@ -62,11 +62,18 @@ - + 批量生成权限组 code records.do_department_to_res_groups() + + 重算部门层级显示名(去掉天河系/集团本部前缀) + + + code + records.action_recompute_hierarchical_name() + \ No newline at end of file