From e20d4d3fe28c944d46b1b354eae66845203a040e Mon Sep 17 00:00:00 2001 From: younes Date: Sun, 13 Jul 2025 14:34:57 +0100 Subject: [PATCH] restrict check-in outside allowed working hours --- .../controllers/rest_api_v2/attendance.py | 13 ++++++++++++- odex25_mobile/odex_mobile/i18n/ar_001.po | 6 +++++- odex25_mobile/odex_mobile/validator.py | 17 +++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/odex25_mobile/odex_mobile/controllers/rest_api_v2/attendance.py b/odex25_mobile/odex_mobile/controllers/rest_api_v2/attendance.py index 5f9b8a45a..f4bac7e9c 100644 --- a/odex25_mobile/odex_mobile/controllers/rest_api_v2/attendance.py +++ b/odex25_mobile/odex_mobile/controllers/rest_api_v2/attendance.py @@ -15,7 +15,7 @@ import logging _logger = logging.getLogger(__name__) from odoo.tools.translate import _ import re - +import pytz from odoo import fields SENSITIVE_FIELDS = ['password', 'password_crypt', 'new_password', 'create_uid', 'write_uid'] @@ -189,6 +189,17 @@ class AttendanceController(http.Controller): employee = http.request.env['hr.employee'].sudo().search([('user_id', '=', user.id)], limit=1) if not employee: return http_helper.response(code=400, message=_("You are not allowed to perform this operation. please check with one of your team admins"), success=False) + if body.get('action') and body.get('action') == 'sign_in': + timezone = user.tz or 'GMT' + local_tz = pytz.timezone(timezone) + now_gmt = datetime.now(local_tz) + current_time_float = now_gmt.hour + now_gmt.minute / 60.0 + calendar = employee.resource_calendar_id + before_work = getattr(calendar, 'grace_hour_before_work', 8.0) + after_work = getattr(calendar, 'grace_hour_after_work', 16.0) + if before_work and after_work: + if current_time_float < before_work or current_time_float > after_work: + return http_helper.response(code=400, message=_("Dear employee, your working hours have not started yet."), success=False) if employee.device_id != body.get('device_id'): return http_helper.errcode(code=403, message=_("Device id not matching with already exist in system please contact system admin")) try: diff --git a/odex25_mobile/odex_mobile/i18n/ar_001.po b/odex25_mobile/odex_mobile/i18n/ar_001.po index dccc4ef48..4886c3df7 100644 --- a/odex25_mobile/odex_mobile/i18n/ar_001.po +++ b/odex25_mobile/odex_mobile/i18n/ar_001.po @@ -2369,4 +2369,8 @@ msgstr "الساعات الإضافية" msgid "Attendance hours" msgstr "ساعات الحضور" - +#. module: odex_mobile +#: code:addons/odex_mobile/controllers/rest_api_v2/attendance.py:0 +#, python-format +msgid "Dear employee, your working hours have not started yet." +msgstr "عزيزي الموظف دوام العمل لم يبدا بعد" diff --git a/odex25_mobile/odex_mobile/validator.py b/odex25_mobile/odex_mobile/validator.py index 8477ef252..7c4c65e9a 100644 --- a/odex25_mobile/odex_mobile/validator.py +++ b/odex25_mobile/odex_mobile/validator.py @@ -1,6 +1,7 @@ import logging import jwt import re +import pytz import datetime import traceback from odoo import http, service, registry, SUPERUSER_ID,_ @@ -38,6 +39,22 @@ class Validator: return page def get_attendance_check(self,employee): + if not employee: + return 'sign_out' + user = employee.user_id + timezone = user.tz or 'GMT' + local_tz = pytz.timezone(timezone) + now_gmt = datetime.datetime.now(local_tz) + current_time_float = now_gmt.hour + now_gmt.minute / 60.0 + + calendar = employee.resource_calendar_id + before_work = getattr(calendar, 'grace_hour_before_work', 8.0) + after_work = getattr(calendar, 'grace_hour_after_work', 16.0) + + if before_work and after_work: + if current_time_float < before_work or current_time_float > after_work: + return 'sign_out' + last = http.request.env['attendance.attendance'].sudo().search([('employee_id', '=', employee.id), ], order='name desc', limit=1) if last: