From 856f0f481cf224b065b1cc1bfdde21a83e7547e7 Mon Sep 17 00:00:00 2001 From: "Thomas Lefebvre (thle)" Date: Tue, 6 Jun 2023 06:40:03 +0000 Subject: [PATCH] [FIX] hr_employee: synchronise if user is removed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Steps to reproduce: ------------------- - create two employees (A and B); - create a user; - add the user in Related User of employee A; - remove the user; - add the user in Related User of employee B; - change the Work Email of the employee B. Issue: ------ The Work Email of the employee A is also updated. Cause: ------ When we add a `user_id` to an employee, we update the `work_contact_id` field. Fields `mobile_phone` and `work_email` are inverse fields. When we modify them, the `_inverse_work_contact_details` method is called. We update the `work_contact_id` linked to the employee. When we delete an employee's `user_id`, we don't update the `work_contact_id`. Therefore, when we update an employee's `work_contact_id`, we call the `_compute_work_contact_details` method method for all employees who have the same `work_contact_id`. The result is that we modify the `mobile_phone` and `ẁork_email` fields for all employees linked to the `work_contact_id`. Solution: --------- Differentiate the case where the `user_id` is `False` (>< `None`) when it is modified and does not contain a value to "synchronise" the `work_contact_id`. opw-3338188 closes odoo/odoo#123965 X-original-commit: 85ab51c7cf7c8ee011a903a9460e2aff1296f215 Signed-off-by: Kevin Baptiste Signed-off-by: Lefebvre Thomas (thle) --- addons/hr/models/hr_employee.py | 2 +- addons/hr/tests/test_hr_employee.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/addons/hr/models/hr_employee.py b/addons/hr/models/hr_employee.py index 386fe5d9fda..f36757482f5 100644 --- a/addons/hr/models/hr_employee.py +++ b/addons/hr/models/hr_employee.py @@ -390,7 +390,7 @@ class HrEmployeePrivate(models.Model): self.message_unsubscribe(self.address_home_id.ids) if vals['address_home_id']: self._message_subscribe([vals['address_home_id']]) - if vals.get('user_id'): + if 'user_id' in vals: # Update the profile pictures with user, except if provided vals.update(self._sync_user(self.env['res.users'].browse(vals['user_id']), (bool(self.image_1920)))) diff --git a/addons/hr/tests/test_hr_employee.py b/addons/hr/tests/test_hr_employee.py index ed9b813b99e..b0f74bd187f 100644 --- a/addons/hr/tests/test_hr_employee.py +++ b/addons/hr/tests/test_hr_employee.py @@ -181,3 +181,31 @@ class TestHrEmployee(TestHrCommon): 'partner_id': partner.id, }) self.assertFalse(self.env['res.users'].search([('login', '=', 'test_user')]).employee_id) + + def test_employee_update_work_contact_id(self): + """ + Check that the `work_contact_id` information is no longer + updated when an employee's `user_id` is removed. + """ + user = self.env['res.users'].create({ + 'name': 'Test', + 'login': 'test', + 'email': 'test@example.com', + }) + employee_A, employee_B = self.env['hr.employee'].create([ + { + 'name': 'Employee A', + 'user_id': user.id, + 'work_email': 'employee_A@example.com', + }, + { + 'name': 'Employee B', + 'user_id': False, + 'work_email': 'employee_B@example.com', + } + ]) + employee_A.user_id = False + employee_B.user_id = user.id + employee_B.work_email = 'new_email@example.com' + self.assertEqual(employee_A.work_email, 'employee_A@example.com') + self.assertEqual(employee_B.work_email, 'new_email@example.com')