diff options
| author | Anh Thao Pham (pta) <pta@odoo.com> | 2020-02-11 16:00:53 +0000 |
|---|---|---|
| committer | Yannick Tivisse <yti@odoo.com> | 2020-04-20 10:32:45 +0000 |
| commit | 3bd345597fa932abdaf780f72bacf331690f549b (patch) | |
| tree | b94e535b7ad2a7d4667595341ff66b5eded537cc /addons/hr_contract/models | |
| parent | f50c3298bb938cd044636956719d986bb91160ae (diff) | |
[REF] hr*: convert onchange methods to stored-editable computed fields
Impacted modules:
hr, hr_contract, hr_recruitment, hr_payroll, fleet, hr_skills, hr_appraisal, ....
Several onchanges have been converted to computed fields in the following modules :
Community :
- hr
- hr_contract
- hr_recruitment
- hr_work_entry
- hr_maintenance
- hr_expense
- hr_expense_check
- hr_holidays
- sale_expense
- account_analytic_default_hr_expense
Enterprise:
- hr_contract_salary
- hr_referral
- hr_payroll
- hr_payroll_expense
- test_l10n_be_hr_payroll_account
There are still 2 onchanges with complex behavior that couldn't be converted easily:
- an onchange that updates "tz" (timezone) that is defined as a related field
to "resource_id.tz". Apparently it is useless except to initialize the default
value of "tz".
- an onchange that updates "name" that is defined as a related field to
"resource_id.name".
the applicant.
closes odoo/odoo#45414
Taskid: 2169099
Related: odoo/enterprise#8572
Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
Diffstat (limited to 'addons/hr_contract/models')
| -rw-r--r-- | addons/hr_contract/models/hr_contract.py | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/addons/hr_contract/models/hr_contract.py b/addons/hr_contract/models/hr_contract.py index 247103b5d7e..9e79cd89b32 100644 --- a/addons/hr_contract/models/hr_contract.py +++ b/addons/hr_contract/models/hr_contract.py @@ -18,8 +18,10 @@ class Contract(models.Model): active = fields.Boolean(default=True) structure_type_id = fields.Many2one('hr.payroll.structure.type', string="Salary Structure Type") employee_id = fields.Many2one('hr.employee', string='Employee', tracking=True, domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]") - department_id = fields.Many2one('hr.department', domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]", string="Department") - job_id = fields.Many2one('hr.job', domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]", string='Job Position') + department_id = fields.Many2one('hr.department', compute='_compute_employee_contract', store=True, readonly=False, + domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]", string="Department") + job_id = fields.Many2one('hr.job', compute='_compute_employee_contract', store=True, readonly=False, + domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]", string='Job Position') date_start = fields.Date('Start Date', required=True, default=fields.Date.today, tracking=True, help="Start date of the contract.") date_end = fields.Date('End Date', tracking=True, @@ -27,7 +29,7 @@ class Contract(models.Model): trial_date_end = fields.Date('End of Trial Period', help="End date of the trial period (if there is one).") resource_calendar_id = fields.Many2one( - 'resource.calendar', 'Working Schedule', + 'resource.calendar', 'Working Schedule', compute='_compute_employee_contract', store=True, readonly=False, default=lambda self: self.env.company.resource_calendar_id.id, copy=False, domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]") wage = fields.Monetary('Wage', required=True, tracking=True, help="Employee's monthly gross wage.") @@ -39,7 +41,8 @@ class Contract(models.Model): ('cancel', 'Cancelled') ], string='Status', group_expand='_expand_states', copy=False, tracking=True, help='Status of the contract', default='draft') - company_id = fields.Many2one('res.company', default=lambda self: self.env.company, required=True) + company_id = fields.Many2one('res.company', compute='_compute_employee_contract', store=True, readonly=False, + default=lambda self: self.env.company, required=True) company_country_id = fields.Many2one('res.country', string="Company country", related='company_id.country_id', readonly=True) """ @@ -70,13 +73,13 @@ class Contract(models.Model): def _expand_states(self, states, domain, order): return [key for key, val in type(self).state.selection] - @api.onchange('employee_id') - def _onchange_employee_id(self): - if self.employee_id: - self.job_id = self.employee_id.job_id - self.department_id = self.employee_id.department_id - self.resource_calendar_id = self.employee_id.resource_calendar_id - self.company_id = self.employee_id.company_id + @api.depends('employee_id') + def _compute_employee_contract(self): + for contract in self.filtered('employee_id'): + contract.job_id = contract.employee_id.job_id + contract.department_id = contract.employee_id.department_id + contract.resource_calendar_id = contract.employee_id.resource_calendar_id + contract.company_id = contract.employee_id.company_id @api.onchange('company_id') def _onchange_company_id(self): |
