Interim autoloaded library/classes via composer classmap, take 4. (#422)
[openemr.git] / interface / patient_file / deleter.php
bloba832611ff465766367d62d8229c2e8ae6b3866e5
1 <?php
2 /**
3 * delete tool, for logging and removing patient data.
5 * Called from many different pages.
7 * Copyright (C) 2005-2013 Rod Roark <rod@sunsetsystems.com>
8 * Copyright (C) 2015 Roberto Vasquez <robertogagliotta@gmail.com>
10 * LICENSE: This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
21 * @package OpenEMR
22 * @author Rod Roark <rod@sunsetsystems.com>
23 * @author Roberto Vasquez <robertogagliotta@gmail.com>
24 * @link http://www.open-emr.org
27 $fake_register_globals=false;
28 $sanitize_all_escapes=true;
30 require_once('../globals.php');
31 require_once($GLOBALS['srcdir'].'/log.inc');
32 require_once($GLOBALS['srcdir'].'/acl.inc');
33 require_once($GLOBALS['srcdir'].'/sl_eob.inc.php');
35 $patient = $_REQUEST['patient'];
36 $encounterid = $_REQUEST['encounterid'];
37 $formid = $_REQUEST['formid'];
38 $issue = $_REQUEST['issue'];
39 $document = $_REQUEST['document'];
40 $payment = $_REQUEST['payment'];
41 $billing = $_REQUEST['billing'];
42 $transaction = $_REQUEST['transaction'];
44 $info_msg = "";
46 // Delete rows, with logging, for the specified table using the
47 // specified WHERE clause.
49 function row_delete($table, $where) {
50 $tres = sqlStatement("SELECT * FROM $table WHERE $where");
51 $count = 0;
52 while ($trow = sqlFetchArray($tres)) {
53 $logstring = "";
54 foreach ($trow as $key => $value) {
55 if (! $value || $value == '0000-00-00 00:00:00') continue;
56 if ($logstring) $logstring .= " ";
57 $logstring .= $key . "= '" . $value . "' ";
59 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
60 ++$count;
62 if ($count) {
63 $query = "DELETE FROM $table WHERE $where";
64 echo text($query) . "<br>\n";
65 sqlStatement($query);
69 // Deactivate rows, with logging, for the specified table using the
70 // specified SET and WHERE clauses.
72 function row_modify($table, $set, $where) {
73 if (sqlQuery("SELECT * FROM $table WHERE $where")) {
74 newEvent("deactivate", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $where");
75 $query = "UPDATE $table SET $set WHERE $where";
76 echo text($query) . "<br>\n";
77 sqlStatement($query);
81 // We use this to put dashes, colons, etc. back into a timestamp.
83 function decorateString($fmt, $str) {
84 $res = '';
85 while ($fmt) {
86 $fc = substr($fmt, 0, 1);
87 $fmt = substr($fmt, 1);
88 if ($fc == '.') {
89 $res .= substr($str, 0, 1);
90 $str = substr($str, 1);
91 } else {
92 $res .= $fc;
95 return $res;
98 // Delete and undo product sales for a given patient or visit.
99 // This is special because it has to replace the inventory.
101 function delete_drug_sales($patient_id, $encounter_id=0) {
102 $where = $encounter_id ? "ds.encounter = '" . add_escape_custom($encounter_id) . "'" :
103 "ds.pid = '" . add_escape_custom($patient_id) . "' AND ds.encounter != 0";
104 sqlStatement("UPDATE drug_sales AS ds, drug_inventory AS di " .
105 "SET di.on_hand = di.on_hand + ds.quantity " .
106 "WHERE $where AND di.inventory_id = ds.inventory_id");
107 if ($encounter_id) {
108 row_delete("drug_sales", "encounter = '" . add_escape_custom($encounter_id) . "'");
110 else {
111 row_delete("drug_sales", "pid = '" . add_escape_custom($patient_id ) . "'");
115 // Delete a form's data from its form-specific table.
117 function form_delete($formdir, $formid) {
118 $formdir = ($formdir == 'newpatient') ? 'encounter' : $formdir;
119 if (substr($formdir,0,3) == 'LBF') {
120 row_delete("lbf_data", "form_id = '" . add_escape_custom($formid) . "'");
122 else if ($formdir == 'procedure_order') {
123 $tres = sqlStatement("SELECT procedure_report_id FROM procedure_report " .
124 "WHERE procedure_order_id = ?", array($formid));
125 while ($trow = sqlFetchArray($tres)) {
126 $reportid = 0 + $trow['procedure_report_id'];
127 row_delete("procedure_result", "procedure_report_id = '" . add_escape_custom($reportid) . "'");
129 row_delete("procedure_report", "procedure_order_id = '" . add_escape_custom($formid) . "'");
130 row_delete("procedure_order_code", "procedure_order_id = '" . add_escape_custom($formid) . "'");
131 row_delete("procedure_order", "procedure_order_id = '" . add_escape_custom($formid) . "'");
133 else if ($formdir == 'physical_exam') {
134 row_delete("form_$formdir", "forms_id = '" . add_escape_custom($formid) . "'");
136 else {
137 row_delete("form_$formdir", "id = '" . add_escape_custom($formid) . "'");
141 // Delete a specified document including its associated relations and file.
143 function delete_document($document) {
144 $trow = sqlQuery("SELECT url, thumb_url, storagemethod, couch_docid, couch_revid FROM documents WHERE id = ?", array($document));
145 $url = $trow['url'];
146 $thumb_url = $trow['thumb_url'];
147 row_delete("categories_to_documents", "document_id = '" . add_escape_custom($document) . "'");
148 row_delete("documents", "id = '" . add_escape_custom($document) . "'");
149 row_delete("gprelations", "type1 = 1 AND id1 = '" . add_escape_custom($document) . "'");
151 switch((int)$trow['storagemethod']) {
152 //for hard disk store
153 case 0:
154 @unlink(substr($url, 7));
156 if(!is_null($thumb_url)){
157 @unlink(substr($thumb_url, 7));
159 break;
160 //for CouchDB store
161 case 1:
162 $couchDB = new CouchDB();
163 $couchDB->DeleteDoc($GLOBALS['couchdb_dbase'], $trow['couch_docid'], $trow['couch_revid']);
164 break;
169 <html>
170 <head>
171 <?php html_header_show();?>
172 <title><?php echo xlt('Delete Patient, Encounter, Form, Issue, Document, Payment, Billing or Transaction'); ?></title>
173 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
174 <script type="text/javascript" src="<?php echo $webroot ?>/interface/main/tabs/js/include_opener.js"></script>
176 <style>
177 td { font-size:10pt; }
178 </style>
180 <script language="javascript">
181 function submit_form()
183 document.deletefrm.submit();
185 // Java script function for closing the popup
186 function popup_close() {
187 if(parent.$==undefined) {
188 window.close();
190 else {
191 parent.$.fn.fancybox.close();
194 </script>
195 </head>
197 <body class="body_top">
198 <?php
199 // If the delete is confirmed...
201 if ($_POST['form_submit']) {
203 if ($patient) {
204 if (!acl_check('admin', 'super') || !$GLOBALS['allow_pat_delete']) die("Not authorized!");
205 row_modify("billing" , "activity = 0", "pid = '" . add_escape_custom($patient) . "'");
206 row_modify("pnotes" , "deleted = 1" , "pid = '" . add_escape_custom($patient) . "'");
207 // row_modify("prescriptions" , "active = 0" , "patient_id = '$patient'");
208 row_delete("prescriptions" , "patient_id = '" . add_escape_custom($patient) . "'");
209 row_delete("claims" , "patient_id = '" . add_escape_custom($patient) . "'");
210 delete_drug_sales($patient);
211 row_delete("payments" , "pid = '" . add_escape_custom($patient) . "'");
212 row_delete("ar_activity" , "pid = '" . add_escape_custom($patient) . "'");
213 row_delete("openemr_postcalendar_events", "pc_pid = '" . add_escape_custom($patient) . "'");
214 row_delete("immunizations" , "patient_id = '" . add_escape_custom($patient) . "'");
215 row_delete("issue_encounter", "pid = '" . add_escape_custom($patient) . "'");
216 row_delete("lists" , "pid = '" . add_escape_custom($patient) . "'");
217 row_delete("transactions" , "pid = '" . add_escape_custom($patient) . "'");
218 row_delete("employer_data" , "pid = '" . add_escape_custom($patient) . "'");
219 row_delete("history_data" , "pid = '" . add_escape_custom($patient) . "'");
220 row_delete("insurance_data" , "pid = '" . add_escape_custom($patient) . "'");
222 $res = sqlStatement("SELECT * FROM forms WHERE pid = ?", array($patient));
223 while ($row = sqlFetchArray($res)) {
224 form_delete($row['formdir'], $row['form_id']);
226 row_delete("forms", "pid = '" . add_escape_custom($patient) . "'");
228 // Delete all documents for the patient.
229 $res = sqlStatement("SELECT id FROM documents WHERE foreign_id = ?", array($patient));
230 while ($row = sqlFetchArray($res)) {
231 delete_document($row['id']);
234 row_delete("patient_data", "pid = '" . add_escape_custom($patient) . "'");
236 else if ($encounterid) {
237 if (!acl_check('admin', 'super')) die("Not authorized!");
238 row_modify("billing", "activity = 0", "encounter = '" . add_escape_custom($encounterid) . "'");
239 delete_drug_sales(0, $encounterid);
240 row_delete("ar_activity", "encounter = '" . add_escape_custom($encounterid) . "'");
241 row_delete("claims", "encounter_id = '" . add_escape_custom($encounterid) . "'");
242 row_delete("issue_encounter", "encounter = '" . add_escape_custom($encounterid) . "'");
243 $res = sqlStatement("SELECT * FROM forms WHERE encounter = ?", array($encounterid));
244 while ($row = sqlFetchArray($res)) {
245 form_delete($row['formdir'], $row['form_id']);
247 row_delete("forms", "encounter = '" . add_escape_custom($encounterid) . "'");
249 else if ($formid) {
250 if (!acl_check('admin', 'super')) die("Not authorized!");
251 $row = sqlQuery("SELECT * FROM forms WHERE id = ?", array($formid));
252 $formdir = $row['formdir'];
253 if (! $formdir) die("There is no form with id '" . text($formid) . "'");
254 form_delete($formdir, $row['form_id']);
255 row_delete("forms", "id = '" . add_escape_custom($formid) . "'");
257 else if ($issue) {
258 if (!acl_check('admin', 'super')) die("Not authorized!");
259 row_delete("issue_encounter", "list_id = '" . add_escape_custom($issue) ."'");
260 row_delete("lists", "id = '" . add_escape_custom($issue) ."'");
262 else if ($document) {
263 if (!acl_check('admin', 'super')) die("Not authorized!");
264 delete_document($document);
266 else if ($payment) {
267 if (!acl_check('admin', 'super')) die("Not authorized!");
268 list($patient_id, $timestamp, $ref_id) = explode(".", $payment);
269 // if (empty($ref_id)) $ref_id = -1;
270 $timestamp = decorateString('....-..-.. ..:..:..', $timestamp);
271 $payres = sqlStatement("SELECT * FROM payments WHERE " .
272 "pid = ? AND dtime = ?", array($patient_id, $timestamp));
273 while ($payrow = sqlFetchArray($payres)) {
274 if ($payrow['encounter']) {
275 $ref_id = -1;
276 // The session ID passed in is useless. Look for the most recent
277 // patient payment session with pay total matching pay amount and with
278 // no adjustments. The resulting session ID may be 0 (no session) which
279 // is why we start with -1.
280 $tpmt = $payrow['amount1'] + $payrow['amount2'];
281 $seres = sqlStatement("SELECT " .
282 "SUM(pay_amount) AS pay_amount, session_id " .
283 "FROM ar_activity WHERE " .
284 "pid = ? AND " .
285 "encounter = ? AND " .
286 "payer_type = 0 AND " .
287 "adj_amount = 0.00 " .
288 "GROUP BY session_id ORDER BY session_id DESC", array($patient_id, $payrow['encounter']));
289 while ($serow = sqlFetchArray($seres)) {
290 if (sprintf("%01.2f", $serow['adj_amount']) != 0.00) continue;
291 if (sprintf("%01.2f", $serow['pay_amount'] - $tpmt) == 0.00) {
292 $ref_id = $serow['session_id'];
293 break;
296 if ($ref_id == -1) {
297 die(xlt('Unable to match this payment in ar_activity') . ": " . text($tpmt));
299 // Delete the payment.
300 row_delete("ar_activity",
301 "pid = '" . add_escape_custom($patient_id) . "' AND " .
302 "encounter = '" . add_escape_custom($payrow['encounter']) . "' AND " .
303 "payer_type = 0 AND " .
304 "pay_amount != 0.00 AND " .
305 "adj_amount = 0.00 AND " .
306 "session_id = '" . add_escape_custom($ref_id) . "'");
307 if ($ref_id) {
308 row_delete("ar_session",
309 "patient_id = '" . add_escape_custom($patient_id) ."' AND " .
310 "session_id = '" . add_escape_custom($ref_id) . "'");
313 else {
314 // Encounter is 0! Seems this happens for pre-payments.
315 $tpmt = sprintf("%01.2f", $payrow['amount1'] + $payrow['amount2']);
316 row_delete("ar_session",
317 "patient_id = '" . add_escape_custom($patient_id) . "' AND " .
318 "payer_id = 0 AND " .
319 "reference = '" . add_escape_custom($payrow['source']) . "' AND " .
320 "pay_total = '" . add_escape_custom($tpmt) . "' AND " .
321 "(SELECT COUNT(*) FROM ar_activity where ar_activity.session_id = ar_session.session_id) = 0 " .
322 "ORDER BY session_id DESC LIMIT 1");
324 row_delete("payments", "id = '" . add_escape_custom($payrow['id']) . "'");
327 else if ($billing) {
328 if (!acl_check('acct','disc')) die("Not authorized!");
329 list($patient_id, $encounter_id) = explode(".", $billing);
330 sqlStatement("DELETE FROM ar_activity WHERE " .
331 "pid = ? AND encounter = ? ", array($patient_id, $encounter_id) );
332 sqlStatement("DELETE ar_session FROM ar_session LEFT JOIN " .
333 "ar_activity ON ar_session.session_id = ar_activity.session_id " .
334 "WHERE ar_activity.session_id IS NULL");
335 row_modify("billing", "activity = 0",
336 "pid = '" . add_escape_custom($patient_id) . "' AND " .
337 "encounter = '" . add_escape_custom($encounter_id) . "' AND " .
338 "code_type = 'COPAY' AND " .
339 "activity = 1");
340 sqlStatement("UPDATE form_encounter SET last_level_billed = 0, " .
341 "last_level_closed = 0, stmt_count = 0, last_stmt_date = NULL " .
342 "WHERE pid = ? AND encounter = ?", array($patient_id, $encounter_id));
343 sqlStatement("UPDATE drug_sales SET billed = 0 WHERE " .
344 "pid = ? AND encounter = ?", array($patient_id, $encounter_id));
345 updateClaim(true, $patient_id, $encounter_id, -1, -1, 1, 0, ''); // clears for rebilling
347 else if ($transaction) {
348 if (!acl_check('admin', 'super')) die("Not authorized!");
349 row_delete("transactions", "id = '" . add_escape_custom($transaction) . "'");
351 else {
352 die("Nothing was recognized to delete!");
355 if (! $info_msg) $info_msg = xl('Delete successful.');
357 // Close this window and tell our opener that it's done.
359 echo "<script language='JavaScript'>\n";
360 if ($info_msg) echo " alert('" . addslashes($info_msg) . "');\n";
361 if ($encounterid) //this code need to be same as 'parent.imdeleted($encounterid)' when the popup is div like
363 echo "window.opener.imdeleted(" . attr($encounterid) . ");\n";
365 else
367 echo " if (opener && opener.imdeleted) opener.imdeleted(); else parent.imdeleted();\n";
369 echo " window.close();\n";
370 echo "</script></body></html>\n";
371 exit();
375 <form method='post' name="deletefrm" action='deleter.php?patient=<?php echo attr($patient) ?>&encounterid=<?php echo attr($encounterid) ?>&formid=<?php echo attr($formid) ?>&issue=<?php echo attr($issue) ?>&document=<?php echo attr($document) ?>&payment=<?php echo attr($payment) ?>&billing=<?php echo attr($billing) ?>&transaction=<?php echo attr($transaction) ?>' onsubmit="javascript:alert('1');document.deleform.submit();">
377 <p class="text">&nbsp;<br><?php echo xlt('Do you really want to delete'); ?>
379 <?php
380 if ($patient) {
381 echo xlt('patient') . " " . text($patient);
382 } else if ($encounterid) {
383 echo xlt('encounter') . " " . text($encounterid);
384 } else if ($formid) {
385 echo xlt('form') . " " . text($formid);
386 } else if ($issue) {
387 echo xlt('issue') . " " .text($issue);
388 } else if ($document) {
389 echo xlt('document') . " " . text($document);
390 } else if ($payment) {
391 echo xlt('payment') . " " .text($payment);
392 } else if ($billing) {
393 echo xlt('invoice') . " " . text($billing);
394 } else if ($transaction) {
395 echo xlt('transaction') . " " . text($transaction);
397 ?> <?php echo xlt('and all subordinate data? This action will be logged'); ?>!</p>
399 <center>
401 <p class="text">&nbsp;<br>
402 <a href="#" onclick="submit_form()" class="css_button"><span><?php echo xlt('Yes, Delete and Log'); ?></span></a>
403 <input type='hidden' name='form_submit' value='<?php echo xla('Yes, Delete and Log'); ?>'/>
404 <a href='#' class="css_button" onclick=popup_close();><span><?php echo xlt('No, Cancel');?></span></a>
405 </p>
407 </center>
408 </form>
409 </body>
410 </html>