edfc98d4b47b3b5c133fb72c6542890b9bb18930
[openemr.git] / library / appointments.inc.php
blobedfc98d4b47b3b5c133fb72c6542890b9bb18930
1 <?php
3 // Copyright (C) 2011 Ken Chapple
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // Holds library functions (and hashes) used by the appointment reporting module
12 $COMPARE_FUNCTION_HASH = array(
13 'doctor' => 'compareAppointmentsByDoctorName',
14 'patient' => 'compareAppointmentsByPatientName',
15 'pubpid' => 'compareAppointmentsByPatientId',
16 'date' => 'compareAppointmentsByDate',
17 'time' => 'compareAppointmentsByTime',
18 'type' => 'compareAppointmentsByType',
19 'comment' => 'compareAppointmentsByComment',
20 'status' => 'compareAppointmentsByStatus'
23 $ORDERHASH = array(
24 'doctor' => array( 'doctor', 'date', 'time' ),
25 'patient' => array( 'patient', 'date', 'time' ),
26 'pubpid' => array( 'pubpid', 'date', 'time' ),
27 'date' => array( 'date', 'time', 'type', 'patient' ),
28 'time' => array( 'time', 'date', 'patient' ),
29 'type' => array( 'type', 'date', 'time', 'patient' ),
30 'comment' => array( 'comment', 'date', 'time', 'patient' ),
31 'status' => array( 'status', 'date', 'time', 'patient' )
34 function fetchEvents( $from_date, $to_date, $where_param = null, $orderby_param = null )
36 $where =
37 "( (e.pc_endDate >= '$from_date' AND e.pc_eventDate <= '$to_date' AND e.pc_recurrtype = '1') OR " .
38 "(e.pc_eventDate >= '$from_date' AND e.pc_eventDate <= '$to_date') )";
39 if ( $where_param ) $where .= $where_param;
41 $order_by = "e.pc_eventDate, e.pc_startTime";
42 if ( $orderby_param ) {
43 $order_by = $orderby_param;
46 $query = "SELECT " .
47 "e.pc_eventDate, e.pc_endDate, e.pc_startTime, e.pc_endTime, e.pc_duration, e.pc_recurrtype, e.pc_recurrspec, e.pc_recurrfreq, e.pc_catid, e.pc_eid, " .
48 "e.pc_title, e.pc_hometext, e.pc_apptstatus, " .
49 "p.fname, p.mname, p.lname, p.pid, p.pubpid, p.phone_home, p.phone_cell, " .
50 "u.fname AS ufname, u.mname AS umname, u.lname AS ulname, u.id AS uprovider_id, " .
51 "c.pc_catname, c.pc_catid " .
52 "FROM openemr_postcalendar_events AS e " .
53 "LEFT OUTER JOIN patient_data AS p ON p.pid = e.pc_pid " .
54 "LEFT OUTER JOIN users AS u ON u.id = e.pc_aid " .
55 "LEFT OUTER JOIN openemr_postcalendar_categories AS c ON c.pc_catid = e.pc_catid " .
56 "WHERE $where " .
57 "ORDER BY $order_by";
59 $res = sqlStatement( $query );
60 $events = array();
61 if ( $res )
63 while ( $row = sqlFetchArray($res) )
65 // if it's a repeating appointment, fetch all occurances in date range
66 if ( $row['pc_recurrtype'] ) {
67 $reccuringEvents = getRecurringEvents( $row, $from_date, $to_date );
68 $events = array_merge( $events, $reccuringEvents );
69 } else {
70 $events []= $row;
75 return $events;
78 function fetchAllEvents( $from_date, $to_date, $provider_id = null, $facility_id = null )
80 $where = "";
81 if ( $provider_id ) $where .= " AND e.pc_aid = '$provider_id'";
83 $facility_filter = '';
84 if ( $facility_id ) {
85 $event_facility_filter = " AND e.pc_facility = '$facility_id'";
86 $provider_facility_filter = " AND u.facility_id = '$facility_id'";
87 $facility_filter = $event_facility_filter . $provider_facility_filter;
90 $where .= $facility_filter;
91 $appointments = fetchEvents( $from_date, $to_date, $where );
92 return $appointments;
95 function fetchAppointments( $from_date, $to_date, $patient_id = null, $provider_id = null, $facility_id = null, $pc_appstatus = null, $with_out_provider = null, $with_out_facility = null )
97 $where = "";
98 if ( $provider_id ) $where .= " AND e.pc_aid = '$provider_id'";
99 if ( $patient_id ) {
100 $where .= " AND e.pc_pid = '$patient_id'";
101 } else {
102 $where .= " AND e.pc_pid != ''";
105 $facility_filter = '';
106 if ( $facility_id ) {
107 $event_facility_filter = " AND e.pc_facility = '$facility_id'";
108 $provider_facility_filter = " AND u.facility_id = '$facility_id'";
109 $facility_filter = $event_facility_filter . $provider_facility_filter;
112 $where .= $facility_filter;
114 //Appointment Status Checking
115 $filter_appstatus = '';
116 if($pc_appstatus != ''){
117 $filter_appstatus = " AND e.pc_apptstatus = '".$pc_appstatus."'";
119 $where .= $filter_appstatus;
121 //Without Provider checking
122 $filter_woprovider = '';
123 if($with_out_provider != ''){
124 $filter_woprovider = " AND e.pc_aid = ''";
126 $where .= $filter_woprovider;
128 //Without Facility checking
129 $filter_wofacility = '';
130 if($with_out_facility != ''){
131 $filter_wofacility = " AND e.pc_facility = 0";
133 $where .= $filter_wofacility;
135 $appointments = fetchEvents( $from_date, $to_date, $where );
136 return $appointments;
139 function getRecurringEvents( $event, $from_date, $to_date )
141 $repeatEvents = array();
142 $from_date_time = strtotime( $from_date . " 00:00:00" );
143 $thistime = strtotime( $event['pc_eventDate'] . " 00:00:00" );
144 //$thistime = max( $thistime, $from_date_time );
145 if ( $event['pc_recurrtype'] )
147 preg_match( '/"event_repeat_freq_type";s:1:"(\d)"/', $event['pc_recurrspec'], $matches );
148 $repeattype = $matches[1];
150 preg_match( '/"event_repeat_freq";s:1:"(\d)"/', $event['pc_recurrspec'], $matches );
151 $repeatfreq = $matches[1];
152 if ( !$repeatfreq ) $repeatfreq = 1;
154 $upToDate = strtotime( $to_date." 23:59:59" ); // set the up-to-date to the last second of the "to_date"
155 $endtime = strtotime( $event['pc_endDate'] . " 23:59:59" );
156 if ( $endtime > $upToDate ) $endtime = $upToDate;
158 $repeatix = 0;
159 while ( $thistime < $endtime )
161 // Skip the event if a repeat frequency > 1 was specified and this is
162 // not the desired occurrence.
163 if ( !$repeatix ) {
164 $inRange = ( $thistime >= $from_date_time && $thistime < $upToDate );
165 if ( $inRange ) {
166 $newEvent = $event;
167 $eventDate = date( "Y-m-d", $thistime );
168 $newEvent['pc_eventDate'] = $eventDate;
169 $newEvent['pc_endDate'] = $eventDate;
170 $repeatEvents []= $newEvent;
174 if ( ++$repeatix >= $repeatfreq ) $repeatix = 0;
176 $adate = getdate($thistime);
177 if ($repeattype == 0) { // daily
178 $adate['mday'] += 1;
179 } else if ($repeattype == 1) { // weekly
180 $adate['mday'] += 7;
181 } else if ($repeattype == 2) { // monthly
182 $adate['mon'] += 1;
183 } else if ($repeattype == 3) { // yearly
184 $adate['year'] += 1;
185 } else if ($repeattype == 4) { // work days
186 if ($adate['wday'] == 5) // if friday, skip to monday
187 $adate['mday'] += 3;
188 else if ($adate['wday'] == 6) // saturday should not happen
189 $adate['mday'] += 2;
190 else
191 $adate['mday'] += 1;
192 } else {
193 die("Invalid repeat type '$repeattype'");
195 $thistime = mktime(0, 0, 0, $adate['mon'], $adate['mday'], $adate['year']);
199 return $repeatEvents;
202 // get the event slot size in seconds
203 function getSlotSize()
205 if ( isset( $GLOBALS['calendar_interval'] ) ) {
206 return $GLOBALS['calendar_interval'] * 60;
208 return 15 * 60;
211 function getAvailableSlots( $from_date, $to_date, $provider_id = null, $facility_id = null )
213 $appointments = fetchAllEvents( $from_date, $to_date, $provider_id, $facility_id );
214 $appointments = sortAppointments( $appointments, "date" );
215 $from_datetime = strtotime( $from_date." 00:00:00" );
216 $to_datetime = strtotime( $to_date." 23:59:59" );
217 $availableSlots = array();
218 $start_time = 0;
219 $date = 0;
220 for ( $i = 0; $i < count( $appointments ); ++$i )
222 if ( $appointments[$i]['pc_catid'] == 2 ) { // 2 == In Office
223 $start_time = $appointments[$i]['pc_startTime'];
224 $date = $appointments[$i]['pc_eventDate'];
225 $provider_id = $appointments[$i]['uprovider_id'];
226 } else if ( $appointments[$i]['pc_catid'] == 3 ) { // 3 == Out Of Office
227 continue;
228 } else {
229 $start_time = $appointments[$i]['pc_endTime'];
230 $date = $appointments[$i]['pc_eventDate'];
231 $provider_id = $appointments[$i]['uprovider_id'];
234 // find next appointment with the same provider
235 $next_appointment_date = 0;
236 $next_appointment_time = 0;
237 for ( $j = $i+1; $j < count( $appointments ); ++$j ) {
238 if ( $appointments[$j]['uprovider_id'] == $provider_id ) {
239 $next_appointment_date = $appointments[$j]['pc_eventDate'];
240 $next_appointment_time = $appointments[$j]['pc_startTime'];
241 break;
245 $same_day = ( strtotime( $next_appointment_date ) == strtotime( $date ) ) ? true : false;
247 if ( $next_appointment_time && $same_day ) {
248 // check the start time of the next appointment
250 $start_datetime = strtotime( $date." ".$start_time );
251 $next_appointment_datetime = strtotime( $next_appointment_date." ".$next_appointment_time );
252 $curr_time = $start_datetime;
253 while ( $curr_time < $next_appointment_datetime - (getSlotSize() / 2) ) {
254 //create a new appointment ever 15 minutes
255 $time = date( "H:i:s", $curr_time );
256 $available_slot = createAvailableSlot(
257 $appointments[$i]['pc_eventDate'],
258 $time,
259 $appointments[$i]['ufname'],
260 $appointments[$i]['ulname'],
261 $appointments[$i]['umname'] );
262 $availableSlots []= $available_slot;
263 $curr_time += getSlotSize(); // add a 15-minute slot
268 return $availableSlots;
271 function createAvailableSlot( $event_date, $start_time, $provider_fname, $provider_lname, $provider_mname = "", $cat_name = "Available" )
273 $newSlot = array();
274 $newSlot['ulname'] = $provider_lname;
275 $newSlot['ufname'] = $provider_fname;
276 $newSlot['umname'] = $provider_mname;
277 $newSlot['pc_eventDate'] = $event_date;
278 $newSlot['pc_startTime'] = $start_time;
279 $newSlot['pc_endTime'] = $start_time;
280 $newSlot['pc_catname'] = $cat_name;
281 return $newSlot;
284 function getCompareFunction( $code ) {
285 global $COMPARE_FUNCTION_HASH;
286 return $COMPARE_FUNCTION_HASH[$code];
289 function getComparisonOrder( $code ) {
290 global $ORDERHASH;
291 return $ORDERHASH[$code];
294 function sortAppointments( array $appointments, $orderBy = 'date' )
296 global $appointment_sort_order;
297 $appointment_sort_order = $orderBy;
298 usort( $appointments, "compareAppointments" );
299 return $appointments;
302 // cmp_function for usort
303 // The comparison function must return an integer less than, equal to,
304 // or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
305 function compareAppointments( $appointment1, $appointment2 )
307 global $appointment_sort_order;
308 $comparisonOrder = getComparisonOrder( $appointment_sort_order );
309 foreach ( $comparisonOrder as $comparison )
311 $cmp_function = getCompareFunction( $comparison );
312 $result = $cmp_function( $appointment1, $appointment2 );
313 if ( 0 != $result ) {
314 return $result;
318 return 0;
321 function compareBasic( $e1, $e2 )
323 if ( $e1 < $e2 ) {
324 return -1;
325 } else if ( $e1 > $e2 ) {
326 return 1;
329 return 0;
332 function compareAppointmentsByDate( $appointment1, $appointment2 )
334 $date1 = strtotime( $appointment1['pc_eventDate'] );
335 $date2 = strtotime( $appointment2['pc_eventDate'] );
337 return compareBasic( $date1, $date2 );
340 function compareAppointmentsByTime( $appointment1, $appointment2 )
342 $time1 = strtotime( $appointment1['pc_startTime'] );
343 $time2 = strtotime( $appointment2['pc_startTime'] );
345 return compareBasic( $time1, $time2 );
348 function compareAppointmentsByDoctorName( $appointment1, $appointment2 )
350 $name1 = $appointment1['ulname'];
351 $name2 = $appointment2['ulname'];
352 $cmp = compareBasic( $name1, $name2 );
353 if ( $cmp == 0 ) {
354 $name1 = $appointment1['ufname'];
355 $name2 = $appointment2['ufname'];
356 return compareBasic( $name1, $name2 );
359 return $cmp;
362 function compareAppointmentsByPatientName( $appointment1, $appointment2 )
364 $name1 = $appointment1['lname'];
365 $name2 = $appointment2['lname'];
366 $cmp = compareBasic( $name1, $name2 );
367 if ( $cmp == 0 ) {
368 $name1 = $appointment1['fname'];
369 $name2 = $appointment2['fname'];
370 return compareBasic( $name1, $name2 );
373 return $cmp;
376 function compareAppointmentsByType( $appointment1, $appointment2 )
378 $type1 = $appointment1['pc_catid'];
379 $type2 = $appointment2['pc_catid'];
380 return compareBasic( $type1, $type2 );
383 function compareAppointmentsByPatientId( $appointment1, $appointment2 )
385 $id1 = $appointment1['pubpid'];
386 $id2 = $appointment2['pubpid'];
387 return compareBasic( $id1, $id2 );
390 function compareAppointmentsByComment( $appointment1, $appointment2 )
392 $comment1 = $appointment1['pc_hometext'];
393 $comment2 = $appointment2['pc_hometext'];
394 return compareBasic( $comment1, $comment2 );
397 function compareAppointmentsByStatus( $appointment1, $appointment2 )
399 $status1 = $appointment1['pc_apptstatus'];
400 $status2 = $appointment2['pc_apptstatus'];
401 return compareBasic( $status1, $status2 );