source: Scheduling/trunk/cs/bsdx0200GUISourceCode/CGDocument.cs@ 773

Last change on this file since 773 was 614, checked in by Sam Habiel, 15 years ago

Initial committ of scheduling package

File size: 28.8 KB
Line 
1using System;
2using System.Collections;
3using System.Data;
4using System.Data.OleDb;
5using System.Diagnostics;
6using System.Drawing;
7using System.Windows.Forms;
8using IndianHealthService.BMXNet;
9
10namespace IndianHealthService.ClinicalScheduling
11{
12 /// <summary>
13 /// Contains the array of appointments and availabily that make up the document class
14 /// </summary>
15 public class CGDocument : System.Object
16 {
17
18 public CGDocument()
19 {
20 m_appointments = new CGAppointments();
21 m_pAvArray = new ArrayList();
22 m_sResourcesArray = new ArrayList();
23 }
24
25 #region Member Variables
26 public int m_nColumnCount; //todo: this should point to the view's member for column count
27 public int m_nTimeUnits;
28 private string m_sDocName;
29 public ArrayList m_sResourcesArray;
30 public ScheduleType m_ScheduleType;
31 private DateTime m_dSelectedDate; //Holds the user's selection from the dtpicker
32 private DateTime m_dStartDate; //Beginning date of document data
33 private DateTime m_dEndDate; //Ending date of document data
34 public CGAppointments m_appointments;
35 private ArrayList m_pAvArray;
36 private CGDocumentManager m_DocManager;
37 private DateTime m_dLastRefresh = DateTime.Now;
38
39 #endregion
40
41 #region Properties
42
43 /// <summary>
44 /// Returns the latest refresh time for this document
45 /// </summary>
46 public DateTime LastRefreshed
47 {
48 get
49 {
50 return m_dLastRefresh;
51 }
52 }
53
54 /// <summary>
55 /// The list of Resource names
56 /// </summary>
57 public ArrayList Resources
58 {
59 get
60 {
61 return this.m_sResourcesArray;
62 }
63 set
64 {
65 this.m_sResourcesArray = value;
66 }
67 }
68
69 /// <summary>
70 /// The array of CGAvailabilities that contains appt type and slots
71 /// </summary>
72 public ArrayList AvailabilityArray
73 {
74 get
75 {
76 return this.m_pAvArray;
77 }
78 }
79
80 public CGDocumentManager DocManager
81 {
82 get
83 {
84 return m_DocManager;
85 }
86 set
87 {
88 m_DocManager = value;
89 }
90 }
91
92 /// <summary>
93 /// Contains the hashtable of appointments
94 /// </summary>
95 public CGAppointments Appointments
96 {
97 get
98 {
99 return m_appointments;
100 }
101 }
102
103 /// <summary>
104 /// Holds the date selected by the user in CGView.dateTimePicker1
105 /// </summary>
106 public DateTime SelectedDate
107 {
108 get
109 {
110 return this.m_dSelectedDate;
111 }
112 set
113 {
114 this.m_dSelectedDate = value;
115 bool bRet = false;
116 if (m_sResourcesArray.Count == 1)
117 {
118 bRet = this.WeekNeedsRefresh(1, m_dSelectedDate, out this.m_dStartDate, out this.m_dEndDate);
119 }
120 else
121 {
122 this.m_dStartDate = m_dSelectedDate;
123 this.m_dEndDate = m_dSelectedDate;
124 this.m_dEndDate = this.m_dEndDate.AddHours(23);
125 this.m_dEndDate = this.m_dEndDate.AddMinutes(59);
126 this.m_dEndDate = this.m_dEndDate.AddSeconds(59);
127 }
128
129 bRet = RefreshSchedule();
130 }
131 }
132
133 /// <summary>
134 /// Contains the beginning date of the appointment document
135 /// </summary>
136 public DateTime StartDate
137 {
138 get
139 {
140 return this.m_dStartDate;
141 }
142 }
143
144 public string DocName
145 {
146 get
147 {
148 return this.m_sDocName;
149 }
150 set
151 {
152 this.m_sDocName = value;
153 }
154 }
155
156 #endregion
157
158 #region Methods
159
160 public void UpdateAllViews()
161 {
162 //iterate through all views and call update.
163 Hashtable h = CGDocumentManager.Current.Views;
164
165 CGDocument d;
166 foreach (CGView v in h.Keys)
167 {
168 d = (CGDocument) h[v];
169 if (d == this)
170 {
171 v.UpdateArrays();
172 }
173 }
174
175 }
176
177 /// <summary>
178 /// Update schedule based on info in RPMS
179 /// </summary>
180 private bool RefreshDaysSchedule()
181 {
182 try
183 {
184 string sPatientName;
185 string sPatientID;
186 DateTime dStart;
187 DateTime dEnd;
188 DateTime dCheckIn;
189 DateTime dAuxTime;
190 int nKeyID;
191 string sNote;
192 string sResource;
193 bool bNoShow = false;
194 string sNoShow = "0";
195 string sHRN = "";
196 int nAccessTypeID; //used in autorebook
197 string sWalkIn = "0";
198 bool bWalkIn;
199 CGAppointment pAppointment;
200 CGDocumentManager pApp = CGDocumentManager.Current;
201 DataTable rAppointmentSchedule;
202
203 m_dLastRefresh = DateTime.Now;
204
205 this.m_appointments.ClearAllAppointments();
206
207 rAppointmentSchedule = CGSchedLib.CreateAppointmentSchedule(m_DocManager, m_sResourcesArray, this.m_dStartDate, this.m_dEndDate);
208 CGSchedLib.OutputArray(rAppointmentSchedule, "rAppointmentSchedule");
209 foreach (DataRow r in rAppointmentSchedule.Rows)
210 {
211
212 if (r["APPOINTMENTID"].ToString() == "0")
213 {
214 string sMsg = r["NOTE"].ToString();
215 throw new BMXNetException(sMsg);
216 }
217 nKeyID = Convert.ToInt32(r["APPOINTMENTID"].ToString());
218 sResource = r["RESOURCENAME"].ToString();
219 sPatientName =r["PATIENTNAME"].ToString();
220 sPatientID =r["PATIENTID"].ToString();
221 dStart = (DateTime) r["START_TIME"];
222 dEnd = (DateTime) r["END_TIME"];
223 dCheckIn = new DateTime();
224 dAuxTime = new DateTime();
225
226 if (r["CHECKIN"].GetType() != typeof(System.DBNull))
227 dCheckIn = (DateTime) r["CHECKIN"];
228 if (r["AUXTIME"].GetType() != typeof(System.DBNull))
229 dCheckIn = (DateTime) r["AUXTIME"];
230 sNote = r["NOTE"].ToString();
231 sNoShow = r["NOSHOW"].ToString();
232 bNoShow = (sNoShow == "1")?true: false;
233 sHRN = r["HRN"].ToString();
234 nAccessTypeID = (int) r["ACCESSTYPEID"];
235 sWalkIn = r["WALKIN"].ToString();
236 bWalkIn = (sWalkIn == "1")?true: false;
237
238 pAppointment = new CGAppointment();
239 pAppointment.CreateAppointment(dStart, dEnd, sNote, nKeyID, sResource);
240 pAppointment.PatientName = sPatientName;
241 pAppointment.PatientID = Convert.ToInt32(sPatientID);
242 if (dCheckIn.Ticks > 0)
243 pAppointment.CheckInTime = dCheckIn;
244 if (dAuxTime.Ticks > 0)
245 pAppointment.AuxTime = dAuxTime;
246 pAppointment.NoShow = bNoShow;
247 pAppointment.HealthRecordNumber = sHRN;
248 pAppointment.AccessTypeID = nAccessTypeID;
249 pAppointment.WalkIn = bWalkIn;
250 this.m_appointments.AddAppointment(pAppointment);
251
252 }
253
254 return true;
255 }
256 catch(Exception Ex)
257 {
258 Debug.Write("CGDocument.RefreshDaysSchedule error: " + Ex.Message + "\n");
259 return false;
260 }
261 }
262
263
264 public void OnNewDocument()
265 {
266 /*
267 * TEST EXCEPTION -- REMOVE AFTER TESTING
268 */
269 //throw new Exception("Simulated Uncaught Exception");
270 /*
271 * TEST EXCEPTION -- REMOVE AFTER TESTING
272 */
273
274 //Open an empty document
275 m_sResourcesArray.Clear();
276 m_ScheduleType = ScheduleType.Resource;
277
278 //Set initial From and To dates based on current day
279 // DateTime dDate = new DateTime(2001,12,05); //Testing line
280 DateTime dDate = DateTime.Today;
281 bool bRet = this.WeekNeedsRefresh(2,dDate, out this.m_dStartDate, out this.m_dEndDate);
282
283 //Create new View
284 CGView view = new CGView();
285 view.InitializeDocView(this,
286 this.DocManager,
287 m_dStartDate,
288 this.Appointments,
289 DocManager.WindowText);
290
291 view.Show();
292 view.SyncTree();
293 view.Activate();
294 this.UpdateAllViews();
295 }
296
297 private void SetDate(DateTime dDate)
298 {
299 bool bRet = false;
300 if (m_ScheduleType == ScheduleType.Resource)
301 {
302 bRet = this.WeekNeedsRefresh(2,dDate, out this.m_dStartDate, out this.m_dEndDate);
303 }
304 else
305 {
306 this.m_dStartDate = dDate;
307 this.m_dEndDate = dDate;
308 this.m_dEndDate = this.m_dEndDate.AddHours(23);
309 this.m_dEndDate = this.m_dEndDate.AddMinutes(59);
310 this.m_dEndDate = this.m_dEndDate.AddSeconds(59);
311 }
312
313 bRet = RefreshSchedule();
314 this.UpdateAllViews();
315 }
316
317 public void RefreshDocument()
318 {
319 bool bRet = RefreshSchedule();
320 this.UpdateAllViews();
321 }
322
323 public void OnOpenDocument()
324 {
325 try
326 {
327 //Create new Document
328 m_ScheduleType = (m_sResourcesArray.Count == 1) ? ScheduleType.Resource: ScheduleType.Clinic;
329 bool bRet = false;
330
331 //Set initial From and To dates based on current day
332 DateTime dDate = DateTime.Today;
333 if (m_ScheduleType == ScheduleType.Resource)
334 {
335 bRet = this.WeekNeedsRefresh(1,dDate, out this.m_dStartDate, out this.m_dEndDate);
336 }
337 else
338 {
339 this.m_dStartDate = dDate;
340 this.m_dEndDate = dDate;
341 this.m_dEndDate = this.m_dEndDate.AddHours(23);
342 this.m_dEndDate = this.m_dEndDate.AddMinutes(59);
343 this.m_dEndDate = this.m_dEndDate.AddSeconds(59);
344 }
345
346 bRet = RefreshSchedule();
347
348 CGView view = null;
349 //If this document already has a view, the use it
350 Hashtable h = CGDocumentManager.Current.Views;
351 CGDocument d;
352 bool bReuseView = false;
353 foreach (CGView v in h.Keys)
354 {
355 d = (CGDocument) h[v];
356 if (d == this)
357 {
358 view = v;
359 bReuseView = true;
360 v.InitializeDocView(this.DocName);
361 break;
362 }
363 }
364
365 //Otherwise, create new View
366 if (bReuseView == false)
367 {
368 view = new CGView();
369
370 view.InitializeDocView(this,
371 this.DocManager,
372 m_dStartDate,
373 this.Appointments,
374 this.DocName);
375
376 view.Show();
377 view.SyncTree();
378
379 }
380 this.UpdateAllViews();
381 }
382 catch (BMXNetException bmxEx)
383 {
384 throw bmxEx;
385 }
386 catch (Exception ex)
387 {
388 throw new BMXNet.BMXNetException("ClinicalScheduling.OnOpenDocument error: " + ex.Message);
389 }
390 }
391
392 private bool RefreshSchedule()
393 {
394 try
395 {
396 bool bRet = this.RefreshAvailabilitySchedule();
397 if (bRet == false)
398 {
399 return bRet;
400 }
401 bRet = this.RefreshDaysSchedule();
402 return bRet;
403 }
404 catch (ApplicationException aex)
405 {
406 Debug.Write("CGDocument.RefreshSchedule Application Error: " + aex.Message + "\n");
407 return false;
408 }
409 catch (Exception ex)
410 {
411 MessageBox.Show("CGDocument.RefreshSchedule error: " + ex.Message + "\n");
412 return false;
413 }
414 }
415
416 private bool RefreshAvailabilitySchedule()
417 {
418 try
419 {
420 if (this.m_DocManager.ConnectInfo.Connected == false)
421 {
422 m_DocManager.ConnectInfo.LoadConnectInfo();
423 }
424 System.IntPtr pHandle = m_DocManager.Handle;
425
426 m_pAvArray.Clear();
427
428 ArrayList saryApptTypes = new ArrayList();
429 int nApptTypeID = 0;
430
431 //Refresh Availability schedules
432 DataTable rAvailabilitySchedule;
433 rAvailabilitySchedule = CGSchedLib.CreateAvailabilitySchedule(m_DocManager, m_sResourcesArray, this.m_dStartDate, this.m_dEndDate, saryApptTypes,/**/ m_ScheduleType, "0");
434 CGSchedLib.OutputArray(rAvailabilitySchedule, "rAvailabilitySchedule");
435
436 //Refresh Type Schedule
437 string sResourceName = "";
438 DataTable rTypeSchedule = new DataTable();;
439 for (int j = 0; j < m_sResourcesArray.Count; j++)
440 {
441 sResourceName = m_sResourcesArray[j].ToString();
442 DataTable dtTemp = CGSchedLib.CreateAssignedTypeSchedule(m_DocManager, sResourceName, this.m_dStartDate, this.m_dEndDate, m_ScheduleType);
443 CGSchedLib.OutputArray(dtTemp, "dtTemp");
444 if (j == 0)
445 {
446 rTypeSchedule = dtTemp;
447 }
448 else
449 {
450 rTypeSchedule = CGSchedLib.UnionBlocks(rTypeSchedule, dtTemp);
451 }
452 }
453 CGSchedLib.OutputArray(rTypeSchedule, "rTypeSchedule");
454
455 DateTime dStart;
456 DateTime dEnd;
457 DateTime dTypeStart;
458 DateTime dTypeEnd;
459 int nSlots;
460 Rectangle crRectA = new Rectangle(0,0,1,0);
461 Rectangle crRectB= new Rectangle(0,0,1,0);
462 bool bIsect;
463 string sResourceList;
464 string sAccessRuleList;
465
466
467 foreach (DataRow rTemp in rAvailabilitySchedule.Rows)
468 {
469 //get StartTime, EndTime and Slots
470 dStart = (DateTime) rTemp["START_TIME"];
471 dEnd = (DateTime) rTemp["END_TIME"];
472
473 //TODO: Fix this slots datatype problem
474 string sSlots = rTemp["SLOTS"].ToString();
475 nSlots = Convert.ToInt16(sSlots);
476
477 sResourceList = rTemp["RESOURCE"].ToString();
478 sAccessRuleList = rTemp["ACCESS_TYPE"].ToString();
479
480 string sNote = rTemp["NOTE"].ToString();
481
482 if ((nSlots < -1000)||(sAccessRuleList == ""))
483 {
484 nApptTypeID = 0;
485 }
486 else
487 {
488 foreach (DataRow rType in rTypeSchedule.Rows)
489 {
490
491 dTypeStart = (DateTime) rType["StartTime"];
492 dTypeEnd = (DateTime) rType["EndTime"];
493 //if start & end times overlap, then
494 string sTypeResource = rType["ResourceName"].ToString();
495 if ((dTypeStart.DayOfYear == dStart.DayOfYear) && (sResourceList == sTypeResource))
496 {
497 crRectA.Y = GetTotalMinutes(dStart);
498 crRectA.Height = GetTotalMinutes(dEnd) - crRectA.Top;
499 crRectB.Y = GetTotalMinutes(dTypeStart);
500 crRectB.Height = GetTotalMinutes(dTypeEnd) - crRectB.Top;
501 bIsect = crRectA.IntersectsWith(crRectB);
502 if (bIsect == true)
503 {
504 //TODO: This code:
505 // nApptTypeID = (int) rType["AppointmentTypeID"];
506 //Causes this exception:
507 //Unhandled Exception: System.InvalidCastException: Specified cast is not valid.
508 string sTemp = rType["AppointmentTypeID"].ToString();
509 nApptTypeID = Convert.ToInt16(sTemp);
510 break;
511 }
512 }
513 }//end foreach datarow rType
514 }
515
516 AddAvailability(dStart, dEnd, nApptTypeID, nSlots, false, sResourceList, sAccessRuleList, sNote);
517 }//end foreach datarow rTemp
518
519 return true;
520 }
521 catch (Exception ex)
522 {
523 Debug.Write("CGDocument.RefreshAvailabilitySchedule error: " + ex.Message + "\n");
524 return false;
525 }
526 }
527
528 private int GetTotalMinutes(DateTime dDate)
529 {
530 return ((dDate.Hour * 60) + dDate.Minute);
531 }
532
533 public int AddAvailability(DateTime StartTime, DateTime EndTime, int nType, int nSlots, bool UpdateView, string sResourceList, string sAccessRuleList, string sNote)
534 {
535 //adds it to the object array
536 //Returns the index in the array
537
538 CGAvailability pNewAv = new CGAvailability();
539 pNewAv.Create(StartTime, EndTime, nType, nSlots, sResourceList, sAccessRuleList);
540
541 pNewAv.Note = sNote;
542
543 //Look up the color and type name using the AppointmentTypes datatable
544 DataTable dtType = this.m_DocManager.GlobalDataSet.Tables["AccessTypes"];
545 DataRow dRow = dtType.Rows.Find(nType.ToString());
546 if (dRow != null)
547 {
548 string sColor = dRow["DISPLAY_COLOR"].ToString();
549 pNewAv.DisplayColor = sColor;
550 string sTemp = dRow["RED"].ToString();
551 sTemp = (sTemp == "")?"0":sTemp;
552 int nRed = Convert.ToInt16(sTemp);
553 pNewAv.Red = nRed;
554 sTemp = dRow["GREEN"].ToString();
555 sTemp = (sTemp == "")?"0":sTemp;
556 int nGreen = Convert.ToInt16(sTemp);
557 pNewAv.Green = nGreen;
558 sTemp = dRow["BLUE"].ToString();
559 sTemp = (sTemp == "")?"0":sTemp;
560 int nBlue = Convert.ToInt16(sTemp);
561 pNewAv.Blue = nBlue;
562
563 string sName = dRow["ACCESS_TYPE_NAME"].ToString();
564 pNewAv.AccessTypeName = sName;
565 }
566
567 int nIndex = 0;
568 nIndex = m_pAvArray.Add(pNewAv);
569 if (UpdateView == true)
570 {
571 this.UpdateAllViews();
572 }
573 return nIndex;
574 }
575
576
577 public void AddResource(string sResource)
578 {
579 //TODO: Test that resource is not currently in list, that it IS a resource, etc
580 this.m_sResourcesArray.Add(sResource);
581 this.UpdateAllViews();
582 }
583
584 public void ClearResources()
585 {
586 this.m_sResourcesArray.Clear();
587 }
588
589 public int SlotsAvailable(DateTime dSelStart, DateTime dSelEnd, string sResource, out string sAccessType, out string sAvailabilityMessage)
590 {
591 sAccessType = "";
592 sAvailabilityMessage = "";
593 DateTime dStart;
594 DateTime dEnd;
595 int nAvailableSlots = 999;
596 int nSlots = 0;
597 int i = 0;
598 CGAvailability pAv;
599 Rectangle crRectA = new Rectangle(0,0,1,0);
600 Rectangle crRectB = new Rectangle(0,0,1,0);
601 bool bIsect;
602 crRectB.Y = GetTotalMinutes(dSelStart);
603 crRectB.Height = GetTotalMinutes(dSelEnd)- crRectB.Y;
604
605 // //loop thru m_pAvArray
606 // //Compare the start time and end time of eachblock
607 while (i < m_pAvArray.Count)
608 {
609 pAv = (CGAvailability) m_pAvArray[i];
610 dStart = pAv.StartTime;
611 dEnd = pAv.EndTime;
612 if ((sResource == pAv.ResourceList) &&
613 ((dSelStart.Date == dStart.Date) || (dSelStart.Date == dEnd.Date)))
614 {
615 crRectA.Y = (dStart.Date < dSelStart.Date) ? 0 : GetTotalMinutes(dStart);
616 crRectA.Height = (dEnd.Date > dSelEnd.Date) ? 1440 : GetTotalMinutes(dEnd);
617 crRectA.Height = crRectA.Height - crRectA.Y;
618 bIsect = crRectA.IntersectsWith(crRectB);
619 if (bIsect != false)
620 {
621 nSlots = pAv.Slots;
622 if (nSlots < 1)
623 {
624 nAvailableSlots = 0;
625 break;
626 }
627 if (nSlots < nAvailableSlots)
628 {
629 nAvailableSlots = nSlots;
630 sAccessType = pAv.AccessTypeName;
631 sAvailabilityMessage = pAv.Note;
632
633 }
634 }
635 }
636 i++;
637 }
638 if (nAvailableSlots == 999)
639 {
640 nAvailableSlots = 0;
641 }
642 return nAvailableSlots;
643 }
644
645 /// <summary>
646 /// Given a selected date,
647 /// Calculates StartDay and End Day and returns them in output params.
648 /// nWeeks == number of Weeks to display
649 /// nColumnCount is number of days displayed per week. If 5 columns, begin on
650 /// Monday, if 7 Columns, begin on Sunday
651 ///
652 /// Returns TRUE if the document's data needs refreshing based on
653 /// this newly selected date.
654 /// </summary>
655 public bool WeekNeedsRefresh(int nWeeks, DateTime SelectedDate,
656 out DateTime WeekStartDay, out DateTime WeekEndDay)
657 {
658 DateTime OldStartDay = m_dStartDate;
659 DateTime OldEndDay = m_dEndDate;
660 int nWeekDay = (int) SelectedDate.DayOfWeek; //0 == Sunday
661
662 int nOff = 1;
663 TimeSpan ts = new TimeSpan(nWeekDay - nOff,0,0,0); //d,h,m,s
664
665 if (m_nColumnCount == 1)
666 {
667 ts = new TimeSpan(0,23,59,59);
668 WeekStartDay = SelectedDate;
669 }
670 else
671 {
672 WeekStartDay = SelectedDate - ts;
673 if (m_nColumnCount == 7)
674 {
675 ts = new TimeSpan(1,0,0,0);
676 WeekStartDay -= ts;
677 }
678 int nEnd = (m_nColumnCount == 7) ? 1 : 3;
679 ts = new TimeSpan((7* nWeeks) - nEnd, 23, 59,59);
680 }
681 WeekEndDay = WeekStartDay + ts;
682 bool bRet = (( WeekStartDay.Date != OldStartDay.Date) || (WeekEndDay.Date != OldEndDay.Date));
683 return bRet;
684 }
685
686 /// <summary>
687 /// Calls RPMS to create appointment then
688 /// adds appointment to the m_appointments collection
689 /// Returns the IEN of the appointment in the RPMS BSDX APPOINTMENT file.
690 /// </summary>
691 /// <param name="rApptInfo"></param>
692 /// <returns></returns>
693 public int CreateAppointment(CGAppointment rApptInfo)
694 {
695 return CreateAppointment(rApptInfo, false);
696 }
697
698 /// <summary>
699 /// Use this overload to create a walkin appointment
700 /// </summary>
701 /// <param name="rApptInfo"></param>
702 /// <param name="bWalkin"></param>
703 /// <returns></returns>
704 public int CreateAppointment(CGAppointment rApptInfo, bool bWalkin)
705 {
706 string sStart;
707 string sEnd;
708 string sPatID;
709 string sResource;
710 string sNote;
711 string sLen;
712 string sApptID;
713
714 sStart = rApptInfo.StartTime.ToString("M-d-yyyy@HH:mm");
715 sEnd = rApptInfo.EndTime.ToString("M-d-yyyy@HH:mm");
716 TimeSpan sp = rApptInfo.EndTime - rApptInfo.StartTime;
717 sLen = sp.TotalMinutes.ToString();
718 sPatID = rApptInfo.PatientID.ToString();
719 sNote = rApptInfo.Note;
720 sResource = rApptInfo.Resource;
721 if (bWalkin == true)
722 {
723 sApptID = "WALKIN";
724 }
725 else
726 {
727 sApptID = rApptInfo.AccessTypeID.ToString();
728 }
729
730 CGAppointment aCopy = new CGAppointment();
731 aCopy.CreateAppointment(rApptInfo.StartTime, rApptInfo.EndTime, sNote, 0, sResource);
732 aCopy.PatientID = rApptInfo.PatientID;
733 aCopy.PatientName = rApptInfo.PatientName;
734 aCopy.HealthRecordNumber = rApptInfo.HealthRecordNumber;
735 aCopy.AccessTypeID = rApptInfo.AccessTypeID;
736
737 string sSql = "BSDX ADD NEW APPOINTMENT^" + sStart + "^" + sEnd + "^" + sPatID + "^" + sResource + "^" + sLen + "^" + sNote + "^" + sApptID ;
738 System.Data.DataTable dtAppt = m_DocManager.RPMSDataTable(sSql, "NewAppointment");
739 int nApptID;
740
741 Debug.Assert(dtAppt.Rows.Count == 1);
742 DataRow r = dtAppt.Rows[0];
743 nApptID =Convert.ToInt32(r["APPOINTMENTID"]);
744 string sErrorID;
745 sErrorID = r["ERRORID"].ToString();
746 if ((sErrorID != "")||(nApptID < 1))
747 throw new Exception(sErrorID);
748 aCopy.AppointmentKey = nApptID;
749 this.m_appointments.AddAppointment(aCopy);
750
751 bool bRet = RefreshAvailabilitySchedule();
752
753 UpdateAllViews();
754
755 return nApptID;
756 }
757
758 public void EditAppointment(CGAppointment pAppt, string sNote)
759 {
760 try
761 {
762 int nApptID = pAppt.AppointmentKey;
763 string sSql = "BSDX EDIT APPOINTMENT^" + nApptID.ToString() + "^" + sNote;
764
765 System.Data.DataTable dtAppt = m_DocManager.RPMSDataTable(sSql, "EditAppointment");
766
767 Debug.Assert(dtAppt.Rows.Count == 1);
768 DataRow r = dtAppt.Rows[0];
769 string sErrorID = r["ERRORID"].ToString();
770 if (sErrorID == "-1")
771 pAppt.Note = sNote;
772
773 if (this.m_appointments.AppointmentTable.ContainsKey(nApptID))
774 {
775 bool bRet = RefreshAvailabilitySchedule();
776 UpdateAllViews();
777 }
778 }
779 catch (Exception ex)
780 {
781 Debug.Write("CGDocument.EditAppointment Failed: " + ex.Message);
782 }
783 }
784
785 public void CheckInAppointment(int nApptID, DateTime dCheckIn,
786 string ClinicStopIEN,
787 string ProviderIEN,
788 string PrintRouteSlip,
789 string PCCClinicIEN,
790 string PCCFormIEN,
791 string PCCOutGuide
792 )
793 {
794 string sCheckIn = dCheckIn.ToString("M-d-yyyy@HH:mm");
795
796 string sSql = "BSDX CHECKIN APPOINTMENT^" + nApptID.ToString() + "^" + sCheckIn + "^";
797 sSql += ClinicStopIEN + "^" + ProviderIEN + "^" + PrintRouteSlip + "^";
798 sSql += PCCClinicIEN + "^" + PCCFormIEN + "^" + PCCOutGuide;
799
800 System.Data.DataTable dtAppt = m_DocManager.RPMSDataTable(sSql, "CheckInAppointment");
801
802 Debug.Assert(dtAppt.Rows.Count == 1);
803 DataRow r = dtAppt.Rows[0];
804 string sErrorID = r["ERRORID"].ToString();
805
806 if (this.m_appointments.AppointmentTable.ContainsKey(nApptID))
807 {
808 bool bRet = RefreshSchedule();
809 UpdateAllViews();
810 }
811 }
812
813 public string DeleteAppointment(int nApptID)
814 {
815 return DeleteAppointment(nApptID, true, 0, "");
816 }
817
818 public string DeleteAppointment(int nApptID, bool bClinicCancelled, int nReason, string sRemarks)
819 {
820 //Returns "" if deletion successful
821 //Otherwise, returns reason for failure
822
823 string sClinicCancelled = (bClinicCancelled == true)?"C":"PC";
824 string sReasonID = nReason.ToString();
825 string sSql = "BSDX CANCEL APPOINTMENT^" + nApptID.ToString();
826 sSql += "^" + sClinicCancelled;
827 sSql += "^" + sReasonID;
828 sSql += "^" + sRemarks;
829 DataTable dtAppt = m_DocManager.RPMSDataTable(sSql, "DeleteAppointment");
830
831 Debug.Assert(dtAppt.Rows.Count == 1);
832 DataRow r = dtAppt.Rows[0];
833 string sErrorID = r["ERRORID"].ToString();
834 if (sErrorID != "")
835 return sErrorID;
836
837 if (this.m_appointments.AppointmentTable.ContainsKey(nApptID))
838 {
839 this.m_appointments.RemoveAppointment(nApptID);
840 bool bRet = RefreshAvailabilitySchedule();
841 UpdateAllViews();
842 }
843 return "";
844 }
845
846 public string AutoRebook(CGAppointment a, int nSearchType, int nMinimumDays, int nMaximumDays, out CGAppointment aRebook)
847 {
848 //If successful Returns "1" and new start date and time returned in aRebook
849 //Otherwise, returns error message
850
851 CGAppointment aCopy = new CGAppointment();
852 aCopy.CreateAppointment(a.StartTime, a.EndTime, a.Note, 0, a.Resource);
853 aCopy.PatientID = a.PatientID;
854 aCopy.PatientName = a.PatientName;
855 aCopy.HealthRecordNumber = a.HealthRecordNumber;
856 aCopy.AccessTypeID = a.AccessTypeID;
857 aRebook = aCopy;
858
859 //Determine Rebook access type
860 //nSearchType = -1: use current, -2: use any non-zero type, >0 use this access type id
861 int nAVType = 0;
862
863 switch (nSearchType)
864 {
865 case -1:
866 nAVType = a.AccessTypeID;
867 break;
868 case -2:
869 nAVType = 0;
870 break;
871 default:
872 nAVType = nSearchType;
873 break;
874 }
875
876 int nSlots = 0;
877 string sSlots = "";
878 int nAccessTypeID; //To compare with nAVType
879
880 DateTime dResult = new DateTime(); //StartTime of access block to autorebook into
881
882 //Next two are empty, but needed to pass to CreateAvailabilitySchedule
883 ArrayList alAccessTypes = new ArrayList();
884 string sSearchInfo = "";
885
886 //Find the StartTime of first availability block of this type for this clinic
887 //between nMinimumDays and nMaximumDays
888
889 string sAVStart = a.StartTime.AddDays(nMinimumDays).ToString("M/d/yyyy@H:mm");
890
891 //dtAVEnd is the last day to search
892 DateTime dtAVEnd = a.StartTime.AddDays(nMinimumDays + nMaximumDays);
893 string sAVEnd = dtAVEnd.ToString("M/d/yyyy@H:mm");
894
895 //Increment start day to search a week (or so) at a time
896 //30 is a test increment. Need to test different values for performance
897 int nIncrement = (nMaximumDays < 30)?nMaximumDays:30;
898
899 //nCount and nCountEnd are the 'moving' counters
900 //that I add to start and end to get the bracket
901 //At the beginning of the DO loop, nCount and nCountEnd are already set
902 bool bFinished = false;
903 bool bFound = false;
904
905 DateTime dStart = a.StartTime.AddDays(nMinimumDays);
906 DateTime dEnd = dStart.AddDays(nIncrement);
907 do
908 {
909 string sSql = "BSDX REBOOK NEXT BLOCK^" + dStart.ToString("M/d/yyyy@H:mm")+ "^" + a.Resource + "^" + nAVType.ToString();
910 DataTable dtNextBlock = this.DocManager.RPMSDataTable(sSql, "NextBlock");
911 Debug.Assert(dtNextBlock.Rows.Count == 1);
912 DataRow drNextBlockRow = dtNextBlock.Rows[0];
913
914 object oNextBlock;
915 oNextBlock = drNextBlockRow["NEXTBLOCK"];
916 if (oNextBlock.GetType() == typeof(System.DBNull))
917 break;
918 DateTime dNextBlock = (DateTime) drNextBlockRow["NEXTBLOCK"];
919 if (dNextBlock > dtAVEnd)
920 {
921 break;
922 }
923
924 dStart = dNextBlock;
925 dEnd = dStart.AddDays(nIncrement);
926 if (dEnd > dtAVEnd)
927 dEnd = dtAVEnd;
928
929 DataTable dtResult = CGSchedLib.CreateAvailabilitySchedule(m_DocManager, this.Resources, dStart, dEnd, alAccessTypes, ScheduleType.Resource, sSearchInfo);
930 //Loop thru dtResult looking for a slot having the required availability type.
931 //If found, set bFinished = true;
932 foreach (DataRow dr in dtResult.Rows)
933 {
934
935 sSlots = dr["SLOTS"].ToString();
936 if (sSlots == "")
937 sSlots = "0";
938 nSlots = Convert.ToInt16(sSlots);
939 if (nSlots > 0)
940 {
941 nAccessTypeID = 0; //holds the access type id of the availability block
942 if (dr["ACCESS_TYPE"].ToString() != "")
943 nAccessTypeID =Convert.ToInt16(dr["ACCESS_TYPE"].ToString());
944 if ((nSearchType == -2) && (nAccessTypeID > 0)) //Match on any non-zero type
945 {
946 bFinished = true;
947 bFound = true;
948 dResult = (DateTime) dr["START_TIME"];
949 break;
950 }
951 if (nAccessTypeID == nAVType)
952 {
953 bFinished = true;
954 bFound = true;
955 dResult = (DateTime) dr["START_TIME"];
956 break;
957 }
958 }
959 }
960 dStart = dEnd.AddDays(1);
961 dEnd = dStart.AddDays(nIncrement);
962 if (dEnd > dtAVEnd)
963 dEnd = dtAVEnd;
964 } while (bFinished == false);
965
966 if (bFound == true)
967 {
968 aCopy.StartTime = dResult;
969 aCopy.EndTime = dResult.AddMinutes(a.Duration);
970 //Create the appointment
971 //Set the AUTOREBOOKED flag
972 //and store the "AutoRebooked To DateTime"
973 //in each autorebooked appointment
974 this.CreateAppointment(aCopy);
975 SetAutoRebook(a, dResult);
976 return "1";
977 }
978 else
979 {
980 return "0";
981 }
982 }
983
984 private void SetAutoRebook(CGAppointment a, DateTime dtRebookedTo)
985 {
986 string sApptKey = a.AppointmentKey.ToString();
987 string sRebookedTo = dtRebookedTo.ToString("M/d/yyyy@HH:mm");
988 string sSql = "BSDX REBOOK SET^" + sApptKey + "^" + sRebookedTo;
989 System.Data.DataTable dtRebook = m_DocManager.RPMSDataTable(sSql, "AutoRebook");
990
991 }
992
993 public string AppointmentNoShow(int nApptID, bool bNoShow)
994 {
995 /*
996 * BSDX NOSHOW RPC Returns 1 in ERRORID if successfully sets NOSHOW flag in BSDX APPOINTMENT and, if applicable, File 2
997 *Otherwise, returns 0 for failure and errormessage in ERRORTXT
998 *THIS routine returns "" if success or the message in ERRORTEXT if failed
999 *Exceptions should be caught by caller
1000 *
1001 */
1002
1003 string sTest = bNoShow.ToString();
1004 string sNoShow = (bNoShow == true)?"1":"0";
1005 string sSql = "BSDX NOSHOW^" + nApptID.ToString();
1006 sSql += "^";
1007 sSql += sNoShow;
1008
1009 DataTable dtAppt = m_DocManager.RPMSDataTable(sSql, "AppointmentNoShow");
1010
1011 Debug.Assert(dtAppt.Rows.Count == 1);
1012 DataRow r = dtAppt.Rows[0];
1013 string sErrorID = r["ERRORID"].ToString();
1014 if (sErrorID != "1")
1015 {
1016 return r["ERRORTEXT"].ToString();
1017 }
1018
1019 bool bRet = RefreshSchedule();
1020
1021 return sErrorID;
1022 }
1023
1024 #endregion Methods
1025
1026 }//End class
1027}//End namespace
Note: See TracBrowser for help on using the repository browser.