Fixes for switching encounters
[openemr.git] / interface / patient_file / deleter.php
blob4c2bfaba93870c9892570adcda1cadf42c3e49cb
1 <?php
2 // Copyright (C) 2005-2010 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'], 1, "$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'], 1, "$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 and undo product sales for a given patient or visit.
78 // This is special because it has to replace the inventory.
80 function delete_drug_sales($patient_id, $encounter_id=0) {
81 $where = $encounter_id ? "ds.encounter = '$encounter_id'" :
82 "ds.pid = '$patient_id' AND ds.encounter != 0";
83 sqlStatement("UPDATE drug_sales AS ds, drug_inventory AS di " .
84 "SET di.on_hand = di.on_hand + ds.quantity " .
85 "WHERE $where AND di.inventory_id = ds.inventory_id");
86 if ($encounter_id) {
87 row_delete("drug_sales", "encounter = '$encounter_id'");
89 else {
90 row_delete("drug_sales", "pid = '$patient_id'");
94 // Delete a form's data from its form-specific table.
96 function form_delete($formdir, $formid) {
97 $formdir = ($formdir == 'newpatient') ? 'encounter' : $formdir;
98 if (substr($formdir,0,3) == 'LBF')
99 row_delete("lbf_data", "form_id = '$formid'");
100 else
101 row_delete("form_$formdir", "id = '$formid'");
105 <html>
106 <head>
107 <?php html_header_show();?>
108 <title><?php xl('Delete Patient, Encounter, Form, Issue, Document, Payment, Billing or Transaction','e'); ?></title>
109 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
111 <style>
112 td { font-size:10pt; }
113 </style>
115 <script language="javascript">
116 function submit_form()
118 document.deletefrm.submit();
120 </script>
121 </head>
123 <body class="body_top">
124 <?php
125 // If the delete is confirmed...
127 if ($_POST['form_submit']) {
129 if ($patient) {
130 if (!acl_check('admin', 'super')) die("Not authorized!");
131 row_modify("billing" , "activity = 0", "pid = '$patient'");
132 row_modify("pnotes" , "activity = 0", "pid = '$patient'");
133 // row_modify("prescriptions" , "active = 0" , "patient_id = '$patient'");
134 row_delete("prescriptions" , "patient_id = '$patient'");
135 row_delete("claims" , "patient_id = '$patient'");
136 delete_drug_sales($patient);
137 row_delete("payments" , "pid = '$patient'");
138 row_delete("ar_activity" , "pid = '$patient'");
139 row_delete("openemr_postcalendar_events", "pc_pid = '$patient'");
140 row_delete("immunizations" , "patient_id = '$patient'");
141 row_delete("issue_encounter", "pid = '$patient'");
142 row_delete("lists" , "pid = '$patient'");
143 row_delete("transactions" , "pid = '$patient'");
144 row_delete("employer_data" , "pid = '$patient'");
145 row_delete("history_data" , "pid = '$patient'");
146 row_delete("insurance_data" , "pid = '$patient'");
147 row_delete("patient_data" , "pid = '$patient'");
149 $res = sqlStatement("SELECT * FROM forms WHERE pid = '$patient'");
150 while ($row = sqlFetchArray($res)) {
151 form_delete($row['formdir'], $row['form_id']);
153 row_delete("forms", "pid = '$patient'");
155 $row = sqlQuery("SELECT id FROM patient_data WHERE pid = '$patient'");
156 row_delete("integration_mapping", "local_table = 'patient_data' AND " .
157 "local_id = '" . $row['id'] . "'");
159 else if ($encounterid) {
160 if (!acl_check('admin', 'super')) die("Not authorized!");
161 row_modify("billing", "activity = 0", "encounter = '$encounterid'");
162 delete_drug_sales(0, $encounterid);
163 row_delete("ar_activity", "encounter = '$encounterid'");
164 row_delete("claims", "encounter_id = '$encounterid'");
165 row_delete("issue_encounter", "encounter = '$encounterid'");
166 $res = sqlStatement("SELECT * FROM forms WHERE encounter = '$encounterid'");
167 while ($row = sqlFetchArray($res)) {
168 form_delete($row['formdir'], $row['form_id']);
170 row_delete("forms", "encounter = '$encounterid'");
172 else if ($formid) {
173 if (!acl_check('admin', 'super')) die("Not authorized!");
174 $row = sqlQuery("SELECT * FROM forms WHERE id = '$formid'");
175 $formdir = $row['formdir'];
176 if (! $formdir) die("There is no form with id '$formid'");
177 form_delete($formdir, $row['form_id']);
178 row_delete("forms", "id = '$formid'");
180 else if ($issue) {
181 if (!acl_check('admin', 'super')) die("Not authorized!");
182 row_delete("issue_encounter", "list_id = '$issue'");
183 row_delete("lists", "id = '$issue'");
185 else if ($document) {
186 if (!acl_check('admin', 'super')) die("Not authorized!");
187 $trow = sqlQuery("SELECT url FROM documents WHERE id = '$document'");
188 $url = $trow['url'];
189 row_delete("categories_to_documents", "document_id = '$document'");
190 row_delete("documents", "id = '$document'");
191 row_delete("gprelations", "type1 = 1 AND id1 = '$document'");
192 if (substr($url, 0, 7) == 'file://') {
193 @unlink(substr($url, 7));
196 else if ($payment) {
197 if (!acl_check('admin', 'super')) die("Not authorized!");
198 list($patient_id, $timestamp) = explode(".", $payment);
199 $timestamp = decorateString('....-..-.. ..:..:..', $timestamp);
200 $payres = sqlStatement("SELECT * FROM payments WHERE " .
201 "pid = '$patient_id' AND dtime = '$timestamp'");
202 while ($payrow = sqlFetchArray($payres)) {
203 if ($payrow['amount1'] != 0) {
204 // Mark the payment as inactive.
205 row_modify("billing", "activity = 0",
206 "pid = '$patient_id' AND " .
207 "encounter = '" . $payrow['encounter'] . "' AND " .
208 "code_type = 'COPAY' AND " .
209 "fee = '" . (0 - $payrow['amount1']) . "' AND " .
210 "LEFT(date, 10) = '" . substr($timestamp, 0, 10) . "' AND " .
211 "activity = 1 LIMIT 1");
213 if ($payrow['amount2'] != 0) {
214 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2) {
215 $thissrc = '';
216 if ($payrow['method']) {
217 $thissrc .= $payrow['method'];
218 if ($payrow['source']) $thissrc .= ' ' . $payrow['source'];
220 $thissrc .= ' front office reversal';
221 $session_id = 0; // Is this OK?
222 arPostPayment($patient_id, $payrow['encounter'], $session_id,
223 0 - $payrow['amount2'], '', 0, $thissrc, 0);
225 else {
226 // Look up the matching invoice and post an offsetting payment.
227 slInitialize();
228 $invnum = "$patient_id." . $payrow['encounter'];
229 $thissrc = 'Pt/';
230 if ($payrow['method']) {
231 $thissrc .= $payrow['method'];
232 if ($payrow['source']) $thissrc .= ' ' . $payrow['source'];
234 $thissrc .= ' front office reversal';
235 $trans_id = SLQueryValue("SELECT id FROM ar WHERE " .
236 "ar.invnumber = '$invnum' LIMIT 1");
237 if ($trans_id) {
238 slPostPayment($trans_id, 0 - $payrow['amount2'], date('Y-m-d'),
239 $thissrc, '', 0, 0);
240 } else {
241 $info_msg .= "Invoice '$invnum' not found; could not delete its " .
242 "payment of \$" . $payrow['amount2'] . ". ";
244 SLClose();
247 row_delete("payments", "id = '" . $payrow['id'] . "'");
250 else if ($billing) {
251 if (!acl_check('acct','disc')) die("Not authorized!");
252 list($patient_id, $encounter_id) = explode(".", $billing);
253 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2) {
254 sqlStatement("DELETE FROM ar_activity WHERE " .
255 "pid = '$patient_id' AND encounter = '$encounter_id'");
256 sqlStatement("DELETE ar_session FROM ar_session LEFT JOIN " .
257 "ar_activity ON ar_session.session_id = ar_activity.session_id " .
258 "WHERE ar_activity.session_id IS NULL");
259 row_modify("billing", "activity = 0",
260 "pid = '$patient_id' AND " .
261 "encounter = '$encounter_id' AND " .
262 "code_type = 'COPAY' AND " .
263 "activity = 1");
264 sqlStatement("UPDATE form_encounter SET last_level_billed = 0, " .
265 "last_level_closed = 0, stmt_count = 0, last_stmt_date = NULL " .
266 "WHERE pid = '$patient_id' AND encounter = '$encounter_id'");
268 else {
269 slInitialize();
270 $trans_id = SLQueryValue("SELECT id FROM ar WHERE ar.invnumber = '$billing' LIMIT 1");
271 if ($trans_id) {
272 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "Invoice $billing from SQL-Ledger");
273 SLQuery("DELETE FROM acc_trans WHERE trans_id = '$trans_id'");
274 if ($sl_err) die($sl_err);
275 SLQuery("DELETE FROM invoice WHERE trans_id = '$trans_id'");
276 if ($sl_err) die($sl_err);
277 SLQuery("DELETE FROM ar WHERE id = '$trans_id'");
278 if ($sl_err) die($sl_err);
279 } else {
280 $info_msg .= "Invoice '$billing' not found!";
282 SLClose();
284 sqlStatement("UPDATE drug_sales SET billed = 0 WHERE " .
285 "pid = '$patient_id' AND encounter = '$encounter_id'");
286 updateClaim(true, $patient_id, $encounter_id, -1, -1, 1, 0, ''); // clears for rebilling
288 else if ($transaction) {
289 if (!acl_check('admin', 'super')) die("Not authorized!");
290 row_delete("transactions", "id = '$transaction'");
292 else {
293 die("Nothing was recognized to delete!");
296 if (! $info_msg) $info_msg = xl('Delete successful.');
298 // Close this window and tell our opener that it's done.
300 echo "<script language='JavaScript'>\n";
301 if ($info_msg) echo " alert('$info_msg');\n";
302 if ($encounterid) //this code need to be same as 'parent.imdeleted($encounterid)' when the popup is div like
304 echo "window.opener.imdeleted($encounterid);\n";
306 else
308 echo "parent.imdeleted();\n";
310 echo "</script></body></html>\n";
311 exit();
315 <form method='post' name="deletefrm" 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 ?>' onsubmit="javascript:alert('1');document.deleform.submit();">
317 <p class="text">&nbsp;<br><?php xl('Do you really want to delete','e'); ?>
319 <?php
320 if ($patient) {
321 echo xl('patient') . " $patient";
322 } else if ($encounterid) {
323 echo xl('encounter') . " $encounterid";
324 } else if ($formid) {
325 echo xl('form') . " $formid";
326 } else if ($issue) {
327 echo xl('issue') . " $issue";
328 } else if ($document) {
329 echo xl('document') . " $document";
330 } else if ($payment) {
331 echo xl('payment') . " $payment";
332 } else if ($billing) {
333 echo xl('invoice') . " $billing";
334 } else if ($transaction) {
335 echo xl('transaction') . " $transaction";
337 ?> <?php xl('and all subordinate data? This action will be logged','e'); ?>!</p>
339 <center>
341 <p class="text">&nbsp;<br>
342 <a href="#" onclick="submit_form()" class="css_button"><span><?php xl('Yes, Delete and Log','e'); ?></span></a>
343 <input type='hidden' name='form_submit' value=<?php xl('Yes, Delete and Log','e','\'','\''); ?>/>
344 <a href='#' class="css_button" onclick='parent.$.fn.fancybox.close();' /><span><?php echo xl('No, Cancel');?></span></a>
345 </p>
347 </center>
348 </form>
349 </body>
350 </html>