# -*- coding: utf-8 -*- from odoo import api, fields, models from odoo.exceptions import ValidationError class ApprovalProcess(models.Model): _name = 'approval.process' _rec_name = 'approval_category_id' approval_category_id = fields.Many2one('approval.category', string="流程名称") approvals_other_user_ids = fields.Many2many('res.users', string="传阅人", compute="_compute_other_user", store=True) user_ids = fields.One2many('approval.users', 'approval_process_id', string="审批流程") other_user_ids = fields.One2many('approval.other.users', 'approval_process_id', string="传阅人流程") company_id = fields.Many2one('res.company', string='公司', index=True, default=lambda self: self.env.company) @api.model_create_multi def create(self, vals_list): res = super(ApprovalProcess, self).create(vals_list) for vals in res: approval_category_id = self.env['approval.category'].search([('id', '=', vals.approval_category_id.id)]) approval_category_id.write({'approval_process_id': vals.id}) approval_category_id.onchange_approval_process_ids() return res def write(self, vals): res = super(ApprovalProcess, self).write(vals) approval_category_id = self.env['approval.category'].search([('id', '=', self.approval_category_id.id)]) approval_category_id.write({'approval_process_id': self.id}) approval_category_id.onchange_approval_process_ids() return res @api.depends('other_user_ids') def _compute_other_user(self): other_user_list = [] for other in self.other_user_ids: other_user_list += other.user_ids.ids self.approvals_other_user_ids = other_user_list class ApprovalUsers(models.Model): _name = 'approval.users' sequence = fields.Integer(default=1, string="审批顺序") approval_process_id = fields.Many2one('approval.process', string='审批流程') approval_user_role_id = fields.Many2one('yuthon.user.role', string='审批角色') user_ids = fields.Many2many('res.users', string="审批人", store=True, readonly=False) @api.onchange('approval_user_role_id') def compute_user_ids(self): for employee in self: employee_ids = employee.env['hr.employee'] if employee.approval_user_role_id: employee.user_ids = employee_ids.search([('user_role_ids', '=', employee.approval_user_role_id.id)]).user_id else: employee.user_ids = [] class ApprovalOtherUsers(models.Model): _name = 'approval.other.users' approval_process_id = fields.Many2one('approval.process', string='审批流程') approval_department_id = fields.Many2one('hr.department', string='审批部门') approval_user_role_id = fields.Many2one('yuthon.user.role', string='审批角色') user_ids = fields.Many2many('res.users', string="传阅人", compute="compute_user_ids", store=True, readonly=False) @api.depends('approval_department_id', 'approval_user_role_id') def compute_user_ids(self): res_users_list = [] for employee in self: employee_ids = employee.env['hr.employee'] department_domain = ('department_id', '=', employee.approval_department_id.id) user_role_domain = ('user_role_ids', '=', employee.approval_user_role_id.id) if employee.approval_department_id and not employee.approval_user_role_id: employee.user_ids = employee_ids.search([department_domain]).user_id res_users_list += employee.user_ids.ids elif employee.approval_user_role_id and not employee.approval_department_id: employee.user_ids = employee_ids.search([user_role_domain]).user_id res_users_list += employee.user_ids.ids elif employee.approval_user_role_id and employee.approval_department_id: employee.user_ids = employee_ids.search([department_domain, user_role_domain]).user_id res_users_list += employee.user_ids.ids else: employee.user_ids = []