[IMP] project_timesheet_holidays : convert onchange to stored-editable computed fields.

The 'timesheet_generate' and 'timesheet_task_id' fields have been set as
stored-editable computed fields.

The condition to compute the timesheet_generat field has changed to be
coherent with the _check_timesheet_generate constraint.

Keep the timesheet_generate field without its default value (True) to avoid
raising a ValidationError through the _check_timesheet_generate constraint
before the timesheet_generate field has been computed.
This commit is contained in:
Rémy Baranx (bar)
2020-06-23 08:57:08 +00:00
committed by Yannick Tivisse
parent d924d89ab8
commit cd4e854e41
2 changed files with 33 additions and 21 deletions
@@ -16,29 +16,31 @@ class HolidaysType(models.Model):
company = self.company_id if self.company_id else self.env.company
return company.leave_timesheet_task_id.id
timesheet_generate = fields.Boolean('Generate Timesheet', default=True, help="If checked, when validating a time off, timesheet will be generated in the Vacation Project of the company.")
timesheet_generate = fields.Boolean(
'Generate Timesheet', compute='_compute_timesheet_generate', store=True, readonly=False,
help="If checked, when validating a time off, timesheet will be generated in the Vacation Project of the company.")
timesheet_project_id = fields.Many2one('project.project', string="Project", default=_default_project_id, domain="[('company_id', '=', company_id)]", help="The project will contain the timesheet generated when a time off is validated.")
timesheet_task_id = fields.Many2one('project.task', string="Task for timesheet", default=_default_task_id, domain="[('project_id', '=', timesheet_project_id), ('company_id', '=', company_id)]")
timesheet_task_id = fields.Many2one(
'project.task', string="Task for timesheet", compute='_compute_timesheet_task_id',
store=True, readonly=False, default=_default_task_id,
domain="[('project_id', '=', timesheet_project_id),"
"('company_id', '=', company_id)]")
@api.onchange('timesheet_task_id')
def _onchange_timesheet_generate(self):
if self.timesheet_task_id or self.timesheet_project_id:
self.timesheet_generate = True
else:
self.timesheet_generate = False
@api.depends('timesheet_task_id', 'timesheet_project_id')
def _compute_timesheet_generate(self):
for leave_type in self:
leave_type.timesheet_generate = leave_type.timesheet_task_id and leave_type.timesheet_project_id
@api.onchange('timesheet_project_id')
def _onchange_timesheet_project(self):
company = self.company_id if self.company_id else self.env.company
default_task_id = company.leave_timesheet_task_id
if default_task_id and default_task_id.project_id == self.timesheet_project_id:
self.timesheet_task_id = default_task_id
else:
self.timesheet_task_id = False
if self.timesheet_project_id:
self.timesheet_generate = True
else:
self.timesheet_generate = False
@api.depends('timesheet_project_id')
def _compute_timesheet_task_id(self):
for leave_type in self:
company = leave_type.company_id if leave_type.company_id else self.env.company
default_task_id = company.leave_timesheet_task_id
if default_task_id and default_task_id.project_id == leave_type.timesheet_project_id:
leave_type.timesheet_task_id = default_task_id
else:
leave_type.timesheet_task_id = False
@api.constrains('timesheet_generate', 'timesheet_project_id', 'timesheet_task_id')
def _check_timesheet_generate(self):
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models
from odoo import fields, models,api
class ResConfigSettings(models.TransientModel):
@@ -9,3 +9,13 @@ class ResConfigSettings(models.TransientModel):
leave_timesheet_project_id = fields.Many2one(related='company_id.leave_timesheet_project_id', string="Internal Project", domain="[('company_id', '=', company_id)]", readonly=False)
leave_timesheet_task_id = fields.Many2one(related='company_id.leave_timesheet_task_id', string="Time Off Task", domain="[('company_id', '=', company_id)]", readonly=False)
@api.onchange('leave_timesheet_project_id')
def _onchange_timesheet_project_id(self):
if self.leave_timesheet_project_id != self.leave_timesheet_task_id.project_id:
self.leave_timesheet_task_id = False
@api.onchange('leave_timesheet_task_id')
def _onchange_timesheet_task_id(self):
if self.leave_timesheet_task_id:
self.leave_timesheet_project_id = self.leave_timesheet_task_id.project_id