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

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

CGDocument:

  • Better constructor to set default values.
  • Removed OnNewDocument and used constructor plus extra arguments instead.
  • Added IsRefreshNeeded method to see if we need to get data from server.

CGDocumentManager:

  • Refactored OnNewDocument out (was going to use Application.Run with the CGView form as argument, that didn't work for re-logging in)
  • Refactored all ChangeServer and ChangeLogin handlers b/c they got broken with previous work.

CGView:

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