Esempio n. 1
0
 private void butCombine_Click(object sender, EventArgs e)
 {
     if (!Security.IsAuthorized(Permissions.Setup))
     {
         return;
     }
     if (gridMain.SelectedIndices.Length < 2)
     {
         MsgBox.Show(this, "Please select multiple items first while holding down the control key.");
         return;
     }
     if (!MsgBox.Show(this, MsgBoxButtons.OKCancel,
                      "Combine all selected operatories into a single operatory?\r\n\r\n"
                      + "This will affect all appointments set in these operatories and could take a while to run.  "
                      + "The next window will let you select which operatory to keep when combining."))
     {
         return;
     }
     #region Get selected OperatoryNums
     bool hasNewOp = gridMain.SelectedIndices.ToList().Exists(x => ((Operatory)gridMain.Rows[x].Tag).IsNew);
     if (hasNewOp)
     {
         //This is needed due to the user adding an operatory then clicking combine.
         //The newly added operatory does not hava and OperatoryNum , so sync.
         ReorderAndSync();
         DataValid.SetInvalid(InvalidType.Operatories); //With sync we don't know if anything changed.
         RefreshList();                                 //Without this the user could cancel out of a prompt below and quickly close FormOperatories, causing a duplicate op from sync.
     }
     List <long> listSelectedOpNums = new List <long>();
     for (int i = 0; i < gridMain.SelectedIndices.Length; i++)
     {
         listSelectedOpNums.Add(((Operatory)gridMain.Rows[gridMain.SelectedIndices[i]].Tag).OperatoryNum);
     }
     #endregion
     #region Determine what Op to keep as the 'master'
     FormOperatoryPick FormOP = new FormOperatoryPick(_listOps.FindAll(x => listSelectedOpNums.Contains(x.OperatoryNum)));
     FormOP.ShowDialog();
     if (FormOP.DialogResult != DialogResult.OK)
     {
         return;
     }
     long masterOpNum = FormOP.SelectedOperatoryNum;
     #endregion
     #region Determine if any appts conflict exist and potentially show them
     //List of all appointments for all child ops. If conflict was detected the appointments OperatoryNum will bet set to -1
     List <ODTuple <Appointment, bool> > listTupleApptsToMerge = Operatories.MergeApptCheck(masterOpNum, listSelectedOpNums.FindAll(x => x != masterOpNum));
     ListConflictingAppts = listTupleApptsToMerge.Where(x => x.Item2).Select(x => x.Item1).ToList();
     List <Appointment> listApptsToMerge = listTupleApptsToMerge.Select(x => x.Item1).ToList();
     if (ListConflictingAppts.Count > 0)             //Appointments conflicts exist, can not merge
     {
         if (!MsgBox.Show(this, true, "Cannot merge operatories due to appointment conflicts.\r\n\r\n"
                          + "These conflicts need to be resolved before combining can occur.\r\n"
                          + "Click OK to view the conflicting appointments."))
         {
             ListConflictingAppts.Clear();
             return;
         }
         Close();                //Having ListConflictingAppts filled with appointments will cause outside windows that care to show the corresponding window.
         return;
     }
     #endregion
     #region Final prompt, displays number of appts to move and the 'master' ops abbr.
     int apptCount = listApptsToMerge.FindAll(x => x.Op != masterOpNum).Count;
     if (apptCount > 0)
     {
         string selectedOpName = _listOps.First(x => x.OperatoryNum == masterOpNum).Abbrev;            //Safe
         if (MessageBox.Show(Lan.g(this, "Would you like to move") + " " + apptCount + " "
                             + Lan.g(this, "appointments from their current operatories to") + " " + selectedOpName + "?\r\n\r\n"
                             + Lan.g(this, "You cannot undo this!")
                             , "WARNING"
                             , MessageBoxButtons.OKCancel) != DialogResult.OK)
         {
             return;
         }
     }
     #endregion
     //Point of no return.
     if (!hasNewOp)             //Avoids running ReorderAndSync() twice.
     //The user could have made other changes within this window, we need to make sure to save those changes before merging.
     {
         ReorderAndSync();
         DataValid.SetInvalid(InvalidType.Operatories);                //With sync we don't know if anything changed.
     }
     try {
         Operatories.MergeOperatoriesIntoMaster(masterOpNum, listSelectedOpNums, listApptsToMerge);
     }
     catch (Exception ex) {
         MessageBox.Show(ex.Message);
         return;
     }
     MessageBox.Show(Lan.g("Operatories", "The following operatories and all of their appointments were merged into the")
                     + " " + _listOps.FirstOrDefault(x => x.OperatoryNum == masterOpNum).Abbrev + " " + Lan.g("Operatories", "operatory;") + "\r\n"
                     + string.Join(", ", _listOps.FindAll(x => x.OperatoryNum != masterOpNum && listSelectedOpNums.Contains(x.OperatoryNum)).Select(x => x.Abbrev)));
     RefreshList();
     FillGrid();
 }