From c1f04bef83aa74edd0cd2c8cf8aa4ff0a6de6aa4 Mon Sep 17 00:00:00 2001 From: Kishan Gajjar Date: Thu, 11 Jan 2018 11:19:10 +0530 Subject: [PATCH] [IMP] hr_timesheet: make it easier to encode timesheets In this commit, we do two small changes to make it easier to encode timesheets. The main issue is that we want to solve the following situation: a user clicks on a cell in a timesheet grid view to open a list view, then clicks on 'Create' to create a new timesheet. This will open a form view, but some fields which could be prefilled are not filled. - do not reset task_id in some cases. This is important for the onchange call which comes after a default_get, when there is a default project value set. - do not set employee_id if there is default_employee_id in context This is useful when we open timesheets from the 'All Timesheets' menu item, then add a new line, select a different employee, and open it in the grid view (by clicking on the + icon from a cell). This opens a list view, which is editable, but when we create a new entry, it ignores the employee value. Note: a commit in enterprise adds support for properly passing contexts in grid view, see https://github.com/odoo/enterprise/commit/f23003917f18b52bb69483cdaac3cb2164230aa6 Task: #35414 --- addons/hr_timesheet/models/hr_timesheet.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/addons/hr_timesheet/models/hr_timesheet.py b/addons/hr_timesheet/models/hr_timesheet.py index 1988a2cd64a..5344a16aed0 100644 --- a/addons/hr_timesheet/models/hr_timesheet.py +++ b/addons/hr_timesheet/models/hr_timesheet.py @@ -11,7 +11,7 @@ class AccountAnalyticLine(models.Model): @api.model def default_get(self, field_list): result = super(AccountAnalyticLine, self).default_get(field_list) - if 'employee_id' in field_list and result.get('user_id'): + if not self.env.context.get('default_employee_id') and 'employee_id' in field_list and result.get('user_id'): result['employee_id'] = self.env['hr.employee'].search([('user_id', '=', result['user_id'])], limit=1).id return result @@ -23,10 +23,11 @@ class AccountAnalyticLine(models.Model): @api.onchange('project_id') def onchange_project_id(self): - # reset task when changing project - self.task_id = False # force domain on task when project is set if self.project_id: + if self.project_id != self.task_id.project_id: + # reset task when changing project + self.task_id = False return {'domain': { 'task_id': [('project_id', '=', self.project_id.id)] }}