major improvements for keeping a history of claims, and to save and use prior insuran...
[openemr.git] / interface / patient_file / deleter.php
blob21178060713d6c9520914bbea869afedc59cd3fb
1 <?
2 // Copyright (C) 2005, 2006 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 include_once("../globals.php");
10 include_once("$srcdir/log.inc");
11 include_once("$srcdir/acl.inc");
13 $patient = $_REQUEST['patient'];
14 $encounterid = $_REQUEST['encounterid'];
15 $formid = $_REQUEST['formid'];
16 $issue = $_REQUEST['issue'];
17 $document = $_REQUEST['document'];
19 $info_msg = "";
21 $thisauth = acl_check('admin', 'super');
22 if (! $thisauth) die("Not authorized!");
24 // Delete rows, with logging, for the specified table using the
25 // specified WHERE clause.
27 function row_delete($table, $where) {
28 $tres = sqlStatement("SELECT * FROM $table WHERE $where");
29 $count = 0;
30 while ($trow = sqlFetchArray($tres)) {
31 $logstring = "";
32 foreach ($trow as $key => $value) {
33 if (! $value || $value == '0000-00-00 00:00:00') continue;
34 if ($logstring) $logstring .= " ";
35 $logstring .= $key . "='" . addslashes($value) . "'";
37 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], "$table: $logstring");
38 ++$count;
40 if ($count) {
41 $query = "DELETE FROM $table WHERE $where";
42 echo $query . "<br>\n";
43 sqlStatement($query);
47 // Deactivate rows, with logging, for the specified table using the
48 // specified SET and WHERE clauses.
50 function row_modify($table, $set, $where) {
51 if (sqlQuery("SELECT * FROM $table WHERE $where")) {
52 newEvent("deactivate", $_SESSION['authUser'], $_SESSION['authProvider'], "$table: $where");
53 $query = "UPDATE $table SET $set WHERE $where";
54 echo $query . "<br>\n";
55 sqlStatement($query);
60 <html>
61 <head>
62 <title><? xl('Delete Patient, Encounter, Form, Issue or Document','e'); ?></title>
63 <link rel=stylesheet href='<? echo $css_header ?>' type='text/css'>
65 <style>
66 td { font-size:10pt; }
67 </style>
69 </head>
71 <body <?echo $top_bg_line;?>>
73 // If the delete is confirmed...
75 if ($_POST['form_submit']) {
77 if ($patient) {
78 row_modify("billing" , "activity = 0", "pid = '$patient'");
79 row_modify("pnotes" , "activity = 0", "pid = '$patient'");
80 row_modify("prescriptions" , "active = 0" , "patient_id = '$patient'");
82 row_delete("claims" , "patient_id = '$patient'");
83 row_delete("drug_sales" , "pid = '$patient'");
84 row_delete("payments" , "pid = '$patient'");
85 row_delete("openemr_postcalendar_events", "pc_pid = '$patient'");
86 row_delete("immunizations" , "patient_id = '$patient'");
87 row_delete("issue_encounter", "pid = '$patient'");
88 row_delete("lists" , "pid = '$patient'");
89 row_delete("transactions" , "pid = '$patient'");
90 row_delete("employer_data" , "pid = '$patient'");
91 row_delete("history_data" , "pid = '$patient'");
92 row_delete("insurance_data" , "pid = '$patient'");
93 row_delete("patient_data" , "pid = '$patient'");
95 $res = sqlStatement("SELECT * FROM forms WHERE pid = '$patient'");
96 while ($row = sqlFetchArray($res)) {
97 $formdir = ($row['formdir'] == 'newpatient') ? 'encounter' : $row['formdir'];
98 row_delete("form_$formdir", "id = '" . $row['form_id'] . "'");
100 row_delete("forms", "pid = '$patient'");
102 $row = sqlQuery("SELECT id FROM patient_data WHERE pid = '$patient'");
103 row_delete("integration_mapping", "local_table = 'patient_data' AND " .
104 "local_id = '" . $row['id'] . "'");
106 else if ($encounterid) {
107 row_modify("billing", "activity = 0", "encounter = '$encounterid'");
108 row_delete("claims", "encounter_id = '$encounterid'");
109 row_delete("issue_encounter", "encounter = '$encounterid'");
110 $res = sqlStatement("SELECT * FROM forms WHERE encounter = '$encounterid'");
111 while ($row = sqlFetchArray($res)) {
112 $formdir = ($row['formdir'] == 'newpatient') ? 'encounter' : $row['formdir'];
113 row_delete("form_$formdir", "id = '" . $row['form_id'] . "'");
115 row_delete("forms", "encounter = '$encounterid'");
117 else if ($formid) {
118 $row = sqlQuery("SELECT * FROM forms WHERE id = '$formid'");
119 $formdir = $row['formdir'];
120 if (! $formdir) die("There is no form with id '$formid'");
121 $formname = ($formdir == 'newpatient') ? 'encounter' : $formdir;
122 row_delete("form_$formname", "id = '" . $row['form_id'] . "'");
123 row_delete("forms", "id = '$formid'");
125 else if ($issue) {
126 row_delete("issue_encounter", "list_id = '$issue'");
127 row_delete("lists", "id = '$issue'");
129 else if ($document) {
130 $trow = sqlQuery("SELECT url FROM documents WHERE id = '$document'");
131 $url = $trow['url'];
132 row_delete("categories_to_documents", "document_id = '$document'");
133 row_delete("documents", "id = '$document'");
134 if (substr($url, 0, 7) == 'file://') {
135 @unlink(substr($url, 7));
138 else {
139 die("Nothing was specified to delete!");
142 if (! $info_msg) $info_msg = "Delete successful.";
144 // Close this window and tell our opener that it's done.
146 echo "<script language='JavaScript'>\n";
147 if ($info_msg) echo " alert('$info_msg');\n";
148 echo " window.close();\n";
149 echo " if (opener.imdeleted) opener.imdeleted();\n";
150 echo "</script></body></html>\n";
151 exit();
155 <form method='post' action='deleter.php?patient=<? echo $patient ?>&encounterid=<? echo $encounterid ?>&formid=<? echo $formid ?>&issue=<? echo $issue ?>&document=<? echo $document ?>'>
157 <p>&nbsp;<br><?php xl('
158 Do you really want to delete','e'); ?>
160 <?php
161 if ($patient) {
162 echo "patient $patient";
163 } else if ($encounterid) {
164 echo "encounter $encounterid";
165 } else if ($formid) {
166 echo "form $formid";
167 } else if ($issue) {
168 echo "issue $issue";
169 } else if ($document) {
170 echo "document $document";
172 ?> <? xl('and all subordinate data? This action will be logged','e'); ?>!</p>
174 <center>
176 <p>&nbsp;<br>
177 <input type='submit' name='form_submit' value='Yes, Delete and Log' />
178 &nbsp;
179 <input type='button' value='No, Cancel' onclick='window.close()' />
180 </p>
182 </center>
183 </form>
184 </body>
185 </html>