Integrate traditional and layout-based forms into their chosen categories in the...
[openemr.git] / library / patient_tracker.inc.php
blob5a38073c82f13300c76e7d19b0cf54ce65580141
1 <?php
3 /**
4 * library/patient_tracker.inc.php Functions used in the Patient Flow Board.
6 * Functions for use in the Patient Flow Board and Patient Flow Board Reports.
9 * Copyright (C) 2015 Terry Hill <terry@lillysystems.com>
11 * LICENSE: This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 3
14 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
22 * @package OpenEMR
23 * @author Terry Hill <terry@lillysystems.com>
24 * @link https://www.open-emr.org
26 * Please help the overall project by sending changes you make to the author and to the OpenEMR community.
30 require_once(dirname(__FILE__) . '/appointments.inc.php');
32 function get_Tracker_Time_Interval($tracker_from_time, $tracker_to_time, $allow_sec = false)
35 $tracker_time_calc = strtotime($tracker_to_time) - strtotime($tracker_from_time);
37 $tracker_time = "";
38 if ($tracker_time_calc > 60 * 60 * 24) {
39 $days = floor($tracker_time_calc / 60 / 60 / 24);
40 if ($days >= 2) {
41 $tracker_time .= "$days " . xl('days');
42 } else {
43 $tracker_time .= "$days " . xl('day');
46 $tracker_time_calc = $tracker_time_calc - ($days * (60 * 60 * 24));
49 if ($tracker_time_calc > 60 * 60) {
50 $hours = floor($tracker_time_calc / 60 / 60);
51 if (strlen($days != 0)) {
52 if ($hours >= 2) {
53 $tracker_time .= ", $hours " . xl('hours');
54 } else {
55 $tracker_time .= ", $hours " . xl('hour');
57 } else {
58 if ($hours >= 2) {
59 $tracker_time .= "$hours " . xl('hours');
60 } else {
61 $tracker_time .= "$hours " . xl('hour');
65 $tracker_time_calc = $tracker_time_calc - ($hours * (60 * 60));
68 if ($allow_sec) {
69 if ($tracker_time_calc > 60) {
70 $minutes = floor($tracker_time_calc / 60);
71 if (strlen($hours != 0)) {
72 if ($minutes >= 2) {
73 $tracker_time .= ", $minutes " . xl('minutes');
74 } else {
75 $tracker_time .= ", $minutes " . xl('minute');
77 } else {
78 if ($minutes >= 2) {
79 $tracker_time .= "$minutes " . xl('minutes');
80 } else {
81 $tracker_time .= "$minutes " . xl('minute');
85 $tracker_time_calc = $tracker_time_calc - ($minutes * 60);
87 } else {
88 $minutes = round($tracker_time_calc / 60);
89 if (!empty($hours) && strlen($hours != 0)) {
90 if ($minutes >= 2) {
91 $tracker_time .= ", $minutes " . xl('minutes');
92 } else {
93 $tracker_time .= ", $minutes " . xl('minute');
95 } else {
96 if ($minutes >= 2) {
97 $tracker_time .= "$minutes " . xl('minutes');
98 } else {
99 if ($minutes > 0) {
100 $tracker_time .= "$minutes " . xl('minute');
105 $tracker_time_calc = $tracker_time_calc - ($minutes * 60);
108 if ($allow_sec) {
109 if ($tracker_time_calc > 0) {
110 if (strlen($minutes != 0)) {
111 if ($tracker_time_calc >= 2) {
112 $tracker_time .= ", $tracker_time_calc " . xl('seconds');
113 } else {
114 $tracker_time .= ", $tracker_time_calc " . xl('second');
116 } else {
117 if ($tracker_time_calc >= 2) {
118 $tracker_time .= "$tracker_time_calc " . xl('seconds');
119 } else {
120 $tracker_time .= "$tracker_time_calc " . xl('second');
126 return $tracker_time ;
129 function fetch_Patient_Tracker_Events($from_date, $to_date, $provider_id = null, $facility_id = null, $form_apptstatus = null, $form_apptcat = null, $form_patient_name = null, $form_patient_id = null)
131 # used to determine which providers to display in the Patient Tracker
132 if ($provider_id == 'ALL') {
133 //set null to $provider id if it's 'all'
134 $provider_id = null;
137 $events = fetchAppointments($from_date, $to_date, $form_patient_id, $provider_id, $facility_id, $form_apptstatus, null, null, $form_apptcat, true, 0, null, $form_patient_name);
138 return $events;
141 #check to see if a status code exist as a check in
142 function is_checkin($option)
145 $row = sqlQuery("SELECT toggle_setting_1 FROM list_options WHERE " .
146 "list_id = 'apptstat' AND option_id = ? AND activity = 1", array($option));
147 if (empty($row['toggle_setting_1'])) {
148 return(false);
151 return(true);
154 #check to see if a status code exist as a check out
155 function is_checkout($option)
158 $row = sqlQuery("SELECT toggle_setting_2 FROM list_options WHERE " .
159 "list_id = 'apptstat' AND option_id = ? AND activity = 1", array($option));
160 if (empty($row['toggle_setting_2'])) {
161 return(false);
164 return(true);
168 # This function will return false for both below scenarios:
169 # 1. The tracker item does not exist
170 # 2. If the tracker item does exist, but the encounter has not been set
171 function is_tracker_encounter_exist($apptdate, $appttime, $pid, $eid)
173 #Check to see if there is an encounter in the patient_tracker table.
174 $enc_yn = sqlQuery("SELECT encounter from patient_tracker WHERE `apptdate` = ? AND encounter > 0 " .
175 "AND `eid` = ? AND `pid` = ?", array($apptdate, $eid, $pid));
176 if (empty($enc_yn['encounter']) || $enc_yn === false) {
177 return (0);
180 return ($enc_yn['encounter']);
183 # this function will return the tracker id that is managed
184 # or will return false if no tracker id was managed (in the case of a recurrent appointment)
185 function manage_tracker_status($apptdate, $appttime, $eid, $pid, $user, $status = '', $room = '', $enc_id = '')
188 #First ensure the eid is not a recurrent appointment. If it is, then do not do anything and return false.
189 $pc_appt = sqlQuery("SELECT `pc_recurrtype` FROM `openemr_postcalendar_events` WHERE `pc_eid` = ?", array($eid));
190 if ($pc_appt['pc_recurrtype'] != 0) {
191 return false;
194 $datetime = date("Y-m-d H:i:s");
195 if (is_null($room)) {
196 $room = '';
199 #Check to see if there is an entry in the patient_tracker table.
200 $tracker = sqlQuery("SELECT id, apptdate, appttime, eid, pid, original_user, encounter, lastseq," .
201 "patient_tracker_element.room AS lastroom,patient_tracker_element.status AS laststatus " .
202 "from `patient_tracker`" .
203 "LEFT JOIN patient_tracker_element " .
204 "ON patient_tracker.id = patient_tracker_element.pt_tracker_id " .
205 "AND patient_tracker.lastseq = patient_tracker_element.seq " .
206 "WHERE `apptdate` = ? AND `appttime` = ? " .
207 "AND `eid` = ? AND `pid` = ?", array($apptdate,$appttime,$eid,$pid));
209 if (empty($tracker)) {
210 #Add a new tracker.
211 $tracker_id = sqlInsert(
212 "INSERT INTO `patient_tracker` " .
213 "(`date`, `apptdate`, `appttime`, `eid`, `pid`, `original_user`, `encounter`, `lastseq`) " .
214 "VALUES (?,?,?,?,?,?,?,'1')",
215 array($datetime,$apptdate,$appttime,$eid,$pid,$user,$enc_id)
217 #If there is a status or a room, then add a tracker item.
218 if (!empty($status) || !empty($room)) {
219 sqlStatement(
220 "INSERT INTO `patient_tracker_element` " .
221 "(`pt_tracker_id`, `start_datetime`, `user`, `status`, `room`, `seq`) " .
222 "VALUES (?,?,?,?,?,'1')",
223 array($tracker_id,$datetime,$user,$status,$room)
226 } else {
227 #Tracker already exists.
228 $tracker_id = $tracker['id'];
229 if (($status != $tracker['laststatus']) || ($room != $tracker['lastroom'])) {
230 #Status or room has changed, so need to update tracker.
231 #Update lastseq in tracker.
232 sqlStatement(
233 "UPDATE `patient_tracker` SET `lastseq` = ? WHERE `id` = ?",
234 array(($tracker['lastseq'] + 1),$tracker_id)
236 #Add a tracker item.
237 sqlStatement(
238 "INSERT INTO `patient_tracker_element` " .
239 "(`pt_tracker_id`, `start_datetime`, `user`, `status`, `room`, `seq`) " .
240 "VALUES (?,?,?,?,?,?)",
241 array($tracker_id,$datetime,$user,$status,$room,($tracker['lastseq'] + 1))
245 if (!empty($enc_id)) {
246 #enc_id (encounter number) is not blank, so update this in tracker.
247 sqlStatement("UPDATE `patient_tracker` SET `encounter` = ? WHERE `id` = ?", array($enc_id,$tracker_id));
251 #Ensure the entry in calendar appt entry has been updated.
252 $pc_appt = sqlQuery("SELECT `pc_apptstatus`, `pc_room` FROM `openemr_postcalendar_events` WHERE `pc_eid` = ?", array($eid));
253 if ($status != $pc_appt['pc_apptstatus']) {
254 sqlStatement("UPDATE `openemr_postcalendar_events` SET `pc_apptstatus` = ? WHERE `pc_eid` = ?", array($status,$eid));
257 if ($room != $pc_appt['pc_room']) {
258 sqlStatement("UPDATE `openemr_postcalendar_events` SET `pc_room` = ? WHERE `pc_eid` = ?", array($room,$eid));
261 if ($GLOBALS['drug_screen'] && !empty($status) && is_checkin($status)) {
262 $yearly_limit = $GLOBALS['maximum_drug_test_yearly'];
263 $percentage = $GLOBALS['drug_testing_percentage'];
264 random_drug_test($tracker_id, $percentage, $yearly_limit);
267 # Returning the tracker id that has been managed
268 return $tracker_id;
271 # This is used to break apart the information contained in the notes field of
272 #list_options. Currently the color and alert time are the only items stored
273 function collectApptStatusSettings($option)
275 $color_settings = array();
276 $row = sqlQuery("SELECT notes FROM list_options WHERE " .
277 "list_id = 'apptstat' AND option_id = ? AND activity = 1", array($option));
278 if (empty($row['notes'])) {
279 return $option;
282 list($color_settings['color'], $color_settings['time_alert']) = explode("|", $row['notes']);
283 return $color_settings;
286 # This is used to collect the tracker elements for the Patient Flow Board Report
287 # returns the elements in an array
288 function collect_Tracker_Elements($trackerid)
290 $res = sqlStatement("SELECT * FROM patient_tracker_element WHERE pt_tracker_id = ? ORDER BY LENGTH(seq), seq ", array($trackerid));
291 for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
292 $returnval[$iter] = $row;
295 return $returnval;
298 #used to determine check in time
299 function collect_checkin($trackerid)
301 $tracker = sqlQuery(
302 "SELECT patient_tracker_element.start_datetime " .
303 "FROM patient_tracker_element " .
304 "INNER JOIN list_options " .
305 "ON patient_tracker_element.status = list_options.option_id " .
306 "WHERE list_options.list_id = 'apptstat' " .
307 "AND list_options.toggle_setting_1 = '1' AND list_options.activity = 1 " .
308 "AND patient_tracker_element.pt_tracker_id = ?",
309 array($trackerid)
311 if (empty($tracker['start_datetime'])) {
312 return false;
313 } else {
314 return $tracker['start_datetime'];
318 #used to determine check out time
319 function collect_checkout($trackerid)
321 $tracker = sqlQuery(
322 "SELECT patient_tracker_element.start_datetime " .
323 "FROM patient_tracker_element " .
324 "INNER JOIN list_options " .
325 "ON patient_tracker_element.status = list_options.option_id " .
326 "WHERE list_options.list_id = 'apptstat' " .
327 "AND list_options.toggle_setting_2 = '1' AND list_options.activity = 1 " .
328 "AND patient_tracker_element.pt_tracker_id = ?",
329 array($trackerid)
331 if (empty($tracker['start_datetime'])) {
332 return false;
333 } else {
334 return $tracker['start_datetime'];
338 function random_drug_test($tracker_id, $percentage, $yearly_limit)
341 # Check if randomization has not yet been done (is random_drug_test NULL). If already done, then exit.
342 $drug_test_done = sqlQuery("SELECT `random_drug_test`, pid from patient_tracker " .
343 "WHERE id =? ", array($tracker_id));
344 $Patient_id = $drug_test_done['pid'];
346 if (is_null($drug_test_done['random_drug_test'])) {
347 # get a count of the number of times the patient has been screened.
348 if ($yearly_limit > 0) {
349 # check to see if screens are within the current year.
350 $lastyear = date("Y-m-d", strtotime("-1 year", strtotime(date("Y-m-d H:i:s"))));
351 $drug_test_count = sqlQuery("SELECT COUNT(*) from patient_tracker " .
352 "WHERE drug_screen_completed = '1' AND apptdate >= ? AND pid =? ", array($lastyear,$Patient_id));
355 # check that the patient is not at the yearly limit.
356 if ($drug_test_count['COUNT(*)'] >= $yearly_limit && ($yearly_limit > 0)) {
357 $drugtest = 0;
358 } else {
359 # Now do the randomization and set random_drug_test to the outcome.
361 $drugtest = 0;
362 $testdrug = mt_rand(0, 100);
363 if ($testdrug <= $percentage) {
364 $drugtest = 1;
368 #Update the tracker file.
369 sqlStatement("UPDATE patient_tracker SET " .
370 "random_drug_test = ? " .
371 "WHERE id =? ", array($drugtest,$tracker_id));
375 /* get information the statuses of the appointments*/
376 function getApptStatus($appointments)
379 $astat = array();
380 $astat['count_all'] = count($appointments);
381 //group the appointment by status
382 foreach ($appointments as $appointment) {
383 $astat[$appointment['pc_apptstatus']] += 1;
386 return $astat;