various bugs (#4331)
[openemr.git] / interface / batchcom / batch_phone_notification.php
blob5d918af4e76c185ea714acde10c630e9ccdeb5d3
1 <?php
3 /**
4 * To be run by cron hourly, sending phone reminders
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @author Matthew Vita <matthewvita48@gmail.com>
10 * @author Jason 'Toolbox' Oettinger <jason@oettinger.email>
11 * @author Maviq
12 * @copyright Copyright (c) 2010 Maviq
13 * @copyright Copyright (c) 2017 Brady Miller <brady.g.miller@gmail.com>
14 * @copyright Copyright (c) 2017 Matthew Vita <matthewvita48@gmail.com>
15 * @copyright Copyright (c) 2017 Jason 'Toolbox' Oettinger <jason@oettinger.email>
16 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
19 // Allow phone notification as a cronjob
20 require_once(dirname(__FILE__, 3) . "/library/allow_cronjobs.php");
22 $backpic = "";
24 //Set the working directory to the path of the file
25 $current_dir = dirname($_SERVER['SCRIPT_FILENAME']);
26 chdir($current_dir);
28 require_once("../../interface/globals.php");
29 require_once("$srcdir/maviq_phone_api.php");
31 use OpenEMR\Common\Crypto\CryptoGen;
32 use OpenEMR\Services\FacilityService;
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 $cryptoGen = new CryptoGen();
44 $phone_token = $cryptoGen->decryptStandard($GLOBALS['phone_gateway_password']);
45 $phone_time_range = $GLOBALS['phone_time_range'];
47 //get the facility_id-message map
48 $facilities = cron_getFacilitiesMap();
49 //print_r($facilities);
50 $fac_phone_map = $facilities['phone_map'];
51 $fac_msg_map = $facilities['msg_map'];
53 // get patient data for send alert
54 $db_patient = cron_getPhoneAlertpatientData($type, $before_trigger_hours);
55 echo "<br />" . xlt("Total Records Found") . ": " . count($db_patient);
57 //Create a new instance of the phone service client
58 $client = new MaviqClient($phone_id, $phone_token, $phone_url);
60 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'];
76 //Set up the parameters for the call
77 $data = array(
78 "firstName" => $prow['fname'],
79 "lastName" => $prow['lname'],
80 "phone" => $prow['phone_home'],
81 "apptDate" => $appt_date,
82 "apptTime" => $appt_time,
83 "doctor" => $prow['pc_aid'],
84 "greeting" => $greeting,
85 "timeRange" => $phone_time_range,
86 "type" => "appointment",
87 "timeZone" => date('P'),
88 "callerId" => $fac_phone_map[$prow['pc_facility']]
91 //Make the call
92 $response = $client->sendRequest("appointment", "POST", $data);
94 // check response for success or error
95 if ($response->IsError) {
96 $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']);
107 //echo $strMsg;
108 WriteLog($strMsg);
111 sqlClose();
113 ////////////////////////////////////////////////////////////////////
114 // Function: cron_updateentry
115 // Purpose: update status yes if alert send to patient
116 ////////////////////////////////////////////////////////////////////
117 function cron_updateentry($type, $pid, $pc_eid)
119 $query = "update openemr_postcalendar_events set ";
121 // larry :: and here again same story - this time for sms pc_sendalertsms - no such field in the table
122 if ($type == 'SMS') {
123 $query .= " pc_sendalertsms='YES' ";
124 } elseif ($type == 'Email') {
125 $query .= " pc_sendalertemail='YES' ";
126 //Added by Yijin for phone reminder.. Uses the same field as SMS.
127 } elseif ($type == 'Phone') {
128 $query .= " pc_sendalertsms='YES' ";
131 $query .= " where pc_pid=? and pc_eid=? ";
132 //echo "<br />".$query;
133 $db_sql = (sqlStatement($query, array($pid, $pc_eid)));
136 ////////////////////////////////////////////////////////////////////
137 // Function: cron_getPhoneAlertpatientData
138 // Purpose: get patient data for send to alert
139 ////////////////////////////////////////////////////////////////////
140 function cron_getPhoneAlertpatientData($type, $trigger_hours)
143 //Added by Yijin 1/12/10 to handle phone reminders. Patient needs to have hipaa Voice flag set to yes and a home phone
144 if ($type == 'Phone') {
145 $ssql = " and pd.hipaa_voice='YES' and pd.phone_home<>'' and ope.pc_sendalertsms='NO' and ope.pc_apptstatus != '*' ";
147 $check_date = date("Y-m-d", mktime(date("H") + $trigger_hours, 0, 0, date("m"), date("d"), date("Y")));
150 $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,";
151 $ssql .= " and (ope.pc_eventDate=?)";
153 $query = "select $patient_field pd.pid,ope.pc_eid,ope.pc_pid,ope.pc_title,
154 ope.pc_hometext,ope.pc_eventDate,ope.pc_endDate,
155 ope.pc_duration,ope.pc_alldayevent,ope.pc_startTime,ope.pc_endTime,ope.pc_facility
156 from
157 openemr_postcalendar_events as ope ,patient_data as pd
158 where
159 ope.pc_pid=pd.pid $ssql
160 order by
161 ope.pc_eventDate,ope.pc_endDate,pd.pid";
163 $db_patient = (sqlStatement($query, array($check_date)));
164 $patient_array = array();
165 $cnt = 0;
166 while ($prow = sqlFetchArray($db_patient)) {
167 $patient_array[$cnt] = $prow;
168 $cnt++;
171 return $patient_array;
174 ////////////////////////////////////////////////////////////////////
175 // Function: cron_InsertNotificationLogEntry
176 // Purpose: insert log entry in table
177 ////////////////////////////////////////////////////////////////////
178 function cron_InsertNotificationLogEntry($prow, $phone_msg, $phone_gateway)
180 $patient_info = $prow['title'] . " " . $prow['fname'] . " " . $prow['mname'] . " " . $prow['lname'] . "|||" . $prow['phone_home'];
182 $message = $phone_msg;
184 $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 ";
185 $sql_loginsert .= "(NULL , ?, ?, ?, 'Phone', ?, ?, ?, ?, ?, ?, ?)";
186 $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'))));
189 ////////////////////////////////////////////////////////////////////
190 // Function: WriteLog
191 // Purpose: written log into file
192 ////////////////////////////////////////////////////////////////////
193 function WriteLog($data)
195 $log_file = $GLOBALS['phone_reminder_log_dir'];
197 if ($log_file != null) {
198 $filename = $log_file . "/" . "phone_reminder_cronlog_" . date("Ymd") . ".html";
199 if (!$fp = fopen($filename, 'a')) {
200 print "Cannot open file ($filename)";
201 } else {
202 $sdata = "\n====================================================================\n";
203 if (!fwrite($fp, $data . $sdata)) {
204 print "Cannot write to file ($filename)";
207 fclose($fp);
211 ////////////////////////////////////////////////////////////////////
212 // Function: cron_getFacilities
213 // Purpose: get facilities data once and store in map
214 ////////////////////////////////////////////////////////////////////
215 function cron_getFacilitiesMap()
217 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
225 $facilities = $facilityService->getAllFacility();
226 foreach ($facilities as $prow) {
227 $facility_msg_map[$prow['id']] = $message_map[$prow['name']];
228 $facility_phone_map[$prow['id']] = $prow['phone'];
231 $facility_map = array(
232 'msg_map' => $facility_msg_map,
233 'phone_map' => $facility_phone_map
236 return $facility_map;