I have a "Timesheet" application I am building for my company.
The way I have the sql tables setup, is each day has a unique record
for a specific user, then there is a TimeEntry table, that has multiple
TimeEntries for one day.
The TimeEntries for a specific day are like so:
7:00AM - 11:00AM : Regular Hours
11:00AM - 12:00PM : Lunch
12:00PM - 5:00PM : Regular Hours
now, when someone edits the lunch time entry and changes it to
11:30AM-12:00PM, I have some code that goes through the other
timeentries for that day and changes their startTime and EndTime to
coincide with the change to the "Lunch" entry, therefore there are no
gaps between time entries. this has turned into a little bit of a
nightmare because I currently have 7 if-elseif statements that does the
logical checking if a timeentry overlaps, and if the others need to be
changed. could there be an easier way to complete this logic without
having a number of if-elseif statements...here is my method that takes
care of changing other time entries:
/// <summary>
/// This checks overlap of times of all other TimeEntries for this
day in the database
/// if a TimeEntry overlaps ts/te, the TimeEntry is changed and saved
back to the database
/// </summary>
/// <param name="ts">TimeStart of the new TimeEntry</param>
/// <param name="te">TimeEnd of the new TimeEntry</param>
private void CheckOverlap(DateTime ts, DateTime te) {
DataSet dsTimeEntryByDay = this.GetTimeEntryByDay(DayID);
TimeSpan workingTime = te - ts;
int changed = 0;
foreach (DataRow dr in dsTimeEntryByDay.Tables["TimeEntry"].Rows) {
if (TimeEntryID != (int)dr["TimeEntryID"]) {
DateTime newTS = new DateTime();
DateTime newTE = new DateTime();
DateTime cTS = Convert.ToDateTime(dr["TimeStart"]);
DateTime cTE = Convert.ToDateTime(dr["TimeEnd"]);
changed = 0;
if (cTS < ts && cTE > ts) {
// If time is before changed time, and cTE overlaps ts
newTE = ts;
newTS = cTS;
changed = 1;
}
else if (cTE > te && cTS < te) {
// If time is after changed time, and cTS overlaps te
newTS = te;
newTE = cTE;
changed = 1;
}
else if (cTS < ts && cTE < ts) {
newTS = cTS;
newTE = ts;
changed = 1;
}
// Problem here, if you change the first entry, it changes the correct
one, and all subsequent entries
// because they all fit the criteria
else if (cTE > te && cTS > te && cTS < newTE) {
newTS = te;
newTE = cTE;
changed = 1;
}
if (changed == 1) {
this.UpdateTimeEntry((int)dr["TimeEntryID"], (int)dr["DayID"],
newTS, newTE, (int)dr["EntryTypeID"]);
}
}
}
}