Modyfied the attendance report API
This commit is contained in:
parent
8ddb414e91
commit
eb3a65428e
@ -449,7 +449,7 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("report-attendance")]
|
[HttpGet("report-attendance")]
|
||||||
public async Task<IActionResult> GetAttendanceReportAsync()
|
public async Task<IActionResult> GetAttendanceReportAsync([FromQuery] bool isCurrentMonth = false)
|
||||||
{
|
{
|
||||||
Guid tenantId = _userHelper.GetTenantId();
|
Guid tenantId = _userHelper.GetTenantId();
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
using var scope = _serviceScopeFactory.CreateScope();
|
||||||
@ -458,48 +458,75 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
DateTime firstDayOfMonth = new DateTime(today.Year, today.Month, 1);
|
DateTime firstDayOfMonth = new DateTime(today.Year, today.Month, 1);
|
||||||
DateTime firstDayOfNextMonth = firstDayOfMonth.AddMonths(1);
|
DateTime firstDayOfNextMonth = firstDayOfMonth.AddMonths(1);
|
||||||
|
|
||||||
|
if (!isCurrentMonth)
|
||||||
|
{
|
||||||
|
firstDayOfNextMonth = firstDayOfMonth;
|
||||||
|
firstDayOfMonth = firstDayOfMonth.AddMonths(-1);
|
||||||
|
}
|
||||||
|
|
||||||
// Generate list of all dates in the month
|
// Generate list of all dates in the month
|
||||||
var allDates = Enumerable.Range(0, (firstDayOfNextMonth - firstDayOfMonth).Days)
|
var allDates = Enumerable.Range(0, (firstDayOfNextMonth - firstDayOfMonth).Days)
|
||||||
.Select(offset => firstDayOfMonth.AddDays(offset))
|
.Select(offset => firstDayOfMonth.AddDays(offset))
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
var attendances = await _context.Attendes
|
var attendancesTask = Task.Run(async () =>
|
||||||
.Include(a => a.Employee)
|
{
|
||||||
.Where(a => a.AttendanceDate >= firstDayOfMonth && a.AttendanceDate < firstDayOfNextMonth && a.Employee != null && a.TenantId == tenantId)
|
await using var context = await _dbContextFactory.CreateDbContextAsync();
|
||||||
.GroupBy(a => a.ProjectID)
|
return await context.Attendes
|
||||||
.ToListAsync();
|
.Where(a => a.AttendanceDate >= firstDayOfMonth && a.AttendanceDate < firstDayOfNextMonth && a.Employee != null && a.TenantId == tenantId)
|
||||||
|
.GroupBy(a => a.ProjectID)
|
||||||
|
.ToListAsync();
|
||||||
|
});
|
||||||
|
|
||||||
|
var projectAllocationTask = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
await using var context = await _dbContextFactory.CreateDbContextAsync();
|
||||||
|
return await context.ProjectAllocations
|
||||||
|
.Include(pa => pa.Employee)
|
||||||
|
.Where(pa => pa.TenantId == tenantId && pa.IsActive)
|
||||||
|
.ToListAsync();
|
||||||
|
});
|
||||||
|
|
||||||
|
await Task.WhenAll(attendancesTask, projectAllocationTask);
|
||||||
|
|
||||||
|
var attendances = attendancesTask.Result;
|
||||||
|
var projectAllocations = projectAllocationTask.Result;
|
||||||
|
|
||||||
var result = attendances.Select(g =>
|
var result = attendances.Select(g =>
|
||||||
{
|
{
|
||||||
|
var projectAllocation = projectAllocations.Where(pa => pa.ProjectId == g.Key && pa.Employee != null).ToList();
|
||||||
|
var projectAttendance = projectAllocation.Select(pa =>
|
||||||
|
{
|
||||||
|
var attendances = g.Where(a => a.EmployeeId == pa.EmployeeId).ToList();
|
||||||
|
var attendanceDate = attendances.Select(a => a.AttendanceDate.Date).ToList();
|
||||||
|
return new
|
||||||
|
{
|
||||||
|
FirstName = pa.Employee!.FirstName,
|
||||||
|
LastName = pa.Employee.LastName,
|
||||||
|
Attendances = allDates.Select(d =>
|
||||||
|
{
|
||||||
|
var attendance = attendances.FirstOrDefault(a => a.AttendanceDate.Date == d);
|
||||||
|
return new
|
||||||
|
{
|
||||||
|
AttendanceDate = d,
|
||||||
|
CheckIn = attendance?.InTime,
|
||||||
|
CheckOut = attendance?.OutTime,
|
||||||
|
Activity = attendance?.Activity,
|
||||||
|
IsApproved = attendance?.ApprovedById.HasValue,
|
||||||
|
};
|
||||||
|
}).ToList(),
|
||||||
|
CheckInCheckOutDone = attendances.Where(a => a.InTime.HasValue && a.OutTime.HasValue && a.Activity == ATTENDANCE_MARK_TYPE.REGULARIZE).Count(),
|
||||||
|
CheckInDone = attendances.Where(a => a.InTime.HasValue).Count(),
|
||||||
|
CheckOutPending = attendances.Where(a => a.InTime.HasValue && !a.OutTime.HasValue).Count(),
|
||||||
|
RejectedRegularize = attendances.Where(a => a.Activity == ATTENDANCE_MARK_TYPE.REGULARIZE_REJECT).Count(),
|
||||||
|
AbsentAttendance = allDates.Where(d => !attendanceDate.Contains(d) && d.DayOfWeek != DayOfWeek.Sunday).Count()
|
||||||
|
};
|
||||||
|
}).OrderBy(ar => ar.FirstName).ThenBy(ar => ar.LastName).ToList();
|
||||||
|
|
||||||
return new
|
return new
|
||||||
{
|
{
|
||||||
ProjectName = _context.Projects.Where(p => p.Id == g.Key && p.TenantId == tenantId).Select(p => p.Name).FirstOrDefault(),
|
ProjectName = _context.Projects.Where(p => p.Id == g.Key && p.TenantId == tenantId).Select(p => p.Name).FirstOrDefault(),
|
||||||
ProjectAttendance = g.GroupBy(a => a.Employee).Select(gp =>
|
ProjectAttendance = projectAttendance
|
||||||
{
|
|
||||||
var attendanceDate = gp.Select(a => a.AttendanceDate.Date).ToList();
|
|
||||||
return new
|
|
||||||
{
|
|
||||||
FirstName = gp.Key!.FirstName,
|
|
||||||
LastName = gp.Key.LastName,
|
|
||||||
Attendances = allDates.Select(d =>
|
|
||||||
{
|
|
||||||
var attendance = gp.FirstOrDefault(a => a.AttendanceDate.Date == d);
|
|
||||||
return new
|
|
||||||
{
|
|
||||||
AttendanceDate = d,
|
|
||||||
CheckIn = attendance?.InTime,
|
|
||||||
CheckOut = attendance?.OutTime,
|
|
||||||
Activity = attendance?.Activity,
|
|
||||||
IsApproved = attendance?.ApprovedById.HasValue,
|
|
||||||
};
|
|
||||||
}).ToList(),
|
|
||||||
CheckInCheckOutDone = gp.Where(a => a.InTime.HasValue && a.OutTime.HasValue && a.Activity == ATTENDANCE_MARK_TYPE.REGULARIZE).Count(),
|
|
||||||
CheckInDone = gp.Where(a => a.InTime.HasValue).Count(),
|
|
||||||
CheckOutPending = gp.Where(a => a.InTime.HasValue && !a.OutTime.HasValue).Count(),
|
|
||||||
RejectedRegularize = gp.Where(a => a.Activity == ATTENDANCE_MARK_TYPE.REGULARIZE_REJECT).Count(),
|
|
||||||
AbsentAttendance = allDates.Where(d => !attendanceDate.Contains(d) && d.DayOfWeek != DayOfWeek.Sunday).Count()
|
|
||||||
};
|
|
||||||
}).OrderBy(ar => ar.FirstName).ThenBy(ar => ar.LastName).ToList()
|
|
||||||
};
|
};
|
||||||
}).ToList();
|
}).ToList();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user