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>;.
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'];
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");
52 while ($trow = sqlFetchArray($tres)) {
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");
63 $query = "DELETE FROM $table WHERE $where";
64 echo text($query) . "<br>\n";
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";
81 // We use this to put dashes, colons, etc. back into a timestamp.
83 function decorateString($fmt, $str) {
86 $fc = substr($fmt, 0, 1);
87 $fmt = substr($fmt, 1);
89 $res .= substr($str, 0, 1);
90 $str = substr($str, 1);
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");
108 row_delete("drug_sales", "encounter = '" . add_escape_custom($encounter_id) . "'");
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) . "'");
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 FROM documents WHERE id = ?", array($document));
146 row_delete("categories_to_documents", "document_id = '" . add_escape_custom($document) . "'");
147 row_delete("documents", "id = '" . add_escape_custom($document) . "'");
148 row_delete("gprelations", "type1 = 1 AND id1 = '" . add_escape_custom($document) . "'");
149 if (substr($url, 0, 7) == 'file://') {
150 @unlink
(substr($url, 7));
156 <?php
html_header_show();?
>
157 <title
><?php
echo xlt('Delete Patient, Encounter, Form, Issue, Document, Payment, Billing or Transaction'); ?
></title
>
158 <link rel
="stylesheet" href
='<?php echo $css_header ?>' type
='text/css'>
161 td
{ font
-size
:10pt
; }
164 <script language
="javascript">
165 function submit_form()
167 document
.deletefrm
.submit();
169 // Java script function for closing the popup
170 function popup_close() {
171 if(parent
.$
==undefined
) {
175 parent
.$
.fn
.fancybox
.close();
181 <body
class="body_top">
183 // If the delete is confirmed...
185 if ($_POST['form_submit']) {
188 if (!acl_check('admin', 'super') ||
!$GLOBALS['allow_pat_delete']) die("Not authorized!");
189 row_modify("billing" , "activity = 0", "pid = '" . add_escape_custom($patient) . "'");
190 row_modify("pnotes" , "deleted = 1" , "pid = '" . add_escape_custom($patient) . "'");
191 // row_modify("prescriptions" , "active = 0" , "patient_id = '$patient'");
192 row_delete("prescriptions" , "patient_id = '" . add_escape_custom($patient) . "'");
193 row_delete("claims" , "patient_id = '" . add_escape_custom($patient) . "'");
194 delete_drug_sales($patient);
195 row_delete("payments" , "pid = '" . add_escape_custom($patient) . "'");
196 row_delete("ar_activity" , "pid = '" . add_escape_custom($patient) . "'");
197 row_delete("openemr_postcalendar_events", "pc_pid = '" . add_escape_custom($patient) . "'");
198 row_delete("immunizations" , "patient_id = '" . add_escape_custom($patient) . "'");
199 row_delete("issue_encounter", "pid = '" . add_escape_custom($patient) . "'");
200 row_delete("lists" , "pid = '" . add_escape_custom($patient) . "'");
201 row_delete("transactions" , "pid = '" . add_escape_custom($patient) . "'");
202 row_delete("employer_data" , "pid = '" . add_escape_custom($patient) . "'");
203 row_delete("history_data" , "pid = '" . add_escape_custom($patient) . "'");
204 row_delete("insurance_data" , "pid = '" . add_escape_custom($patient) . "'");
206 $res = sqlStatement("SELECT * FROM forms WHERE pid = ?", array($patient));
207 while ($row = sqlFetchArray($res)) {
208 form_delete($row['formdir'], $row['form_id']);
210 row_delete("forms", "pid = '" . add_escape_custom($patient) . "'");
212 // integration_mapping was used for sql-ledger (which is no longer supported) and is virtually obsolete now.
213 $row = sqlQuery("SELECT id FROM patient_data WHERE pid = ?", array($patient));
214 row_delete("integration_mapping", "local_table = 'patient_data' AND " .
215 "local_id = '" . add_escape_custom($row['id']) . "'");
217 // Delete all documents for the patient.
218 $res = sqlStatement("SELECT id FROM documents WHERE foreign_id = ?", array($patient));
219 while ($row = sqlFetchArray($res)) {
220 delete_document($row['id']);
223 // This table exists only for athletic teams.
224 $tmp = sqlQuery("SHOW TABLES LIKE 'daily_fitness'");
226 row_delete("daily_fitness", "pid = '" . add_escape_custom($patient) . "'");
229 row_delete("patient_data", "pid = '" . add_escape_custom($patient) . "'");
231 else if ($encounterid) {
232 if (!acl_check('admin', 'super')) die("Not authorized!");
233 row_modify("billing", "activity = 0", "encounter = '" . add_escape_custom($encounterid) . "'");
234 delete_drug_sales(0, $encounterid);
235 row_delete("ar_activity", "encounter = '" . add_escape_custom($encounterid) . "'");
236 row_delete("claims", "encounter_id = '" . add_escape_custom($encounterid) . "'");
237 row_delete("issue_encounter", "encounter = '" . add_escape_custom($encounterid) . "'");
238 $res = sqlStatement("SELECT * FROM forms WHERE encounter = ?", array($encounterid));
239 while ($row = sqlFetchArray($res)) {
240 form_delete($row['formdir'], $row['form_id']);
242 row_delete("forms", "encounter = '" . add_escape_custom($encounterid) . "'");
245 if (!acl_check('admin', 'super')) die("Not authorized!");
246 $row = sqlQuery("SELECT * FROM forms WHERE id = ?", array($formid));
247 $formdir = $row['formdir'];
248 if (! $formdir) die("There is no form with id '" . text($formid) . "'");
249 form_delete($formdir, $row['form_id']);
250 row_delete("forms", "id = '" . add_escape_custom($formid) . "'");
253 if (!acl_check('admin', 'super')) die("Not authorized!");
254 row_delete("issue_encounter", "list_id = '" . add_escape_custom($issue) ."'");
255 row_delete("lists", "id = '" . add_escape_custom($issue) ."'");
257 else if ($document) {
258 if (!acl_check('admin', 'super')) die("Not authorized!");
259 delete_document($document);
262 if (!acl_check('admin', 'super')) die("Not authorized!");
263 list($patient_id, $timestamp, $ref_id) = explode(".", $payment);
264 // if (empty($ref_id)) $ref_id = -1;
265 $timestamp = decorateString('....-..-.. ..:..:..', $timestamp);
266 $payres = sqlStatement("SELECT * FROM payments WHERE " .
267 "pid = ? AND dtime = ?", array($patient_id, $timestamp));
268 while ($payrow = sqlFetchArray($payres)) {
269 if ($payrow['encounter']) {
271 // The session ID passed in is useless. Look for the most recent
272 // patient payment session with pay total matching pay amount and with
273 // no adjustments. The resulting session ID may be 0 (no session) which
274 // is why we start with -1.
275 $tpmt = $payrow['amount1'] +
$payrow['amount2'];
276 $seres = sqlStatement("SELECT " .
277 "SUM(pay_amount) AS pay_amount, session_id " .
278 "FROM ar_activity WHERE " .
280 "encounter = ? AND " .
281 "payer_type = 0 AND " .
282 "adj_amount = 0.00 " .
283 "GROUP BY session_id ORDER BY session_id DESC", array($patient_id, $payrow['encounter']));
284 while ($serow = sqlFetchArray($seres)) {
285 if (sprintf("%01.2f", $serow['adj_amount']) != 0.00) continue;
286 if (sprintf("%01.2f", $serow['pay_amount'] - $tpmt) == 0.00) {
287 $ref_id = $serow['session_id'];
292 die(xlt('Unable to match this payment in ar_activity') . ": " . text($tpmt));
294 // Delete the payment.
295 row_delete("ar_activity",
296 "pid = '" . add_escape_custom($patient_id) . "' AND " .
297 "encounter = '" . add_escape_custom($payrow['encounter']) . "' AND " .
298 "payer_type = 0 AND " .
299 "pay_amount != 0.00 AND " .
300 "adj_amount = 0.00 AND " .
301 "session_id = '" . add_escape_custom($ref_id) . "'");
303 row_delete("ar_session",
304 "patient_id = '" . add_escape_custom($patient_id) ."' AND " .
305 "session_id = '" . add_escape_custom($ref_id) . "'");
309 // Encounter is 0! Seems this happens for pre-payments.
310 $tpmt = sprintf("%01.2f", $payrow['amount1'] +
$payrow['amount2']);
311 row_delete("ar_session",
312 "patient_id = '" . add_escape_custom($patient_id) . "' AND " .
313 "payer_id = 0 AND " .
314 "reference = '" . add_escape_custom($payrow['source']) . "' AND " .
315 "pay_total = '" . add_escape_custom($tpmt) . "' AND " .
316 "(SELECT COUNT(*) FROM ar_activity where ar_activity.session_id = ar_session.session_id) = 0 " .
317 "ORDER BY session_id DESC LIMIT 1");
319 row_delete("payments", "id = '" . add_escape_custom($payrow['id']) . "'");
323 if (!acl_check('acct','disc')) die("Not authorized!");
324 list($patient_id, $encounter_id) = explode(".", $billing);
325 sqlStatement("DELETE FROM ar_activity WHERE " .
326 "pid = ? AND encounter = ? ", array($patient_id, $encounter_id) );
327 sqlStatement("DELETE ar_session FROM ar_session LEFT JOIN " .
328 "ar_activity ON ar_session.session_id = ar_activity.session_id " .
329 "WHERE ar_activity.session_id IS NULL");
330 row_modify("billing", "activity = 0",
331 "pid = '" . add_escape_custom($patient_id) . "' AND " .
332 "encounter = '" . add_escape_custom($encounter_id) . "' AND " .
333 "code_type = 'COPAY' AND " .
335 sqlStatement("UPDATE form_encounter SET last_level_billed = 0, " .
336 "last_level_closed = 0, stmt_count = 0, last_stmt_date = NULL " .
337 "WHERE pid = ? AND encounter = ?", array($patient_id, $encounter_id));
338 sqlStatement("UPDATE drug_sales SET billed = 0 WHERE " .
339 "pid = ? AND encounter = ?", array($patient_id, $encounter_id));
340 updateClaim(true, $patient_id, $encounter_id, -1, -1, 1, 0, ''); // clears for rebilling
342 else if ($transaction) {
343 if (!acl_check('admin', 'super')) die("Not authorized!");
344 row_delete("transactions", "id = '" . add_escape_custom($transaction) . "'");
347 die("Nothing was recognized to delete!");
350 if (! $info_msg) $info_msg = xl('Delete successful.');
352 // Close this window and tell our opener that it's done.
354 echo "<script language='JavaScript'>\n";
355 if ($info_msg) echo " alert('" . addslashes($info_msg) . "');\n";
356 if ($encounterid) //this code need to be same as 'parent.imdeleted($encounterid)' when the popup is div like
358 echo "window.opener.imdeleted(" . attr($encounterid) . ");\n";
362 echo " if (opener && opener.imdeleted) opener.imdeleted(); else parent.imdeleted();\n";
364 echo " window.close();\n";
365 echo "</script></body></html>\n";
370 <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();">
372 <p
class="text"> 
;<br
><?php
echo xlt('Do you really want to delete'); ?
>
376 echo xlt('patient') . " " . text($patient);
377 } else if ($encounterid) {
378 echo xlt('encounter') . " " . text($encounterid);
379 } else if ($formid) {
380 echo xlt('form') . " " . text($formid);
382 echo xlt('issue') . " " .text($issue);
383 } else if ($document) {
384 echo xlt('document') . " " . text($document);
385 } else if ($payment) {
386 echo xlt('payment') . " " .text($payment);
387 } else if ($billing) {
388 echo xlt('invoice') . " " . text($billing);
389 } else if ($transaction) {
390 echo xlt('transaction') . " " . text($transaction);
392 ?
> <?php
echo xlt('and all subordinate data? This action will be logged'); ?
>!</p
>
396 <p
class="text"> 
;<br
>
397 <a href
="#" onclick
="submit_form()" class="css_button"><span
><?php
echo xlt('Yes, Delete and Log'); ?
></span
></a
>
398 <input type
='hidden' name
='form_submit' value
='<?php echo xla('Yes
, Delete
and Log
'); ?>'/>
399 <a href
='#' class="css_button" onclick
=popup_close();><span
><?php
echo xlt('No, Cancel');?
></span
></a
>