[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) <sgv@odoo.com>
Signed-off-by: Stroobant Paul (stpa) <stpa@odoo.com>
This commit is contained in:
Paul Stroobant
2023-12-20 12:36:44 +00:00
parent b5306fcd1b
commit f032256c4f
2 changed files with 6 additions and 1 deletions
+1 -1
View File
@@ -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")
@@ -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.