Index: Scheduling/trunk/cs/bsdx0200GUISourceCode/DPatientLetter.cs
===================================================================
--- Scheduling/trunk/cs/bsdx0200GUISourceCode/DPatientLetter.cs	(revision 614)
+++ Scheduling/trunk/cs/bsdx0200GUISourceCode/DPatientLetter.cs	(revision 772)
@@ -1,12 +1,7 @@
 using System;
-using System.Drawing;
-using System.Collections;
-using System.ComponentModel;
 using System.Windows.Forms;
 using System.Data;
-using CrystalDecisions.Windows;
-using CrystalDecisions.Shared;
-using CrystalDecisions.CrystalReports.Engine;
-using IndianHealthService.BMXNet;
+using System.Drawing.Printing;
+using System.Drawing;
 
 namespace IndianHealthService.ClinicalScheduling
@@ -15,61 +10,18 @@
 	/// Summary description for DPatientLetter.
 	/// </summary>
-	public class DPatientLetter : System.Windows.Forms.Form
-	{
-		private CrystalDecisions.Windows.Forms.CrystalReportViewer crViewer1;
+	public class DPatientLetter : System.Windows.Forms.PrintPreviewDialog
+    {
 		/// <summary>
 		/// Required designer variable.
 		/// </summary>
-		private System.ComponentModel.Container components = null;
+        private System.ComponentModel.Container components = null;
+        private System.Drawing.Printing.PrintDocument printDocument1;
 
 		#region Fields
-
-		private string	m_sBodyText;
+        DateTime _dtBegin, _dtEnd; //global fields to use in passing to printing method
+        int _currentResourcePrinting = 0;
+        int _currentApptPrinting = 0;
+        dsPatientApptDisplay2 _ds;
 		#endregion Fields
-
-		#region Properties
-
-		public string BodyText
-		{
-			get
-			{
-				return m_sBodyText;
-			}
-			set
-			{
-				m_sBodyText = value;
-			}
-		}
-
-		#endregion Properties
-
-		public void InitializeForm(CGDocumentManager docManager, int nPatientID)
-		{
-			try
-			{	
-				crViewer1.DisplayGroupTree = true;
-
-				ClinicalScheduling.crPatientLetter cr = new crPatientLetter();
-				string sSql = "BSDX PATIENT APPT DISPLAY^" + nPatientID.ToString();
-				System.Data.DataSet ds = new System.Data.DataSet();
-				DataTable dtAppt = docManager.RPMSDataTable(sSql, "PatientAppts");
-				ds.Tables.Add(dtAppt.Copy());
-
-				System.Data.DataTable dt = 
-					docManager.GlobalDataSet.Tables["Resources"].Copy();
-				ds.Tables.Add(dt);
-
-				cr.Database.Tables["PatientAppts"].SetDataSource(ds.Tables["PatientAppts"]);
-				cr.Database.Tables["BSDXResource"].SetDataSource(ds.Tables["Resources"]);
-
-				crViewer1.SelectionFormula = "{PatientAppts.ApptDate} >= CurrentDate";
-				this.crViewer1.ReportSource = cr;
-				
-			}
-			catch (Exception ex)
-			{
-				throw ex;
-			}
-		}
 
 		public void InitializeFormClinicSchedule(CGDocumentManager docManager,
@@ -84,17 +36,17 @@
 					throw new Exception("At least one clinic must be selected.");
 				}
+                _dtBegin = dtBegin;
+                _dtEnd = dtEnd;
 				string sBegin = dtBegin.ToShortDateString();
 				string sEnd = dtEnd.ToShortDateString();
-				crViewer1.DisplayGroupTree = true;
 				this.Text="Clinic Schedules";
 
-				ClinicalScheduling.crAppointmentList cr = new crAppointmentList();
 				string sSql = "BSDX CLINIC LETTERS^" + sClinicList + "^" + sBegin + "^" + sEnd;
-
-				DataTable dtAppt = docManager.RPMSDataTable(sSql, "PatientAppts");				
-				cr.Database.Tables["PatientAppts"].SetDataSource(dtAppt);
-
-				this.crViewer1.ReportSource = cr;
-			}
+                string sSql2 = "BSDX RESOURCE LETTERS^" + sClinicList;
+
+                _ds = new dsPatientApptDisplay2();
+                _ds.BSDXResource.Merge(docManager.RPMSDataTable(sSql2, "Resources"));
+                _ds.PatientAppts.Merge(docManager.RPMSDataTable(sSql, "PatientAppts"));
+            }
 			catch (Exception ex)
 			{
@@ -113,7 +65,28 @@
 					throw new Exception("At least one clinic must be selected.");
 				}
