Updated translations (#1517)
[openemr.git] / modules / sms_email_reminder / batch_phone_notification.php
blob1ed98e629223241d6cc39362a841c98003538ef3
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 use OpenEMR\Services\FacilityService;
33 $facilityService = new FacilityService();
35 $type = "Phone";
36 $before_trigger_hours = 72; // 3 days is default
37 //Get the values from Global
38 $before_trigger_hours = $GLOBALS['phone_notification_hour'];
39 //set up the phone notification settings for external phone service
40 $phone_url = $GLOBALS['phone_gateway_url'] ;
41 $phone_id = $GLOBALS['phone_gateway_username'];
42 $phone_token = $GLOBALS['phone_gateway_password'];
43 $phone_time_range = $GLOBALS['phone_time_range'];
45 //get the facility_id-message map
46 $facilities = cron_getFacilitiesMap();
47 //print_r($facilities);
48 $fac_phone_map = $facilities['phone_map'];
49 $fac_msg_map = $facilities['msg_map'];
51 // get patient data for send alert
52 $db_patient = cron_getPhoneAlertpatientData($type, $before_trigger_hours);
53 echo "<br>" . htmlspecialchars(xl("Total Records Found") . ": " . count($db_patient), ENT_QUOTES);
55 //Create a new instance of the phone service client
56 $client = new MaviqClient($phone_id, $phone_token, $phone_url);
58 for ($p=0; $p<count($db_patient); $p++) {
59 $prow =$db_patient[$p];
61 //Get the apptDate and apptTime
62 $p_date = $prow['pc_eventDate'];
63 //Need to format date to m/d/Y for Maviq API
64 $pieces = explode("-", $p_date);
65 $appt_date = date("m/d/Y", mktime(0, 0, 0, $pieces[1], $pieces[2], $pieces[0]));
66 $appt_time = $prow['pc_startTime'];
67 //get the greeting
68 $greeting = $fac_msg_map[$prow['pc_facility']];
69 if ($greeting == null) {
70 //Use the default when the message is not found
71 $greeting = $GLOBALS['phone_appt_message']['Default'];
74 //Set up the parameters for the call
75 $data = array(
76 "firstName" => $prow['fname'],
77 "lastName" => $prow['lname'],
78 "phone" => $prow['phone_home'],
79 "apptDate" => $appt_date,
80 "apptTime" => $appt_time,
81 "doctor" => $prow['pc_aid'],
82 "greeting" => $greeting,
83 "timeRange" => $phone_time_range,
84 "type" => "appointment",
85 "timeZone" => date('P'),
86 "callerId" => $fac_phone_map[$prow['pc_facility']]
89 //Make the call
90 $response = $client->sendRequest("appointment", "POST", $data);
92 // check response for success or error
93 if ($response->IsError) {
94 $strMsg = "Error starting phone call for {$prow['fname']} | {$prow['lname']} | {$prow['phone_home']} | {$appt_date} | {$appt_time} | {$response->ErrorMessage}\n";
95 } else {
96 $strMsg = "\n========================".$type." || ".date("Y-m-d H:i:s")."=========================";
97 $strMsg .= "\nPhone reminder sent successfully: {$prow['fname']} | {$prow['lname']} | | {$prow['phone_home']} | {$appt_date} | {$appt_time} ";
98 // insert entry in notification_log table
99 cron_InsertNotificationLogEntry($prow, $greeting, $phone_url);
101 //update entry >> pc_sendalertsms='Yes'
102 cron_updateentry($type, $prow['pid'], $prow['pc_eid']);
105 //echo $strMsg;
106 WriteLog($strMsg);
109 sqlClose();
111 ////////////////////////////////////////////////////////////////////
112 // Function: cron_updateentry
113 // Purpose: update status yes if alert send to patient
114 ////////////////////////////////////////////////////////////////////
115 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";
199 if (!$fp = fopen($filename, 'a')) {
200 print "Cannot open file ($filename)";
201 } else {
202 $sdata = "\n====================================================================\n";
204 if (!fwrite($fp, $data.$sdata)) {
205 print "Cannot write to file ($filename)";
208 fclose($fp);
212 ////////////////////////////////////////////////////////////////////
213 // Function: cron_getFacilities
214 // Purpose: get facilities data once and store in map
215 ////////////////////////////////////////////////////////////////////
216 function cron_getFacilitiesMap()
218 global $facilityService;
220 //get the facility_name-message map from Globals
221 $message_map = $GLOBALS['phone_appt_message'];
222 //create a new array to store facility_id to message map
223 $facility_msg_map = array();
224 $facility_phone_map = array();
225 //get facilities from the database
226 $fres = $facilityService->getAll();
227 foreach ($fres as $frow) {
228 $facility_msg_map[$frow['id']] = $message_map[$frow['name']];
229 $facility_phone_map[$frow['id']] = $frow['phone'];
232 $facility_map = array(
233 'msg_map' => $facility_msg_map,
234 'phone_map' => $facility_phone_map
237 return $facility_map;