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

Last change on this file since 864 was 864, checked in by Sam Habiel, 14 years ago

Fixes for i18n.

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