from odoo import models, fields, api, _ class HrEmployee(models.Model): _inherit = 'hr.employee' company_ids = fields.Many2many( 'res.company', string='Branches', domain="[('parent_id', '!=', False)]", help="Branch companies this employee is associated with." ) # Overriding company_id to be computed from company_ids # This maintains compatibility with standard Odoo logic that expects a single company_id company_id = fields.Many2one( 'res.company', string='Company', compute='_compute_company_id', store=True, readonly=False, required=True, help="The primary company of the employee. Automatically set to the first branch in Branches." ) attendance_manager_id = fields.Many2one( 'res.users', domain="[('share', '=', False), ('company_ids', 'in', company_ids)]" ) @api.model_create_multi def create(self, vals_list): employees = super().create(vals_list) employees.sudo()._sync_user_company_ids() return employees def write(self, vals): res = super().write(vals) if any(f in vals for f in ['company_ids', 'company_id', 'user_id']): self.sudo()._sync_user_company_ids() return res def _sync_user_company_ids(self): for employee in self: if employee.user_id: user = employee.user_id employee_companies = employee.company_ids | employee.company_id companies_to_add = employee_companies - user.company_ids if companies_to_add: user.write({ 'company_ids': [(4, cid.id) for cid in companies_to_add] }) @api.depends('company_ids') def _compute_company_id(self): for employee in self: if employee.company_ids: employee.company_id = employee.company_ids[0] elif employee.company_id: # Sync company_ids from company_id for existing records employee.company_ids = [(6, 0, [employee.company_id.id])] else: employee.company_id = self.env.company employee.company_ids = [(6, 0, [self.env.company.id])] @api.model def _search(self, domain, offset=0, limit=None, order=None, **kwargs): """ Override search to include company_ids and fallback to company_id if empty """ def replace_company_leaf(dom): if not dom: return dom new_dom = [] for leaf in dom: if isinstance(leaf, (list, tuple)): if leaf[0] == 'company_id': # Match if Branch is in search OR (Branch is empty AND Company is in search) new_dom.append('|') new_dom.append(('company_ids', leaf[1], leaf[2])) new_dom.append('&') new_dom.append(('company_ids', '=', False)) new_dom.append(('company_id', leaf[1], leaf[2])) else: new_dom.append(leaf) elif isinstance(leaf, list): new_dom.append(replace_company_leaf(leaf)) else: new_dom.append(leaf) return new_dom if domain: domain = replace_company_leaf(domain) return super()._search(domain, offset=offset, limit=limit, order=order, **kwargs) class HrEmployeePublic(models.Model): _inherit = 'hr.employee.public' company_ids = fields.Many2many( 'res.company', related='employee_id.company_ids', readonly=True, string='Branches' )