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