Interim autoloaded library/classes via composer classmap, take 4. (#422)
[openemr.git] / interface / patient_file / merge_patients.php
blob3041c433237a8bdb45e595c169169c645187e81e
1 <?php
2 /**
3 * This script merges two patient charts into a single patient chart.
4 * It is to correct the error of creating a duplicate patient.
6 * Copyright (C) 2013 Rod Roark <rod@sunsetsystems.com>
8 * LICENSE: This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>.
19 * @package OpenEMR
20 * @author Rod Roark <rod@sunsetsystems.com>
23 set_time_limit(0);
25 $sanitize_all_escapes = true;
26 $fake_register_globals = false;
28 require_once("../globals.php");
29 require_once("$srcdir/acl.inc");
30 require_once("$srcdir/log.inc");
32 // Set this to true for production use. If false you will get a "dry run" with no updates.
33 $PRODUCTION = true;
35 if (!acl_check('admin', 'super')) die(xlt('Not authorized'));
37 <html>
39 <head>
40 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
41 <title><?php echo xlt('Merge Patients'); ?></title>
43 <script type="text/javascript" src="../../library/dialog.js?v=<?php echo $v_js_includes; ?>"></script>
45 <script language="JavaScript">
47 var mypcc = '<?php echo $GLOBALS['phone_country_code']; ?>';
49 var el_pt_name;
50 var el_pt_id;
52 // This is for callback by the find-patient popup.
53 function setpatient(pid, lname, fname, dob) {
54 el_pt_name.value = lname + ', ' + fname + ' (' + pid + ')';
55 el_pt_id.value = pid;
58 // This invokes the find-patient popup.
59 function sel_patient(ename, epid) {
60 el_pt_name = ename;
61 el_pt_id = epid;
62 dlgopen('../main/calendar/find_patient_popup.php', '_blank', 500, 400);
65 </script>
67 </head>
69 <body class="body_top">
71 <center><h2><?php echo xlt('Merge Patients') ?></h2></center>
73 <?php
75 function deleteRows($tblname, $colname, $source_pid) {
76 global $PRODUCTION;
77 $crow = sqlQuery("SELECT COUNT(*) AS count FROM `$tblname` WHERE `$colname` = $source_pid");
78 $count = $crow['count'];
79 if ($count) {
80 $sql = "DELETE FROM `$tblname` WHERE `$colname` = $source_pid";
81 echo "<br />$sql ($count)";
82 if ($PRODUCTION) sqlStatement($sql);
86 function updateRows($tblname, $colname, $source_pid, $target_pid) {
87 global $PRODUCTION;
88 $crow = sqlQuery("SELECT COUNT(*) AS count FROM `$tblname` WHERE `$colname` = $source_pid");
89 $count = $crow['count'];
90 if ($count) {
91 $sql = "UPDATE `$tblname` SET `$colname` = '$target_pid' WHERE `$colname` = $source_pid";
92 echo "<br />$sql ($count)";
93 if ($PRODUCTION) sqlStatement($sql);
97 if (!empty($_POST['form_submit'])) {
98 $target_pid = intval($_POST['form_target_pid']);
99 $source_pid = intval($_POST['form_source_pid']);
101 if ($target_pid == $source_pid) die(xlt('Target and source pid may not be the same!'));
103 $tprow = sqlQuery("SELECT * FROM patient_data WHERE pid = ?", array($target_pid));
104 $sprow = sqlQuery("SELECT * FROM patient_data WHERE pid = ?", array($source_pid));
106 // Do some checking to make sure source and target exist and are the same person.
107 if (empty($tprow['pid'])) die(xlt('Target patient not found'));
108 if (empty($sprow['pid'])) die(xlt('Source patient not found'));
109 if ($tprow['ss'] != $sprow['ss']) die(xlt('Target and source SSN do not match'));
110 if (empty($tprow['DOB']) || $tprow['DOB'] == '0000-00-00') die(xlt('Target patient has no DOB'));
111 if (empty($sprow['DOB']) || $sprow['DOB'] == '0000-00-00') die(xlt('Source patient has no DOB'));
112 if ($tprow['DOB'] != $sprow['DOB']) die(xlt('Target and source DOB do not match'));
114 $tdocdir = "$OE_SITE_DIR/documents/$target_pid";
115 $sdocdir = "$OE_SITE_DIR/documents/$source_pid";
116 $sencdir = "$sdocdir/encounters";
117 $tencdir = "$tdocdir/encounters";
119 // Change normal documents first as that could fail if CouchDB connection fails.
120 $dres = sqlStatement("SELECT * FROM `documents` WHERE `foreign_id` = '$source_pid'");
121 while ($drow = sqlFetchArray($dres)) {
122 $d = new Document($drow['id']);
123 echo "<br />" . xlt('Changing patient ID for document') . ' ' . text($d->get_url_file());
124 if ($PRODUCTION) {
125 if (!$d->change_patient($target_pid)) {
126 die("<br />" . xlt('Change failed! CouchDB connect error?'));
131 // Move scanned encounter documents and delete their container.
132 if (is_dir($sencdir)) {
133 if ($PRODUCTION && !file_exists($tdocdir)) mkdir($tdocdir);
134 if ($PRODUCTION && !file_exists($tencdir)) mkdir($tencdir);
135 $dh = opendir($sencdir);
136 if (!$dh) die(xlt('Cannot read directory') . " '$sencdir'");
137 while (false !== ($sfname = readdir($dh))) {
138 if ($sfname == '.' || $sfname == '..') continue;
139 if ($sfname == 'index.html') {
140 echo "<br />" . xlt('Deleting') . " $sencdir/$sfname";
141 if ($PRODUCTION) {
142 if (!unlink("$sencdir/$sfname"))
143 die("<br />" . xlt('Delete failed!'));
145 continue;
147 echo "<br />" . xlt('Moving') . " $sencdir/$sfname " . xlt('to') . " $tencdir/$sfname";
148 if ($PRODUCTION) {
149 if (!rename("$sencdir/$sfname", "$tencdir/$sfname"))
150 die("<br />" . xlt('Move failed!'));
153 closedir($dh);
154 echo "<br />" . xlt('Deleting') . " $sencdir";
155 if ($PRODUCTION) {
156 if (!rmdir($sencdir))
157 echo "<br />" . xlt('Directory delete failed; continuing.');
161 $tres = sqlStatement("SHOW TABLES");
162 while ($trow = sqlFetchArray($tres)) {
163 $tblname = array_shift($trow);
164 if ($tblname == 'patient_data' || $tblname == 'history_data' || $tblname == 'insurance_data') {
165 deleteRows($tblname, 'pid', $source_pid);
167 else if ($tblname == 'chart_tracker') {
168 updateRows($tblname, 'ct_pid', $source_pid, $target_pid);
170 else if ($tblname == 'documents') {
171 // Documents already handled.
173 else if ($tblname == 'openemr_postcalendar_events') {
174 updateRows($tblname, 'pc_pid', $source_pid, $target_pid);
176 else if ($tblname == 'log') {
177 // Don't mess with log data.
179 else {
180 $crow = sqlQuery("SHOW COLUMNS FROM `$tblname` WHERE " .
181 "`Field` LIKE 'pid' OR `Field` LIKE 'patient_id'");
182 if (!empty($crow['Field'])) {
183 $colname = $crow['Field'];
184 updateRows($tblname, $colname, $source_pid, $target_pid);
189 echo "<br />" . xlt('Merge complete.');
191 exit(0);
197 </p>
199 <form method='post' action='merge_patients.php'>
200 <center>
201 <table style='width:90%'>
202 <tr>
203 <td>
204 <?php echo xlt('Target Patient') ?>
205 </td>
206 <td>
207 <input type='text' size='30' name='form_target_patient'
208 value=' (<?php echo xla('Click to select'); ?>)'
209 onclick='sel_patient(this, this.form.form_target_pid)'
210 title='<?php echo xla('Click to select patient'); ?>' readonly />
211 <input type='hidden' name='form_target_pid' value='0' />
212 </td>
213 <td>
214 <?php echo xlt('This is the main chart that is to receive the merged data.'); ?>
215 </td>
216 </tr>
217 <tr>
218 <td>
219 <?php echo xlt('Source Patient') ?>
220 </td>
221 <td>
222 <input type='text' size='30' name='form_source_patient'
223 value=' (<?php echo xla('Click to select'); ?>)'
224 onclick='sel_patient(this, this.form.form_source_pid)'
225 title='<?php echo xla('Click to select patient'); ?>' readonly />
226 <input type='hidden' name='form_source_pid' value='0' />
227 </td>
228 <td>
229 <?php echo xlt('This is the chart that is to be merged into the main chart and then deleted.'); ?>
230 </td>
231 </tr>
232 </table>
233 <p><input type='submit' name='form_submit' value='<?php echo xla('Merge'); ?>' /></p>
234 </center>
235 </form>
237 <p><?php echo xlt('This utility is experimental. Back up your database and documents before using it!'); ?></p>
239 <?php if (!$PRODUCTION) { ?>
240 <p><?php echo xlt('This will be a "dry run" with no physical data updates.'); ?></p>
241 <?php } ?>
243 <p><?php echo xlt('This will merge two patient charts into one. It is useful when a patient has been duplicated by mistake. If that happens often, fix your office procedures - do not run this routinely!'); ?></p>
245 <p><?php echo xlt('The first ("target") chart is the one that is considered the most complete and accurate. Demographics, history and insurance sections for this one will be retained.'); ?></p>
247 <p><?php echo xlt('The second ("source") chart will have its demographics, history and insurance sections discarded. Its other data will be merged into the target chart.'); ?></p>
249 <p><?php echo xlt('The merge will not run unless SSN and DOB for the two charts are identical. DOBs cannot be empty.'); ?></p>
251 </body>
252 </html>