diff --git a/addons/hr_timesheet/i18n/hr_timesheet.pot b/addons/hr_timesheet/i18n/hr_timesheet.pot index e88fcdda686..dbcf4fa6107 100644 --- a/addons/hr_timesheet/i18n/hr_timesheet.pot +++ b/addons/hr_timesheet/i18n/hr_timesheet.pot @@ -1206,6 +1206,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" @@ -1344,6 +1350,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 9efd1a9cc50..dadbdb3af71 100644 --- a/addons/hr_timesheet/models/hr_timesheet.py +++ b/addons/hr_timesheet/models/hr_timesheet.py @@ -124,11 +124,12 @@ class AccountAnalyticLine(models.Model): # 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'): @@ -137,7 +138,10 @@ class AccountAnalyticLine(models.Model): 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 @@ -150,6 +154,10 @@ class AccountAnalyticLine(models.Model): 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 5ecc2fb4051..8de9c1a40fc 100644 --- a/addons/hr_timesheet/tests/test_timesheet.py +++ b/addons/hr_timesheet/tests/test_timesheet.py @@ -498,3 +498,22 @@ class TestTimesheet(TestCommonTimesheet): 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 64cf21409f0..2e2cc70db69 100644 --- a/addons/hr_timesheet/views/hr_timesheet_views.xml +++ b/addons/hr_timesheet/views/hr_timesheet_views.xml @@ -47,6 +47,7 @@ 0 1 many2one_avatar_employee + {'active_test': True} @@ -163,7 +164,7 @@ 10 - + diff --git a/addons/hr_timesheet/views/project_views.xml b/addons/hr_timesheet/views/project_views.xml index f3df8dcc5e8..12a0c810269 100644 --- a/addons/hr_timesheet/views/project_views.xml +++ b/addons/hr_timesheet/views/project_views.xml @@ -151,7 +151,7 @@ - + @@ -171,7 +171,7 @@
- +
@@ -197,7 +197,7 @@ - +