Internationalization: Translation Tables Update.
[openemr.git] / contrib / util / dupecheck / mergerecords.php
blob8de661b03a6d7828c169a092ddf110097c29d25f
1 <?php
2 require_once("../../../interface/globals.php");
3 require_once("../../../library/pnotes.inc");
4 require_once("../../../library/log.inc");
5 require_once("./Utils.php");
7 $parameters = GetParameters();
8 $oemrdb = $GLOBALS['dbh'];
9 ?>
11 <html>
12 <body>
14 <?php
15 // check for required data
16 if (! isset($parameters['masterid'])) { echo "Missing a Master Merge ID"; exit; }
17 if (! isset($parameters['otherid'])) { echo "Missing a Other matching IDs"; exit; }
19 // get the PID matching the masterid
20 $sqlstmt = "select pid from patient_data where id='".$parameters['masterid']."'";
21 $qResults = mysql_query($sqlstmt, $oemrdb);
22 if (! $qResults) { echo "Error fetching master PID."; exit; }
23 $row = mysql_fetch_assoc($qResults);
24 $masterPID = $row['pid'];
26 $commitchanges = false;
27 if ($parameters['confirm'] == 'yes') { $commitchanges = true; }
29 // loop over the other IDs and alter their database records
30 foreach ($parameters['otherid'] as $otherID) {
32 // get info about the "otherID"
33 $sqlstmt = "select lname, pid from patient_data where id='".$otherID."'";
34 $qResults = mysql_query($sqlstmt, $oemrdb);
35 if (! $qResults) { echo "Error fetching master PID."; exit; }
36 $orow = mysql_fetch_assoc($qResults);
37 $otherPID = $orow['pid'];
39 echo "Merging PID ".$otherPID." into the master PID ".$masterPID."<br>";
41 UpdateTable("batchcom", "patient_id", $otherPID, $masterPID);
42 UpdateTable("immunizations", "patient_id", $otherPID, $masterPID);
43 UpdateTable("prescriptions", "patient_id", $otherPID, $masterPID);
44 UpdateTable("claims", "patient_id", $otherPID, $masterPID);
46 UpdateTable("ar_activity", "pid", $otherPID, $masterPID);
47 UpdateTable("billing", "pid", $otherPID, $masterPID);
48 UpdateTable("drug_sales", "pid", $otherPID, $masterPID);
49 UpdateTable("issue_encounter", "pid", $otherPID, $masterPID);
50 UpdateTable("lists", "pid", $otherPID, $masterPID);
51 UpdateTable("payments", "pid", $otherPID, $masterPID);
52 UpdateTable("pnotes", "pid", $otherPID, $masterPID);
53 UpdateTable("transactions", "pid", $otherPID, $masterPID);
55 UpdateTable("chart_tracker", "ct_pid", $otherPID, $masterPID);
56 UpdateTable("openemr_postcalendar_events", "pc_pid", $otherPID, $masterPID);
57 UpdateTable("documents", "foreign_id", $otherPID, $masterPID);
59 // update all the forms* tables
60 $sqlstmt = "show tables like 'form%'";
61 $qResults = mysql_query($sqlstmt, $oemrdb);
62 while ($row = mysql_fetch_assoc($qResults)) {
63 UpdateTable($row['Tables_in_'.$sqlconf["dbase"].' (form%)'], "pid", $otherPID, $masterPID);
66 // How to handle the data that should be unique to each patient:
67 // Demographics, Employment, Insurance, and History ??
69 //UpdateTable("patient_data", "pid", $otherID, $$parameters['masterid']);
70 //UpdateTable("employer_data", "pid", $otherPID, $masterPID);
71 //UpdateTable("history_data", "pid", $otherPID, $masterPID);
72 //UpdateTable("insurance_data", "pid", $otherPID, $masterPID);
74 // alter the patient's last name to indicate they have been merged into another record
75 $newlname = "~~~MERGED~~~".$orow['lname'];
76 $sqlstmt = "update patient_data set lname='".$newlname."' where pid='".$otherPID."'";
77 if ($commitchanges == true) $qResults = mysql_query($sqlstmt, $oemrdb);
78 echo "<li>Altered last name of PID ".$otherPID." to '".$newlname."'</li>";
80 // add patient notes regarding the merged data
81 $notetext = "All related patient data has been merged into patient record PID# ".$masterPID;
82 echo "<li>Added note about the merge to the PID ".$otherPID."</li>";
83 if ($commitchanges == true) addPnote($otherPID, $notetext);
85 $notetext = "All related patient data has been merged from patient record PID# ".$otherPID;
86 echo "<li>Added note about the merge to the Master PID ".$masterPID."</li>";
87 if ($commitchanges == true) addPnote($masterPID, $notetext);
89 // add a log entry regarding the merged data
90 if ($commitchanges == true) newEvent("data_merge", $_SESSION['authUser'], "Default", 1, "Merged PID ".$otherPID." data into master PID ".$masterPID);
91 echo "<li>Added entry to log</li>";
93 echo "<br><br>";
94 } // end of otherID loop
96 function UpdateTable($tablename, $pid_col, $oldvalue, $newvalue) {
97 global $commitchanges, $oemrdb;
99 $sqlstmt = "select count(*) as numrows from ".$tablename." where ".$pid_col."='".$oldvalue."'";
100 $qResults = mysql_query($sqlstmt, $oemrdb);
102 if ($qResults) {
103 $row = mysql_fetch_assoc($qResults);
104 if ($row['numrows'] > 0) {
105 $sqlstmt = "update ".$tablename." set ".$pid_col."='".$newvalue."' where ".$pid_col."='".$oldvalue."'";
106 if ($commitchanges == true) {
107 $qResults = mysql_query($sqlstmt, $oemrdb);
109 $rowsupdated = mysql_affected_rows($oemrdb);
110 echo "<li>";
111 echo "".$tablename.": ".$rowsupdated." row(s) updated<br>";
112 echo "</li>";
121 <?php if ($commitchanges == false) : ?>
122 Nothing has been changed yet. What you see above are the changes that will be made if you choose to commit them.<br>
123 Do you wish to commit these changes to the database?
124 <form method="post" action="mergerecords.php">
125 <input type="hidden" name="masterid" value="<?php echo $parameters['masterid']; ?>">
126 <input type="hidden" name="dupecount" value="<?php echo $parameters['dupecount']; ?>">
127 <?php
128 foreach ($parameters['otherid'] as $otherID) {
129 echo "<input type='hidden' name='otherid[]' value='$otherID'>";
132 <input type="submit" name="confirm" value="yes">
133 <input type="button" value="no" onclick="javascript:window.close();"?>
134 </form>
135 <?php else: ?>
136 <a href="" onclick="javascript:window.close();">Close this window</a>
137 <?php endif; ?>
139 </body>
140 <?php if ($commitchanges == true) : ?>
141 <script language="javascript">
142 window.opener.removedupe(<?php echo $parameters['dupecount']; ?>);
143 window.close();
144 </script>
145 <?php endif; ?>
146 </html>