apostrophe bug fix in drug name, tracker artifact-2926772
[openemr.git] / interface / patient_file / deleter.php
blob7e2687558c39978362d21fa9ae39acd5fa9370ce
1 <?php
2 // Copyright (C) 2005-2009 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 require_once("../globals.php");
10 require_once("$srcdir/log.inc");
11 require_once("$srcdir/acl.inc");
12 require_once("$srcdir/sl_eob.inc.php");
14 $patient = $_REQUEST['patient'];
15 $encounterid = $_REQUEST['encounterid'];
16 $formid = $_REQUEST['formid'];
17 $issue = $_REQUEST['issue'];
18 $document = $_REQUEST['document'];
19 $payment = $_REQUEST['payment'];
20 $billing = $_REQUEST['billing'];
21 $transaction = $_REQUEST['transaction'];
23 $info_msg = "";
25 // Delete rows, with logging, for the specified table using the
26 // specified WHERE clause.
28 function row_delete($table, $where) {
29 $tres = sqlStatement("SELECT * FROM $table WHERE $where");
30 $count = 0;
31 while ($trow = sqlFetchArray($tres)) {
32 $logstring = "";
33 foreach ($trow as $key => $value) {
34 if (! $value || $value == '0000-00-00 00:00:00') continue;
35 if ($logstring) $logstring .= " ";
36 $logstring .= $key . "='" . addslashes($value) . "'";
38 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], "$table: $logstring");
39 ++$count;
41 if ($count) {
42 $query = "DELETE FROM $table WHERE $where";
43 echo $query . "<br>\n";
44 sqlStatement($query);
48 // Deactivate rows, with logging, for the specified table using the
49 // specified SET and WHERE clauses.
51 function row_modify($table, $set, $where) {
52 if (sqlQuery("SELECT * FROM $table WHERE $where")) {
53 newEvent("deactivate", $_SESSION['authUser'], $_SESSION['authProvider'], "$table: $where");
54 $query = "UPDATE $table SET $set WHERE $where";
55 echo $query . "<br>\n";
56 sqlStatement($query);
60 // We use this to put dashes, colons, etc. back into a timestamp.
62 function decorateString($fmt, $str) {
63 $res = '';
64 while ($fmt) {
65 $fc = substr($fmt, 0, 1);
66 $fmt = substr($fmt, 1);
67 if ($fc == '.') {
68 $res .= substr($str, 0, 1);
69 $str = substr($str, 1);
70 } else {
71 $res .= $fc;
74 return $res;
77 // Delete a form's data from its form-specific table.
79 function form_delete($formdir, $formid) {
80 $formdir = ($formdir == 'newpatient') ? 'encounter' : $formdir;
81 if (substr($formdir,0,3) == 'LBF')
82 row_delete("lbf_data", "form_id = '$formid'");
83 else
84 row_delete("form_$formdir", "id = '$formid'");
88 <html>
89 <head>
90 <?php html_header_show();?>
91 <title><?php xl('Delete Patient, Encounter, Form, Issue, Document, Payment, Billing or Transaction','e'); ?></title>
92 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
94 <style>
95 td { font-size:10pt; }
96 </style>
98 </head>
100 <body class="body_top">
101 <?php
102 // If the delete is confirmed...
104 if ($_POST['form_submit']) {
106 if ($patient) {
107 if (!acl_check('admin', 'super')) die("Not authorized!");
108 row_modify("billing" , "activity = 0", "pid = '$patient'");
109 row_modify("pnotes" , "activity = 0", "pid = '$patient'");
110 // row_modify("prescriptions" , "active = 0" , "patient_id = '$patient'");
111 row_delete("prescriptions" , "patient_id = '$patient'");
112 row_delete("claims" , "patient_id = '$patient'");
113 row_delete("drug_sales" , "pid = '$patient'");
114 row_delete("payments" , "pid = '$patient'");
115 row_delete("ar_activity" , "pid = '$patient'");
116 row_delete("openemr_postcalendar_events", "pc_pid = '$patient'");
117 row_delete("immunizations" , "patient_id = '$patient'");
118 row_delete("issue_encounter", "pid = '$patient'");
119 row_delete("lists" , "pid = '$patient'");
120 row_delete("transactions" , "pid = '$patient'");
121 row_delete("employer_data" , "pid = '$patient'");
122 row_delete("history_data" , "pid = '$patient'");
123 row_delete("insurance_data" , "pid = '$patient'");
124 row_delete("patient_data" , "pid = '$patient'");
126 $res = sqlStatement("SELECT * FROM forms WHERE pid = '$patient'");
127 while ($row = sqlFetchArray($res)) {
128 form_delete($row['formdir'], $row['form_id']);
130 row_delete("forms", "pid = '$patient'");
132 $row = sqlQuery("SELECT id FROM patient_data WHERE pid = '$patient'");
133 row_delete("integration_mapping", "local_table = 'patient_data' AND " .
134 "local_id = '" . $row['id'] . "'");
136 else if ($encounterid) {
137 if (!acl_check('admin', 'super')) die("Not authorized!");
138 row_modify("billing", "activity = 0", "encounter = '$encounterid'");
139 row_delete("ar_activity", "pid = '$patient' AND encounter = '$encounterid'");
140 row_delete("claims", "encounter_id = '$encounterid'");
141 row_delete("issue_encounter", "encounter = '$encounterid'");
142 $res = sqlStatement("SELECT * FROM forms WHERE encounter = '$encounterid'");
143 while ($row = sqlFetchArray($res)) {
144 form_delete($row['formdir'], $row['form_id']);
146 row_delete("forms", "encounter = '$encounterid'");
148 else if ($formid) {
149 if (!acl_check('admin', 'super')) die("Not authorized!");
150 $row = sqlQuery("SELECT * FROM forms WHERE id = '$formid'");
151 $formdir = $row['formdir'];
152 if (! $formdir) die("There is no form with id '$formid'");
153 form_delete($formdir, $row['form_id']);
154 row_delete("forms", "id = '$formid'");
156 else if ($issue) {
157 if (!acl_check('admin', 'super')) die("Not authorized!");
158 row_delete("issue_encounter", "list_id = '$issue'");
159 row_delete("lists", "id = '$issue'");
161 else if ($document) {
162 if (!acl_check('admin', 'super')) die("Not authorized!");
163 $trow = sqlQuery("SELECT url FROM documents WHERE id = '$document'");
164 $url = $trow['url'];
165 row_delete("categories_to_documents", "document_id = '$document'");
166 row_delete("documents", "id = '$document'");
167 row_delete("gprelations", "type1 = 1 AND id1 = '$document'");
168 if (substr($url, 0, 7) == 'file://') {
169 @unlink(substr($url, 7));
172 else if ($payment) {
173 if (!acl_check('admin', 'super')) die("Not authorized!");
174 list($patient_id, $timestamp) = explode(".", $payment);
175 $timestamp = decorateString('....-..-.. ..:..:..', $timestamp);
176 $payres = sqlStatement("SELECT * FROM payments WHERE " .
177 "pid = '$patient_id' AND dtime = '$timestamp'");
178 while ($payrow = sqlFetchArray($payres)) {
179 if ($payrow['amount1'] != 0) {
180 // Mark the payment as inactive.
181 row_modify("billing", "activity = 0",
182 "pid = '$patient_id' AND " .
183 "encounter = '" . $payrow['encounter'] . "' AND " .
184 "code_type = 'COPAY' AND " .
185 "fee = '" . (0 - $payrow['amount1']) . "' AND " .
186 "LEFT(date, 10) = '" . substr($timestamp, 0, 10) . "' AND " .
187 "activity = 1 LIMIT 1");
189 if ($payrow['amount2'] != 0) {
190 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2) {
191 $thissrc = '';
192 if ($payrow['method']) {
193 $thissrc .= $payrow['method'];
194 if ($payrow['source']) $thissrc .= ' ' . $payrow['source'];
196 $thissrc .= ' front office reversal';
197 $session_id = 0; // Is this OK?
198 arPostPayment($patient_id, $payrow['encounter'], $session_id,
199 0 - $payrow['amount2'], '', 0, $thissrc, 0);
201 else {
202 // Look up the matching invoice and post an offsetting payment.
203 slInitialize();
204 $invnum = "$patient_id." . $payrow['encounter'];
205 $thissrc = 'Pt/';
206 if ($payrow['method']) {
207 $thissrc .= $payrow['method'];
208 if ($payrow['source']) $thissrc .= ' ' . $payrow['source'];
210 $thissrc .= ' front office reversal';
211 $trans_id = SLQueryValue("SELECT id FROM ar WHERE " .
212 "ar.invnumber = '$invnum' LIMIT 1");
213 if ($trans_id) {
214 slPostPayment($trans_id, 0 - $payrow['amount2'], date('Y-m-d'),
215 $thissrc, '', 0, 0);
216 } else {
217 $info_msg .= "Invoice '$invnum' not found; could not delete its " .
218 "payment of \$" . $payrow['amount2'] . ". ";
220 SLClose();
223 row_delete("payments", "id = '" . $payrow['id'] . "'");
226 else if ($billing) {
227 if (!acl_check('acct','disc')) die("Not authorized!");
228 list($patient_id, $encounter_id) = explode(".", $billing);
229 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2) {
230 sqlStatement("DELETE FROM ar_activity WHERE " .
231 "pid = '$patient_id' AND encounter = '$encounter_id'");
232 sqlStatement("DELETE ar_session FROM ar_session LEFT JOIN " .
233 "ar_activity ON ar_session.session_id = ar_activity.session_id " .
234 "WHERE ar_activity.session_id IS NULL");
235 row_modify("billing", "activity = 0",
236 "pid = '$patient_id' AND " .
237 "encounter = '$encounter_id' AND " .
238 "code_type = 'COPAY' AND " .
239 "activity = 1");
240 sqlStatement("UPDATE form_encounter SET last_level_billed = 0, " .
241 "last_level_closed = 0, stmt_count = 0, last_stmt_date = NULL " .
242 "WHERE pid = '$patient_id' AND encounter = '$encounter_id'");
244 else {
245 slInitialize();
246 $trans_id = SLQueryValue("SELECT id FROM ar WHERE ar.invnumber = '$billing' LIMIT 1");
247 if ($trans_id) {
248 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], "Invoice $billing from SQL-Ledger");
249 SLQuery("DELETE FROM acc_trans WHERE trans_id = '$trans_id'");
250 if ($sl_err) die($sl_err);
251 SLQuery("DELETE FROM invoice WHERE trans_id = '$trans_id'");
252 if ($sl_err) die($sl_err);
253 SLQuery("DELETE FROM ar WHERE id = '$trans_id'");
254 if ($sl_err) die($sl_err);
255 } else {
256 $info_msg .= "Invoice '$billing' not found!";
258 SLClose();
260 sqlStatement("UPDATE drug_sales SET billed = 0 WHERE " .
261 "pid = '$patient_id' AND encounter = '$encounter_id'");
262 updateClaim(true, $patient_id, $encounter_id, -1, -1, 1, 0, ''); // clears for rebilling
264 else if ($transaction) {
265 if (!acl_check('admin', 'super')) die("Not authorized!");
266 row_delete("transactions", "id = '$transaction'");
268 else {
269 die("Nothing was recognized to delete!");
272 if (! $info_msg) $info_msg = xl('Delete successful.');
274 // Close this window and tell our opener that it's done.
276 echo "<script language='JavaScript'>\n";
277 if ($info_msg) echo " alert('$info_msg');\n";
278 echo " window.close();\n";
279 echo " if (opener.imdeleted) opener.imdeleted();\n";
280 echo "</script></body></html>\n";
281 exit();
285 <form method='post' action='deleter.php?patient=<?php echo $patient ?>&encounterid=<?php echo $encounterid ?>&formid=<?php echo $formid ?>&issue=<?php echo $issue ?>&document=<?php echo $document ?>&payment=<?php echo $payment ?>&billing=<?php echo $billing ?>&transaction=<?php echo $transaction ?>'>
287 <p>&nbsp;<br><?php xl('
288 Do you really want to delete','e'); ?>
290 <?php
291 if ($patient) {
292 echo xl('patient') . " $patient";
293 } else if ($encounterid) {
294 echo xl('encounter') . " $encounterid";
295 } else if ($formid) {
296 echo xl('form') . " $formid";
297 } else if ($issue) {
298 echo xl('issue') . " $issue";
299 } else if ($document) {
300 echo xl('document') . " $document";
301 } else if ($payment) {
302 echo xl('payment') . " $payment";
303 } else if ($billing) {
304 echo xl('invoice') . " $billing";
305 } else if ($transaction) {
306 echo xl('transaction') . " $transaction";
308 ?> <?php xl('and all subordinate data? This action will be logged','e'); ?>!</p>
310 <center>
312 <p>&nbsp;<br>
313 <input type='submit' name='form_submit' value=<?php xl('Yes, Delete and Log','e','\'','\''); ?> />
314 &nbsp;
315 <input type='button' value=<?php xl('No, Cancel','e','\'','\''); ?> onclick='window.close()' />
316 </p>
318 </center>
319 </form>
320 </body>
321 </html>