From c2fafa05de628b24ee810419e059052b09c72b5a Mon Sep 17 00:00:00 2001 From: Prakash Prajapati Date: Wed, 18 Jan 2023 07:30:13 +0000 Subject: [PATCH] [IMP] hr_timesheet: use name_get to add employee's company Currently, if both companies have employees with the same name, it is not known which company the selected employee belongs to. After this commit: - The name of the company will only be appended by the name_get when more than one company is present in the database. Example - Administrator - My Company - Administrator - Tiny ERP task-3101249 closes odoo/odoo#107938 Signed-off-by: Xavier Bol (xbo) --- addons/hr_timesheet/models/hr_employee.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/addons/hr_timesheet/models/hr_employee.py b/addons/hr_timesheet/models/hr_employee.py index 04248322a36..30d58ce0f87 100644 --- a/addons/hr_timesheet/models/hr_employee.py +++ b/addons/hr_timesheet/models/hr_employee.py @@ -23,6 +23,26 @@ class HrEmployee(models.Model): for employee in self: employee.has_timesheet = result.get(employee.id, False) + def name_get(self): + res = super().name_get() + allowed_company_ids = self.env.context.get('allowed_company_ids', []) + if len(allowed_company_ids) <= 1: + return res + + name_mapping = dict(res) + employees_count_per_user = { + res['user_id'][0]: res['user_id_count'] + for res in self.env['hr.employee'].sudo()._read_group( + [('user_id', 'in', self.user_id.ids), ('company_id', 'in', allowed_company_ids)], + ['user_id'], + ['user_id'], + ) + } + for employee in self: + if employees_count_per_user.get(employee.user_id.id, 0) > 1: + name_mapping[employee.id] = f'{name_mapping[employee.id]} - {employee.company_id.name}' + return list(name_mapping.items()) + def action_unlink_wizard(self): wizard = self.env['hr.employee.delete.wizard'].create({ 'employee_ids': self.ids,