From eb05e4889d650f06ae0c4e19115ddc1ba77b9746 Mon Sep 17 00:00:00 2001 From: Xavier ALT Date: Thu, 21 Dec 2023 11:39:48 +0100 Subject: [PATCH] [FIX] hr_contract: fix wrong salary structure in applicant salary conf. To reproduce (on runbot): - Enable "debug" mode - Switch to company "My Begian Company" - Go to: Payroll / Configuration / Contracts / Template - Create a new contract template: * Name: Student contract template * Salary structure: Belgian Student * Job position: Student - Create a new applicant for job position (Student) - name: John Doe - email: john@example.com - Click "Generate Offer": - Contract Template: Student contract template - Click "Save" - On the offer, click "Salary Configurator" => by default student has only 1 benefic in kind (regular pay) but on the salary configurator you see many more entries listed (ex: 13th Month) that are coming from another salary structure (Belgian Employee). During the simulation as no employee exists (it's an applicant), a new "temporary" employee is created and assignee to the contract; this update the company which trigger a recomputation of the `structure_type_id` field (since odoo/odoo@21aca4704) resetting its value to the country's default. This commit ensure we only reset that field when the country do change closes odoo/odoo#147079 Signed-off-by: Sofie Gvaladze (sgv) --- addons/hr_contract/models/hr_contract.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/addons/hr_contract/models/hr_contract.py b/addons/hr_contract/models/hr_contract.py index 5b78718b189..3b5e9189351 100644 --- a/addons/hr_contract/models/hr_contract.py +++ b/addons/hr_contract/models/hr_contract.py @@ -96,11 +96,21 @@ class Contract(models.Model): @api.depends('company_id') def _compute_structure_type_id(self): - structure_type_id = self.env['hr.payroll.structure.type'].search([('country_id', '=', self.company_id.country_id.id)], limit=1) - if not structure_type_id: - structure_type_id = self.env['hr.payroll.structure.type'].search([('country_id', '=', False)], limit=1) + + default_structure_by_country = {} + + def _default_salary_structure(country_id): + default_structure = default_structure_by_country.get(country_id) + if default_structure is None: + default_structure = default_structure_by_country[country_id] = ( + self.env['hr.payroll.structure.type'].search([('country_id', '=', country_id)], limit=1) + or self.env['hr.payroll.structure.type'].search([('country_id', '=', False)], limit=1) + ) + return default_structure + for contract in self: - contract.structure_type_id = structure_type_id + if not contract.structure_type_id or contract.structure_type_id.country_id != contract.company_id.country_id: + contract.structure_type_id = _default_salary_structure(contract.company_id.country_id.id) @api.onchange('structure_type_id') def _onchange_structure_type_id(self):