78 lines
3.4 KiB
Python
78 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
from odoo import api, fields, models
|
|
from pypinyin import pinyin,lazy_pinyin
|
|
from odoo.osv import expression
|
|
|
|
class YuthonHrDepartment(models.Model):
|
|
_inherit = "hr.department"
|
|
_rec_name = 'name'
|
|
_order = 'department_code asc'
|
|
|
|
def change_company_id(self):
|
|
department_ids = self.browse(self._context.get('active_ids', self._context.get('active_id')))
|
|
for i in department_ids:
|
|
company_id = self.env['res.company'].search([('name', '=', i.parent_id.name)])
|
|
if company_id:
|
|
i.write({'company_id': company_id})
|
|
|
|
department_phone = fields.Char(string="部门电话")
|
|
department_fax = fields.Char(string="部门传真")
|
|
department_location = fields.Char(string="部门地址")
|
|
department_function = fields.Char(string="部门职能")
|
|
is_institution = fields.Boolean(string="是否机构")
|
|
is_global_department = fields.Boolean(string="是否全局部门")
|
|
create_date = fields.Date(string="创建时间", default=fields.Date.today())
|
|
start_date = fields.Date(string="生效日期", default=fields.Date.today())
|
|
expiration_date = fields.Date(string="失效日期", default=fields.Date.today())
|
|
end_date = fields.Date(string="结束时间")
|
|
department_code = fields.Char(string="部门序号")
|
|
company_id = fields.Many2one('res.company', string="所属公司", index=True, default=lambda self: self.env.company)
|
|
pinyin_name = fields.Char(string="拼音名称", compute='_compute_pinyin', index=True, store=True)
|
|
|
|
def do_department_to_res_groups(self):
|
|
"""
|
|
批量生成新的权限组
|
|
:return:
|
|
"""
|
|
for record in self:
|
|
res_groups_objs = self.sudo().env["res.groups"].search([("department_ids", "in", record.ids)])
|
|
user_ids = self.sudo().env["hr.employee"].search([("department_id", "=", record.id)]).user_id.ids
|
|
if not res_groups_objs:
|
|
# 查询当前部门下的员工的数量
|
|
self.sudo().env["res.groups"].create({
|
|
"name": record.name,
|
|
"department_ids": [(4, id) for id in record.ids] ,
|
|
"users": [(4, id) for id in user_ids]
|
|
})
|
|
else:
|
|
for res_groups_obj in res_groups_objs:
|
|
res_groups_obj.sudo().write({
|
|
"users": [(4, id) for id in user_ids]
|
|
})
|
|
|
|
|
|
@api.onchange('department_code', 'parent_id')
|
|
def _onchange_department_code(self):
|
|
for code in self:
|
|
if code.parent_id.department_code:
|
|
code.department_code = code.parent_id.department_code + (code.department_code.split('.')[-1] if code.department_code else '')
|
|
else:
|
|
code.department_code = code.department_code
|
|
|
|
@api.depends('name')
|
|
def _compute_pinyin(self):
|
|
for employee in self:
|
|
employee.pinyin_name = ''.join(lazy_pinyin(employee.name))
|
|
|
|
|
|
@api.model
|
|
def _name_search(self, name, domain=None, operator='ilike', limit=None, order=None):
|
|
domain = domain or []
|
|
if name:
|
|
input_pinyin = ''.join(lazy_pinyin(name))
|
|
search_domain = ['|', ('pinyin_name', operator, input_pinyin), ('name', operator, name)]
|
|
domain = expression.AND([search_domain, domain])
|
|
return self._search(domain, limit=limit, order=order)
|
|
|
|
|