From f032256c4f92f6ce7aed4902cd09c94f66e44625 Mon Sep 17 00:00:00 2001 From: Paul Stroobant Date: Wed, 20 Dec 2023 11:04:50 +0100 Subject: [PATCH] [FIX] hr_contract: prevent copy of contract_id on employee duplicate Steps to reproduce issue: 1. Create two companies, **A** and **B**. 2. Create employee *X* in **A**. 3. Create a contract, assign it to *X* and change its state to Running. 4. Duplicate *X* to create employee *Y* 5. Change *Y*'s company to **B** (bypass warnings and errors). 6. Try to access Payroll Dashboard. 7. A Multi-Company access errors is raised Explanation: When duplicating *X*, the value from `contract_id` is copied to *Y*. After *Y*'s company is changed to **B**, Payroll Dashboard tries to retrieve data from its `contract_id` and, the contract still being related to **A**, a Multi-Company access error occurs. Suggested fix: Preventing the value being copied solves the problem. It also lines up with the `contract_ids` not being copied. The root issue existing in *v15.0*, the fix will be implemented in that version too. opw-3623483 closes odoo/odoo#146890 X-original-commit: 703863d86c603a4cbbd6115713cd5d49e1887a22 Signed-off-by: Sofie Gvaladze (sgv) Signed-off-by: Stroobant Paul (stpa) --- addons/hr_contract/models/hr_employee.py | 2 +- addons/hr_contract/tests/test_contract.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/addons/hr_contract/models/hr_employee.py b/addons/hr_contract/models/hr_employee.py index ba1536f4e99..1c4d12c2234 100644 --- a/addons/hr_contract/models/hr_employee.py +++ b/addons/hr_contract/models/hr_employee.py @@ -35,7 +35,7 @@ class Employee(models.Model): contract_ids = fields.One2many('hr.contract', 'employee_id', string='Employee Contracts') contract_id = fields.Many2one( 'hr.contract', string='Current Contract', groups="hr.group_hr_user", - domain="[('company_id', '=', company_id), ('employee_id', '=', id)]", help='Current contract of the employee') + domain="[('company_id', '=', company_id), ('employee_id', '=', id)]", help='Current contract of the employee', copy=False) calendar_mismatch = fields.Boolean(related='contract_id.calendar_mismatch') contracts_count = fields.Integer(compute='_compute_contracts_count', string='Contract Count') contract_warning = fields.Boolean(string='Contract Warning', store=True, compute='_compute_contract_warning', groups="hr.group_hr_user") diff --git a/addons/hr_contract/tests/test_contract.py b/addons/hr_contract/tests/test_contract.py index 5cae97df191..55d9c4545e7 100644 --- a/addons/hr_contract/tests/test_contract.py +++ b/addons/hr_contract/tests/test_contract.py @@ -134,6 +134,11 @@ class TestHrContracts(TestContractCommon): draft_contract.state = 'draft' self.assertEqual(self.employee.contract_id, contract) + def test_copy_employee_contract_create(self): + contract = self.create_contract('open', 'normal', date(2018, 1, 1), date(2018, 1, 2)) + duplicate_employee = self.employee.copy() + self.assertNotEqual(duplicate_employee.contract_id, contract) + def test_check_multi_company_contract_expiration(self): """ Check that the expiration warnings for contracts and work permits are posted based on the res settings.