added support for optional custom pdf logic
[openemr.git] / interface / patient_file / deleter.php
bloba0d314d7aca965f6f682ae83c4df6f440cfc35e8
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 if (substr($url, 0, 7) == 'file://') {
168 @unlink(substr($url, 7));
171 else if ($payment) {
172 if (!acl_check('admin', 'super')) die("Not authorized!");
173 list($patient_id, $timestamp) = explode(".", $payment);
174 $timestamp = decorateString('....-..-.. ..:..:..', $timestamp);
175 $payres = sqlStatement("SELECT * FROM payments WHERE " .
176 "pid = '$patient_id' AND dtime = '$timestamp'");
177 while ($payrow = sqlFetchArray($payres)) {
178 if ($payrow['amount1'] != 0) {
179 // Mark the payment as inactive.
180 row_modify("billing", "activity = 0",
181 "pid = '$patient_id' AND " .
182 "encounter = '" . $payrow['encounter'] . "' AND " .
183 "code_type = 'COPAY' AND " .
184 "fee = '" . (0 - $payrow['amount1']) . "' AND " .
185 "LEFT(date, 10) = '" . substr($timestamp, 0, 10) . "' AND " .
186 "activity = 1 LIMIT 1");
188 if ($payrow['amount2'] != 0) {
189 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2) {
190 $thissrc = '';
191 if ($payrow['method']) {
192 $thissrc .= $payrow['method'];
193 if ($payrow['source']) $thissrc .= ' ' . $payrow['source'];
195 $thissrc .= ' front office reversal';
196 $session_id = 0; // Is this OK?
197 arPostPayment($patient_id, $payrow['encounter'], $session_id,
198 0 - $payrow['amount2'], '', 0, $thissrc, 0);
200 else {
201 // Look up the matching invoice and post an offsetting payment.
202 slInitialize();
203 $invnum = "$patient_id." . $payrow['encounter'];
204 $thissrc = 'Pt/';
205 if ($payrow['method']) {
206 $thissrc .= $payrow['method'];
207 if ($payrow['source']) $thissrc .= ' ' . $payrow['source'];
209 $thissrc .= ' front office reversal';
210 $trans_id = SLQueryValue("SELECT id FROM ar WHERE " .
211 "ar.invnumber = '$invnum' LIMIT 1");
212 if ($trans_id) {
213 slPostPayment($trans_id, 0 - $payrow['amount2'], date('Y-m-d'),
214 $thissrc, '', 0, 0);
215 } else {
216 $info_msg .= "Invoice '$invnum' not found; could not delete its " .
217 "payment of \$" . $payrow['amount2'] . ". ";
219 SLClose();
222 row_delete("payments", "id = '" . $payrow['id'] . "'");
225 else if ($billing) {
226 if (!acl_check('acct','disc')) die("Not authorized!");
227 list($patient_id, $encounter_id) = explode(".", $billing);
228 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2) {
229 sqlStatement("DELETE FROM ar_activity WHERE " .
230 "pid = '$patient_id' AND encounter = '$encounter_id'");
231 sqlStatement("DELETE ar_session FROM ar_session LEFT JOIN " .
232 "ar_activity ON ar_session.session_id = ar_activity.session_id " .
233 "WHERE ar_activity.session_id IS NULL");
234 row_modify("billing", "activity = 0",
235 "pid = '$patient_id' AND " .
236 "encounter = '$encounter_id' AND " .
237 "code_type = 'COPAY' AND " .
238 "activity = 1");
239 sqlStatement("UPDATE form_encounter SET last_level_billed = 0, " .
240 "last_level_closed = 0, stmt_count = 0, last_stmt_date = NULL " .
241 "WHERE pid = '$patient_id' AND encounter = '$encounter_id'");
243 else {
244 slInitialize();
245 $trans_id = SLQueryValue("SELECT id FROM ar WHERE ar.invnumber = '$billing' LIMIT 1");
246 if ($trans_id) {
247 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], "Invoice $billing from SQL-Ledger");
248 SLQuery("DELETE FROM acc_trans WHERE trans_id = '$trans_id'");
249 if ($sl_err) die($sl_err);
250 SLQuery("DELETE FROM invoice WHERE trans_id = '$trans_id'");
251 if ($sl_err) die($sl_err);
252 SLQuery("DELETE FROM ar WHERE id = '$trans_id'");
253 if ($sl_err) die($sl_err);
254 } else {
255 $info_msg .= "Invoice '$billing' not found!";
257 SLClose();
259 sqlStatement("UPDATE drug_sales SET billed = 0 WHERE " .
260 "pid = '$patient_id' AND encounter = '$encounter_id'");
261 updateClaim(true, $patient_id, $encounter_id, -1, -1, 1, 0, ''); // clears for rebilling
263 else if ($transaction) {
264 if (!acl_check('admin', 'super')) die("Not authorized!");
265 row_delete("transactions", "id = '$transaction'");
267 else {
268 die("Nothing was recognized to delete!");
271 if (! $info_msg) $info_msg = xl('Delete successful.');
273 // Close this window and tell our opener that it's done.
275 echo "<script language='JavaScript'>\n";
276 if ($info_msg) echo " alert('$info_msg');\n";
277 echo " window.close();\n";
278 echo " if (opener.imdeleted) opener.imdeleted();\n";
279 echo "</script></body></html>\n";
280 exit();
284 <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 ?>'>
286 <p>&nbsp;<br><?php xl('
287 Do you really want to delete','e'); ?>
289 <?php
290 if ($patient) {
291 echo xl('patient') . " $patient";
292 } else if ($encounterid) {
293 echo xl('encounter') . " $encounterid";
294 } else if ($formid) {
295 echo xl('form') . " $formid";
296 } else if ($issue) {
297 echo xl('issue') . " $issue";
298 } else if ($document) {
299 echo xl('document') . " $document";
300 } else if ($payment) {
301 echo xl('payment') . " $payment";
302 } else if ($billing) {
303 echo xl('invoice') . " $billing";
304 } else if ($transaction) {
305 echo xl('transaction') . " $transaction";
307 ?> <?php xl('and all subordinate data? This action will be logged','e'); ?>!</p>
309 <center>
311 <p>&nbsp;<br>
312 <input type='submit' name='form_submit' value=<?php xl('Yes, Delete and Log','e','\'','\''); ?> />
313 &nbsp;
314 <input type='button' value=<?php xl('No, Cancel','e','\'','\''); ?> onclick='window.close()' />
315 </p>
317 </center>
318 </form>
319 </body>
320 </html>