This commit is contained in:
Bakry 2025-01-09 15:23:20 +03:00
parent bdf64ee960
commit 492d2a1796
1 changed files with 60 additions and 50 deletions

View File

@ -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)
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',
}
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':
# 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 attendance:
time_object = fields.Datetime.from_string(attendance[-1].name)
if last_attendance:
time_object = fields.Datetime.from_string(last_attendance.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
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
# 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
if not attendance:
else:
is_attendance = True
# 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
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
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_date
return attendance_response