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>.
20 * @author Rod Roark <rod@sunsetsystems.com>
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.
35 if (!acl_check('admin', 'super')) {
36 die(xlt('Not authorized'));
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
']; ?>';
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 +
')';
60 // This invokes the find-patient popup.
61 function sel_patient(ename
, epid
) {
64 dlgopen('../main/calendar/find_patient_popup.php', '_blank', 500, 400);
71 <body
class="body_top">
73 <center
><h2
><?php
echo xlt('Merge Patients') ?
></h2
></center
>
77 function deleteRows($tblname, $colname, $source_pid)
80 $crow = sqlQuery("SELECT COUNT(*) AS count FROM `$tblname` WHERE `$colname` = $source_pid");
81 $count = $crow['count'];
83 $sql = "DELETE FROM `$tblname` WHERE `$colname` = $source_pid";
84 echo "<br />$sql ($count)";
91 function updateRows($tblname, $colname, $source_pid, $target_pid)
94 $crow = sqlQuery("SELECT COUNT(*) AS count FROM `$tblname` WHERE `$colname` = $source_pid");
95 $count = $crow['count'];
97 $sql = "UPDATE `$tblname` SET `$colname` = '$target_pid' WHERE `$colname` = $source_pid";
98 echo "<br />$sql ($count)";
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());
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)) {
164 if ($PRODUCTION && !file_exists($tencdir)) {
168 $dh = opendir($sencdir);
170 die(xlt('Cannot read directory') . " '$sencdir'");
173 while (false !== ($sfname = readdir($dh))) {
174 if ($sfname == '.' ||
$sfname == '..') {
178 if ($sfname == 'index.html') {
179 echo "<br />" . xlt('Deleting') . " $sencdir/$sfname";
181 if (!unlink("$sencdir/$sfname")) {
182 die("<br />" . xlt('Delete failed!'));
189 echo "<br />" . xlt('Moving') . " $sencdir/$sfname " . xlt('to') . " $tencdir/$sfname";
191 if (!rename("$sencdir/$sfname", "$tencdir/$sfname")) {
192 die("<br />" . xlt('Move failed!'));
198 echo "<br />" . xlt('Deleting') . " $sencdir";
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.
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.');
239 <form method
='post' action
='merge_patients.php'>
241 <table style
='width:90%'>
244 <?php
echo xlt('Target Patient') ?
>
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' />
254 <?php
echo xlt('This is the main chart that is to receive the merged data.'); ?
>
259 <?php
echo xlt('Source Patient') ?
>
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' />
269 <?php
echo xlt('This is the chart that is to be merged into the main chart and then deleted.'); ?
>
273 <p
><input type
='submit' name
='form_submit' value
='<?php echo xla('Merge
'); ?>' /></p
>
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
>
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
>