From 492d2a1796d2e81cbeda1f500e2f34f3f33110a2 Mon Sep 17 00:00:00 2001 From: Bakry Date: Thu, 9 Jan 2025 15:23:20 +0300 Subject: [PATCH] fix --- .../system_dashboard_classic/models/models.py | 110 ++++++++++-------- 1 file changed, 60 insertions(+), 50 deletions(-) diff --git a/odex25_base/system_dashboard_classic/models/models.py b/odex25_base/system_dashboard_classic/models/models.py index e5248e877..e5f2c8031 100644 --- a/odex25_base/system_dashboard_classic/models/models.py +++ b/odex25_base/system_dashboard_classic/models/models.py @@ -401,57 +401,67 @@ class SystemDashboard(models.Model): @api.model def checkin_checkout(self): ctx = self._context + attendance_status = ctx.get('check', False) t_date = date.today() user = self.env.user - user_id = self.env['res.users'].sudo().search_read( - [('id', '=', user.id)], limit=1) - - employee_object = self.env['hr.employee'].sudo().search( - [('user_id', '=', user.id)], limit=1) - now = fields.Datetime.now - attendance = self.env['attendance.attendance'] - vals_in = {'employee_id': employee_object.id, - 'action': 'sign_in', - 'action_type': 'manual', - } - vals_out = {'employee_id': employee_object.id, - 'action': 'sign_out', - 'action_type': 'manual', - } - - if ctx.get('check', False): - result = attendance.create(vals_out) - # check whether last action sign in or out and its date - attendance = self.env['attendance.attendance'].sudo().search( - [('employee_id', '=', employee_object.id), ('action_date', '=', t_date)]) - is_attendance = True - if not attendance: - is_attendance = False - if attendance and attendance[-1].action == 'sign_out': - is_attendance = False - user_tz = pytz.timezone(self.env.context.get('tz', 'Asia/Riyadh') or self.env.user.tz) - if attendance: - time_object = fields.Datetime.from_string(attendance[-1].name) - time_in_timezone = pytz.utc.localize(time_object).astimezone(user_tz) - attendance_date = {'is_attendance': is_attendance, - 'time': time_object if attendance else False - } - return attendance_date + employee_object = self.env['hr.employee'].sudo().search([('user_id', '=', user.id)], limit=1) + + vals_in = {'employee_id': employee_object.id,'action': 'sign_in','action_type': 'manual'} + vals_out = {'employee_id': employee_object.id,'action': 'sign_out','action_type': 'manual'} + + # check last attendance record before do any action + Attendance = self.env['attendance.attendance'] + is_attendance = False + + # get last attendance record + last_attendance = self.env['attendance.attendance'].sudo().search( + [('employee_id', '=', employee_object.id), ('action_date', '=', t_date)], limit=1, order="id desc") + user_tz = pytz.timezone(self.env.context.get('tz', 'Asia/Riyadh') or self.env.user.tz) + if last_attendance: + time_object = fields.Datetime.from_string(last_attendance.name) + time_in_timezone = pytz.utc.localize(time_object).astimezone(user_tz) + + # check current attendance status + # (True == user already signed in --> create new record with signout action) + # (Fasle == user already signed out --> create new record with signin action) + if attendance_status: + is_attendance = False + # check last attendance record action + if last_attendance: + if last_attendance.action == 'sign_in': + result = Attendance.create(vals_out) # create signout record + last_attendance = self.env['attendance.attendance'].sudo().search( + [('employee_id', '=', employee_object.id), ('action_date', '=', t_date)], limit=1, order="id desc") + time_object = fields.Datetime.from_string(last_attendance.name) + time_in_timezone = pytz.utc.localize(time_object).astimezone(user_tz) + elif last_attendance.action == 'sign_out': + is_attendance = False + else: + is_attendance = True else: - - result = attendance.create(vals_in) - # check whether last action sign in or out and its date - attendance = self.env['attendance.attendance'].sudo().search( - [('employee_id', '=', employee_object.id), ('action_date', '=', t_date)]) - user_tz = pytz.timezone(self.env.context.get('tz', 'Asia/Riyadh') or self.env.user.tz) #add - time_object = fields.Datetime.from_string(attendance[-1].name) #add - time_in_timezone = pytz.utc.localize(time_object).astimezone(user_tz) #add is_attendance = True - if not attendance: - is_attendance = False - if attendance and attendance[-1].action == 'sign_out': - is_attendance = False - attendance_date = {'is_attendance': is_attendance, - 'time': time_in_timezone if attendance else False - } - return attendance_date + # check last attendance record action + if last_attendance: + if last_attendance.action == 'sign_out': + result = Attendance.create(vals_in) # create signin record + last_attendance = self.env['attendance.attendance'].sudo().search( + [('employee_id', '=', employee_object.id), ('action_date', '=', t_date)], limit=1, order="id desc") + time_object = fields.Datetime.from_string(last_attendance.name) + time_in_timezone = pytz.utc.localize(time_object).astimezone(user_tz) + elif last_attendance.action == 'sign_in': + is_attendance = True + else: + is_attendance = False + else: + result = Attendance.create(vals_in) # create signin record + last_attendance = self.env['attendance.attendance'].sudo().search( + [('employee_id', '=', employee_object.id), ('action_date', '=', t_date)], limit=1, order="id desc") + time_object = fields.Datetime.from_string(last_attendance.name) + time_in_timezone = pytz.utc.localize(time_object).astimezone(user_tz) + + attendance_response = { + 'is_attendance': is_attendance, + 'time': time_in_timezone if last_attendance else False + } + + return attendance_response