Highway to PSR2
[openemr.git] / modules / sms_email_reminder / batch_phone_notification.php
blobf4e36016a577b444aa71634227c4091507f70d1f
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);
28 require_once("../../interface/globals.php");
29 require_once("$srcdir/maviq_phone_api.php");
31 $facilityService = new \services\FacilityService();
33 $type = "Phone";
34 $before_trigger_hours = 72; // 3 days is default
35 //Get the values from Global
36 $before_trigger_hours = $GLOBALS['phone_notification_hour'];
37 //set up the phone notification settings for external phone service
38 $phone_url = $GLOBALS['phone_gateway_url'] ;
39 $phone_id = $GLOBALS['phone_gateway_username'];
40 $phone_token = $GLOBALS['phone_gateway_password'];
41 $phone_time_range = $GLOBALS['phone_time_range'];
43 //get the facility_id-message map
44 $facilities = cron_getFacilitiesMap();
45 //print_r($facilities);
46 $fac_phone_map = $facilities['phone_map'];
47 $fac_msg_map = $facilities['msg_map'];
49 // get patient data for send alert
50 $db_patient = cron_getPhoneAlertpatientData($type, $before_trigger_hours);
51 echo "<br>" . htmlspecialchars(xl("Total Records Found") . ": " . count($db_patient), ENT_QUOTES);
53 //Create a new instance of the phone service client
54 $client = new MaviqClient($phone_id, $phone_token, $phone_url);
56 for ($p=0; $p<count($db_patient); $p++) {
57 $prow =$db_patient[$p];
59 //Get the apptDate and apptTime
60 $p_date = $prow['pc_eventDate'];
61 //Need to format date to m/d/Y for Maviq API
62 $pieces = explode("-", $p_date);
63 $appt_date = date("m/d/Y", mktime(0, 0, 0, $pieces[1], $pieces[2], $pieces[0]));
64 $appt_time = $prow['pc_startTime'];
65 //get the greeting
66 $greeting = $fac_msg_map[$prow['pc_facility']];
67 if ($greeting == null) {
68 //Use the default when the message is not found
69 $greeting = $GLOBALS['phone_appt_message']['Default'];
72 //Set up the parameters for the call
73 $data = array(
74 "firstName" => $prow['fname'],
75 "lastName" => $prow['lname'],
76 "phone" => $prow['phone_home'],
77 "apptDate" => $appt_date,
78 "apptTime" => $appt_time,
79 "doctor" => $prow['pc_aid'],
80 "greeting" => $greeting,
81 "timeRange" => $phone_time_range,
82 "type" => "appointment",
83 "timeZone" => date('P'),
84 "callerId" => $fac_phone_map[$prow['pc_facility']]
87 //Make the call
88 $response = $client->sendRequest("appointment", "POST", $data);
90 // check response for success or error
91 if ($response->IsError) {
92 $strMsg = "Error starting phone call for {$prow['fname']} | {$prow['lname']} | {$prow['phone_home']} | {$appt_date} | {$appt_time} | {$response->ErrorMessage}\n";
93 } else {
94 $strMsg = "\n========================".$type." || ".date("Y-m-d H:i:s")."=========================";
95 $strMsg .= "\nPhone reminder sent successfully: {$prow['fname']} | {$prow['lname']} | | {$prow['phone_home']} | {$appt_date} | {$appt_time} ";
96 // insert entry in notification_log table
97 cron_InsertNotificationLogEntry($prow, $greeting, $phone_url);
99 //update entry >> pc_sendalertsms='Yes'
100 cron_updateentry($type, $prow['pid'], $prow['pc_eid']);
103 //echo $strMsg;
104 WriteLog($strMsg);
107 sqlClose();
109 ////////////////////////////////////////////////////////////////////
110 // Function: cron_updateentry
111 // Purpose: update status yes if alert send to patient
112 ////////////////////////////////////////////////////////////////////
113 function cron_updateentry($type, $pid, $pc_eid)
116 $query = "update openemr_postcalendar_events set ";
118 // larry :: and here again same story - this time for sms pc_sendalertsms - no such field in the table
119 if ($type=='SMS') {
120 $query.=" pc_sendalertsms='YES' ";
121 } elseif ($type=='Email') {
122 $query.=" pc_sendalertemail='YES' ";
123 } //Added by Yijin for phone reminder.. Uses the same field as SMS.
124 elseif ($type=='Phone') {
125 $query.=" pc_sendalertsms='YES' ";
128 $query .=" where pc_pid=? and pc_eid=? ";
129 //echo "<br>".$query;
130 $db_sql = (sqlStatement($query, array($pid, $pc_eid)));
133 ////////////////////////////////////////////////////////////////////
134 // Function: cron_getPhoneAlertpatientData
135 // Purpose: get patient data for send to alert
136 ////////////////////////////////////////////////////////////////////
137 function cron_getPhoneAlertpatientData($type, $trigger_hours)
140 //Added by Yijin 1/12/10 to handle phone reminders. Patient needs to have hipaa Voice flag set to yes and a home phone
141 if ($type=='Phone') {
142 $ssql = " and pd.hipaa_voice='YES' and pd.phone_home<>'' and ope.pc_sendalertsms='NO' and ope.pc_apptstatus != '*' ";
144 $check_date = date("Y-m-d", mktime(date("H")+$trigger_hours, 0, 0, date("m"), date("d"), date("Y")));
147 $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,";
148 $ssql .= " and (ope.pc_eventDate=?)";
150 $query = "select $patient_field pd.pid,ope.pc_eid,ope.pc_pid,ope.pc_title,
151 ope.pc_hometext,ope.pc_eventDate,ope.pc_endDate,
152 ope.pc_duration,ope.pc_alldayevent,ope.pc_startTime,ope.pc_endTime,ope.pc_facility
153 from
154 openemr_postcalendar_events as ope ,patient_data as pd
155 where
156 ope.pc_pid=pd.pid $ssql
157 order by
158 ope.pc_eventDate,ope.pc_endDate,pd.pid";
160 $db_patient = (sqlStatement($query, array($check_date)));
161 $patient_array = array();
162 $cnt=0;
163 while ($prow = sqlFetchArray($db_patient)) {
164 $patient_array[$cnt] = $prow;
165 $cnt++;
168 return $patient_array;
171 ////////////////////////////////////////////////////////////////////
172 // Function: cron_InsertNotificationLogEntry
173 // Purpose: insert log entry in table
174 ////////////////////////////////////////////////////////////////////
175 function cron_InsertNotificationLogEntry($prow, $phone_msg, $phone_gateway)
177 $patient_info = $prow['title']." ".$prow['fname']." ".$prow['mname']." ".$prow['lname']."|||".$prow['phone_home'];
179 $message = $phone_msg;
181 $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 ";
182 $sql_loginsert .= "(NULL , ?, ?, ?, 'Phone', ?, ?, ?, ?, ?, ?, ?)";
183 $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"))));
186 ////////////////////////////////////////////////////////////////////
187 // Function: WriteLog
188 // Purpose: written log into file
189 ////////////////////////////////////////////////////////////////////
190 function WriteLog($data)
192 $log_file = $GLOBALS['phone_reminder_log_dir'];
194 if ($log_file != null) {
195 $filename = $log_file . "/"."phone_reminder_cronlog_".date("Ymd").".html";
197 if (!$fp = fopen($filename, 'a')) {
198 print "Cannot open file ($filename)";
199 } else {
200 $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;
218 //get the facility_name-message map from Globals
219 $message_map = $GLOBALS['phone_appt_message'];
220 //create a new array to store facility_id to message map
221 $facility_msg_map = array();
222 $facility_phone_map = array();
223 //get facilities from the database
224 $fres = $facilityService->getAll();
225 foreach ($fres as $frow) {
226 $facility_msg_map[$frow['id']] = $message_map[$frow['name']];
227 $facility_phone_map[$frow['id']] = $frow['phone'];
230 $facility_map = array(
231 'msg_map' => $facility_msg_map,
232 'phone_map' => $facility_phone_map
235 return $facility_map;