private void butRunAllocatorTool_Click(object sender,EventArgs e) {
			if(!rbutIHaveRead.Checked) {
				PU.MB = Lan.g(this,"You must indicate that you have read the text in the box!");
				return;
			}

			if(MessageBox.Show("Do you want to run the batch allocation?","Please Respond",MessageBoxButtons.YesNo) == DialogResult.Yes) {
				FormWarnToCloseComputers fwc = new FormWarnToCloseComputers();
				if(fwc.ShowDialog() == DialogResult.Yes) {
					Reporting.Allocators.MyAllocator1_ProviderPayment allocator1 = new OpenDental.Reporting.Allocators.MyAllocator1_ProviderPayment();
					SecurityLogs.MakeLogEntry(OpenDentBusiness.Permissions.Setup,0,"Started Batch Allocation For Provider Allocation Tool");
					allocator1.StartBatchAllocation();
					SecurityLogs.MakeLogEntry(OpenDentBusiness.Permissions.Setup,0,"Finished Batch Allocation For Provider Allocation Tool");

					List<string> commands = new List<string>();
					if(!PrefC.ContainsKey(MyAllocator1_ProviderPayment.Pref_AllocatorProvider1_ToolHasRun)) {
						commands.Add("INSERT INTO preference VALUES ('"
							+ MyAllocator1_ProviderPayment.Pref_AllocatorProvider1_ToolHasRun + "','0')");
					}
					if(!PrefC.ContainsKey(MyAllocator1_ProviderPayment.Pref_AllocatorProvider1_Use)) {
						commands.Add("INSERT INTO preference VALUES ('"
							+ MyAllocator1_ProviderPayment.Pref_AllocatorProvider1_Use + "','0')");
					}
					if(commands.Count != 0) {
						Db.NonQOld(commands.ToArray());
						Cache.Refresh(InvalidType.Prefs);
					}
					Prefs.UpdateRaw(MyAllocator1_ProviderPayment.Pref_AllocatorProvider1_ToolHasRun,"1");
					Prefs.UpdateRaw(MyAllocator1_ProviderPayment.Pref_AllocatorProvider1_Use,"1");
				}
			}
			RefreshForm();

		}
        private void StartBatch_DoWork_Handler(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            System.ComponentModel.BackgroundWorker bw = (System.ComponentModel.BackgroundWorker)sender;
            if (bw == null)
            {
                PU.MB = "This method is to be called by a a background worker.\n" + PU.Method;
                return;
            }
            DateTime StartTime = DateTime.Now;

            bw.ReportProgress(0, "Batch Processing Started at: " + String.Format("{0}h:{1}m.{2}", StartTime.Hour, StartTime.Minute, StartTime.Second));
            // Generate list of Guarantors to allocate
            int[] GuarantorsToAllocate = Generate_GuarantorList_ToAllocate();
            if (GuarantorsToAllocate == null || GuarantorsToAllocate.Length == 0)
            {
                MessageBox.Show(Lan.g(this, "No Guarantors to Allocate. Exiting."));
                return;
            }
            int BatchSize = 50;
            int iProgress = 0;

            OpenDental.Reporting.Allocators.MyAllocator1_ProviderPayment allocator = new OpenDental.Reporting.Allocators.MyAllocator1_ProviderPayment();
            for (int i = 0; i < GuarantorsToAllocate.Length + BatchSize; i = i + BatchSize)
            {
                int from = i;
                int to   = (i + BatchSize > GuarantorsToAllocate.Length ? GuarantorsToAllocate.Length : i + BatchSize);
                iProgress = (to * 100) / (GuarantorsToAllocate.Length - 1);
                bw.ReportProgress(iProgress, "Beginning Batch " + from + "-" + to + " of " + GuarantorsToAllocate.Length + " Guarantors.");
                for (int j = from; j < to; j++)
                {
                    string UpdateTempTableCommand = "INSERT INTO " + TABLENAME + "_temp (Guarantor,AllocStatus) VALUES "
                                                    + "( " + GuarantorsToAllocate[j].ToString() + ", " + ((int)ProcessingState.Started_and_Incomplete).ToString() + " ) ";
                    Db.NonQOld(UpdateTempTableCommand);
                    allocator.AllocateWithOutToolCheck(GuarantorsToAllocate[j]);
                    iProgress = (j * 100) / (GuarantorsToAllocate.Length - 1);
                    if (bw.CancellationPending)                                    // Try to cancel between allocations which does a lot of writting
                    {
                        i         = GuarantorsToAllocate.Length;                   // end loop
                        j         = to;
                        iProgress = (j * 100) / (GuarantorsToAllocate.Length - 1); // recalculate progrss.
                        bw.ReportProgress(iProgress, "User Cancelled Requested");
                        e.Cancel = true;
                    }
                    UpdateTempTableCommand = "UPDATE " + TABLENAME + "_temp SET AllocStatus =" + ((int)ProcessingState.Complete).ToString()
                                             + "  WHERE Guarantor = " + GuarantorsToAllocate[j].ToString();
                    Db.NonQOld(UpdateTempTableCommand);
                }
                iProgress = (to * 100) / (GuarantorsToAllocate.Length - 1);
                bw.ReportProgress(iProgress, "Finished Batch " + from + "-" + to + " of " + GuarantorsToAllocate.Length + " Guarantors.");
            }
            DateTime EndTime = DateTime.Now;

            bw.ReportProgress(iProgress, "Batch Processing Ended at: " + String.Format("{0}h:{1}m.{2}", EndTime.Hour, EndTime.Minute, EndTime.Second));
            TimeSpan timeSpan1 = new TimeSpan(EndTime.Ticks - StartTime.Ticks);

            bw.ReportProgress(iProgress, "Batch Processing Total Time is: " + String.Format("{0}h:{1}m.{2}", timeSpan1.Hours, timeSpan1.Minutes, timeSpan1.Seconds));
            e.Result = "Completed Processing of all " + GuarantorsToAllocate.Length + " Guarantors.";
        }
        private void butRunAllocatorTool_Click(object sender, EventArgs e)
        {
            if (!rbutIHaveRead.Checked)
            {
                PU.MB = Lan.g(this, "You must indicate that you have read the text in the box!");
                return;
            }

            if (MessageBox.Show("Do you want to run the batch allocation?", "Please Respond", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                FormWarnToCloseComputers fwc = new FormWarnToCloseComputers();
                if (fwc.ShowDialog() == DialogResult.Yes)
                {
                    Reporting.Allocators.MyAllocator1_ProviderPayment allocator1 = new OpenDental.Reporting.Allocators.MyAllocator1_ProviderPayment();
                    SecurityLogs.MakeLogEntry(OpenDentBusiness.Permissions.Setup, 0, "Started Batch Allocation For Provider Allocation Tool");
                    allocator1.StartBatchAllocation();
                    SecurityLogs.MakeLogEntry(OpenDentBusiness.Permissions.Setup, 0, "Finished Batch Allocation For Provider Allocation Tool");

                    List <string> commands = new List <string>();
                    if (!PrefC.ContainsKey(MyAllocator1_ProviderPayment.Pref_AllocatorProvider1_ToolHasRun))
                    {
                        commands.Add("INSERT INTO preference VALUES ('"
                                     + MyAllocator1_ProviderPayment.Pref_AllocatorProvider1_ToolHasRun + "','0')");
                    }
                    if (!PrefC.ContainsKey(MyAllocator1_ProviderPayment.Pref_AllocatorProvider1_Use))
                    {
                        commands.Add("INSERT INTO preference VALUES ('"
                                     + MyAllocator1_ProviderPayment.Pref_AllocatorProvider1_Use + "','0')");
                    }
                    if (commands.Count != 0)
                    {
                        Db.NonQOld(commands.ToArray());
                        Cache.Refresh(InvalidType.Prefs);
                    }
                    Prefs.UpdateRaw(MyAllocator1_ProviderPayment.Pref_AllocatorProvider1_ToolHasRun, "1");
                    Prefs.UpdateRaw(MyAllocator1_ProviderPayment.Pref_AllocatorProvider1_Use, "1");
                }
            }
            RefreshForm();
        }
        private void StartBatch_DoWork_Handler(object sender,System.ComponentModel.DoWorkEventArgs e)
        {
            System.ComponentModel.BackgroundWorker bw = (System.ComponentModel.BackgroundWorker)sender;
            if(bw == null) {
                PU.MB = "This method is to be called by a a background worker.\n" + PU.Method;
                return;
            }
            DateTime StartTime = DateTime.Now;
            bw.ReportProgress(0,"Batch Processing Started at: " + String.Format("{0}h:{1}m.{2}",StartTime.Hour,StartTime.Minute,StartTime.Second));
            // Generate list of Guarantors to allocate
            int[] GuarantorsToAllocate = Generate_GuarantorList_ToAllocate();
            if(GuarantorsToAllocate == null || GuarantorsToAllocate.Length == 0) {
                MessageBox.Show(Lan.g(this,"No Guarantors to Allocate. Exiting."));
                return;
            }
            int BatchSize = 50;
            int iProgress = 0;
            OpenDental.Reporting.Allocators.MyAllocator1_ProviderPayment allocator = new OpenDental.Reporting.Allocators.MyAllocator1_ProviderPayment();
            for(int i = 0;i < GuarantorsToAllocate.Length + BatchSize;i = i + BatchSize) {

                int from = i;
                int to = (i + BatchSize > GuarantorsToAllocate.Length ? GuarantorsToAllocate.Length : i + BatchSize);
                iProgress = (to * 100) / (GuarantorsToAllocate.Length - 1);
                bw.ReportProgress(iProgress,"Beginning Batch " + from + "-" + to + " of " + GuarantorsToAllocate.Length + " Guarantors.");
                for(int j = from;j <to;j++) {
                    string UpdateTempTableCommand = "INSERT INTO " + TABLENAME + "_temp (Guarantor,AllocStatus) VALUES "
                        + "( " + GuarantorsToAllocate[j].ToString() + ", " + ((int)ProcessingState.Started_and_Incomplete).ToString() + " ) ";
                    Db.NonQOld(UpdateTempTableCommand);
                    allocator.AllocateWithOutToolCheck(GuarantorsToAllocate[j]);
                    iProgress = (j*100) / (GuarantorsToAllocate.Length - 1);
                    if(bw.CancellationPending) // Try to cancel between allocations which does a lot of writting
                    {
                        i = GuarantorsToAllocate.Length; // end loop
                        j = to;
                        iProgress = (j * 100) / (GuarantorsToAllocate.Length - 1); // recalculate progrss.
                        bw.ReportProgress(iProgress,"User Cancelled Requested");
                        e.Cancel = true;
                    }
                    UpdateTempTableCommand = "UPDATE " + TABLENAME + "_temp SET AllocStatus =" + ((int)ProcessingState.Complete).ToString()
                            +"  WHERE Guarantor = " + GuarantorsToAllocate[j].ToString();
                    Db.NonQOld(UpdateTempTableCommand);
                }
                iProgress = (to * 100) / (GuarantorsToAllocate.Length - 1);
                bw.ReportProgress(iProgress,"Finished Batch " + from + "-" + to + " of " + GuarantorsToAllocate.Length + " Guarantors.");
            }
            DateTime EndTime = DateTime.Now;
            bw.ReportProgress(iProgress,"Batch Processing Ended at: " + String.Format("{0}h:{1}m.{2}",EndTime.Hour,EndTime.Minute,EndTime.Second));
            TimeSpan timeSpan1 = new TimeSpan(EndTime.Ticks - StartTime.Ticks);
            bw.ReportProgress(iProgress,"Batch Processing Total Time is: " + String.Format("{0}h:{1}m.{2}",timeSpan1.Hours,timeSpan1.Minutes,timeSpan1.Seconds));
            e.Result = "Completed Processing of all " + GuarantorsToAllocate.Length + " Guarantors.";
        }