public async Task <ServiceResponse <object> > GetParentChildAttendance() { var Parent = _context.Users.Where(m => m.Id == _LoggedIn_UserID).FirstOrDefault(); var Students = await(from u in _context.Users join csU in _context.ClassSectionUsers on u.Id equals csU.UserId join cs in _context.ClassSections on csU.ClassSectionId equals cs.Id where u.ParentContactNumber == Parent.ParentContactNumber && u.ParentEmail == Parent.ParentEmail && u.Role == Enumm.UserType.Student.ToString() select new ParentChildAttendanceForListDto { Id = u.Id, FullName = u.FullName, TeacherName = _context.ClassSectionUsers.FirstOrDefault(m => m.ClassSectionId == cs.Id && m.UserTypeId == (int)Enumm.UserType.Teacher).User.FullName, ClassSectionId = cs.Id, ClassSection = cs.Class.Name + " " + cs.Section.SectionName, Photos = _context.Photos.Where(m => m.UserId == u.Id && m.IsPrimary == true).Select(x => new PhotoDto { Id = x.Id, Name = x.Name, Url = _File.AppendImagePath(x.Name) }).ToList(), }).ToListAsync(); foreach (var item in Students) { string[] Months = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; foreach (var month in Months) { var StartDateByMonth = new DateTime(DateTime.UtcNow.Year, Array.IndexOf(Months, month) + 1, 1); var LastDateByMonth = StartDateByMonth.AddMonths(1).AddDays(-1); var DaysCountByMonth = GenericFunctions.BusinessDaysUntil(StartDateByMonth, LastDateByMonth); var UserPresentCountByMonth = (from u in _context.Users join att in _context.Attendances on u.Id equals att.UserId where u.Id == item.Id && att.Present == true && att.CreatedDatetime.Date >= StartDateByMonth.Date && att.CreatedDatetime.Date <= LastDateByMonth.Date select att).ToList().Count(); item.AttendancePercentage.Add(new ThisMonthAttendancePercentageDto { MonthName = month, Month = (Array.IndexOf(Months, month) + 1), Percentage = GenericFunctions.CalculatePercentage(UserPresentCountByMonth, DaysCountByMonth) }); } } _serviceResponse.Data = new { Students, StudentCount = Students.Count(), }; _serviceResponse.Success = true; return(_serviceResponse); }
public async Task <ServiceResponse <object> > GetLoggedUserAttendancePercentage() { var userDetails = _context.Users.Where(m => m.Id == _LoggedIn_UserID).FirstOrDefault(); if (userDetails != null) { var StartDate = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1); var LastDate = DateTime.UtcNow.Date; var DaysCount = GenericFunctions.BusinessDaysUntil(StartDate, LastDate); var UserPresentCount = (from u in _context.Users join att in _context.Attendances on u.Id equals att.UserId where u.UserTypeId == userDetails.UserTypeId && u.Id == _LoggedIn_UserID && att.Present == true && att.CreatedDatetime.Date >= StartDate.Date && att.CreatedDatetime.Date <= LastDate.Date select att).ToList().Count(); var CurrentMonthLoggedUserPercentage = GenericFunctions.CalculatePercentage(UserPresentCount, DaysCount); var LoggedUserAttendanceByMonthPercentage = new List <ThisMonthAttendancePercentageDto>(); string[] Months = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; for (int i = 0; i < Months.Length; i++) { string month = Months[i]; var StartDateByMonth = new DateTime(DateTime.UtcNow.Year, (Array.IndexOf(Months, month) + 1), 1); var LastDateByMonth = StartDateByMonth.AddMonths(1).AddDays(-1); var DaysCountByMonth = GenericFunctions.BusinessDaysUntil(StartDateByMonth, LastDateByMonth); var UserPresentCountByMonth = (from u in _context.Users join att in _context.Attendances on u.Id equals att.UserId where u.UserTypeId == userDetails.UserTypeId && u.Id == _LoggedIn_UserID && att.Present == true && att.CreatedDatetime.Date >= StartDateByMonth.Date && att.CreatedDatetime.Date <= LastDateByMonth.Date select att).ToList().Count(); LoggedUserAttendanceByMonthPercentage.Add(new ThisMonthAttendancePercentageDto { MonthName = month, Month = Array.IndexOf(Months, month) + 1, Percentage = GenericFunctions.CalculatePercentage(UserPresentCountByMonth, DaysCountByMonth) }); } _serviceResponse.Data = new { CurrentMonthLoggedUserPercentage, LoggedUserAttendanceByMonthPercentage }; _serviceResponse.Success = true; } else { _serviceResponse.Success = false; _serviceResponse.Message = CustomMessage.UserNotLoggedIn; } return(_serviceResponse); }
public async Task <ServiceResponse <object> > GetThisMonthAttendanceOfSemesterStudent() { if (_LoggedIn_UserID != 0) { var StartDate = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1); var LastDate = DateTime.Today.Date; var DaysCount = GenericFunctions.BusinessDaysUntil(StartDate, LastDate); var UserPresentCount = (from u in _context.Users join att in _context.Attendances on u.Id equals att.UserId where u.UserTypeId == (int)Enumm.UserType.Student && u.Id == _LoggedIn_UserID && att.Present == true && att.CreatedDatetime.Date >= StartDate.Date && att.CreatedDatetime.Date <= LastDate.Date select att).ToList().Count(); var CurrentMonthLoggedUserPercentage = GenericFunctions.CalculatePercentage(UserPresentCount, DaysCount); var studentSemesters = (from csU in _context.ClassSectionUsers join cs in _context.ClassSections on csU.ClassSectionId equals cs.Id join sem in _context.Semesters on cs.SemesterId equals sem.Id where csU.UserId == _LoggedIn_UserID select new { cs.SemesterId, cs.SemesterObj.Name, sem.StartDate, sem.EndDate }).ToList(); var SemesterDetails = $"{studentSemesters[0].Name} From {studentSemesters[0].StartDate.ToShortDateString()} To {studentSemesters[0].EndDate.ToShortDateString()}"; var studentSubjects = await(from sa in _context.SubjectAssignments where studentSemesters.Select(m => m.SemesterId).Contains(sa.SemesterId) select new { sa.SubjectId, sa.Subject.Name, //l.StartDate, //l.EndDate }).Distinct().ToListAsync(); var LoggedUserAttendanceByMonthPercentage = new List <ThisMonthAttendanceOfSemesterStdDto>(); var Start = studentSemesters[0].StartDate; var End = studentSemesters[0].EndDate; End = new DateTime(End.Year, End.Month, DateTime.DaysInMonth(End.Year, End.Month)); var Months = Enumerable.Range(0, Int32.MaxValue).Select(e => Start.AddMonths(e)).TakeWhile(e => e <= End).Select(e => e.Month).ToArray(); decimal allMonthPercentage = 0; for (int i = 0; i < studentSubjects.Count(); i++) { var item = studentSubjects[i]; for (int j = 0; j < Months.Length; j++) { var month = Months[j]; var StartDateByMonth = new DateTime(DateTime.UtcNow.Year, month, 1); var LastDateByMonth = StartDateByMonth.AddMonths(1).AddDays(-1); var DaysCountByMonth = GenericFunctions.BusinessDaysUntil(StartDateByMonth, LastDateByMonth); var UserPresentCountByMonth = (from u in _context.Users join att in _context.Attendances on u.Id equals att.UserId where u.UserTypeId == (int)Enumm.UserType.Student && att.Id == _LoggedIn_UserID && att.Present == true && att.CreatedDatetime.Date >= StartDateByMonth.Date && att.CreatedDatetime.Date <= LastDateByMonth.Date && att.SubjectId == item.SubjectId select att).ToList().Count(); allMonthPercentage += GenericFunctions.CalculatePercentage(UserPresentCountByMonth, DaysCountByMonth); } LoggedUserAttendanceByMonthPercentage.Add(new ThisMonthAttendanceOfSemesterStdDto { SubjectName = item.Name, Percentage = allMonthPercentage / Months.Length, }); } _serviceResponse.Data = new { CurrentMonthLoggedUserPercentage, LoggedUserAttendanceByMonthPercentage, SemesterDetails }; _serviceResponse.Success = true; } else { _serviceResponse.Success = false; _serviceResponse.Message = CustomMessage.UserNotLoggedIn; } return(_serviceResponse); }