Portal Update Forum Request (#2068)
[openemr.git] / interface / batchcom / batch_phone_notification.php
blob05cbdcf9cfbab50e642eba02e27cc07721ae1e89
1 <?php
2 /**
3 * To be run by cron hourly, sending phone reminders
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Brady Miller <brady.g.miller@gmail.com>
8 * @author Matthew Vita <matthewvita48@gmail.com>
9 * @author Jason 'Toolbox' Oettinger <jason@oettinger.email>
10 * @author Maviq
11 * @copyright Copyright (c) 2010 Maviq
12 * @copyright Copyright (c) 2017 Brady Miller <brady.g.miller@gmail.com>
13 * @copyright Copyright (c) 2017 Matthew Vita <matthewvita48@gmail.com>
14 * @copyright Copyright (c) 2017 Jason 'Toolbox' Oettinger <jason@oettinger.email>
15 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
18 // Allow phone notification as a cronjob
19 require_once(dirname(__FILE__, 3)."/library/allow_cronjobs.php");
21 use OpenEMR\Services\FacilityService;
23 $backpic = "";
25 //Set the working directory to the path of the file
26 $current_dir = dirname($_SERVER['SCRIPT_FILENAME']);
27 chdir($current_dir);
31 require_once("../../interface/globals.php");
32 require_once("$srcdir/maviq_phone_api.php");
34 $facilityService = new FacilityService();
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>" . xlt("Total Records Found") . ": " . count($db_patient);
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++) {
60 $prow =$db_patient[$p];
62 //Get the apptDate and apptTime
63 $p_date = $prow['pc_eventDate'];
64 //Need to format date to m/d/Y for Maviq API
65 $pieces = explode("-", $p_date);
66 $appt_date = date("m/d/Y", mktime(0, 0, 0, $pieces[1], $pieces[2], $pieces[0]));
67 $appt_time = $prow['pc_startTime'];
68 //get the greeting
69 $greeting = $fac_msg_map[$prow['pc_facility']];
70 if ($greeting == null) {
71 //Use the default when the message is not found
72 $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";
96 } else {
97 $strMsg = "\n========================".$type." || ".date("Y-m-d H:i:s")."=========================";
98 $strMsg .= "\nPhone reminder sent successfully: {$prow['fname']} | {$prow['lname']} | | {$prow['phone_home']} | {$appt_date} | {$appt_time} ";
99 // insert entry in notification_log table
100 cron_InsertNotificationLogEntry($prow, $greeting, $phone_url);
102 //update entry >> pc_sendalertsms='Yes'
103 cron_updateentry($type, $prow['pid'], $prow['pc_eid']);
106 //echo $strMsg;
107 WriteLog($strMsg);
110 sqlClose();
112 ////////////////////////////////////////////////////////////////////
113 // Function: cron_updateentry
114 // Purpose: update status yes if alert send to patient
115 ////////////////////////////////////////////////////////////////////
116 function cron_updateentry($type, $pid, $pc_eid)
118 $query = "update openemr_postcalendar_events set ";
120 // larry :: and here again same story - this time for sms pc_sendalertsms - no such field in the table
121 if ($type=='SMS') {
122 $query.=" pc_sendalertsms='YES' ";
123 } elseif ($type=='Email') {
124 $query.=" pc_sendalertemail='YES' ";
125 //Added by Yijin for phone reminder.. Uses the same field as SMS.
126 } elseif ($type=='Phone') {
127 $query.=" pc_sendalertsms='YES' ";
130 $query .=" where pc_pid=? and pc_eid=? ";
131 //echo "<br>".$query;
132 $db_sql = (sqlStatement($query, array($pid, $pc_eid)));
135 ////////////////////////////////////////////////////////////////////
136 // Function: cron_getPhoneAlertpatientData
137 // Purpose: get patient data for send to alert
138 ////////////////////////////////////////////////////////////////////
139 function cron_getPhoneAlertpatientData($type, $trigger_hours)
142 //Added by Yijin 1/12/10 to handle phone reminders. Patient needs to have hipaa Voice flag set to yes and a home phone
143 if ($type == 'Phone') {
144 $ssql = " and pd.hipaa_voice='YES' and pd.phone_home<>'' and ope.pc_sendalertsms='NO' and ope.pc_apptstatus != '*' ";
146 $check_date = date("Y-m-d", mktime(date("H")+$trigger_hours, 0, 0, date("m"), date("d"), date("Y")));
149 $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,";
150 $ssql .= " and (ope.pc_eventDate=?)";
152 $query = "select $patient_field pd.pid,ope.pc_eid,ope.pc_pid,ope.pc_title,
153 ope.pc_hometext,ope.pc_eventDate,ope.pc_endDate,
154 ope.pc_duration,ope.pc_alldayevent,ope.pc_startTime,ope.pc_endTime,ope.pc_facility
155 from
156 openemr_postcalendar_events as ope ,patient_data as pd
157 where
158 ope.pc_pid=pd.pid $ssql
159 order by
160 ope.pc_eventDate,ope.pc_endDate,pd.pid";
162 $db_patient = (sqlStatement($query, array($check_date)));
163 $patient_array = array();
164 $cnt=0;
165 while ($prow = sqlFetchArray($db_patient)) {
166 $patient_array[$cnt] = $prow;
167 $cnt++;
170 return $patient_array;
173 ////////////////////////////////////////////////////////////////////
174 // Function: cron_InsertNotificationLogEntry
175 // Purpose: insert log entry in table
176 ////////////////////////////////////////////////////////////////////
177 function cron_InsertNotificationLogEntry($prow, $phone_msg, $phone_gateway)
179 $patient_info = $prow['title']." ".$prow['fname']." ".$prow['mname']." ".$prow['lname']."|||".$prow['phone_home'];
181 $message = $phone_msg;
183 $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 ";
184 $sql_loginsert .= "(NULL , ?, ?, ?, 'Phone', ?, ?, ?, ?, ?, ?, ?)";
185 $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'))));
188 ////////////////////////////////////////////////////////////////////
189 // Function: WriteLog
190 // Purpose: written log into file
191 ////////////////////////////////////////////////////////////////////
192 function WriteLog($data)
194 $log_file = $GLOBALS['phone_reminder_log_dir'];
196 if ($log_file != null) {
197 $filename = $log_file . "/"."phone_reminder_cronlog_".date("Ymd").".html";
198 if (!$fp = fopen($filename, 'a')) {
199 print "Cannot open file ($filename)";
200 } else {
201 $sdata = "\n====================================================================\n";
202 if (!fwrite($fp, $data.$sdata)) {
203 print "Cannot write to file ($filename)";
206 fclose($fp);
210 ////////////////////////////////////////////////////////////////////
211 // Function: cron_getFacilities
212 // Purpose: get facilities data once and store in map
213 ////////////////////////////////////////////////////////////////////
214 function cron_getFacilitiesMap()
216 global $facilityService;
217 //get the facility_name-message map from Globals
218 $message_map = $GLOBALS['phone_appt_message'];
219 //create a new array to store facility_id to message map
220 $facility_msg_map = array();
221 $facility_phone_map = array();
222 //get facilities from the database
224 $facilities = $facilityService->getAll();
225 foreach ($facilities as $prow) {
226 $facility_msg_map[$prow['id']] = $message_map[$prow['name']];
227 $facility_phone_map[$prow['id']] = $prow['phone'];
230 $facility_map = array(
231 'msg_map' => $facility_msg_map,
232 'phone_map' => $facility_phone_map
235 return $facility_map;