diff --git a/addons/hr_timesheet/i18n/hr_timesheet.pot b/addons/hr_timesheet/i18n/hr_timesheet.pot index e16c31d3cab93..769138cc0138f 100644 --- a/addons/hr_timesheet/i18n/hr_timesheet.pot +++ b/addons/hr_timesheet/i18n/hr_timesheet.pot @@ -1137,6 +1137,12 @@ msgstr "" msgid "Timesheets can be logged on this task." msgstr "" +#. module: hr_timesheet +#: code:addons/hr_timesheet/models/hr_timesheet.py:0 +#, python-format +msgid "Timesheets must be created with an active employee." +msgstr "" + #. module: hr_timesheet #: model:digest.tip,name:hr_timesheet.digest_tip_hr_timesheet_0 msgid "Tip: Record your Timesheets faster" @@ -1282,6 +1288,12 @@ msgid "" "to timesheet on the project." msgstr "" +#. module: hr_timesheet +#: code:addons/hr_timesheet/models/hr_timesheet.py:0 +#, python-format +msgid "You cannot set an archived employee to the existing timesheets." +msgstr "" + #. module: hr_timesheet #: code:addons/hr_timesheet/models/project.py:0 #, python-format diff --git a/addons/hr_timesheet/models/hr_timesheet.py b/addons/hr_timesheet/models/hr_timesheet.py index cdabc9c01f335..450fa4a083e4a 100644 --- a/addons/hr_timesheet/models/hr_timesheet.py +++ b/addons/hr_timesheet/models/hr_timesheet.py @@ -120,11 +120,12 @@ def create(self, vals_list): # Although this make a second loop on the vals, we need to wait the preprocess as it could change the company_id in the vals # TODO To be refactored in master - employees = self.env['hr.employee'].sudo().with_context(active_test=False).search([('user_id', 'in', user_ids)]) + employees = self.env['hr.employee'].sudo().search([('user_id', 'in', user_ids)]) employee_for_user_company = defaultdict(dict) for employee in employees: employee_for_user_company[employee.user_id.id][employee.company_id.id] = employee.id + employee_ids = set() for vals in vals_list: # compute employee only for timesheet lines, makes no sense for other lines if not vals.get('employee_id') and vals.get('project_id'): @@ -133,7 +134,10 @@ def create(self, vals_list): continue company_id = list(employee_for_company)[0] if len(employee_for_company) == 1 else vals.get('company_id', self.env.company.id) vals['employee_id'] = employee_for_company.get(company_id, False) - + elif vals.get('employee_id'): + employee_ids.add(vals['employee_id']) + if any(not emp.active for emp in self.env['hr.employee'].browse(list(employee_ids))): + raise UserError(_('Timesheets must be created with an active employee.')) lines = super(AccountAnalyticLine, self).create(vals_list) for line, values in zip(lines, vals_list): if line.project_id: # applied only for timesheet @@ -146,6 +150,10 @@ def write(self, values): raise AccessError(_("You cannot access timesheets that are not yours.")) values = self._timesheet_preprocess(values) + if values.get('employee_id'): + employee = self.env['hr.employee'].browse(values['employee_id']) + if not employee.active: + raise UserError(_('You cannot set an archived employee to the existing timesheets.')) if 'name' in values and not values.get('name'): values['name'] = '/' result = super(AccountAnalyticLine, self).write(values) diff --git a/addons/hr_timesheet/tests/test_timesheet.py b/addons/hr_timesheet/tests/test_timesheet.py index b70ec5839ef36..fdbce0dee548b 100644 --- a/addons/hr_timesheet/tests/test_timesheet.py +++ b/addons/hr_timesheet/tests/test_timesheet.py @@ -419,3 +419,22 @@ def test_ensure_product_uom_set_in_timesheet(self): timesheet1.unit_amount + timesheet2.unit_amount, 'The total timesheet time of this project should be equal to 4.' ) + def test_create_timesheet_with_archived_employee(self): + ''' the timesheet can be created or edited only with an active employee + ''' + self.empl_employee2.active = False + batch_vals = { + 'project_id': self.project_customer.id, + 'task_id': self.task1.id, + 'name': 'archived employee timesheet', + 'unit_amount': 3, + 'employee_id': self.empl_employee2.id + } + + self.assertRaises(UserError, self.env['account.analytic.line'].create, batch_vals) + + batch_vals["employee_id"] = self.empl_employee.id + timesheet = self.env['account.analytic.line'].create(batch_vals) + + with self.assertRaises(UserError): + timesheet.employee_id = self.empl_employee2 diff --git a/addons/hr_timesheet/views/hr_timesheet_views.xml b/addons/hr_timesheet/views/hr_timesheet_views.xml index b0a1fc36ca049..9038a8ceafc31 100644 --- a/addons/hr_timesheet/views/hr_timesheet_views.xml +++ b/addons/hr_timesheet/views/hr_timesheet_views.xml @@ -46,6 +46,7 @@ 0 1 many2one_avatar_employee + {'active_test': True} @@ -151,7 +152,7 @@ 10 - + diff --git a/addons/hr_timesheet/views/project_views.xml b/addons/hr_timesheet/views/project_views.xml index 9c7a0cfb30743..05fdcf4d3cdf8 100644 --- a/addons/hr_timesheet/views/project_views.xml +++ b/addons/hr_timesheet/views/project_views.xml @@ -112,7 +112,7 @@ - + @@ -122,7 +122,7 @@ - + @@ -157,7 +157,7 @@ - +