private void butOK_Click(object sender, System.EventArgs e) { #region Validation DateTime startDateT = DateTime.MinValue; DateTime stopDateT = DateTime.MinValue; if (!_isHolidayOrNote) { if (listOps.SelectedIndices.Count == 0) { MsgBox.Show(this, "Please select ops first."); return; } if (listOps.SelectedIndices.Count > 1 && listOps.SelectedIndices.Contains(0)) { MsgBox.Show(this, "Invalid selection of ops."); return; } startDateT = PIn.DateT(comboStart.Text); stopDateT = PIn.DateT(comboStop.Text); if (startDateT == DateTime.MinValue || stopDateT == DateTime.MinValue) { MsgBox.Show(this, "Incorrect time format"); return; } if (startDateT > stopDateT) { MsgBox.Show(this, "Stop time must be later than start time."); return; } List <long> listSelectedOps = new List <long>(); List <Schedule> listProvSchedsOnly = ListScheds.FindAll(x => x.SchedType == ScheduleType.Provider); if (!listOps.SelectedIndices.Contains(0)) //add selected operatories into listSelectedOps { listSelectedOps = listOps.SelectedIndices.OfType <int>().Select(x => _listOps[x - 1].OperatoryNum).ToList(); } SchedCur.Ops = listSelectedOps.ToList(); //deep copy of list. (because it is value type.) SchedCur.StartTime = startDateT.TimeOfDay; SchedCur.StopTime = stopDateT.TimeOfDay; List <long> listProvsOverlap; //====================Pre-Emptive Overlaps==================== //Because this window is explicitly designed for one start and one stop time we know that there will be overlapping if multiple providers are //selected with at least one specific operatory selected. if (ListProvNums.Count > 1 && listSelectedOps.Count > 0) { listProvsOverlap = ListProvNums.Distinct().ToList(); } else //Go see if there is going to be overlapping issues with this new schedule. { listProvsOverlap = Schedules.GetOverlappingSchedProvNums(ListProvNums, SchedCur, listProvSchedsOnly, listSelectedOps); } if (listProvsOverlap.Count > 0 && MessageBox.Show(Lan.g(this, "Overlapping provider schedules detected, would you like to continue anyway?") + "\r\n" + Lan.g(this, "Providers affected") + ":\r\n " + string.Join("\r\n ", listProvsOverlap.Select(x => Providers.GetLongDesc(x))), "", MessageBoxButtons.YesNo) != DialogResult.Yes) { return; } } else if (SchedCur.Status != SchedStatus.Holiday && textNote.Text == "") //don't allow blank schedule notes { MsgBox.Show(this, "Please enter a note first."); return; } long clinicNum = 0; if (_isHolidayOrNote && SchedCur.SchedType == ScheduleType.Practice && PrefC.HasClinicsEnabled) //prov notes do not have a clinic { int indexCur = comboClinic.SelectedIndex; if (!Security.CurUser.ClinicIsRestricted) //user isn't restricted, -1 for HQ { indexCur--; } if (indexCur > -1) //will be -1 if HQ is selected, leave clinicNum=0 { clinicNum = _listClinics[indexCur].ClinicNum; } if (SchedCur.Status == SchedStatus.Holiday) //duplicate holiday check { List <Schedule> listScheds = ListScheds.FindAll(x => x.SchedType == ScheduleType.Practice && x.Status == SchedStatus.Holiday); //scheds in local list listScheds.AddRange(Schedules.GetAllForDateAndType(SchedCur.SchedDate, ScheduleType.Practice) .FindAll(x => x.ScheduleNum != SchedCur.ScheduleNum && x.Status == SchedStatus.Holiday && listScheds.All(y => y.ScheduleNum != x.ScheduleNum))); //add any in db that aren't in local list if (listScheds.Any(x => x.ClinicNum == 0 || x.ClinicNum == clinicNum) || //already a holiday for HQ in db or duplicate holiday for a clinic (clinicNum == 0 && listScheds.Count > 0)) //OR trying to create a HQ holiday when a clinic already has one for this day { MsgBox.Show(this, "There is already a Holiday for the practice or clinic on this date."); return; } } } #endregion Validation #region Set Schedule Fields SchedCur.Note = textNote.Text; SchedCur.Ops = new List <long>(); if (listOps.SelectedIndices.Count > 0 && !listOps.SelectedIndices.Contains(0)) { listOps.SelectedIndices.OfType <int>().ToList().ForEach(x => SchedCur.Ops.Add(_listOps[x - 1].OperatoryNum)); } SchedCur.ClinicNum = clinicNum; //0 if HQ selected or clinics not enabled or not a holiday or practice note #endregion Set Schedule Fields DialogResult = DialogResult.OK; }
private void butOK_Click(object sender, System.EventArgs e) { #region Validation DateTime startDateT = DateTime.MinValue; DateTime stopDateT = DateTime.MinValue; if (!_isHolidayOrNote) { if (listOps.SelectedIndices.Count == 0) { MsgBox.Show(this, "Please select ops first."); return; } if (listOps.SelectedIndices.Count > 1 && listOps.SelectedIndices.Contains(0)) { MsgBox.Show(this, "Invalid selection of ops."); return; } startDateT = PIn.DateT(comboStart.Text); stopDateT = PIn.DateT(comboStop.Text); if (startDateT == DateTime.MinValue || stopDateT == DateTime.MinValue) { MsgBox.Show(this, "Incorrect time format"); return; } if (startDateT > stopDateT) { MsgBox.Show(this, "Stop time must be later than start time."); return; } List <long> listSelectedOps = new List <long>(); List <Schedule> listProvSchedsOnly = ListScheds.FindAll(x => x.SchedType == ScheduleType.Provider); if (!listOps.SelectedIndices.Contains(0)) //add selected operatories into listSelectedOps { listSelectedOps = listOps.SelectedIndices.OfType <int>().Select(x => _listOps[x - 1].OperatoryNum).ToList(); } SchedCur.Ops = listSelectedOps.ToList(); //deep copy of list. (because it is value type.) SchedCur.StartTime = startDateT.TimeOfDay; SchedCur.StopTime = stopDateT.TimeOfDay; List <long> listProvsOverlap = new List <long>(); foreach (long provNum in ListProvNums) //Potentially check each provider for overlap. { Schedule schedTemp = SchedCur.Copy(); schedTemp.ProvNum = provNum; if (schedTemp.IsNew) { listProvSchedsOnly.Add(schedTemp); //new scheds will be added to check if overlap. } bool isOverlapDetected = false; //====================Pre-Emptive Overlaps==================== isOverlapDetected = ListProvNums.Count > 1 && listSelectedOps.Count > 0; //====================SIMPLE OVERLAP, No-Ops==================== if (!isOverlapDetected) { isOverlapDetected = schedTemp.Ops.Count == 0 && //only look at schedules without operatories listProvSchedsOnly.FindAll(x => x.ProvNum == schedTemp.ProvNum && //Only consider current provider for overlaps w/o Ops x.Ops.Count == 0 && //Also doesn't have an operatory schedTemp.StartTime <= x.StopTime && //Overlapping Time schedTemp.StopTime >= x.StartTime) //Overlapping Time .Count > 1; //count scheds that overlap that also do not have operatories. } //====================COMPLEX OVERLAP, Ops and All==================== if (!isOverlapDetected) //If we did not find a simple overlap, attemptto find a "complicated" overlap { isOverlapDetected = schedTemp.Ops.Count > 0 && listProvSchedsOnly //Select into groups of overlapping comparable schedule objects. .FindAll(x => x.Ops.Count > 0) //can only overlap if Ops are involved .FindAll(x => schedTemp.StartTime <= x.StopTime && schedTemp.StopTime >= x.StartTime) //Find all overlaps .SelectMany(x => x.Ops.Intersect(listSelectedOps)) //can/must contain duplicates. Intersect ignores Ops not used by SchedCur .GroupBy(x => x) //group each overlapping list into operatories and flatten into large collection .Any(x => x.Count() > 1); //count any group that has more tha one schedule for the same time and operatory } if (isOverlapDetected) { listProvsOverlap.Add(provNum); } } //End foraech ProvNum listProvsOverlap = listProvsOverlap.Distinct().ToList(); if (listProvsOverlap.Count > 0 && MessageBox.Show(Lan.g(this, "Overlapping provider schedules detected, would you like to continue anyway?") + "\r\n" + Lan.g(this, "Providers affected") + ":\r\n " + string.Join("\r\n ", listProvsOverlap.Select(x => Providers.GetLongDesc(x))), "", MessageBoxButtons.YesNo) != DialogResult.Yes) { return; } } else if (SchedCur.Status != SchedStatus.Holiday && textNote.Text == "") //don't allow blank schedule notes { MsgBox.Show(this, "Please enter a note first."); return; } long clinicNum = 0; if (_isHolidayOrNote && SchedCur.SchedType == ScheduleType.Practice && PrefC.HasClinicsEnabled) //prov notes do not have a clinic { int indexCur = comboClinic.SelectedIndex; if (!Security.CurUser.ClinicIsRestricted) //user isn't restricted, -1 for HQ { indexCur--; } if (indexCur > -1) //will be -1 if HQ is selected, leave clinicNum=0 { clinicNum = _listClinics[indexCur].ClinicNum; } if (SchedCur.Status == SchedStatus.Holiday) //duplicate holiday check { List <Schedule> listScheds = ListScheds.FindAll(x => x.SchedType == ScheduleType.Practice && x.Status == SchedStatus.Holiday); //scheds in local list listScheds.AddRange(Schedules.GetAllForDateAndType(SchedCur.SchedDate, ScheduleType.Practice) .FindAll(x => x.ScheduleNum != SchedCur.ScheduleNum && x.Status == SchedStatus.Holiday && listScheds.All(y => y.ScheduleNum != x.ScheduleNum))); //add any in db that aren't in local list if (listScheds.Any(x => x.ClinicNum == 0 || x.ClinicNum == clinicNum) || //already a holiday for HQ in db or duplicate holiday for a clinic (clinicNum == 0 && listScheds.Count > 0)) //OR trying to create a HQ holiday when a clinic already has one for this day { MsgBox.Show(this, "There is already a Holiday for the practice or clinic on this date."); return; } } } #endregion Validation #region Set Schedule Fields SchedCur.Note = textNote.Text; SchedCur.Ops = new List <long>(); if (listOps.SelectedIndices.Count > 0 && !listOps.SelectedIndices.Contains(0)) { listOps.SelectedIndices.OfType <int>().ToList().ForEach(x => SchedCur.Ops.Add(_listOps[x - 1].OperatoryNum)); } SchedCur.ClinicNum = clinicNum; //0 if HQ selected or clinics not enabled or not a holiday or practice note #endregion Set Schedule Fields DialogResult = DialogResult.OK; }