-				crViewer1.DisplayGroupTree = true;
-
-				ClinicalScheduling.crRebookLetter cr = new crRebookLetter();
+
+				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());				
+			}
+			catch (Exception ex)
+			{
+				throw ex;
+			}
+		}
+
+		public void InitializeFormCancellationLetters(CGDocumentManager docManager,
+			string sClinicList,
+			DataTable dtLetters)
+		{
+			try
+			{	
+				if (sClinicList == "")
+				{
+					throw new Exception("At least one clinic must be selected.");
+				}
 
 				System.Data.DataSet ds = new System.Data.DataSet();
@@ -123,42 +96,4 @@
 				DataTable dt = docManager.RPMSDataTable(sSql, "Resources");				
 				ds.Tables.Add(dt.Copy());
-
-				cr.Database.Tables["PatientAppts"].SetDataSource(ds.Tables["PatientAppts"]);
-				cr.Database.Tables["BSDXResource"].SetDataSource(ds.Tables["Resources"]);
-
-				this.crViewer1.ReportSource = cr;
-				
-			}
-			catch (Exception ex)
-			{
-				throw ex;
-			}
-		}
-
-		public void InitializeFormCancellationLetters(CGDocumentManager docManager,
-			string sClinicList,
-			DataTable dtLetters)
-		{
-			try
-			{	
-				if (sClinicList == "")
-				{
-					throw new Exception("At least one clinic must be selected.");
-				}
-				crViewer1.DisplayGroupTree = true;
-
-				ClinicalScheduling.crCancelLetter cr = new crCancelLetter();
-
-				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());
-
-				cr.Database.Tables["PatientAppts"].SetDataSource(ds.Tables["PatientAppts"]);
-				cr.Database.Tables["BSDXResource"].SetDataSource(ds.Tables["Resources"]);
-
-				this.crViewer1.ReportSource = cr;
 				
 			}
@@ -183,7 +118,5 @@
 				string sBegin = dtBegin.ToShortDateString();
 				string sEnd = dtEnd.ToShortDateString();
-				crViewer1.DisplayGroupTree = true;
-
-				ClinicalScheduling.crPatientLetter cr = new crPatientLetter();
+
 				string sSql = "BSDX CLINIC LETTERS^" + sClinicList + "^" + sBegin + "^" + sEnd;
 
@@ -195,9 +128,4 @@
 				DataTable dt = docManager.RPMSDataTable(sSql, "Resources");				
 				ds.Tables.Add(dt.Copy());
-
-				cr.Database.Tables["PatientAppts"].SetDataSource(ds.Tables["PatientAppts"]);
-				cr.Database.Tables["BSDXResource"].SetDataSource(ds.Tables["Resources"]);
-
-				this.crViewer1.ReportSource = cr;
 				
 			}
@@ -243,29 +171,43 @@
 		private void InitializeComponent()
 		{
-			this.crViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
-			this.SuspendLayout();
-			// 
-			// crViewer1
-			// 
-			this.crViewer1.ActiveViewIndex = -1;
-			this.crViewer1.Dock = System.Windows.Forms.DockStyle.Fill;
-			this.crViewer1.Location = new System.Drawing.Point(0, 0);
-			this.crViewer1.Name = "crViewer1";
-			this.crViewer1.ReportSource = null;
-			this.crViewer1.Size = new System.Drawing.Size(648, 398);
-			this.crViewer1.TabIndex = 0;
-			// 
-			// DPatientLetter
-			// 
-			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
-			this.ClientSize = new System.Drawing.Size(648, 398);
-			this.Controls.Add(this.crViewer1);
-			this.Name = "DPatientLetter";
-			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
-			this.Text = "Patient Letter";
-			this.ResumeLayout(false);
+            this.printDocument1 = new System.Drawing.Printing.PrintDocument();
+            this.SuspendLayout();
+            // 
+            // printDocument1
+            // 
+            this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
+            // 
+            // DPatientLetter
+            // 
+            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;
+            this.Text = "Patient Letter";
+            this.ResumeLayout(false);
 
 		}
 		#endregion
+
+        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
+        {
+            // _currentResourcePrinting starts with zero. There will be at least this one.
+            ClinicalScheduling.Printing.PrintAppointments(_ds, e, _dtBegin, _dtEnd,
+                    _currentResourcePrinting, ref _currentApptPrinting);
+
+            //If the printing routine says it needs more pages to print the appointments,
+            //return here and have it print again.
+            if (e.HasMorePages == true) return;
+
+            // 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)
+            {
+                _currentResourcePrinting++;
+                e.HasMorePages = true;
+            }
+            
+        }
 	}
 }
