This is by patient, but you may be able to tweak it for resource:
{! fn timeCompare(t1Hour,t1Minute,t1AMPM,t2Hour,t2Minute,t2AMPM)
{ /* Does NOT compare date, only time */
if (t1AMPM == "PM" and t2AMPM == "AM") then return 1 endif
if (t1AMPM == "AM" and t2AMPM == "PM") then return 2 endif
if (t1Hour > t2Hour) then return 1 endif
if (t1Hour < t2Hour) then return 2 endif
if (t1Minute > t2Minute) then return 1 endif
if (t1Minute < t2Minute) then return 2 endif
return 0
}
}{! fn APPT_NEXT_H1(){
local retVal = ""
local retVal2 = ""
local retVal3 = ""
local i = getRowCount("_MELCurPatientAppt")
local t1Hour
local t1Minute
local t1AMPM
local t2Hour
local t2Minute
local t2AMPM
local currentTime
local apptDateTime
while i > 0 do
i = i-1
local a = getRow("_MELCurPatientAppt",i,"AppointmentsId","ApptStart","ApptTypeId","FacilityId","DoctorID","ResourceId","Status","Canceled","ApptKind")
if (a[8] <> "" or a[9] <> 1)
then continue
endif
/* Appointment type lookup in _MELApptType data object */
if (find("_MELApptType","Name","ApptTypeID",a[3]) <> "") then
a[3] = find("_MELApptType","Name","ApptTypeID",a[3])
endif
/* Facility lookup in _MELLocation data object */
if (find("_MELLocation","ListName","DoctorFacilityID",a[4]) <> "") then
a[4] = find("_MELLocation","ListName","DoctorFacilityID",a[4])
endif
/* Doctor lookup in _DoctorFacilityForPatAppt data object */
if (find("_DoctorFacilityForPatAppt","ListName","DoctorFacilityID",a[5]) <> "") then
a[5] = find("_DoctorFacilityForPatAppt","ListName","DoctorFacilityID",a[5])
endif
/* Resource lookup in _DoctorFacilityForPatAppt data object */
if (find("_DoctorFacilityForPatAppt","ListName","DoctorFacilityID",a[6]) <> "") then
a[6] = find("_DoctorFacilityForPatAppt","ListName","DoctorFacilityID",a[6])
endif
/* ApptStatus is actually a combination of two fields, Canceled or ApptStatus. If Canceled <> "" then
write Canceled, else write contents of ApptStatus
RM- I replaced this with an earlier check, right after the record pull, for speed
if (a[8] <> "" or a[9] <> 1) then
a[7] = "Canceled"
endif */
//Set up the time values for comparison
if size(TIMESTAMP()) == 7 then
currentTime = "0" + TIMESTAMP()
else
currentTime = TIMESTAMP()
endif
if sub(str(a[2]),13,1) == ":" then
apptDateTime = "0" + sub(str(a[2]),12,7)
else
apptDateTime = sub(str(a[2]),12,8)
endif
t1Hour = sub(currentTime, 1, 2)
t1Minute = sub(currentTime, 4, 2)
t1AMPM = sub(currentTime, 7, 2)
t2Hour = sub(apptDateTime, 1, 2)
t2Minute = sub(apptDateTime, 4, 2)
t2AMPM = sub(apptDateTime, 7, 2)
// Build the result if date is in the future
if
(
DURATIONDAYS(sub(str(a[2]),1,10),str(._TODAYSDATE)) > 0
or
(
DURATIONDAYS(sub(str(a[2]),1,10),str(._TODAYSDATE)) == 0
and
timeCompare(t1Hour,t1Minute,t1AMPM,t2Hour,t2Minute,t2AMPM) == 1
)
)
then break
else
// if a[7] <> "Canceled"
// then
retVal3 = retVal2
retVal2 = retVal
retVal = str(a[2]," - ",a[4]," - ",a[6])
// endif
endif
endwhile
if (retVal == "") then
return "Next Appointment: <None>"
endif
if (retVal2 == "") then
return "Next Appointment: " + retVal
endif
if (retVal3 == "") then
return "Next Two Appointments: " + HRET + retVal + HRET + retVal2
else
return "Next Three Appointments: " + HRET + retVal + HRET + retVal2 + HRET + retVal3
endif