[IMP] hr_timesheet: convert onchange to stored-editable computed fields
The fields 'project_id' and 'user_id' have been converted to stored-editable computed fields. Convert the 'allow_timesheets' field. Improvement of the _compute_task_id method to check the task_id field before using it. Update hr_timesheet tests by removing calls to onchange methods because now, compute methods are always called. Get back the onchange on the 'project_id' to compute the task_id field due to a complex bug found in sale_timesheet tests.
This commit is contained in:
committed by
Yannick Tivisse
parent
0d327e2d81
commit
d924d89ab8
@@ -40,10 +40,11 @@ class AccountAnalyticLine(models.Model):
|
||||
|
||||
task_id = fields.Many2one(
|
||||
'project.task', 'Task', index=True,
|
||||
domain="[('company_id', '=', company_id), ('project_id.allow_timesheets', '=', True), ('project_id', '=?', project_id)]"
|
||||
)
|
||||
project_id = fields.Many2one('project.project', 'Project', domain=_domain_project_id)
|
||||
|
||||
domain="[('company_id', '=', company_id), ('project_id.allow_timesheets', '=', True), ('project_id', '=?', project_id)]")
|
||||
project_id = fields.Many2one(
|
||||
'project.project', 'Project', compute='_compute_project_id', store=True, readonly=False,
|
||||
domain=_domain_project_id)
|
||||
user_id = fields.Many2one(compute='_compute_user_id', store=True, readonly=False)
|
||||
employee_id = fields.Many2one('hr.employee', "Employee", check_company=True, domain=_domain_employee_id)
|
||||
department_id = fields.Many2one('hr.department', "Department", compute='_compute_department_id', store=True, compute_sudo=True)
|
||||
encoding_uom_id = fields.Many2one('uom.uom', compute='_compute_encoding_uom_id')
|
||||
@@ -58,17 +59,15 @@ class AccountAnalyticLine(models.Model):
|
||||
# reset task when changing project
|
||||
self.task_id = False
|
||||
|
||||
@api.onchange('task_id')
|
||||
def _onchange_task_id(self):
|
||||
if not self.project_id:
|
||||
self.project_id = self.task_id.project_id
|
||||
@api.depends('task_id.project_id')
|
||||
def _compute_project_id(self):
|
||||
for line in self.filtered(lambda line: not line.project_id):
|
||||
line.project_id = line.task_id.project_id
|
||||
|
||||
@api.onchange('employee_id')
|
||||
def _onchange_employee_id(self):
|
||||
if self.employee_id:
|
||||
self.user_id = self.employee_id.user_id
|
||||
else:
|
||||
self.user_id = self._default_user()
|
||||
@api.depends('employee_id')
|
||||
def _compute_user_id(self):
|
||||
for line in self:
|
||||
line.user_id = line.employee_id.user_id if line.employee_id else line._default_user()
|
||||
|
||||
@api.depends('employee_id')
|
||||
def _compute_department_id(self):
|
||||
|
||||
@@ -8,7 +8,9 @@ from odoo.exceptions import UserError, ValidationError, RedirectWarning
|
||||
class Project(models.Model):
|
||||
_inherit = "project.project"
|
||||
|
||||
allow_timesheets = fields.Boolean("Timesheets", default=True, help="Enable timesheeting on the project.")
|
||||
allow_timesheets = fields.Boolean(
|
||||
"Timesheets", compute='_compute_allow_timesheets', store=True, readonly=False,
|
||||
default=True, help="Enable timesheeting on the project.")
|
||||
analytic_account_id = fields.Many2one(
|
||||
# note: replaces ['|', ('company_id', '=', False), ('company_id', '=', company_id)]
|
||||
domain="""[
|
||||
@@ -23,10 +25,10 @@ class Project(models.Model):
|
||||
compute='_compute_total_timesheet_time',
|
||||
help="Total number of time (in the proper UoM) recorded in the project, rounded to the unit.")
|
||||
|
||||
@api.onchange('analytic_account_id')
|
||||
def _onchange_analytic_account(self):
|
||||
if not self.analytic_account_id and self._origin:
|
||||
self.allow_timesheets = False
|
||||
@api.depends('analytic_account_id')
|
||||
def _compute_allow_timesheets(self):
|
||||
without_account = self.filtered(lambda t: not t.analytic_account_id and t._origin)
|
||||
without_account.update({'allow_timesheets': False})
|
||||
|
||||
@api.constrains('allow_timesheets', 'analytic_account_id')
|
||||
def _check_allow_timesheet(self):
|
||||
|
||||
@@ -128,7 +128,6 @@ class TestTimesheet(TestCommonTimesheet):
|
||||
'unit_amount': 7,
|
||||
'employee_id': self.empl_employee2.id,
|
||||
})
|
||||
timesheet3._onchange_employee_id()
|
||||
self.assertEqual(timesheet3.user_id, self.user_employee2, 'Timesheet user should be the one linked to the given employee')
|
||||
self.assertEqual(timesheet3.product_uom_id, timesheet_uom, "The UoM of the timesheet 3 should be the one set on the company of the analytic account.")
|
||||
|
||||
|
||||
@@ -104,6 +104,7 @@
|
||||
<field name="name" required="0"/>
|
||||
<field name="unit_amount" widget="timesheet_uom" decoration-danger="unit_amount > 24"/>
|
||||
<field name="project_id" invisible="1"/>
|
||||
<field name="task_id" invisible="1"/>
|
||||
<field name="company_id" invisible="1"/>
|
||||
</tree>
|
||||
<kanban class="o_kanban_mobile">
|
||||
@@ -113,6 +114,7 @@
|
||||
<field name="name"/>
|
||||
<field name="unit_amount" decoration-danger="unit_amount > 24"/>
|
||||
<field name="project_id"/>
|
||||
<field name="task_id" invisible="1"/>
|
||||
<templates>
|
||||
<t t-name="kanban-box">
|
||||
<div t-attf-class="oe_kanban_card oe_kanban_global_click">
|
||||
@@ -147,6 +149,7 @@
|
||||
<field name="name"/>
|
||||
<field name="unit_amount" string="Duration" widget="float_time" decoration-danger="unit_amount > 24"/>
|
||||
<field name="project_id" invisible="1"/>
|
||||
<field name="task_id" invisible="1"/>
|
||||
<field name="company_id" invisible="1"/>
|
||||
</group>
|
||||
</sheet>
|
||||
|
||||
Reference in New Issue
Block a user