Skip to content

Commit

Permalink
now fully functional
Browse files Browse the repository at this point in the history
  • Loading branch information
cfapress committed Jun 24, 2009
1 parent 901cb5f commit 1ae304b
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 36 deletions.
40 changes: 26 additions & 14 deletions contrib/util/dupecheck/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
</style>
</head>
<body>
<form name="search_form" id="search_form" method="post" action="dupecheck.php">
<form name="search_form" id="search_form" method="post" action="index.php">
<input type="hidden" name="go" value="Go">
Matching criteria:
<input type="checkbox" name="match_name" id="match_name" <?php if ($parameters['match_name']) echo "CHECKED"; ?>>
Expand Down Expand Up @@ -99,7 +99,7 @@
$dupecount = 0;

// for EACH patient in OpenEMR find potential matches
$sqlstmt = "select id, pid, fname, lname, dob, sex, ss, change_date from patient_data";
$sqlstmt = "select id, pid, fname, lname, dob, sex, ss from patient_data";
switch ($parameters['sortby']) {
case 'dob':
$orderby = " ORDER BY dob";
Expand All @@ -125,7 +125,7 @@

if ($dupelist[$row['id']] == 1) continue;

$sqlstmt = "select id, pid, fname, lname, dob, sex, ss, change_date ".
$sqlstmt = "select id, pid, fname, lname, dob, sex, ss ".
" from patient_data where ";
$sqland = "";
if ($parameters['match_name']) {
Expand All @@ -151,33 +151,31 @@
if (mysql_num_rows($mResults) <= 1) continue;


echo "<div class='match_block' style='padding: 5px 0px 5px 0px;'>";
echo "<div class='match_block' style='padding: 5px 0px 5px 0px;' id='dupediv".$dupecount."'>";
echo "<table>";

echo "<tr class='onerow' id='".$row['id']."' oemrid='".$row['id']."' dupecount='".$dupecount."'>";
echo "<tr class='onerow' id='".$row['id']."' oemrid='".$row['id']."' dupecount='".$dupecount."' title='Merge duplicates into this record'>";
echo "<td>".$row['lname'].", ".$row['fname']."</td>";
echo "<td>".$row['dob']."</td>";
echo "<td>".$row['sex']."</td>";
echo "<td>".$row['ss']."</td>";
echo "<td>".$row['change_date']."</td>";
echo "<td><input type='button' value=' ? ' class='moreinfo' oemrid='".$row['pid']."' title='More info'></td>";
echo "</tr>";

while ($mrow = mysql_fetch_assoc($mResults)) {
if ($row['id'] == $mrow['id']) continue;
echo "<tr class='onerow' id='".$mrow['id']."' oemrid='".$mrow['id']."' dupecount='".$dupecount."'>";
echo "<tr class='onerow' id='".$mrow['id']."' oemrid='".$mrow['id']."' dupecount='".$dupecount."' title='Merge duplicates into this record'>";
echo "<td>".$mrow['lname'].", ".$mrow['fname']."</td>";
echo "<td>".$mrow['dob']."</td>";
echo "<td>".$mrow['sex']."</td>";
echo "<td>".$mrow['ss']."</td>";
echo "<td>".$mrow['change_date']."</td>";
echo "<td><input type='button' value=' ? ' class='moreinfo' oemrid='".$mrow['pid']."' title='More info'></td>";
echo "</tr>";
// to keep the output clean let's not repeat IDs already tagged as dupes
$dupelist[$row['id']] = 1;
$dupelist[$mrow['id']] = 1;
$dupecount++;
}
$dupecount++;

echo "</table>";
echo "</div>\n";
Expand All @@ -186,13 +184,16 @@

?>
</div> <!-- end the big list -->
<?php if ($dupecount > 0) { echo $dupecount." duplicates found."; } ?><br>
<input type="button" id="do_resolve" value="Merge">
<?php if ($dupecount > 0) : ?>
<div id="dupecounter" style='display:inline;'><?php echo $dupecount; ?></div>
&nbsp;duplicates found
<?php endif; ?>
</form>

</body>

<script language="javascript">

$(document).ready(function(){

// capture RETURN keypress
Expand All @@ -216,19 +217,30 @@
// highlight the block of matching records
$(".match_block").mouseover(function() { $(this).toggleClass("highlight_block"); });
$(".match_block").mouseout(function() { $(this).toggleClass("highlight_block"); });
$(".onerow").mouseover(function() { $(this).toggleClass("highlight"); });
$(".onerow").mouseout(function() { $(this).toggleClass("highlight"); });

// begin the merge of a block into a single record
$(".onerow").click(function() {
var dupecount = $(this).attr("dupecount");
var masterid = $(this).attr("oemrid");
var newurl = "mergerecords.php?masterid="+masterid;
var newurl = "mergerecords.php?dupecount="+dupecount+"&masterid="+masterid;
$("[dupecount="+dupecount+"]").each(function (i) {
if (this.id != masterid) { newurl += "&otherid[]="+this.id; }
});
//alert(newurl);
document.location.href = newurl;
// open a new window and show the merge results
moreinfoWin = window.open(newurl, "mergewin");
});
});

function removedupe(dupeid) {
// remove the merged records from the list of duplicates
$("#dupediv"+dupeid).remove();
// reduce the duplicate counter
var dcounter = parseInt($("#dupecounter").html());
$("#dupecounter").html(dcounter-1);
}

</script>

</html>
110 changes: 88 additions & 22 deletions contrib/util/dupecheck/mergerecords.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
<?php
require_once("../../../library/sql.inc");
require_once("../../../interface/globals.php");
require_once("../../../library/pnotes.inc");
require_once("../../../library/log.inc");
require_once("./Utils.php");

$parameters = GetParameters();
$oemrdb = $GLOBALS['dbh'];
?>

<html>
<body>

<?php
// check for required data
if (! isset($parameters['masterid'])) { echo "Missing a Master Merge ID"; exit; }
if (! isset($parameters['otherid'])) { echo "Missing a Other matching IDs"; exit; }

// get the PID matching the masterid
$sqlstmt = "select pid from patient_data where id='".$parameters['masterid']."'";
$qResults = mysql_query($sqlstmt);
$qResults = mysql_query($sqlstmt, $oemrdb);
if (! $qResults) { echo "Error fetching master PID."; exit; }
$row = mysql_fetch_assoc($qResults);
$masterPID = $row['pid'];

$commitchanges = false;
if ($parameters['confirm'] == 'yes') { $commitchanges = true; }

// loop over the other IDs and alter their database records
foreach ($parameters['otherid'] as $otherID) {

// get the PID matching the other ID
$sqlstmt = "select pid from patient_data where id='".$otherID."'";
$qResults = mysql_query($sqlstmt);
// get info about the "otherID"
$sqlstmt = "select lname, pid from patient_data where id='".$otherID."'";
$qResults = mysql_query($sqlstmt, $oemrdb);
if (! $qResults) { echo "Error fetching master PID."; exit; }
$row = mysql_fetch_assoc($qResults);
$otherPID = $row['pid'];
$orow = mysql_fetch_assoc($qResults);
$otherPID = $orow['pid'];

echo "Merging PID ".$otherPID." into the master PID ".$masterPID."<br>";

UpdateTable("batchcom", "patient_id", $otherPID, $masterPID);
UpdateTable("immunizations", "patient_id", $otherPID, $masterPID);
Expand All @@ -34,9 +46,6 @@
UpdateTable("ar_activity", "pid", $otherPID, $masterPID);
UpdateTable("billing", "pid", $otherPID, $masterPID);
UpdateTable("drug_sales", "pid", $otherPID, $masterPID);
UpdateTable("employer_data", "pid", $otherPID, $masterPID);
UpdateTable("history_data", "pid", $otherPID, $masterPID);
UpdateTable("insurance_data", "pid", $otherPID, $masterPID);
UpdateTable("issue_encounter", "pid", $otherPID, $masterPID);
UpdateTable("lists", "pid", $otherPID, $masterPID);
UpdateTable("payments", "pid", $otherPID, $masterPID);
Expand All @@ -49,32 +58,89 @@

// update all the forms* tables
$sqlstmt = "show tables like 'form%'";
$qResults = mysql_query($sqlstmt);
$qResults = mysql_query($sqlstmt, $oemrdb);
while ($row = mysql_fetch_assoc($qResults)) {
UpdateTable($row['Tables_in_openemr (form%)'], "pid", $otherPID, $masterPID);
UpdateTable($row['Tables_in_'.$sqlconf["dbase"].' (form%)'], "pid", $otherPID, $masterPID);
}

// How to handle the actual patient_data record, delete it? alter fields?
// How to handle the data that should be unique to each patient:
// Demographics, Employment, Insurance, and History ??
//
//UpdateTable("patient_data", "pid", $otherID, $$parameters['masterid']);
//$sqlstmt = "update patient_data set pid='".$parameters['masterid']."' where pid='".$otherID."'";
//echo $sqlstmt."<br>";
//UpdateTable("employer_data", "pid", $otherPID, $masterPID);
//UpdateTable("history_data", "pid", $otherPID, $masterPID);
//UpdateTable("insurance_data", "pid", $otherPID, $masterPID);

}
// alter the patient's last name to indicate they have been merged into another record
$newlname = "~~~MERGED~~~".$orow['lname'];
$sqlstmt = "update patient_data set lname='".$newlname."' where pid='".$otherPID."'";
if ($commitchanges == true) $qResults = mysql_query($sqlstmt, $oemrdb);
echo "<li>Altered last name of PID ".$otherPID." to '".$newlname."'</li>";

// add patient notes regarding the merged data
$notetext = "All related patient data has been merged into patient record PID# ".$masterPID;
echo "<li>Added note about the merge to the PID ".$otherPID."</li>";
if ($commitchanges == true) addPnote($otherPID, $notetext);

$notetext = "All related patient data has been merged from patient record PID# ".$otherPID;
echo "<li>Added note about the merge to the Master PID ".$masterPID."</li>";
if ($commitchanges == true) addPnote($masterPID, $notetext);

// add a log entry regarding the merged data
if ($commitchanges == true) newEvent("data_merge", $_SESSION['authUser'], "Default", "Merged PID ".$otherPID." data into master PID ".$masterPID);
echo "<li>Added entry to log</li>";

echo "<br><br>";
} // end of otherID loop

function UpdateTable($tablename, $pid_col, $oldvalue, $newvalue) {
//$sqlstmt = "update ".$tablename." set ".$pid_col."='".$newvalue."' where ".$pid_col."='".$oldvalue."'";
global $commitchanges, $oemrdb;

$sqlstmt = "select count(*) as numrows from ".$tablename." where ".$pid_col."='".$oldvalue."'";
echo $sqlstmt;
$qResults = mysql_query($sqlstmt);
$qResults = mysql_query($sqlstmt, $oemrdb);

if ($qResults) {
$row = mysql_fetch_assoc($qResults);
if ($row['numrows'] > 0) {
echo "<br> ==> ".$row['numrows']." rows";
$sqlstmt = "update ".$tablename." set ".$pid_col."='".$newvalue."' where ".$pid_col."='".$oldvalue."'";
if ($commitchanges == true) {
$qResults = mysql_query($sqlstmt, $oemrdb);
}
$rowsupdated = mysql_affected_rows($oemrdb);
echo "<li>";
echo "".$tablename.": ".$rowsupdated." row(s) updated<br>";
echo "</li>";
}
}
echo "<br>";


}

?>

<?php if ($commitchanges == false) : ?>
Nothing has been changed yet. What you see above are the changes that will be made if you choose to commit them.<br>
Do you wish to commit these changes to the database?
<form method="post" action="mergerecords.php">
<input type="hidden" name="masterid" value="<?php echo $parameters['masterid']; ?>">
<input type="hidden" name="dupecount" value="<?php echo $parameters['dupecount']; ?>">
<?php
foreach ($parameters['otherid'] as $otherID) {
echo "<input type='hidden' name='otherid[]' value='$otherID'>";
}
?>
<input type="submit" name="confirm" value="yes">
<input type="button" value="no" onclick="javascript:window.close();"?>
</form>
<?php else: ?>
<a href="" onclick="javascript:window.close();">Close this window</a>
<?php endif; ?>

</body>
<?php if ($commitchanges == true) : ?>
<script language="javascript">
window.opener.removedupe(<?php echo $parameters['dupecount']; ?>);
window.close();
</script>
<?php endif; ?>
</html>

0 comments on commit 1ae304b

Please sign in to comment.