Merge branch 'master' of git://github.com/openemr/openemr
[openemr.git] / modules / sms_email_reminder / cron_sms_notification.php
blob170967f20a085306c02f0c5e088be194af90b59d
1 <?
2 ////////////////////////////////////////////////////////////////////
3 // Package: sms_cron_notification
4 // Purpose: to be run by cron every hour, look for appointments
5 // in the pre-notification period and send an sms reminder
6 //
7 // Created by:
8 // Updated by: Larry Lart on 11/03/2008
9 ////////////////////////////////////////////////////////////////////
11 // larry :: hack add for command line version
12 $_SERVER['REQUEST_URI']=$_SERVER['PHP_SELF'];
13 $_SERVER['SERVER_NAME']='localhost';
14 $backpic = "";
16 // email notification
17 $ignoreAuth=1;
18 include_once("../../interface/globals.php");
19 include_once("cron_functions.php");
21 // check command line for quite option
22 $bTestRun = 0;
23 if( $argc > 1 && $argv[1] == 'test' ) $bTestRun = 1;
25 $TYPE = "SMS";
26 $CRON_TIME = 5;
28 $curr_date = date("Y-m-d");
29 $curr_time = time();
30 $check_date = date("Y-m-d", mktime(date("h")+$SMS_NOTIFICATION_HOUR, 0, 0, date("m"), date("d"), date("Y")));
32 // larry :: move this in the loop to keep it fresh - perhaps try to use it without change
33 // it's content - to do latter
34 $db_email_msg = cron_getNotificationData($TYPE);
36 // object for sms
37 global $mysms;
38 if( $db_email_msg['sms_gateway_type']=='CLICKATELL' )
40 include_once("sms_clickatell.php");
42 }else if($db_email_msg['sms_gateway_type']=='TMB4')
44 include_once("sms_tmb4.php");
47 // get notification settings
48 $vectNotificationSettings = cron_GetNotificationSettings( );
49 $SMS_GATEWAY_USENAME = $vectNotificationSettings['SMS_gateway_username'];
50 $SMS_GATEWAY_PASSWORD = $vectNotificationSettings['SMS_gateway_password'];
51 $SMS_GATEWAY_APIKEY = $vectNotificationSettings['SMS_gateway_apikey'];
52 // set cron time (time to event ?) - todo extra tests
53 $CRON_TIME = $vectNotificationSettings['Send_SMS_Before_Hours'];
55 //echo "\nDEBUG :: user=".$vectNotificationSettings['SMS_gateway_username']."\n";
57 // create sms object
58 $mysms = new sms( $SMS_GATEWAY_USENAME, $SMS_GATEWAY_PASSWORD, $SMS_GATEWAY_APIKEY );
60 $db_patient = cron_getAlertpatientData($TYPE);
61 echo "\n<br>Total ".count($db_patient)." Records Found";
63 // for every event found
64 for( $p=0; $p<count($db_patient); $p++ )
66 $prow =$db_patient[$p];
68 //echo "\n-----\nDEBUG :cron_sms: found patient = ".$prow['fname']." ".$prow['lname']."\n";
70 // my_print_r($prow);
72 if($prow['pc_eventDate'] < $check_date)
74 $app_date = date("Y-m-d")." ".$prow['pc_startTime'];
75 }else{
76 $app_date = $prow['pc_eventDate']." ".$prow['pc_startTime'];
79 $app_date = $prow['pc_eventDate']." ".$prow['pc_startTime'];
80 $app_time = strtotime($app_date);
82 $app_time_hour = round($app_time/3600);
83 $curr_total_hour = round(time()/3600);
85 $remaining_app_hour = round($app_time_hour - $curr_total_hour);
86 $remain_hour = round($remaining_app_hour - $SMS_NOTIFICATION_HOUR);
88 // larry :: debug
89 //echo "\nDEBUG :: checkdate=$check_date, app_date=$app_date, apptime=$app_time remain_hour=$remain_hour -- CRON_TIME=$CRON_TIME\n";
91 // build log message
92 $strMsg = "\n========================".$TYPE." || ".date("Y-m-d H:i:s")."=========================";
93 $strMsg .= "\nSEND NOTIFICATION BEFORE:".$SMS_NOTIFICATION_HOUR." || CRONJOB RUN EVERY:".$CRON_TIME." || APPDATETIME:".$app_date." || REMAINING APP HOUR:".($remaining_app_hour)." || SEND ALERT AFTER:".($remain_hour);
95 // check in the interval
96 if( $remain_hour >= -($CRON_TIME) && $remain_hour <= $CRON_TIME )
98 // insert entry in notification_log table
99 cron_InsertNotificationLogEntry($TYPE,$prow,$db_email_msg);
101 //set message
102 $db_email_msg['message'] = cron_setmessage($prow,$db_email_msg);
104 // send sms to patinet - if not in test mode
105 if( $bTestRun == 0 )
107 cron_SendSMS( $prow['phone_cell'], $db_email_msg['email_subject'],
108 $db_email_msg['message'], $db_email_msg['email_sender'] );
111 // larry :: debug
112 echo "\nDEBUG :: sms was sent to= ".$prow['phone_cell'].
113 " \nsender= ".$db_email_msg['email_sender'].
114 " \nsbj= ". $db_email_msg['email_subject'].
115 " \nmsg= ".$db_email_msg['message']."\n";
117 //update entry >> pc_sendalertsms='Yes'
118 cron_updateentry($TYPE,$prow['pid'],$prow['pc_eid']);
120 $strMsg .= " || ALERT SENT SUCCESSFULLY TO ".$prow['phone_cell'];
121 $strMsg .= "\n".$patient_info."\n".$smsgateway_info."\n".$data_info."\n".$db_email_msg['message'];
123 // write logs for every reminder sent
124 WriteLog($strMsg);
126 // larry :: update notification data again - todo :: fix change in cron_updateentry
127 $db_email_msg = cron_getNotificationData($TYPE);
131 unset($mysms);
132 sqlClose();
136 <html>
137 <head>
138 <title>Conrjob - SMS Notification</title>
139 </head>
140 <body>
141 <center>
142 </center>
143 </body>
144 </html>