MedEx api 5.0.2 (#1949)
[openemr.git] / interface / main / messages / save.php
blob89c9c53479f03bad00d5f09ba75ed4f18e4795d9
1 <?php
2 /**
3 * /interface/main/messages/save.php
5 * @package MedEx
6 * @link http://www.MedExBank.com
7 * @author MedEx <support@MedExBank.com>
8 * @copyright Copyright (c) 2017 MedEx <support@MedExBank.com>
9 * @license https://www.gnu.org/licenses/agpl-3.0.en.html GNU Affero General Public License 3
12 require_once "../../globals.php";
13 require_once "$srcdir/acl.inc";
14 require_once "$srcdir/lists.inc";
15 require_once "$srcdir/forms.inc";
16 require_once "$srcdir/patient.inc";
17 require_once "$srcdir/MedEx/API.php";
19 $MedEx = new MedExApi\MedEx('MedExBank.com');
20 if ($_REQUEST['go'] == 'sms_search') {
21 $param = "%" . $_GET['term'] . "%";
22 $query = "SELECT * FROM patient_data WHERE fname LIKE ? OR lname LIKE ?";
23 $result = sqlStatement($query, array($param, $param));
24 while ($frow = sqlFetchArray($result)) {
25 $data['Label'] = 'Name';
26 $data['value'] = $frow['fname'] . " " . $frow['lname'];
27 $data['pid'] = $frow['pid'];
28 $data['mobile'] = $frow['phone_cell'];
29 $data['allow'] = $frow['hipaa_allowsms'];
30 $results[] = $data;
32 echo json_encode($results);
33 exit;
35 //you need admin privileges to update this.
36 if ($_REQUEST['go'] == 'Preferences') {
37 if (acl_check('admin', 'super')) {
38 $sql = "UPDATE `medex_prefs` SET `ME_facilities`=?,`ME_providers`=?,`ME_hipaa_default_override`=?,
39 `PHONE_country_code`=? ,`MSGS_default_yes`=?,
40 `POSTCARDS_local`=?,`POSTCARDS_remote`=?,
41 `LABELS_local`=?,`LABELS_choice`=?,
42 `combine_time`=?, postcard_top=?";
44 $facilities = implode("|", $_REQUEST['facilities']);
45 $providers = implode("|", $_REQUEST['providers']);
46 $HIPAA = ($_REQUEST['ME_hipaa_default_override'] ? $_REQUEST['ME_hipaa_default_override'] : '');
47 $MSGS = ($_REQUEST['MSGS_default_yes'] ? $_REQUEST['MSGS_default_yes'] : '');
48 $country_code = ($_REQUEST['PHONE_country_code'] ? $_REQUEST['PHONE_country_code'] : '1');
50 $myValues = array($facilities, $providers, $HIPAA, $country_code, $MSGS, $_REQUEST['POSTCARDS_local'], $_REQUEST['POSTCARDS_remote'], $_REQUEST['LABELS_local'], $_REQUEST['chart_label_type'], $_REQUEST['combine_time'], $_REQUEST['postcard_top']);
52 $_GLOBALS['chart_label_type'] = $_REQUEST['chart_label_type'];
53 sqlStatement('UPDATE `globals` SET gl_value = ? WHERE gl_name LIKE "chart_label_type" ', array($_REQUEST['chart_label_type']));
55 $result['output'] = sqlQuery($sql, $myValues);
56 if ($result['output'] == false) {
57 $result['success'] = "medex_prefs updated";
59 $result['logged_in'] = $MedEx->login();
60 $result['response'] = $MedEx->practice->sync($result['logged_in']['token']);
61 echo json_encode($result);
63 exit;
65 if ($_REQUEST['MedEx'] == "start") {
66 if (acl_check('admin', 'super')) {
67 $query = "SELECT * FROM users WHERE id = ?";
68 $user_data = sqlQuery($query, array($_SESSION['authUserID']));
69 $query = "SELECT * FROM facility WHERE primary_business_entity='1' LIMIT 1";
70 $facility = sqlFetchArray(sqlStatement($query));
72 $data['firstname'] = $user_data['fname'];
73 $data['lastname'] = $user_data['lname'];
74 $data['username'] = $_SESSION['authUser'];
75 $data['password'] = $_REQUEST['new_password'];
76 $data['email'] = $_REQUEST['new_email'];
77 $data['telephone'] = $facility['phone'];
78 $data['fax'] = $facility['fax'];
79 $data['company'] = $facility['name'];
80 $data['address_1'] = $facility['street'];
81 $data['city'] = $facility['city'];
82 $data['state'] = $facility['state'];
83 $data['postcode'] = $facility['postal_code'];
84 $data['country'] = $facility['country_code'];
85 $data['sender_name'] = $user_data['fname'] . " " . $user_data['lname'];
86 $data['sender_email'] = $facility['email'];
87 $data['callerid'] = $facility['phone'];
88 $data['MedEx'] = "1";
89 $data['ipaddress'] = $_SERVER['REMOTE_ADDR'];
91 $prefix = 'http://';
92 if ($_SERVER["SSL_TLS_SNI"]) {
93 $prefix = "https://";
95 $data['website_url'] = $prefix . $_SERVER['HTTP_HOST'] . $web_root;
96 $practice_logo = "$OE_SITE_DIR/images/practice_logo.gif";
97 if (!file_exists($practice_logo)) {
98 $data['logo_url'] = $prefix . $_SERVER['HTTP_HOST'] . $web_root . "/sites/" . $_SESSION["site_id"] . "/images/practice_logo.gif";
99 } else {
100 $data['logo_url'] = $prefix . $_SERVER['HTTP_HOST'] . $GLOBALS['images_static_relative'] . "/menu-logo.png";
102 $response = $MedEx->setup->autoReg($data);
103 if (($response['API_key'] > '') && ($response['customer_id'] > '')) {
104 sqlQuery("DELETE FROM medex_prefs");
105 $runQuery = "SELECT * FROM facility ORDER BY name";
106 $fetch = sqlStatement($runQuery);
107 while ($frow = sqlFetchArray($fetch)) {
108 $facilities[] = $frow['id'];
110 $runQuery = "SELECT * FROM users WHERE username != '' AND active = '1' AND authorized = '1'";
111 $prove = sqlStatement($runQuery);
112 while ($prow = sqlFetchArray($prove)) {
113 $providers[] = $prow['id'];
115 $facilities = implode("|", $facilities);
116 $providers = implode("|", $providers);
117 $sqlINSERT = "INSERT INTO `medex_prefs` (
118 MedEx_id,ME_api_key,ME_username,
119 ME_facilities,ME_providers,ME_hipaa_default_override,MSGS_default_yes,
120 PHONE_country_code,LABELS_local,LABELS_choice)
121 VALUES (?,?,?,?,?,?,?,?,?,?)";
122 sqlStatement($sqlINSERT, array($response['customer_id'], $response['API_key'], $_POST['new_email'], $facilities, $providers, "1", "1", "1", "1", "5160"));
125 $info = $MedEx->login('1');
127 if ($info['status']['token']) {
128 $info['status']['show'] = xlt("Sign-up successful for") . " " . $data['company'] . ".<br />" . xlt("Proceeding to Preferences") . ".<br />" .
129 xlt("If this page does not refresh, reload the Messages page manually") . ".<br />";
130 //get js to reroute user to preferences.
131 echo json_encode($info['status']);
132 sqlQuery("UPDATE `background_services` SET `active`='1',`execute_interval`='29' WHERE `name`='MedEx'");
133 } else {
134 $response_prob = array();
135 $response_prob['show'] = xlt("We ran into some problems connecting your EHR to the MedEx servers") . ".<br >
136 " .xlt('Most often this is due to a Username/Password mismatch')."<br />"
137 .xlt('Run Setup again or contact support for assistance').
138 " <a href='https://medexbank.com/cart/upload/'>MedEx Bank</a>.<br />";
139 echo json_encode($response_prob);
140 sqlQuery("UPDATE `background_services` SET `active`='0',`execute_interval`='29' WHERE `name`='MedEx'");
142 //then redirect user to preferences with a success message!
143 } else {
144 echo xlt("Sorry you are not privileged enough. Enrollment is limited to Adminstrator accounts.");
146 exit;
149 if (($_REQUEST['pid']) && ($_REQUEST['action'] == "new_recall")) {
150 $query = "SELECT * FROM patient_data WHERE pid=?";
151 $result = sqlQuery($query, array($_REQUEST['pid']));
152 $result['age'] = $MedEx->events->getAge($result['DOB']);
155 * Did the clinician create a PLAN at the last visit?
156 * To do an in office test, and get paid for it,
157 * we must have an order (and a report of the findings).
158 * If the practice is using the eye form then uncomment the 3 lines below.
159 * It provides the PLAN and orders for next visit.
160 * As forms mature, there should be a uniform way to find the PLAN?
161 * And when that day comes we'll put it here...
162 * The other option is to use Visit Categories here. Maybe both? Consensus?
164 $query = "SELECT ORDER_DETAILS FROM form_eye_mag_orders WHERE PID=? AND ORDER_DATE_PLACED < NOW() ORDER BY ORDER_DATE_PLACED DESC LIMIT 1";
165 $result2 = sqlQuery($query, array($_REQUEST['pid']));
166 if (!empty($result2)) {
167 $result['PLAN'] = $result2['ORDER_DETAILS'];
170 $query = "SELECT * FROM openemr_postcalendar_events WHERE pc_pid =? ORDER BY pc_eventDate DESC LIMIT 1";
171 $result2 = sqlQuery($query, array($_REQUEST['pid']));
172 if ($result2) { //if they were never actually scheduled this would be blank
173 $result['DOLV'] = oeFormatShortDate($result2['pc_eventDate']);
174 $result['provider'] = $result2['pc_aid'];
175 $result['facility'] = $result2['pc_facility'];
178 * Is there an existing Recall in place already????
179 * If so we need to use that info...
181 $query = "SELECT * from medex_recalls where r_pid=?";
182 $result3 = sqlQuery($query, array($_REQUEST['pid']));
183 if ($result3) {
184 $result['recall_date'] = $result3['r_eventDate'];
185 $result['PLAN'] = $result3['r_reason'];
186 $result['facility'] = $result3['r_facility'];
187 $result['provider'] = $result3['r_provider'];
189 echo json_encode($result);
190 exit;
193 if (($_REQUEST['action'] == 'addRecall') || ($_REQUEST['add_new'])) {
194 $result = $MedEx->events->save_recall($_REQUEST);
195 echo json_encode('saved');
196 exit;
199 if (($_REQUEST['action'] == 'delete_Recall') && ($_REQUEST['pid'])) {
200 $MedEx->events->delete_recall();
201 echo json_encode('deleted');
202 exit;
205 // Clear the pidList session whenever this page is loaded.
206 // $_SESSION['pidList'] will hold array of patient ids
207 // which is then used to print 'postcards' and 'Address Labels'
208 // Thanks Terry!
209 unset($_SESSION['pidList']);
210 $pid_list = array();
212 if ($_REQUEST['action'] == "process") {
213 $new_pid = json_decode($_POST['parameter'], true);
214 $new_pc_eid = json_decode($_POST['pc_eid'], true);
216 if (($_POST['item'] == "phone") || (($_POST['item'] == "notes") && ($_POST['msg_notes'] > ''))) {
217 $sql = "INSERT INTO medex_outgoing (msg_pc_eid, msg_type, msg_reply, msg_extra_text) VALUES (?,?,?,?)";
218 sqlQuery($sql, array('recall_' . $new_pid[0], $_POST['item'], $_SESSION['authUserID'], $_POST['msg_notes']));
219 return "done";
221 $pc_eidList = json_decode($_POST['pc_eid'], true);
222 $_SESSION['pc_eidList'] = $pc_eidList[0];
223 $pidList = json_decode($_POST['parameter'], true);
224 $_SESSION['pidList'] = $pidList;
225 if ($_POST['item'] == "postcards") {
226 foreach ($pidList as $pid) {
227 $sql = "INSERT INTO medex_outgoing (msg_pc_eid, msg_type, msg_reply, msg_extra_text) VALUES (?,?,?,?)";
228 sqlQuery($sql, array('recall_' . $pid, $_POST['item'], $_SESSION['authUserID'], 'Postcard printed locally'));
231 if ($_POST['item'] == "labels") {
232 foreach ($pidList as $pid) {
233 $sql = "INSERT INTO medex_outgoing (msg_pc_eid, msg_type, msg_reply, msg_extra_text) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE msg_extra_text='Label repeat'";
234 sqlQuery($sql, array('recall_' . $pid, $_POST['item'], $_SESSION['authUserID'], 'Label printed locally'));
237 echo json_encode($pidList);
238 exit;
240 if ($_REQUEST['go'] == "Messages") {
241 if ($_REQUEST['msg_id']) {
242 $result = updateMessage($_REQUEST['msg_id']);
243 echo json_encode($result);
244 exit;
247 exit;