Acknowledgements page update
[openemr.git] / modules / sms_email_reminder / batch_phone_notification.php
blob3f065538d72cfdaecd8ca067908cf91470ad7aed
1 <?php
2 // Copyright (C) 2010 Maviq <info@maviq.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 ////////////////////////////////////////////////////////////////////
10 // Package: cron_phone_notification
11 // Purpose: to be run by cron every hour, look for appointments
12 // in the pre-notification period and send an phone reminder
13 // Based on cron_email_notification by Larry Lart
14 // Created by:
15 // Updated by: Maviq on 01/12/2010
16 ////////////////////////////////////////////////////////////////////
18 $backpic = "";
19 //phone notification
20 $ignoreAuth=1;
22 //Set the working directory to the path of the file
23 $current_dir = dirname($_SERVER['SCRIPT_FILENAME']);
24 chdir($current_dir);
26 //SANITIZE ALL ESCAPES
27 $sanitize_all_escapes=true;
29 //STOP FAKE REGISTER GLOBALS
30 $fake_register_globals=false;
32 require_once("../../interface/globals.php");
33 require_once("$srcdir/maviq_phone_api.php");
34 require_once("$srcdir/formdata.inc.php");
36 $type = "Phone";
37 $before_trigger_hours = 72; // 3 days is default
38 //Get the values from Global
39 $before_trigger_hours = $GLOBALS['phone_notification_hour'];
40 //set up the phone notification settings for external phone service
41 $phone_url = $GLOBALS['phone_gateway_url'] ;
42 $phone_id = $GLOBALS['phone_gateway_username'];
43 $phone_token = $GLOBALS['phone_gateway_password'];
44 $phone_time_range = $GLOBALS['phone_time_range'];
46 //get the facility_id-message map
47 $facilities = cron_getFacilitiesMap();
48 //print_r($facilities);
49 $fac_phone_map = $facilities['phone_map'];
50 $fac_msg_map = $facilities['msg_map'];
52 // get patient data for send alert
53 $db_patient = cron_getPhoneAlertpatientData($type, $before_trigger_hours);
54 echo "<br>" . htmlspecialchars( xl("Total Records Found") . ": " . count($db_patient), ENT_QUOTES);
56 //Create a new instance of the phone service client
57 $client = new MaviqClient($phone_id, $phone_token, $phone_url);
59 for($p=0;$p<count($db_patient);$p++)
61 $prow =$db_patient[$p];
63 //Get the apptDate and apptTime
64 $p_date = $prow['pc_eventDate'];
65 //Need to format date to m/d/Y for Maviq API
66 $pieces = explode("-",$p_date);
67 $appt_date = date("m/d/Y", mktime( 0,0,0,$pieces[1],$pieces[2],$pieces[0]));
68 $appt_time = $prow['pc_startTime'];
69 //get the greeting
70 $greeting = $fac_msg_map[$prow['pc_facility']];
71 if ($greeting == null) {
72 //Use the default when the message is not found
73 $greeting = $GLOBALS['phone_appt_message']['Default'];
75 //Set up the parameters for the call
76 $data = array(
77 "firstName" => $prow['fname'],
78 "lastName" => $prow['lname'],
79 "phone" => $prow['phone_home'],
80 "apptDate" => $appt_date,
81 "apptTime" => $appt_time,
82 "doctor" => $prow['pc_aid'],
83 "greeting" => $greeting,
84 "timeRange" => $phone_time_range,
85 "type" => "appointment",
86 "timeZone" => date('P'),
87 "callerId" => $fac_phone_map[$prow['pc_facility']]
90 //Make the call
91 $response = $client->sendRequest("appointment", "POST", $data);
93 // check response for success or error
94 if($response->IsError) {
95 $strMsg = "Error starting phone call for {$prow['fname']} | {$prow['lname']} | {$prow['phone_home']} | {$appt_date} | {$appt_time} | {$response->ErrorMessage}\n";
97 else {
98 $strMsg = "\n========================".$type." || ".date("Y-m-d H:i:s")."=========================";
99 $strMsg .= "\nPhone reminder sent successfully: {$prow['fname']} | {$prow['lname']} | | {$prow['phone_home']} | {$appt_date} | {$appt_time} ";
100 // insert entry in notification_log table
101 cron_InsertNotificationLogEntry($prow,$greeting,$phone_url);
103 //update entry >> pc_sendalertsms='Yes'
104 cron_updateentry($type,$prow['pid'],$prow['pc_eid']);
108 //echo $strMsg;
109 WriteLog( $strMsg );
113 sqlClose();
115 ////////////////////////////////////////////////////////////////////
116 // Function: cron_updateentry
117 // Purpose: update status yes if alert send to patient
118 ////////////////////////////////////////////////////////////////////
119 function cron_updateentry($type,$pid,$pc_eid)
122 $query = "update openemr_postcalendar_events set ";
124 // larry :: and here again same story - this time for sms pc_sendalertsms - no such field in the table
125 if($type=='SMS')
126 $query.=" pc_sendalertsms='YES' ";
127 elseif ($type=='Email')
128 $query.=" pc_sendalertemail='YES' ";
129 //Added by Yijin for phone reminder.. Uses the same field as SMS.
130 elseif($type=='Phone')
131 $query.=" pc_sendalertsms='YES' ";
133 $query .=" where pc_pid=? and pc_eid=? ";
134 //echo "<br>".$query;
135 $db_sql = (sqlStatement($query, array($pid, $pc_eid)));
138 ////////////////////////////////////////////////////////////////////
139 // Function: cron_getPhoneAlertpatientData
140 // Purpose: get patient data for send to alert
141 ////////////////////////////////////////////////////////////////////
142 function cron_getPhoneAlertpatientData( $type, $trigger_hours )
145 //Added by Yijin 1/12/10 to handle phone reminders. Patient needs to have hipaa Voice flag set to yes and a home phone
146 if($type=='Phone'){
147 $ssql = " and pd.hipaa_voice='YES' and pd.phone_home<>'' and ope.pc_sendalertsms='NO' and ope.pc_apptstatus != '*' ";
149 $check_date = date("Y-m-d", mktime(date("H")+$trigger_hours, 0, 0, date("m"), date("d"), date("Y")));
153 $patient_field = "pd.pid,pd.title,pd.fname,pd.lname,pd.mname,pd.phone_cell,pd.email,pd.hipaa_allowsms,pd.hipaa_allowemail,pd.phone_home,pd.hipaa_voice,";
154 $ssql .= " and (ope.pc_eventDate=?)";
156 $query = "select $patient_field pd.pid,ope.pc_eid,ope.pc_pid,ope.pc_title,
157 ope.pc_hometext,ope.pc_eventDate,ope.pc_endDate,
158 ope.pc_duration,ope.pc_alldayevent,ope.pc_startTime,ope.pc_endTime,ope.pc_facility
159 from
160 openemr_postcalendar_events as ope ,patient_data as pd
161 where
162 ope.pc_pid=pd.pid $ssql
163 order by
164 ope.pc_eventDate,ope.pc_endDate,pd.pid";
166 $db_patient = (sqlStatement($query, array($check_date)));
167 $patient_array = array();
168 $cnt=0;
169 while ($prow = sqlFetchArray($db_patient))
171 $patient_array[$cnt] = $prow;
172 $cnt++;
174 return $patient_array;
177 ////////////////////////////////////////////////////////////////////
178 // Function: cron_InsertNotificationLogEntry
179 // Purpose: insert log entry in table
180 ////////////////////////////////////////////////////////////////////
181 function cron_InsertNotificationLogEntry($prow,$phone_msg,$phone_gateway)
183 $patient_info = $prow['title']." ".$prow['fname']." ".$prow['mname']." ".$prow['lname']."|||".$prow['phone_home'];
185 $message = $phone_msg;
187 $sql_loginsert = "INSERT INTO `notification_log` ( `iLogId` , `pid` , `pc_eid` , `message`, `type` , `patient_info` , `smsgateway_info` , `pc_eventDate` , `pc_endDate` , `pc_startTime` , `pc_endTime` , `dSentDateTime` ) VALUES ";
188 $sql_loginsert .= "(NULL , ?, ?, ?, 'Phone', ?, ?, ?, ?, ?, ?, ?)";
189 $db_loginsert = ( sqlStatement( $sql_loginsert, array($prow[pid], $prow[pc_eid], $message, $patient_info, $phone_gateway, $prow[pc_eventDate], $prow[pc_endDate], $prow[pc_startTime], $prow[pc_endTime], date("Y-m-d H:i:s"))));
192 ////////////////////////////////////////////////////////////////////
193 // Function: WriteLog
194 // Purpose: written log into file
195 ////////////////////////////////////////////////////////////////////
196 function WriteLog( $data )
198 $log_file = $GLOBALS['phone_reminder_log_dir'];
200 if ($log_file != null) {
202 $filename = $log_file . "/"."phone_reminder_cronlog_".date("Ymd").".html";
204 if (!$fp = fopen($filename, 'a'))
206 print "Cannot open file ($filename)";
208 }else {
210 $sdata = "\n====================================================================\n";
212 if (!fwrite($fp, $data.$sdata))
214 print "Cannot write to file ($filename)";
217 fclose($fp);
221 ////////////////////////////////////////////////////////////////////
222 // Function: cron_getFacilities
223 // Purpose: get facilities data once and store in map
224 ////////////////////////////////////////////////////////////////////
225 function cron_getFacilitiesMap()
227 //get the facility_name-message map from Globals
228 $message_map = $GLOBALS['phone_appt_message'];
229 //create a new array to store facility_id to message map
230 $facility_msg_map = array();
231 $facility_phone_map = array();
232 //get facilities from the database
233 $query = "select fac.id, fac.name, fac.phone from facility as fac";
234 $db_res = (sqlStatement($query));
235 while ($prow = sqlFetchArray($db_res))
237 $facility_msg_map[$prow['id']] = $message_map[$prow['name']];
238 $facility_phone_map[$prow['id']] = $prow['phone'];
241 $facility_map = array(
242 'msg_map' => $facility_msg_map,
243 'phone_map' => $facility_phone_map
246 return $facility_map;