diff --git a/addons/hr_timesheet/models/hr_timesheet.py b/addons/hr_timesheet/models/hr_timesheet.py index dadbdb3af71..2f1de1d4d76 100644 --- a/addons/hr_timesheet/models/hr_timesheet.py +++ b/addons/hr_timesheet/models/hr_timesheet.py @@ -12,6 +12,13 @@ from odoo.osv import expression class AccountAnalyticLine(models.Model): _inherit = 'account.analytic.line' + @api.model + def _get_favorite_project_id(self): + last_timesheet_ids = self.search([('user_id', '=', self._uid), ('project_id', '!=', False)], limit=5) + if len(last_timesheet_ids) == 5 and len(last_timesheet_ids.project_id) == 1: + return last_timesheet_ids.project_id.id + return False + @api.model def default_get(self, field_list): result = super(AccountAnalyticLine, self).default_get(field_list) @@ -19,6 +26,10 @@ class AccountAnalyticLine(models.Model): result['encoding_uom_id'] = self.env.company.timesheet_encode_uom_id.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']), ('company_id', '=', result.get('company_id', self.env.company.id))], limit=1).id + if not self._context.get('default_project_id') and 'project_id' in field_list: + favorite_project_id = self._get_favorite_project_id() + if favorite_project_id: + result['project_id'] = favorite_project_id return result def _domain_project_id(self):