Index: Scheduling/trunk/cs/bsdx0200GUISourceCode/DPatientLetter.cs
===================================================================
--- Scheduling/trunk/cs/bsdx0200GUISourceCode/DPatientLetter.cs	(revision 772)
+++ Scheduling/trunk/cs/bsdx0200GUISourceCode/DPatientLetter.cs	(revision 788)
@@ -16,14 +16,30 @@
 		/// </summary>
         private System.ComponentModel.Container components = null;
-        private System.Drawing.Printing.PrintDocument printDocument1;
+        private System.Drawing.Printing.PrintDocument printAppts;
+        private System.Drawing.Printing.PrintDocument printReminderLetters;
 
 		#region Fields
         DateTime _dtBegin, _dtEnd; //global fields to use in passing to printing method
+        
+        //stuff to track what got printed and where we are -- very ugly, I know
+        //but I don't know if there is a better way to do it.
         int _currentResourcePrinting = 0;
         int _currentApptPrinting = 0;
-        dsPatientApptDisplay2 _ds;
+        int _pageNumber = 0;
+        
+        //dataset to load the results of queries into and set to print routines
+        dsPatientApptDisplay2 _dsApptDisplay;
+        private PrintDocument printCancelLetters;
+        dsRebookAppts _dsRebookAppts;
 		#endregion Fields
 
-		public void InitializeFormClinicSchedule(CGDocumentManager docManager,
+		/// <summary>
+		/// Print Clinic Schedules
+		/// </summary>
+		/// <param name="docManager">This docManger</param>
+		/// <param name="sClinicList">Clinics for which to print</param>
+		/// <param name="dtBegin">Beginning Date</param>
+		/// <param name="dtEnd">End Date</param>
+        public void InitializeFormClinicSchedule(CGDocumentManager docManager,
 			string sClinicList,
 			DateTime dtBegin,
@@ -45,7 +61,9 @@
                 string sSql2 = "BSDX RESOURCE LETTERS^" + sClinicList;
 
-                _ds = new dsPatientApptDisplay2();
-                _ds.BSDXResource.Merge(docManager.RPMSDataTable(sSql2, "Resources"));
-                _ds.PatientAppts.Merge(docManager.RPMSDataTable(sSql, "PatientAppts"));
+                _dsApptDisplay = new dsPatientApptDisplay2();
+                _dsApptDisplay.BSDXResource.Merge(docManager.RPMSDataTable(sSql2, "Resources"));
+                _dsApptDisplay.PatientAppts.Merge(docManager.RPMSDataTable(sSql, "PatientAppts"));
+
+                this.PrintPreviewControl.Document = printAppts;
             }
 			catch (Exception ex)
@@ -57,5 +75,6 @@
 		public void InitializeFormRebookLetters(CGDocumentManager docManager,
 			string sClinicList,
-			DataTable dtLetters)
+		    DateTime dtBegin,
+            DateTime dtEnd)
 		{
 			try
@@ -66,11 +85,14 @@
 				}
 
-				System.Data.DataSet ds = new System.Data.DataSet();
-				ds.Tables.Add(dtLetters.Copy());
+                string sBegin = dtBegin.ToString("M/d/yyyy@HH:mm");
+                string sEnd = dtEnd.ToString("M/d/yyyy@HH:mm");
+
+                //Call RPC to get list of appt ids that have been rebooked for these clinics on these dates
+                string sSql1 = "BSDX REBOOK CLINIC LIST^" + sClinicList + "^" + sBegin + "^" + sEnd;
+                DataTable dtLetters = docManager.RPMSDataTable(sSql1, "PatientAppts");
 				
-				string sSql = "BSDX RESOURCE LETTERS^" + sClinicList;
-				DataTable dt = docManager.RPMSDataTable(sSql, "Resources");				
-				ds.Tables.Add(dt.Copy());				
-			}
+				string sSql2 = "BSDX RESOURCE LETTERS^" + sClinicList;
+				DataTable dt = docManager.RPMSDataTable(sSql2, "Resources");				
+            }
 			catch (Exception ex)
 			{
@@ -78,9 +100,40 @@
 			}
 		}
