From cdfd0443e9689eea00624e8f776ec14932b00cf5 Mon Sep 17 00:00:00 2001 From: zhou <123@> Date: Mon, 9 Jun 2025 14:20:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yuthon_will_task/models/yuthon_will_task.py | 40 ++++++++++--------- .../views/yuthon_will_task_views.xml | 2 +- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/yuthon_will_task/models/yuthon_will_task.py b/yuthon_will_task/models/yuthon_will_task.py index 7ec8512ea..c00d2f8a4 100644 --- a/yuthon_will_task/models/yuthon_will_task.py +++ b/yuthon_will_task/models/yuthon_will_task.py @@ -74,7 +74,7 @@ class YuthonWillTask(models.Model): remarks = fields.Text(string='备注') creation_time = fields.Date(string='创建时间', default=fields.Date.today()) employee_ids = fields.Many2many('hr.employee', 'will_task_employee_rel', 'custom_id', 'employee_id', - string='人员', store=True, index=True) + string='人员', store=True, index=True, search='_search_part_of_company') employee_ids2 = fields.Many2many('hr.employee', 'will_task_employee_rel2', 'custom_id', 'employee_id', string='司机', store=True, index=True) users_ids = fields.Many2many('res.users', string='用户', store=True, index=True, default=lambda self: self.env.user) @@ -88,24 +88,26 @@ class YuthonWillTask(models.Model): @api.onchange('is_this_company') def _onchange_is_this_company(self): - pass - # """当是否为本公司字段变化时,更新员工列表和domain""" - # if self.is_this_company: - # # 获取当前用户所属的公司列表 - # company_ids = self.env.companies.ids - # # 过滤已选员工,只保留属于当前公司的员工 - # self.employee_ids = self.employee_ids.filtered( - # lambda emp: emp.company_id.id in company_ids - # ) - # # 返回domain,限制可选员工为当前公司的员工 - # return { - # 'domain': { - # 'employee_ids': [('company_id', 'in', company_ids)] - # } - # } - # else: - # # 当不限制为本公司时,清空domain,允许选择所有员工 - # return {'domain': {'employee_ids': []}} + for record in self: + if record.is_this_company: + # 返回本公司员工的domain + return {'domain': {'employee_ids': [('company_id', '=', self.env.company.id)]}} + else: + # 返回所有员工的domain + return {'domain': {'employee_ids': []}} + + def _search_part_of_company(self, operator, value): + if operator not in ('=', '!=') or not isinstance(value, bool): + raise UserError(('Operation not supported')) + company_ids = self.env.companies.ids # 获取公司ID列表 + # 处理反向操作符 + if not value: + operator = '!=' if operator == '=' else '=' + # 使用company_id in company_ids的结构 + if operator == '=': + return [('company_id', 'in', company_ids)] + else: # operator == '!=' + return [('company_id', 'not in', company_ids)] @api.onchange('employee_ids', 'user_id') def _onchange_employee_ids(self): diff --git a/yuthon_will_task/views/yuthon_will_task_views.xml b/yuthon_will_task/views/yuthon_will_task_views.xml index c3e9c9c20..c62ec8395 100644 --- a/yuthon_will_task/views/yuthon_will_task_views.xml +++ b/yuthon_will_task/views/yuthon_will_task_views.xml @@ -84,7 +84,7 @@ - +