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 f4bac7e9c..28581af93 100644 --- a/odex25_mobile/odex_mobile/controllers/rest_api_v2/attendance.py +++ b/odex25_mobile/odex_mobile/controllers/rest_api_v2/attendance.py @@ -190,16 +190,25 @@ class AttendanceController(http.Controller): 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' + timezone = user.tz or 'UTC' 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 before_work > 0.0 and current_time_float < before_work: + return http_helper.response( + code=400, + message=_("Dear employee, your working hours have not started yet."), + success=False + ) + if after_work > 0.0 and current_time_float > after_work: + return http_helper.response( + code=400, + message=_("Dear employee, your working hours have ended."), + 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/models/resource.py b/odex25_mobile/odex_mobile/models/resource.py index 35d081843..b86036b96 100644 --- a/odex25_mobile/odex_mobile/models/resource.py +++ b/odex25_mobile/odex_mobile/models/resource.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- -from odoo import fields, models +from odoo import fields, models, api, _ +from odoo.exceptions import ValidationError class ResourceCalendar(models.Model): @@ -8,3 +9,20 @@ class ResourceCalendar(models.Model): grace_hour_before_work = fields.Float(string="Grace Hours Before Work") grace_hour_after_work = fields.Float(string="Grace Hours After Work") + + @api.constrains('grace_hour_before_work', 'grace_hour_after_work') + def grace_hour_constrains(self): + for rec in self: + if hasattr(rec, 'is_full_day') and hasattr(rec, 'noke'): + if rec.is_full_day and not rec.noke: + if rec.grace_hour_before_work and rec.full_min_sign_in: + if rec.grace_hour_before_work > rec.full_min_sign_in: + raise ValidationError(_( + "Grace hours before work must be less than or equal to minimum sign-in time." + )) + + if rec.grace_hour_after_work and rec.full_max_sign_out: + if rec.grace_hour_after_work < rec.full_max_sign_out: + raise ValidationError(_( + "Grace hours after work must be **greater** than or equal to maximum sign-out time." + )) diff --git a/odex25_mobile/odex_mobile/validator.py b/odex25_mobile/odex_mobile/validator.py index 7c4c65e9a..145566da4 100644 --- a/odex25_mobile/odex_mobile/validator.py +++ b/odex25_mobile/odex_mobile/validator.py @@ -38,25 +38,26 @@ class Validator: else: return page - def get_attendance_check(self,employee): + 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 - + timezone = employee.user_id.tz or 'UTC' + now = datetime.datetime.now(pytz.timezone(timezone)) + current_time = now.hour + now.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' + if calendar: + if calendar.grace_hour_before_work > 0.0: + if current_time < calendar.grace_hour_before_work: + return 'sign_out' - last = http.request.env['attendance.attendance'].sudo().search([('employee_id', '=', employee.id), ], order='name desc', - limit=1) + if calendar.grace_hour_after_work > 0.0: + if current_time > calendar.grace_hour_after_work: + return 'sign_out' + + last = http.request.env['attendance.attendance'].sudo().search([('employee_id', '=', employee.id), ], + order='name desc', + limit=1) if last: return last.action else: