Highway to PSR2
[openemr.git] / contrib / util / dupecheck / mergerecords.php
blobf47dafa4480ca67e78c232b7d68e9c9d8aef3eba
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'])) {
17 echo "Missing a Master Merge ID";
18 exit;
21 if (! isset($parameters['otherid'])) {
22 echo "Missing a Other matching IDs";
23 exit;
26 // get the PID matching the masterid
27 $sqlstmt = "select pid from patient_data where id='".$parameters['masterid']."'";
28 $qResults = sqlStatement($sqlstmt);
29 if (! $qResults) {
30 echo "Error fetching master PID.";
31 exit;
34 $row = sqlFetchArray($qResults);
35 $masterPID = $row['pid'];
37 $commitchanges = false;
38 if ($parameters['confirm'] == 'yes') {
39 $commitchanges = true;
42 // loop over the other IDs and alter their database records
43 foreach ($parameters['otherid'] as $otherID) {
44 // get info about the "otherID"
45 $sqlstmt = "select lname, pid from patient_data where id='".$otherID."'";
46 $qResults = sqlStatement($sqlstmt);
47 if (! $qResults) {
48 echo "Error fetching master PID.";
49 exit;
52 $orow = sqlFetchArray($qResults);
53 $otherPID = $orow['pid'];
55 echo "Merging PID ".$otherPID." into the master PID ".$masterPID."<br>";
57 UpdateTable("batchcom", "patient_id", $otherPID, $masterPID);
58 UpdateTable("immunizations", "patient_id", $otherPID, $masterPID);
59 UpdateTable("prescriptions", "patient_id", $otherPID, $masterPID);
60 UpdateTable("claims", "patient_id", $otherPID, $masterPID);
62 UpdateTable("ar_activity", "pid", $otherPID, $masterPID);
63 UpdateTable("billing", "pid", $otherPID, $masterPID);
64 UpdateTable("drug_sales", "pid", $otherPID, $masterPID);
65 UpdateTable("issue_encounter", "pid", $otherPID, $masterPID);
66 UpdateTable("lists", "pid", $otherPID, $masterPID);
67 UpdateTable("payments", "pid", $otherPID, $masterPID);
68 UpdateTable("pnotes", "pid", $otherPID, $masterPID);
69 UpdateTable("transactions", "pid", $otherPID, $masterPID);
71 UpdateTable("chart_tracker", "ct_pid", $otherPID, $masterPID);
72 UpdateTable("openemr_postcalendar_events", "pc_pid", $otherPID, $masterPID);
73 UpdateTable("documents", "foreign_id", $otherPID, $masterPID);
75 // update all the forms* tables
76 $sqlstmt = "show tables like 'form%'";
77 $qResults = sqlStatement($sqlstmt);
78 while ($row = sqlFetchArray($qResults)) {
79 UpdateTable($row['Tables_in_'.$sqlconf["dbase"].' (form%)'], "pid", $otherPID, $masterPID);
82 // How to handle the data that should be unique to each patient:
83 // Demographics, Employment, Insurance, and History ??
85 //UpdateTable("patient_data", "pid", $otherID, $$parameters['masterid']);
86 //UpdateTable("employer_data", "pid", $otherPID, $masterPID);
87 //UpdateTable("history_data", "pid", $otherPID, $masterPID);
88 //UpdateTable("insurance_data", "pid", $otherPID, $masterPID);
90 // alter the patient's last name to indicate they have been merged into another record
91 $newlname = "~~~MERGED~~~".$orow['lname'];
92 $sqlstmt = "update patient_data set lname='".$newlname."' where pid='".$otherPID."'";
93 if ($commitchanges == true) {
94 $qResults = sqlStatement($sqlstmt);
97 echo "<li>Altered last name of PID ".$otherPID." to '".$newlname."'</li>";
99 // add patient notes regarding the merged data
100 $notetext = "All related patient data has been merged into patient record PID# ".$masterPID;
101 echo "<li>Added note about the merge to the PID ".$otherPID."</li>";
102 if ($commitchanges == true) {
103 addPnote($otherPID, $notetext);
106 $notetext = "All related patient data has been merged from patient record PID# ".$otherPID;
107 echo "<li>Added note about the merge to the Master PID ".$masterPID."</li>";
108 if ($commitchanges == true) {
109 addPnote($masterPID, $notetext);
112 // add a log entry regarding the merged data
113 if ($commitchanges == true) {
114 newEvent("data_merge", $_SESSION['authUser'], "Default", 1, "Merged PID ".$otherPID." data into master PID ".$masterPID);
117 echo "<li>Added entry to log</li>";
119 echo "<br><br>";
120 } // end of otherID loop
122 function UpdateTable($tablename, $pid_col, $oldvalue, $newvalue)
124 global $commitchanges, $oemrdb;
126 $sqlstmt = "select count(*) as numrows from ".$tablename." where ".$pid_col."='".$oldvalue."'";
127 $qResults = sqlStatement($sqlstmt);
129 if ($qResults) {
130 $row = sqlFetchArray($qResults);
131 if ($row['numrows'] > 0) {
132 $sqlstmt = "update ".$tablename." set ".$pid_col."='".$newvalue."' where ".$pid_col."='".$oldvalue."'";
133 if ($commitchanges == true) {
134 $qResults = sqlStatement($sqlstmt);
137 $rowsupdated = generic_sql_affected_rows();
138 echo "<li>";
139 echo "".$tablename.": ".$rowsupdated." row(s) updated<br>";
140 echo "</li>";
147 <?php if ($commitchanges == false) : ?>
148 Nothing has been changed yet. What you see above are the changes that will be made if you choose to commit them.<br>
149 Do you wish to commit these changes to the database?
150 <form method="post" action="mergerecords.php">
151 <input type="hidden" name="masterid" value="<?php echo $parameters['masterid']; ?>">
152 <input type="hidden" name="dupecount" value="<?php echo $parameters['dupecount']; ?>">
153 <?php
154 foreach ($parameters['otherid'] as $otherID) {
155 echo "<input type='hidden' name='otherid[]' value='$otherID'>";
158 <input type="submit" name="confirm" value="yes">
159 <input type="button" value="no" onclick="javascript:window.close();"?>
160 </form>
161 <?php else : ?>
162 <a href="" onclick="javascript:window.close();">Close this window</a>
163 <?php endif; ?>
165 </body>
166 <?php if ($commitchanges == true) : ?>
167 <script language="javascript">
168 window.opener.removedupe(<?php echo $parameters['dupecount']; ?>);
169 window.close();
170 </script>
171 <?php endif; ?>
172 </html>