PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/sms_email_reminder/batch_reminders.php

https://github.com/med2000/openemr
PHP | 202 lines | 168 code | 10 blank | 24 comment | 40 complexity | 4e5117ec5b239d9da5053b73248ebc26 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0
  1. <?php
  2. // Copyright (C) 2010 OpenEMR Support LLC
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public License
  5. // as published by the Free Software Foundation; either version 2
  6. // of the License, or (at your option) any later version.
  7. //SANITIZE ALL ESCAPES
  8. $sanitize_all_escapes=true;
  9. //STOP FAKE REGISTER GLOBALS
  10. $fake_register_globals=false;
  11. $backpic = "";
  12. $ignoreAuth=1;
  13. require_once(dirname(__FILE__)."/../../interface/globals.php");
  14. include_once("$srcdir/sql.inc");
  15. include_once("$srcdir/formdata.inc.php");
  16. ?>
  17. <html>
  18. <head>
  19. <?php html_header_show();?>
  20. <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
  21. <link rel="stylesheet" href="batchcom.css" type="text/css">
  22. </head>
  23. <body class="body_top">
  24. <span class="title"><?php echo htmlspecialchars(xl('Patient Reminder Batch Job'), ENT_QUOTES)?></span>
  25. <table>
  26. <tr>
  27. <td class='text' align='left' colspan="3"><br>
  28. <span class="text"><?php echo htmlspecialchars(xl('The patient reminders have been processed according their scheduled dates.'), ENT_QUOTES)?></span><br><br>
  29. <span class="text"><?php echo htmlspecialchars(xl('Email delivery is immediate, while automated VOIP is sent to the service provider for further processing.'), ENT_QUOTES)?></span><br><br>
  30. <input type="button" value="<?php echo htmlspecialchars(xl('Close'), ENT_QUOTES); ?>" onClick="window.close()"><br><br><br>
  31. </td>
  32. </tr>
  33. </table>
  34. <br><br>
  35. <?php
  36. require_once ($GLOBALS['srcdir'] . "/classes/postmaster.php");
  37. require_once ($GLOBALS['srcdir'] . "/maviq_phone_api.php");
  38. require_once("$include_root/patient_file/health_plans/health_plans_functions.php");
  39. // Recreate the reminder follow the actions frequency
  40. $enrollsql = "select * from health_plan_enrollment order by enroll_id";
  41. $enrollresult = sqlStatement($enrollsql);
  42. while ($myenrollrow = sqlFetchArray($enrollresult)) {
  43. $action_date = explode(",", htmlspecialchars($myenrollrow['action_date'], ENT_QUOTES));
  44. $action_completed = explode(",", htmlspecialchars($myenrollrow['action_completed'], ENT_QUOTES));
  45. $actionsql = "select * from health_plan_actions where plan_id = ? order by action_id";
  46. $actionresult = sqlStatement($actionsql, array($myenrollrow['plan_id']));
  47. $count = 0;
  48. $reminder_recreate = "";
  49. while ($myactionrow = sqlFetchArray($actionresult)) {
  50. if ($myactionrow['subactions'] != "" && $myactionrow['subactions'] != NULL) {
  51. if ($myactionrow['subactions'] == 0)
  52. {
  53. $action_frequency = htmlspecialchars($myactionrow['frequency'], ENT_QUOTES);
  54. if ($action_date[$count] != "0000-00-00" && substr($action_completed[$count], -3) == "YES") {
  55. if ($action_frequency != "::") {
  56. $tmp_frequency = explode("::", $action_frequency);
  57. $tmp_actiondate = explode("-", $action_date[$count]);
  58. $action_date[$count] = date("Y-m-d", mktime(0, 0, 0, $tmp_actiondate[1] + $tmp_frequency[1], $tmp_actiondate[2], $tmp_actiondate[0] + $tmp_frequency[0]));
  59. $action_completed[$count] = str_replace("YES", "NO", $action_completed[$count]);
  60. $reminder_recreate = "YES";
  61. }
  62. }
  63. ${"form_action_targetdate_".($count + 1)} = $action_date[$count];
  64. ${"form_action_completed_".($count + 1)} = $action_completed[$count];
  65. ++$count;
  66. }
  67. else
  68. {
  69. $action_frequency = htmlspecialchars($myactionrow['frequency'], ENT_QUOTES);
  70. }
  71. }
  72. else {
  73. if ($action_date[$count] != "0000-00-00" && substr($action_completed[$count], -3) == "YES") {
  74. if ($action_frequency != "::") {
  75. $tmp_frequency = explode("::", $action_frequency);
  76. $tmp_actiondate = explode("-", $action_date[$count]);
  77. $action_date[$count] = date("Y-m-d", mktime(0, 0, 0, $tmp_actiondate[1] + $tmp_frequency[1], $tmp_actiondate[2], $tmp_actiondate[0] + $tmp_frequency[0]));
  78. $action_completed[$count] = str_replace("YES", "NO", $action_completed[$count]);
  79. $reminder_recreate = "YES";
  80. }
  81. }
  82. ${"form_action_targetdate_".($count + 1)} = $action_date[$count];
  83. ${"form_action_completed_".($count + 1)} = $action_completed[$count];
  84. ++$count;
  85. }
  86. }
  87. if ($reminder_recreate == "YES")
  88. {
  89. // Delete the existing reminder of the enrollment.
  90. sqlQuery("delete from patient_reminders WHERE patient_id = ? AND plan_id = ? AND enroll_id= ?", array($myenrollrow['patient_id'], $myenrollrow['plan_id'], $myenrollrow['enroll_id']));
  91. // Enroll the health plan for the patient
  92. health_plans_enrollment($myenrollrow['plan_id'], $myenrollrow['patient_id'], $myenrollrow['enroll_id'], "pending", "batch");
  93. }
  94. }
  95. // Get the reminder sender details.
  96. $sql = "select sender_name, email_address, phone_number from patient_reminders where reminder_name=?";
  97. $result = sqlStatement($sql, array('Sender Details'));
  98. if ($myrow = sqlFetchArray($result)) {
  99. $sender_name = htmlspecialchars($myrow['sender_name'], ENT_QUOTES);
  100. $email_address = htmlspecialchars($myrow['email_address'], ENT_QUOTES);
  101. $phone_number = htmlspecialchars($myrow['phone_number'], ENT_QUOTES);
  102. }
  103. if ($GLOBALS['clinical_decision_rules_and_patient_reminders']) {
  104. // Get the reminder where scheduled date is less than or equal to today's date and has a Pending status.
  105. $sql = "select reminder_id, reminder_name, patient_id, enroll_id, action_id, reminder_content, scheduled_date, voice_status, email_status from patient_reminders where scheduled_date = ? and scheduled_date != ? and scheduled_date IS NOT NULL";
  106. $result = sqlStatement($sql, array(date("Y-m-d"), '0000-00-00'));
  107. while ($myrow = sqlFetchArray($result))
  108. {
  109. // Select the action completions status based on enrollment ID.
  110. $enrollrow = sqlQuery("select action_completed from health_plan_enrollment where enroll_id= ?", array($myrow['enroll_id']));
  111. $action_completed = explode(",", $enrollrow['action_completed']);
  112. if (!in_array($myrow['action_id']."-YES", $action_completed)) {
  113. $scheduled_date = explode("-", $myrow['scheduled_date']);
  114. // Get the patient details based on patient ID.
  115. $sql = "select fname, lname, email, phone_cell, hipaa_voice, hipaa_allowemail from patient_data where pid=?";
  116. $patientresult = sqlStatement($sql, array($myrow['patient_id']));
  117. if ($mypatientrow = sqlFetchArray($patientresult))
  118. {
  119. $patientfname = htmlspecialchars($mypatientrow['fname'], ENT_QUOTES);
  120. $patientlname = htmlspecialchars($mypatientrow['lname'], ENT_QUOTES);
  121. $patientemail = htmlspecialchars($mypatientrow['email'], ENT_QUOTES);
  122. $patientphone = htmlspecialchars($mypatientrow['phone_cell'], ENT_QUOTES);
  123. $hipaa_voice = htmlspecialchars($mypatientrow['hipaa_voice'], ENT_QUOTES);
  124. $hipaa_allowemail = htmlspecialchars($mypatientrow['hipaa_allowemail'], ENT_QUOTES);
  125. }
  126. // Email to patient if Allow Email field in Demographics is set to Yes.
  127. if ($hipaa_allowemail == "YES" and ($myrow['email_status'] == "pending" or $myrow['email_status'] == "failed"))
  128. {
  129. $mail = new MyMailer();
  130. $mail->FromName = $sender_name; // required
  131. $mail->Sender = $sender_name; // required
  132. $mail->From = $email_address; // required
  133. $mail->AddAddress($patientemail, $patientfname.", ".$patientlname); // required
  134. $mail->AddReplyTo($email_address,$sender_name); // required
  135. $mail->Body = str_replace("[[sender]]", $sender_name, str_replace("[[patient_name]]", $patientfname, $myrow['reminder_content']));
  136. if(stristr($myrow['reminder_name'], "Followup") == TRUE)
  137. {
  138. $mail->Subject = "Health Maintenance Action Followup";
  139. }
  140. else
  141. {
  142. $mail->Subject = "Health Maintenance Action Reminder";
  143. }
  144. if(!$mail->Send()) {
  145. //echo "Email sent fail...<br><br>";
  146. sqlQuery("update patient_reminders set email_status='failed' where reminder_id=?", array($myrow['reminder_id']));
  147. }
  148. else
  149. {
  150. //echo "Email sent successfully...<br><br>";
  151. sqlQuery("update patient_reminders set email_status='sent' where reminder_id=?", array($myrow['reminder_id']));
  152. }
  153. }
  154. // Do a calling to patient if Allow Voice Message in Demographics is set to Yes.
  155. if ($hipaa_voice == "YES" and $myrow['voice_status'] == "pending")
  156. {
  157. // Automated VOIP service provided by Maviq. Please visit http://signup.maviq.com for more information.
  158. $siteId = $GLOBALS['phone_gateway_username'];
  159. $token = $GLOBALS['phone_gateway_password'];
  160. $endpoint = $GLOBALS['phone_gateway_url'];
  161. $client = new MaviqClient($siteId, $token, $endpoint);
  162. //Set up params.
  163. $data = array(
  164. "firstName" => $patientfname,
  165. "lastName" => $patientlname,
  166. "phone" => $patientphone,
  167. "apptDate" => "$scheduled_date[1]/$scheduled_date[2]/$scheduled_date[0]",
  168. "timeRange" => "10-18",
  169. "type" => "reminder",
  170. "timeZone" => date('P'),
  171. "greeting" => str_replace("[[sender]]", $sender_name, str_replace("[[patient_name]]", $patientfname, $myrow['reminder_content']))
  172. );
  173. // Make the call.
  174. $response = $client->sendRequest("appointment", "POST", $data);
  175. // Check response for calling success or error.
  176. if($response->IsError)
  177. {
  178. // echo "Phone call failed...<br><br>";
  179. sqlQuery("update patient_reminders set voice_status='failed' where reminder_id=?", array($myrow['reminder_id']));
  180. }
  181. else
  182. {
  183. // echo "Phone call done successfully...<br><br>";
  184. sqlQuery("update patient_reminders set voice_status='sent' where reminder_id=?", array($myrow['reminder_id']));
  185. }
  186. }
  187. }
  188. }
  189. }
  190. ?>
  191. </body>
  192. </html>