Moved recent v5.0.0 statement fix to current master.
[openemr.git] / interface / patient_file / merge_patients.php
blobd6f26be4f7d28b8688394a27d4c8a01094fcd58f
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);
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')) {
36 die(xlt('Not authorized'));
39 <html>
41 <head>
42 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
43 <title><?php echo xlt('Merge Patients'); ?></title>
45 <script type="text/javascript" src="../../library/dialog.js?v=<?php echo $v_js_includes; ?>"></script>
47 <script language="JavaScript">
49 var mypcc = '<?php echo $GLOBALS['phone_country_code']; ?>';
51 var el_pt_name;
52 var el_pt_id;
54 // This is for callback by the find-patient popup.
55 function setpatient(pid, lname, fname, dob) {
56 el_pt_name.value = lname + ', ' + fname + ' (' + pid + ')';
57 el_pt_id.value = pid;
60 // This invokes the find-patient popup.
61 function sel_patient(ename, epid) {
62 el_pt_name = ename;
63 el_pt_id = epid;
64 dlgopen('../main/calendar/find_patient_popup.php', '_blank', 500, 400);
67 </script>
69 </head>
71 <body class="body_top">
73 <center><h2><?php echo xlt('Merge Patients') ?></h2></center>
75 <?php
77 function deleteRows($tblname, $colname, $source_pid)
79 global $PRODUCTION;
80 $crow = sqlQuery("SELECT COUNT(*) AS count FROM `$tblname` WHERE `$colname` = $source_pid");
81 $count = $crow['count'];
82 if ($count) {
83 $sql = "DELETE FROM `$tblname` WHERE `$colname` = $source_pid";
84 echo "<br />$sql ($count)";
85 if ($PRODUCTION) {
86 sqlStatement($sql);
91 function updateRows($tblname, $colname, $source_pid, $target_pid)
93 global $PRODUCTION;
94 $crow = sqlQuery("SELECT COUNT(*) AS count FROM `$tblname` WHERE `$colname` = $source_pid");
95 $count = $crow['count'];
96 if ($count) {
97 $sql = "UPDATE `$tblname` SET `$colname` = '$target_pid' WHERE `$colname` = $source_pid";
98 echo "<br />$sql ($count)";
99 if ($PRODUCTION) {
100 sqlStatement($sql);
105 if (!empty($_POST['form_submit'])) {
106 $target_pid = intval($_POST['form_target_pid']);
107 $source_pid = intval($_POST['form_source_pid']);
109 if ($target_pid == $source_pid) {
110 die(xlt('Target and source pid may not be the same!'));
113 $tprow = sqlQuery("SELECT * FROM patient_data WHERE pid = ?", array($target_pid));
114 $sprow = sqlQuery("SELECT * FROM patient_data WHERE pid = ?", array($source_pid));
116 // Do some checking to make sure source and target exist and are the same person.
117 if (empty($tprow['pid'])) {
118 die(xlt('Target patient not found'));
121 if (empty($sprow['pid'])) {
122 die(xlt('Source patient not found'));
125 if ($tprow['ss'] != $sprow['ss']) {
126 die(xlt('Target and source SSN do not match'));
129 if (empty($tprow['DOB']) || $tprow['DOB'] == '0000-00-00') {
130 die(xlt('Target patient has no DOB'));
133 if (empty($sprow['DOB']) || $sprow['DOB'] == '0000-00-00') {
134 die(xlt('Source patient has no DOB'));
137 if ($tprow['DOB'] != $sprow['DOB']) {
138 die(xlt('Target and source DOB do not match'));
141 $tdocdir = "$OE_SITE_DIR/documents/$target_pid";
142 $sdocdir = "$OE_SITE_DIR/documents/$source_pid";
143 $sencdir = "$sdocdir/encounters";
144 $tencdir = "$tdocdir/encounters";
146 // Change normal documents first as that could fail if CouchDB connection fails.
147 $dres = sqlStatement("SELECT * FROM `documents` WHERE `foreign_id` = '$source_pid'");
148 while ($drow = sqlFetchArray($dres)) {
149 $d = new Document($drow['id']);
150 echo "<br />" . xlt('Changing patient ID for document') . ' ' . text($d->get_url_file());
151 if ($PRODUCTION) {
152 if (!$d->change_patient($target_pid)) {
153 die("<br />" . xlt('Change failed! CouchDB connect error?'));
158 // Move scanned encounter documents and delete their container.
159 if (is_dir($sencdir)) {
160 if ($PRODUCTION && !file_exists($tdocdir)) {
161 mkdir($tdocdir);
164 if ($PRODUCTION && !file_exists($tencdir)) {
165 mkdir($tencdir);
168 $dh = opendir($sencdir);
169 if (!$dh) {
170 die(xlt('Cannot read directory') . " '$sencdir'");
173 while (false !== ($sfname = readdir($dh))) {
174 if ($sfname == '.' || $sfname == '..') {
175 continue;
178 if ($sfname == 'index.html') {
179 echo "<br />" . xlt('Deleting') . " $sencdir/$sfname";
180 if ($PRODUCTION) {
181 if (!unlink("$sencdir/$sfname")) {
182 die("<br />" . xlt('Delete failed!'));
186 continue;
189 echo "<br />" . xlt('Moving') . " $sencdir/$sfname " . xlt('to') . " $tencdir/$sfname";
190 if ($PRODUCTION) {
191 if (!rename("$sencdir/$sfname", "$tencdir/$sfname")) {
192 die("<br />" . xlt('Move failed!'));
197 closedir($dh);
198 echo "<br />" . xlt('Deleting') . " $sencdir";
199 if ($PRODUCTION) {
200 if (!rmdir($sencdir)) {
201 echo "<br />" . xlt('Directory delete failed; continuing.');
206 $tres = sqlStatement("SHOW TABLES");
207 while ($trow = sqlFetchArray($tres)) {
208 $tblname = array_shift($trow);
209 if ($tblname == 'patient_data' || $tblname == 'history_data' || $tblname == 'insurance_data') {
210 deleteRows($tblname, 'pid', $source_pid);
211 } else if ($tblname == 'chart_tracker') {
212 updateRows($tblname, 'ct_pid', $source_pid, $target_pid);
213 } else if ($tblname == 'documents') {
214 // Documents already handled.
215 } else if ($tblname == 'openemr_postcalendar_events') {
216 updateRows($tblname, 'pc_pid', $source_pid, $target_pid);
217 } else if ($tblname == 'log') {
218 // Don't mess with log data.
219 } else {
220 $crow = sqlQuery("SHOW COLUMNS FROM `$tblname` WHERE " .
221 "`Field` LIKE 'pid' OR `Field` LIKE 'patient_id'");
222 if (!empty($crow['Field'])) {
223 $colname = $crow['Field'];
224 updateRows($tblname, $colname, $source_pid, $target_pid);
229 echo "<br />" . xlt('Merge complete.');
231 exit(0);
237 </p>
239 <form method='post' action='merge_patients.php'>
240 <center>
241 <table style='width:90%'>
242 <tr>
243 <td>
244 <?php echo xlt('Target Patient') ?>
245 </td>
246 <td>
247 <input type='text' size='30' name='form_target_patient'
248 value=' (<?php echo xla('Click to select'); ?>)'
249 onclick='sel_patient(this, this.form.form_target_pid)'
250 title='<?php echo xla('Click to select patient'); ?>' readonly />
251 <input type='hidden' name='form_target_pid' value='0' />
252 </td>
253 <td>
254 <?php echo xlt('This is the main chart that is to receive the merged data.'); ?>
255 </td>
256 </tr>
257 <tr>
258 <td>
259 <?php echo xlt('Source Patient') ?>
260 </td>
261 <td>
262 <input type='text' size='30' name='form_source_patient'
263 value=' (<?php echo xla('Click to select'); ?>)'
264 onclick='sel_patient(this, this.form.form_source_pid)'
265 title='<?php echo xla('Click to select patient'); ?>' readonly />
266 <input type='hidden' name='form_source_pid' value='0' />
267 </td>
268 <td>
269 <?php echo xlt('This is the chart that is to be merged into the main chart and then deleted.'); ?>
270 </td>
271 </tr>
272 </table>
273 <p><input type='submit' name='form_submit' value='<?php echo xla('Merge'); ?>' /></p>
274 </center>
275 </form>
277 <p><?php echo xlt('This utility is experimental. Back up your database and documents before using it!'); ?></p>
279 <?php if (!$PRODUCTION) { ?>
280 <p><?php echo xlt('This will be a "dry run" with no physical data updates.'); ?></p>
281 <?php } ?>
283 <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>
285 <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>
287 <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>
289 <p><?php echo xlt('The merge will not run unless SSN and DOB for the two charts are identical. DOBs cannot be empty.'); ?></p>
291 </body>
292 </html>