private void FixEmployeeTotalWorkHours(DateTime startDate, DateTime endDate, ta_CutOffAttendanceSummary eda, mf_Employee201 _201) { var workDays = this._repoWorkDay .Query() .Filter(x => x.mf_EmployeeWorkDays.Any(ewd => ewd.employeeId == eda.employeeId && !eda.deleted)) .Get() .ToList(); var attendance = this._repoEmployeeAttendance.Query().Filter(x => x.employeeId == eda.employeeId && x.workDate >= startDate && x.workDate <= endDate) .Get() .OrderBy(x => x.workDate) .ToList(); var userId = this.GetCurrentUserId(); DateTime date = startDate.Date; while (date <= endDate.Date) { var wDays = workDays.Where(x => checkDaysOfWeek(date.Date.DayOfWeek, x)); var atts = attendance.Where(x => x.workDate.Date == date).OrderBy(x => x.timeLog); var workHours = 0d; var undertimeHours = 0d; var lateHours = 0d; var overtimeHours = 0d; if (wDays.Any() && atts.Any()) { workHours = wDays .Sum(x => { var hours = new TimeSpan(x.toTimeHour, x.toTimeMinute, 0).TotalHours - new TimeSpan(x.fromTimeHour, x.fromTimeMinute, 0).TotalHours; hours -= x.breakHours; return(hours); }); foreach (var wd in wDays) { var dutyOn = atts.Where(x => (TIME_LOG_TYPE)x.timeLogType == TIME_LOG_TYPE.DutyOn && x.workDayId == wd.id).FirstOrDefault(); var dutyOff = atts.Where(x => (TIME_LOG_TYPE)x.timeLogType == TIME_LOG_TYPE.DutyOff && x.workDayId == wd.id).FirstOrDefault(); if (dutyOn == null) { continue; } if (dutyOff == null) { continue; } var totalHoursOnCurrentDay = (dutyOff.timeLog - dutyOn.timeLog).TotalHours - wd.breakHours; if (dutyOn.timeLog > dutyOn.timeLog.Date.AddHours(wd.fromTimeHour).AddMinutes(wd.fromTimeMinute)) { lateHours += (dutyOn.timeLog - dutyOn.timeLog.Date.AddHours(wd.fromTimeHour).AddMinutes(wd.fromTimeMinute)).TotalHours; } if (dutyOff.timeLog < dutyOff.timeLog.Date.AddHours(wd.toTimeHour).AddMinutes(wd.toTimeMinute)) { undertimeHours += (dutyOff.timeLog.Date.AddHours(wd.toTimeHour).AddMinutes(wd.toTimeMinute) - dutyOff.timeLog).TotalHours; } //workHours -= lateHours; //workHours -= undertimeHours; if (_201.entitledOvertime == true) { overtimeHours = (dutyOff.timeLog - dutyOff.timeLog.Date.AddHours(wd.toTimeHour).AddMinutes(wd.toTimeMinute)).TotalHours; if (overtimeHours < 0) { overtimeHours = 0; } ; } } } bool absent = wDays.Any() && workHours == 0; if (_holidayService.IsHolidayDate(date.Date)) { absent = false; } eda.ta_CutOffAttendanceSummaryDetails.Add(new ta_CutOffAttendanceSummaryDetail() { workHours = workHours, undertimeHours = undertimeHours, lateHours = lateHours, overtimeHours = overtimeHours, workDate = date, absent = absent, updatedBy = userId, ObjectState = ObjectState.Added, }); date = date.AddDays(1); } }