public static CustomTimeTrack GetUserTimeTrackHistoryForSpecifiedPeriod(string userName, DateTime startDate, DateTime endDate)
{
var weekEndDateToSearchInDatabase = endDate.AddDays(1);
var customTimeTrack = new CustomTimeTrack
{
CustomStartDate = startDate.Date,
CustomEndDate = endDate.Date,
UserName=userName,
DailyTimeTracks = new List<DailyTimeTrack>()
};
using (var dbContext = new TimeTrackingEntities())
{
var currentUser = dbContext.ExtendedUserProfiles.FirstOrDefault(c => c.UserName.ToLower().Equals(userName));
customTimeTrack.EmployeeName = currentUser.FirstName + " " + currentUser.LastName;
var userClockInOutTimings = (from utsh in dbContext.UserTimeTrackHistories
where utsh.IsDeleted == false &&
utsh.UserName.ToLower().Equals(userName.ToLower()) &&
(utsh.StampDate >= startDate && utsh.StampDate < weekEndDateToSearchInDatabase) &&
utsh.ClockInTime.Length > 0
select utsh).ToList();
if (userClockInOutTimings.Any())
{
var currentDay = startDate.Date;
while (currentDay <= endDate.Date)
{
var dailyUserStampList =
userClockInOutTimings.Where(
daily =>
daily.StampDate >= currentDay && daily.StampDate < currentDay.AddDays(1)).ToList();
var dailyTimeTrack = new DailyTimeTrack(startDate, currentUser.HourlyRate.HasValue ? currentUser.HourlyRate.Value : 0)
{
StampDate = currentDay,
TimeTrackList =
GetTimeTrackList(dailyUserStampList, currentDay).
OrderByDescending(c => c.ClockInTime).ThenByDescending(
d => d.ClockOutTime).ToList()
};
customTimeTrack.DailyTimeTracks.Add(dailyTimeTrack);
customTimeTrack.DailyTimeTracks = customTimeTrack.DailyTimeTracks.OrderByDescending(c => c.StampDate).ToList();
currentDay = currentDay.AddDays(1);
}
}
}
return customTimeTrack;
}