Various changes and fixes (#7424)
[openemr.git] / interface / patient_file / manage_dup_patients.php
blob68958cb58ed64d085e8ee9f5097f89f5183ac2ee
1 <?php
3 /*
4 * This tool helps with identifying and merging duplicate patients.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Rod Roark <rod@sunsetsystems.com>
9 * @copyright Copyright (c) 2017-2021 Rod Roark <rod@sunsetsystems.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 require_once("../globals.php");
14 require_once("$srcdir/patient.inc.php");
15 require_once("$srcdir/options.inc.php");
17 use OpenEMR\Common\Acl\AclMain;
18 use OpenEMR\Common\Csrf\CsrfUtils;
19 use OpenEMR\Common\Twig\TwigContainer;
20 use OpenEMR\Core\Header;
21 use OpenEMR\Services\FacilityService;
23 $firsttime = true;
25 function displayRow($row, $pid = '')
27 global $firsttime;
29 $bgcolor = '#ffdddd';
30 $myscore = '';
31 $options = '';
33 if (empty($pid)) {
34 $pid = $row['pid'];
37 if (isset($row['myscore'])) {
38 $myscore = $row['myscore'];
39 $options = "<option value=''></option>" .
40 "<option value='MK'>" . xlt('Merge and Keep') . "</option>" .
41 "<option value='MD'>" . xlt('Merge and Discard') . "</option>";
42 } else {
43 $myscore = $row['dupscore'];
44 $options = "<option value=''></option>" .
45 "<option value='U'>" . xlt('Mark as Unique') . "</option>" .
46 "<option value='R'>" . xlt('Recompute Score') . "</option>";
47 if (!$firsttime) {
48 echo " <tr bgcolor='#dddddd'><td class='detail' colspan='12'>&nbsp;</td></tr>\n";
52 $firsttime = false;
53 $ptname = $row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname'];
54 $phones = array();
55 if (trim($row['phone_home'])) {
56 $phones[] = trim($row['phone_home']);
58 if (trim($row['phone_biz' ])) {
59 $phones[] = trim($row['phone_biz' ]);
61 if (trim($row['phone_cell'])) {
62 $phones[] = trim($row['phone_cell']);
64 $phones = implode(', ', $phones);
66 $facname = '';
67 if ($row['home_facility']) {
68 $facrow = getFacility($row['home_facility']);
69 if (!empty($facrow['name'])) {
70 $facname = $facrow['name'];
74 <tr bgcolor='<?php echo $bgcolor; ?>'>
75 <td class="detail" bgcolor="#dddddd">
76 <select onchange='selchange(this, <?php echo attr_js($pid); ?>, <?php echo attr_js($row['pid']); ?>)' style='width:100%'>
77 <?php echo $options; // this is html and already escaped as required ?>
78 </select>
79 </td>
80 <td class="detail" align="right">
81 <?php echo text($myscore); ?>
82 </td>
83 <td class="detail" align="right" onclick="openNewTopWindow(<?php echo attr_js($row['pid']); ?>)"
84 title="<?php echo xla('Click to open in a new window or tab'); ?>" style="color:blue;cursor:pointer">
85 <?php echo text($row['pid']); ?>
86 </td>
87 <td class="detail">
88 <?php echo text($row['pubpid']); ?>
89 </td>
90 <td class="detail">
91 <?php echo text($ptname); ?>
92 </td>
93 <td class="detail">
94 <?php echo text(oeFormatShortDate($row['DOB'])); ?>
95 </td>
96 <td class="detail">
97 <?php echo text($row['ss']); ?>
98 </td>
99 <td class="detail">
100 <?php echo text($row['email']); ?>
101 </td>
102 <td class="detail">
103 <?php echo text($phones); ?>
104 </td>
105 <td class="detail">
106 <?php echo text(oeFormatShortDate($row['regdate'])); ?>
107 </td>
108 <td class="detail">
109 <?php echo text($facname); ?>
110 </td>
111 <td class="detail">
112 <?php echo text($row['street']); ?>
113 </td>
114 </tr>
115 <?php
118 if (!empty($_POST)) {
119 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
120 CsrfUtils::csrfNotVerified();
124 if (!AclMain::aclCheckCore('admin', 'super')) {
125 echo (new TwigContainer(null, $GLOBALS['kernel']))->getTwig()->render('core/unauthorized.html.twig', ['pageTitle' => xl("Duplicate Patient Management")]);
126 exit;
129 $scorecalc = getDupScoreSQL();
131 <html>
132 <head>
133 <title><?php echo xlt('Duplicate Patient Management') ?></title>
135 <?php Header::setupHeader(['report-helper']); ?>
137 <style type="text/css">
139 .dehead { color:#000000; font-family:sans-serif; font-size:10pt; font-weight:bold }
140 .detail { color:#000000; font-family:sans-serif; font-size:10pt; font-weight:normal }
141 .delink { color:#0000cc; font-family:sans-serif; font-size:10pt; font-weight:normal; cursor:pointer }
143 table.mymaintable, table.mymaintable td {
144 border: 1px solid #aaaaaa;
145 border-collapse: collapse;
147 table.mymaintable td {
148 padding: 1pt 4pt 1pt 4pt;
151 </style>
153 <script>
155 $(function () {
156 // Enable fixed headers when scrolling the report.
157 if (window.oeFixedHeaderSetup) {
158 oeFixedHeaderSetup(document.getElementById('mymaintable'));
162 function openNewTopWindow(pid) {
163 document.fnew.patientID.value = pid;
164 top.restoreSession();
165 document.fnew.submit();
168 function selchange(sel, toppid, rowpid) {
169 var f = document.forms[0];
170 if (sel.value == '') return;
171 top.restoreSession();
172 if (sel.value == 'MK') {
173 window.location = 'merge_patients.php?pid1=' + encodeURIComponent(rowpid) + '&pid2=' + encodeURIComponent(toppid);
175 else if (sel.value == 'MD') {
176 window.location = 'merge_patients.php?pid1=' + encodeURIComponent(toppid) + '&pid2=' + encodeURIComponent(rowpid);
178 else {
179 // Currently 'U' and 'R' actions are supported and rowpid is meaningless.
180 f.form_action.value = sel.value;
181 f.form_toppid.value = toppid;
182 f.form_rowpid.value = rowpid;
183 f.submit();
187 </script>
189 </head>
191 <body style='margin: 2em; background-color: #dddddd' >
192 <center>
194 <h2><?php echo xlt('Duplicate Patient Management')?></h2>
196 <form method='post' action='manage_dup_patients.php'>
197 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
199 <table border='0' cellpadding='3'>
200 <tr>
201 <td align='center'>
202 <input type='submit' name='form_refresh' value="<?php echo xla('Refresh') ?>">
203 &nbsp;
204 <input type='button' value='<?php echo xla('Print'); ?>' onclick='window.print()' />
205 </td>
206 </tr>
207 <tr>
208 <td height="1">
209 </td>
210 </tr>
211 </table>
213 <table id='mymaintable' class='mymaintable'>
214 <thead>
215 <tr bgcolor="#dddddd">
216 <td class="dehead">
217 <?php echo xlt('Actions'); ?>
218 </td>
219 <td class="dehead" align="right">
220 <?php echo xlt('Score'); ?>
221 </td>
222 <td class="dehead" align="right">
223 <?php echo xlt('Pid'); ?>
224 </td>
225 <td class="dehead">
226 <?php echo xlt('ID'); ?>
227 </td>
228 <td class="dehead">
229 <?php echo xlt('Name'); ?>
230 </td>
231 <td class="dehead">
232 <?php echo xlt('DOB'); ?>
233 </td>
234 <td class="dehead">
235 <?php echo xlt('SSN'); ?>
236 </td>
237 <td class="dehead">
238 <?php echo xlt('Email'); ?>
239 </td>
240 <td class="dehead">
241 <?php echo xlt('Telephone'); ?>
242 </td>
243 <td class="dehead">
244 <?php echo xlt('Registered'); ?>
245 </td>
246 <td class="dehead">
247 <?php echo xlt('Home Facility'); ?>
248 </td>
249 <td class="dehead">
250 <?php echo xlt('Address'); ?>
251 </td>
252 </tr>
253 </thead>
254 <tbody>
255 <?php
257 $form_action = $_POST['form_action'] ?? '';
259 if ($form_action == 'U') {
260 sqlStatement(
261 "UPDATE patient_data SET dupscore = -1 WHERE pid = ?",
262 array($_POST['form_toppid'])
264 } else if ($form_action == 'R') {
265 updateDupScore($_POST['form_toppid']);
268 $query = "SELECT * FROM patient_data WHERE dupscore > 7 " .
269 "ORDER BY dupscore DESC, pid DESC LIMIT 100";
270 $res1 = sqlStatement($query);
271 while ($row1 = sqlFetchArray($res1)) {
272 displayRow($row1);
273 $query = "SELECT p2.*, ($scorecalc) AS myscore " .
274 "FROM patient_data AS p1, patient_data AS p2 WHERE " .
275 "p1.pid = ? AND p2.pid < p1.pid AND ($scorecalc) > 7 " .
276 "ORDER BY myscore DESC, p2.pid DESC";
277 $res2 = sqlStatement($query, array($row1['pid']));
278 while ($row2 = sqlFetchArray($res2)) {
279 displayRow($row2, $row1['pid']);
283 </tbody>
284 </table>
285 <input type='hidden' name='form_action' value='' />
286 <input type='hidden' name='form_toppid' value='0' />
287 <input type='hidden' name='form_rowpid' value='0' />
288 </form>
289 </center>
291 <!-- form used to open a new top level window when a patient row is clicked -->
292 <form name='fnew' method='post' target='_blank'
293 action='../main/main_screen.php?auth=login&site=<?php echo attr_url($_SESSION['site_id']); ?>'>
294 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
295 <input type='hidden' name='patientID' value='0' />
296 </form>
298 </body>
299 </html>