private void UpdateStaffAttendanceStatus(BiometricLog log, AttendanceDbContext dbContext) { DateTime processDate = log.datetime_record.Date; var staff = dbContext.Staffs.Where(x => x.employee_id == log.ind_reg_iD.ToString()) .FirstOrDefault(); if (staff == null) { logger.Info($"ID: {log.ind_reg_iD} not found."); return; } ///get Biometric Logs according to process date and employee id var signInData = dbContext.BiometricLogs.Where(x => x.ind_reg_iD == log.ind_reg_iD && x.datetime_record > processDate) .GroupBy(x => x.ind_reg_iD) .Select(x => new { RegId = x.Key, SignIn = x.Min(y => y.datetime_record), SignOut = x.Count() > 1 ? x.Max(y => y.datetime_record) : processDate, IsSignInLog = x.Min(y => y.datetime_record) == log.datetime_record, PunchCount = x.Count() }).FirstOrDefault(); if (signInData != null) { logger.Info($"Record Date Time: {log.datetime_record}, Staff Name: {staff.name}, ID: {log.ind_reg_iD}, Sign In: {signInData.SignIn}, Sign Out: {signInData.SignOut}, Is Sign In Log: {signInData.IsSignInLog}"); var attendance = dbContext.StaffAttendences.Where(x => x.staff_id == staff.id && x.date == processDate).FirstOrDefault(); if (attendance != null) { int attTypeId = signInData.SignIn.TimeOfDay <= this.AttendanceConfig.staff_late_attendance_cutoff_time ? StaffPresentTypeId : StaffLatePresentTypeId; if (signInData.IsSignInLog) { attendance.staff_attendance_type_id = attTypeId; attendance.is_active = 1; attendance.created_at = signInData.SignIn; attendance.updated_at = log.datetime_record; } else { attendance.staff_attendance_type_id = attTypeId; //Present=1, Late=3 attendance.is_active = signInData.PunchCount % 2 == 0 ? 0 : 1; attendance.updated_at = log.datetime_record; } } else { dbContext.StaffAttendences.Add(new Data.Models.StaffAttendence { staff_id = staff.id, staff_attendance_type_id = StaffPresentTypeId, date = processDate, is_active = 1, remark = "", created_at = signInData.SignIn, updated_at = log.datetime_record }); } } }
private bool UpdateStudentAttendanceStatus(BiometricLog log, AttendanceDbContext dbContext) { DateTime processDate = log.datetime_record.Date; Student student = dbContext.Students.Where(x => x.admission_no == log.ind_reg_iD) .Include(x => x.StudentSessions) .FirstOrDefault(); if (student == null) { return(false); } int sessionId = student.StudentSessions.Last().id; var signInData = dbContext.BiometricLogs.Where(x => x.ind_reg_iD == log.ind_reg_iD && x.datetime_record > processDate) .GroupBy(x => x.ind_reg_iD) .Select(x => new { RegId = x.Key, SignIn = x.Min(y => y.datetime_record), SignOut = x.Count() > 1 ? x.Max(y => y.datetime_record) : processDate, IsSignInLog = x.Min(y => y.datetime_record) == log.datetime_record, PunchCount = x.Count() }).FirstOrDefault(); if (signInData != null) { logger.Info($"Record Date Time: {log.datetime_record}, Student Name: {student.firstname} {student.lastname}, ID: {log.ind_reg_iD}, Sign In: {signInData.SignIn}, Sign Out: {signInData.SignOut}, Is Sign In Log: {signInData.IsSignInLog}"); var attendance = dbContext.StudentAttendences.Where(x => x.student_session_id == sessionId && x.date == processDate).FirstOrDefault(); int attTypeId = signInData.SignIn.TimeOfDay <= this.AttendanceConfig.shift1_late_attendance_cutoff_time ? StudentPresentTypeId : StudentLatePresentTypeId; if (attendance != null) { if (signInData.IsSignInLog) { attendance.attendence_type_id = attTypeId; //Present=1, Late=3 attendance.is_active = "yes"; attendance.created_at = signInData.SignIn; attendance.updated_at = log.datetime_record; } else { attendance.attendence_type_id = attTypeId; //Present=1, Late=3 attendance.is_active = signInData.PunchCount % 2 == 0 ? "no" : "yes"; attendance.updated_at = log.datetime_record; } } else { //Add new record attendance = dbContext.StudentAttendences.Add(new Data.Models.StudentAttendence { student_session_id = sessionId, attendence_type_id = StudentPresentTypeId, date = processDate, is_active = "yes", remark = "", created_at = log.datetime_record, updated_at = log.datetime_record }); } if (signInData.IsSignInLog && processDate == DateTime.Now.Date) { if (this.SendSms(attTypeId == StudentPresentTypeId ? SmsType.Present : SmsType.Late, student, log.datetime_record)) { attendance.remark = "SMS sent."; } else { attendance.remark = "SMS fail to send!"; } } } return(true); }