restrict check-in outside allowed working hours

This commit is contained in:
younes 2025-07-13 14:34:57 +01:00
parent 56944f2a85
commit e20d4d3fe2
3 changed files with 34 additions and 2 deletions

View File

@ -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:

View File

@ -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 "عزيزي الموظف دوام العمل لم يبدا بعد"

View File

@ -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: