static List <ShiftByDay> ConsolidateShifts(List <ShiftByDay> left, List <ShiftByDay> right) { List <ShiftByDay> finalList = new List <ShiftByDay>(); // Create the union of both left and right sets. foreach (ShiftByDay day in left) { finalList.Add(day); // Start with the left list. } foreach (ShiftByDay day in right) { ShiftByDay existingDay = finalList.Where(m => m.date == day.date).FirstOrDefault(); if (existingDay == null) // No merge required - just add the right entry. { finalList.Add(day); continue; } // Okay this will fail if the same check-in and // check-out times show up again as duplicate records. // I.e. I assume the clock times are unique per employee. existingDay.total += day.total; existingDay.labour_by_time_period.period1 += day.labour_by_time_period.period1; existingDay.labour_by_time_period.period2 += day.labour_by_time_period.period2; existingDay.labour_by_time_period.period3 += day.labour_by_time_period.period3; existingDay.labour_by_time_period.period4 += day.labour_by_time_period.period4; } return(finalList.OrderBy(m => m.date).ToList()); }
static List <ShiftByDay> GetShiftsByDay(DateTime clockIn, DateTime clockOut) { List <ShiftByDay> outputList = new List <ShiftByDay>(); // We need to count from the day before because Period 4 can go until 5 AM. DateTime currentTime = new DateTime(clockIn.Year, clockIn.Month, clockIn.Day, 5, 0, 0).AddDays(-1); while (DateTime.Compare(currentTime, clockOut) < 0) // currentDate < end { ShiftByDay shift = new ShiftByDay(); shift.date = currentTime.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); // Time to calculate the periods. DayPeriods day = new DayPeriods(); day.period1 = CalculateNextPeriod(ref currentTime, clockIn, clockOut, 7); // 5 am + 7 = 12 pm day.period2 = CalculateNextPeriod(ref currentTime, clockIn, clockOut, 6); // 12 pm + 6 = 6 pm day.period3 = CalculateNextPeriod(ref currentTime, clockIn, clockOut, 5); // 6 pm + 5 = 11 pm day.period4 = CalculateNextPeriod(ref currentTime, clockIn, clockOut, 6); // 11 pm + 6 = 5 am // If this day has any hours, then add to our result set. decimal total = day.period1 + day.period2 + day.period3 + day.period4; if (total > 0) { shift.labour_by_time_period = day; shift.total = total; outputList.Add(shift); } } return(outputList); }