-
+        public void InitializeFormRebookLetters(CGDocumentManager docManager,
+            string sClinicList,
+            string sApptIDList)
+        {
+            try
+            {
+                if (sClinicList == "")
+                {
+                    throw new Exception("At least one clinic must be selected.");
+                }
+
+                string sSql = "BSDX REBOOK LIST^" + sApptIDList;
+                DataTable dtLetters = docManager.RPMSDataTable(sSql, "PatientAppts");
+
+                string sSql2 = "BSDX RESOURCE LETTERS^" + sClinicList;
+                DataTable dt = docManager.RPMSDataTable(sSql2, "Resources");
+
+            }
+            catch (Exception ex)
+            {
+                throw ex;
+            }
+        }
+
+        /// <summary>
+        /// Print Cancellation letters to mail to patients
+        /// </summary>
+        /// <param name="docManager">This Docmanager</param>
+        /// <param name="sClinicList">| delemited clinic list (IEN's)</param>
+        /// <param name="dtBegin">Beginning Date</param>
+        /// <param name="dtEnd">Ending Date</param>
 		public void InitializeFormCancellationLetters(CGDocumentManager docManager,
 			string sClinicList,
-			DataTable dtLetters)
-		{
+            DateTime dtBegin,
+            DateTime dtEnd)
+        {
 			try
 			{	
@@ -90,11 +143,17 @@
 				}
 
-				System.Data.DataSet ds = new System.Data.DataSet();
-				ds.Tables.Add(dtLetters.Copy());
-				
-				string sSql = "BSDX RESOURCE LETTERS^" + sClinicList;
-				DataTable dt = docManager.RPMSDataTable(sSql, "Resources");				
-				ds.Tables.Add(dt.Copy());
-				
+                string sBegin = dtBegin.ToString("M/d/yyyy@HH:mm");
+                string sEnd = dtEnd.ToString("M/d/yyyy@HH:mm");
+
+                //Call RPC to get list of appt ids that have been cancelled for these clinics on these dates
+                string sSql1 = "BSDX CANCEL CLINIC LIST^" + sClinicList + "^" + sBegin + "^" + sEnd;
+				string sSql2 = "BSDX RESOURCE LETTERS^" + sClinicList;
+
+                _dsRebookAppts = new dsRebookAppts();
+                _dsRebookAppts.PatientAppts.Merge(docManager.RPMSDataTable(sSql1, "PatientAppts"));
+                _dsRebookAppts.BSDXResource.Merge(docManager.RPMSDataTable(sSql2, "Resources"));
+
+                PrintPreviewControl.Document = printCancelLetters;
+
 			}
 			catch (Exception ex)
@@ -103,6 +162,12 @@
 			}
 		}
-
-		public void InitializeFormClinicLetters(CGDocumentManager docManager,
+        /// <summary>
+        /// Print Reminder Letters to give or mail to patients
+        /// </summary>
+        /// <param name="docManager">This docManger</param>
+        /// <param name="sClinicList">Clinics for which to print</param>
+        /// <param name="dtBegin">Beginning Date</param>
+        /// <param name="dtEnd">End Date</param>
+        public void InitializeFormPatientReminderLetters(CGDocumentManager docManager,
 			string sClinicList,
 			DateTime dtBegin,
@@ -115,18 +180,20 @@
 					throw new Exception("At least one clinic must be selected.");
 				}
+
+                _dtBegin = dtBegin;
+                _dtEnd = dtEnd;
+                string sBegin = dtBegin.ToShortDateString();
+                string sEnd = dtEnd.ToShortDateString();
+                this.Text = "Clinic Schedules";
+
+                string sSql = "BSDX CLINIC LETTERS^" + sClinicList + "^" + sBegin + "^" + sEnd;
+                string sSql2 = "BSDX RESOURCE LETTERS^" + sClinicList;
+
+                _dsApptDisplay = new dsPatientApptDisplay2();
+                _dsApptDisplay.BSDXResource.Merge(docManager.RPMSDataTable(sSql2, "Resources"));
+                _dsApptDisplay.PatientAppts.Merge(docManager.RPMSDataTable(sSql, "PatientAppts"));
+
+                this.PrintPreviewControl.Document = printReminderLetters;
 				
-				string sBegin = dtBegin.ToShortDateString();
-				string sEnd = dtEnd.ToShortDateString();
-
-				string sSql = "BSDX CLINIC LETTERS^" + sClinicList + "^" + sBegin + "^" + sEnd;
-
-				System.Data.DataSet ds = new System.Data.DataSet();
-				DataTable dtAppt = docManager.RPMSDataTable(sSql, "PatientAppts");				
-				ds.Tables.Add(dtAppt.Copy());
-				
-				sSql = "BSDX RESOURCE LETTERS^" + sClinicList;
-				DataTable dt = docManager.RPMSDataTable(sSql, "Resources");				
-				ds.Tables.Add(dt.Copy());
-				
 			}
 			catch (Exception ex)
@@ -137,5 +204,5 @@
 
 
-		public DPatientLetter()
+		public DPatientLetter() : base()
 		{
 			//
@@ -171,10 +238,20 @@
 		private void InitializeComponent()
 		{
-            this.printDocument1 = new System.Drawing.Printing.PrintDocument();
+            this.printAppts = new System.Drawing.Printing.PrintDocument();
+            this.printReminderLetters = new System.Drawing.Printing.PrintDocument();
+            this.printCancelLetters = new System.Drawing.Printing.PrintDocument();
             this.SuspendLayout();
             // 
-            // printDocument1
-            // 
-            this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
+            // printAppts
+            // 
+            this.printAppts.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printAppts_PrintPage);
+            // 
+            // printReminderLetters
+            // 
+            this.printReminderLetters.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printReminderLetters_PrintPage);
+            // 
+            // printCancelLetters
+            // 
+            this.printCancelLetters.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printCancelLetters_PrintPage);
             // 
             // DPatientLetter
@@ -182,5 +259,4 @@
             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
             this.ClientSize = new System.Drawing.Size(648, 398);
-            this.Document = this.printDocument1;
             this.Name = "DPatientLetter";
             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
@@ -191,9 +267,12 @@
 		#endregion
 
-        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
-        {
+        private void printAppts_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
+        {
+            //Each time we enter here, we start off with a new page number - we start off with zero
+            _pageNumber++; //becomes one first time through
+            
             // _currentResourcePrinting starts with zero. There will be at least this one.
-            ClinicalScheduling.Printing.PrintAppointments(_ds, e, _dtBegin, _dtEnd,
-                    _currentResourcePrinting, ref _currentApptPrinting);
+            ClinicalScheduling.Printing.PrintAppointments(_dsApptDisplay, e, _dtBegin, _dtEnd,
+                    _currentResourcePrinting, ref _currentApptPrinting, _pageNumber);
 
             //If the printing routine says it needs more pages to print the appointments,
@@ -203,10 +282,53 @@
             // if there are more resouces to print, increment. Setting HasMorePages to true
             // calls this routine again, so printing will happen with the incremented _currentResourcePrinting
-            if (_currentResourcePrinting < _ds.BSDXResource.Rows.Count - 1)
+            if (_currentResourcePrinting < _dsApptDisplay.BSDXResource.Rows.Count - 1)
             {
                 _currentResourcePrinting++;
+                //reset _currentApptPrinting
+                _currentApptPrinting = 0;
                 e.HasMorePages = true;
+            }            
+        }
+
+        private void printReminderLetters_PrintPage(object sender, PrintPageEventArgs e)
+        {
+            // no patients
+            if (_dsApptDisplay.PatientAppts.Count == 0)
+            {
+                ClinicalScheduling.Printing.PrintMessage("No Appointments found", e);
+                return;
+            }
+            // if there are patients
+            else if (_currentApptPrinting < _dsApptDisplay.PatientAppts.Count)
+            {
+                dsPatientApptDisplay2.BSDXResourceRow c = (dsPatientApptDisplay2.BSDXResourceRow)
+                   _dsApptDisplay.PatientAppts[_currentApptPrinting].GetParentRow(_dsApptDisplay.Relations[0]);
+                ClinicalScheduling.Printing.PrintReminderLetter(_dsApptDisplay.PatientAppts[_currentApptPrinting], e, c.LETTER_TEXT, "Reminder Letter");
+                _currentApptPrinting++;
+                if (_currentApptPrinting < _dsApptDisplay.PatientAppts.Count)
+                    e.HasMorePages = true;
             }
             
+        }
+
+        private void printCancelLetters_PrintPage(object sender, PrintPageEventArgs e)
+        {
+            // no patients
+            if (_dsRebookAppts.PatientAppts.Count == 0)
+            {
+                ClinicalScheduling.Printing.PrintMessage("No Appointments found", e);
+                return;
+            }
+            // if there are patients
+            else if (_currentApptPrinting < _dsRebookAppts.PatientAppts.Count)
+            {
+                dsRebookAppts.BSDXResourceRow c = (dsRebookAppts.BSDXResourceRow)
+                   _dsRebookAppts.PatientAppts[_currentApptPrinting].GetParentRow(_dsRebookAppts.Relations[0]);
+                ClinicalScheduling.Printing.PrintCancelLetter(_dsRebookAppts.PatientAppts[_currentApptPrinting], e, c.CLINIC_CANCELLATION_LETTER, "Cancellation Letter");
+                _currentApptPrinting++;
+                if (_currentApptPrinting < _dsRebookAppts.PatientAppts.Count)
+                    e.HasMorePages = true;
+            }
+
         }
 	}
