Fixes #7590 wrongful use of break tag (#7624)
[openemr.git] / contrib / util / dupecheck / mergerecords.php
blobde99cea186e548a7450c5384ba5a1135b4e377ab
1 <?php
3 /**
4 * dupecheck mergerecords.php
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 require_once("../../../interface/globals.php");
14 require_once("../../../library/pnotes.inc.php");
15 require_once("./Utils.php");
17 use OpenEMR\Common\Acl\AclMain;
18 use OpenEMR\Common\Csrf\CsrfUtils;
19 use OpenEMR\Common\Logging\EventAuditLogger;
20 use OpenEMR\Common\Twig\TwigContainer;
22 if (!empty($_POST)) {
23 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
24 CsrfUtils::csrfNotVerified();
26 foreach ($_POST as $key => $value) {
27 $parameters[$key] = $value;
31 if (!empty($_GET)) {
32 if (!CsrfUtils::verifyCsrfToken($_GET["csrf_token_form"])) {
33 CsrfUtils::csrfNotVerified();
35 foreach ($_GET as $key => $value) {
36 $parameters[$key] = $value;
40 if (!AclMain::aclCheckCore('admin', 'super')) {
41 echo (new TwigContainer(null, $GLOBALS['kernel']))->getTwig()->render('core/unauthorized.html.twig', ['pageTitle' => xl("Merge Records")]);
42 exit;
47 <html>
48 <body>
50 <?php
51 // check for required data
52 if (! isset($parameters['masterid'])) {
53 echo "Missing a Master Merge ID";
54 exit;
57 if (! isset($parameters['otherid'])) {
58 echo "Missing a Other matching IDs";
59 exit;
62 // get the PID matching the masterid
63 $sqlstmt = "select pid from patient_data where id=?";
64 $qResults = sqlStatement($sqlstmt, array($parameters['masterid']));
65 if (! $qResults) {
66 echo "Error fetching master PID.";
67 exit;
70 $row = sqlFetchArray($qResults);
71 $masterPID = $row['pid'];
73 $commitchanges = false;
74 if ($parameters['confirm'] == 'yes') {
75 $commitchanges = true;
78 // loop over the other IDs and alter their database records
79 foreach ($parameters['otherid'] as $otherID) {
80 // get info about the "otherID"
81 $sqlstmt = "select lname, pid from patient_data where id=?";
82 $qResults = sqlStatement($sqlstmt, array($otherID));
83 if (! $qResults) {
84 echo "Error fetching master PID.";
85 exit;
88 $orow = sqlFetchArray($qResults);
89 $otherPID = $orow['pid'];
91 echo "Merging PID " . text($otherPID) . " into the master PID " . text($masterPID) . "<br />";
93 UpdateTable("batchcom", "patient_id", $otherPID, $masterPID);
94 UpdateTable("immunizations", "patient_id", $otherPID, $masterPID);
95 UpdateTable("prescriptions", "patient_id", $otherPID, $masterPID);
96 UpdateTable("claims", "patient_id", $otherPID, $masterPID);
98 UpdateTable("ar_activity", "pid", $otherPID, $masterPID);
99 UpdateTable("billing", "pid", $otherPID, $masterPID);
100 UpdateTable("drug_sales", "pid", $otherPID, $masterPID);
101 UpdateTable("issue_encounter", "pid", $otherPID, $masterPID);
102 UpdateTable("lists", "pid", $otherPID, $masterPID);
103 UpdateTable("payments", "pid", $otherPID, $masterPID);
104 UpdateTable("pnotes", "pid", $otherPID, $masterPID);
105 UpdateTable("transactions", "pid", $otherPID, $masterPID);
107 UpdateTable("chart_tracker", "ct_pid", $otherPID, $masterPID);
108 UpdateTable("openemr_postcalendar_events", "pc_pid", $otherPID, $masterPID);
109 UpdateTable("documents", "foreign_id", $otherPID, $masterPID);
111 // update all the forms* tables
112 $sqlstmt = "show tables like 'form%'";
113 $qResults = sqlStatement($sqlstmt);
114 while ($row = sqlFetchArray($qResults)) {
115 UpdateTable($row['Tables_in_' . $sqlconf["dbase"] . ' (form%)'], "pid", $otherPID, $masterPID);
118 // How to handle the data that should be unique to each patient:
119 // Demographics, Employment, Insurance, and History ??
121 //UpdateTable("patient_data", "pid", $otherID, $$parameters['masterid']);
122 //UpdateTable("employer_data", "pid", $otherPID, $masterPID);
123 //UpdateTable("history_data", "pid", $otherPID, $masterPID);
124 //UpdateTable("insurance_data", "pid", $otherPID, $masterPID);
126 // alter the patient's last name to indicate they have been merged into another record
127 $newlname = "~~~MERGED~~~" . $orow['lname'];
128 $sqlstmt = "update patient_data set lname=? where pid=?";
129 if ($commitchanges == true) {
130 $qResults = sqlStatement($sqlstmt, array($newlname, $otherPID));
133 echo "<li>Altered last name of PID " . text($otherPID) . " to '" . text($newlname) . "'</li>";
135 // add patient notes regarding the merged data
136 $notetext = "All related patient data has been merged into patient record PID# " . $masterPID;
137 echo "<li>Added note about the merge to the PID " . text($otherPID) . "</li>";
138 if ($commitchanges == true) {
139 addPnote($otherPID, $notetext);
142 $notetext = "All related patient data has been merged from patient record PID# " . $otherPID;
143 echo "<li>Added note about the merge to the Master PID " . text($masterPID) . "</li>";
144 if ($commitchanges == true) {
145 addPnote($masterPID, $notetext);
148 // add a log entry regarding the merged data
149 if ($commitchanges == true) {
150 EventAuditLogger::instance()->newEvent("data_merge", $_SESSION['authUser'], "Default", 1, "Merged PID " . $otherPID . " data into master PID " . $masterPID);
153 echo "<li>Added entry to log</li>";
155 echo "<br /><br />";
156 } // end of otherID loop
158 function UpdateTable($tablename, $pid_col, $oldvalue, $newvalue)
160 global $commitchanges;
162 $sqlstmt = "select count(*) as numrows from " . $tablename . " where " . $pid_col . "='" . $oldvalue . "'";
163 $qResults = sqlStatement($sqlstmt);
165 if ($qResults) {
166 $row = sqlFetchArray($qResults);
167 if ($row['numrows'] > 0) {
168 $sqlstmt = "update " . escape_table_name($tablename) . " set " . escape_sql_column_name($pid_col, array($tablename)) . "=? where " . escape_sql_column_name($pid_col, array($tablename)) . "=?";
169 if ($commitchanges == true) {
170 $qResults = sqlStatement($sqlstmt, array($newvalue, $oldvalue));
173 $rowsupdated = generic_sql_affected_rows();
174 echo "<li>";
175 echo "" . text($tablename) . ": " . text($rowsupdated) . " row(s) updated<br />";
176 echo "</li>";
183 <?php if ($commitchanges == false) : ?>
184 Nothing has been changed yet. What you see above are the changes that will be made if you choose to commit them.<br />
185 Do you wish to commit these changes to the database?
186 <form method="post" action="mergerecords.php">
187 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
188 <input type="hidden" name="masterid" value="<?php echo attr($parameters['masterid']); ?>">
189 <input type="hidden" name="dupecount" value="<?php echo attr($parameters['dupecount']); ?>">
190 <?php
191 foreach ($parameters['otherid'] as $otherID) {
192 echo "<input type='hidden' name='otherid[]' value='<?php echo attr($otherID); ?>'>";
195 <input type="submit" name="confirm" value="yes">
196 <input type="button" value="no" onclick="javascript:window.close();"?>
197 </form>
198 <?php else : ?>
199 <a href="" onclick="javascript:window.close();">Close this window</a>
200 <?php endif; ?>
202 </body>
203 <?php if ($commitchanges == true) : ?>
204 <script>
205 window.opener.removedupe(<?php echo js_escape($parameters['dupecount']); ?>);
206 window.close();
207 </script>
208 <?php endif; ?>
209 </html>