diff --git a/hr_attendance_overtime/models/hr_employee.py b/hr_attendance_overtime/models/hr_employee.py index 70e0b74e..c845dfeb 100644 --- a/hr_attendance_overtime/models/hr_employee.py +++ b/hr_attendance_overtime/models/hr_employee.py @@ -5,7 +5,8 @@ from dateutil.relativedelta import relativedelta from pytz import timezone, utc -from odoo import SUPERUSER_ID, api, fields, models +from odoo import SUPERUSER_ID, _, api, fields, models +from odoo.exceptions import UserError from odoo.osv import expression @@ -17,6 +18,8 @@ def todays_working_times(self, empl_domain): """Method used by my attendance/kiosk view in order to display employee planning and working times""" employee = self.search(empl_domain) + if not employee: + raise UserError(_("Employee not found or not created for current user")) employee.ensure_one() now = fields.Datetime.now() tz = timezone(employee.tz) diff --git a/hr_attendance_overtime/tests/test_attendance_overtime.py b/hr_attendance_overtime/tests/test_attendance_overtime.py index a14ddbcf..7f01bace 100644 --- a/hr_attendance_overtime/tests/test_attendance_overtime.py +++ b/hr_attendance_overtime/tests/test_attendance_overtime.py @@ -9,6 +9,7 @@ from odoo.tests.common import TransactionCase from odoo.tools import mute_logger +from odoo.exceptions import UserError class TestHrAttendanceOverTime(TransactionCase): @@ -610,6 +611,18 @@ def test_check_in_on_out_time(self): self.assertFalse(attendance.is_overtime) self.assertFalse(attendance.attendance_reason_ids) + def test_todays_working_times_user_without_employee(self): + user = self.env["res.users"].create( + { + "name": "Test User", + "login": "test_user", + "email": "test2@user.com", + "tz": "UTC", + } + ) + with self.assertRaises(UserError): + self.employee.todays_working_times([("id", "=", user.id)]) + def test_todays_working_times(self): self.maxDiff = None with freeze_time("2021-12-13 05:01", tz_offset=0):