Highway to PSR2
[openemr.git] / contrib / util / dupecheck / index.php
blob2034483d330776c7508aacaf34b5df8ffb479ee5
1 <?php
2 require_once("../../../interface/globals.php");
3 require_once("./Utils.php");
5 /* Use this code to identify duplicate patients in OpenEMR
7 */
8 $parameters = GetParameters();
10 // establish some defaults
11 if (! isset($parameters['sortby'])) {
12 $parameters['sortby'] = "name";
15 if (! isset($parameters['limit'])) {
16 $parameters['limit'] = 100;
19 if (! isset($parameters['match_name']) &&
20 ! isset($parameters['match_dob']) &&
21 ! isset($parameters['match_sex']) &&
22 ! isset($parameters['match_ssn'])) {
23 $parameters['match_name'] = 'on';
24 $parameters['match_dob'] = 'on';
27 $oemrdb = $GLOBALS['dbh'];
30 <html>
31 <head>
32 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-2-2/index.js"></script>
33 <style>
34 body {
35 font-family: arial, helvetica, times new roman;
36 font-size: 1em;
37 background-color: #eee;
39 .match_block {
40 border: 1px solid #eee;
41 background-color: white;
42 padding: 5px;
45 .match_block table {
46 border-collapse: collapse;
48 .match_block table tr {
49 cursor: pointer;
51 .match_block table td {
52 padding: 5px;
55 .highlight {
56 background-color: #99a;
57 color: white;
59 .highlight_block {
60 background-color: #ffa;
62 .bold {
63 font-weight: bold;
66 </style>
67 </head>
68 <body>
69 <form name="search_form" id="search_form" method="post" action="index.php">
70 <input type="hidden" name="go" value="Go">
71 Matching criteria:
72 <input type="checkbox" name="match_name" id="match_name" <?php if ($parameters['match_name']) {
73 echo "CHECKED";
74 } ?>>
75 <label for="match_name">Name</label>
76 <input type="checkbox" name="match_dob" id="match_dob" <?php if ($parameters['match_dob']) {
77 echo "CHECKED";
78 } ?>>
79 <label for="match_dob">DOB</label>
80 <input type="checkbox" name="match_sex" id="match_sex" <?php if ($parameters['match_sex']) {
81 echo "CHECKED";
82 } ?>>
83 <label for="match_sex">Gender</label>
84 <input type="checkbox" name="match_ssn" id="match_ssn" <?php if ($parameters['match_ssn']) {
85 echo "CHECKED";
86 } ?>>
87 <label for="match_ssn">SSN</label>
88 <br>
89 Order results by:
90 <input type='radio' name='sortby' value='name' id="name" <?php if ($parameters['sortby']=='name') {
91 echo "CHECKED";
92 } ?>>
93 <label for="name">Name</label>
94 <input type='radio' name='sortby' value='dob' id="dob" <?php if ($parameters['sortby']=='dob') {
95 echo "CHECKED";
96 } ?>>
97 <label for="dob">DOB</label>
98 <input type='radio' name='sortby' value='sex' id="sex" <?php if ($parameters['sortby']=='sex') {
99 echo "CHECKED";
100 } ?>>
101 <label for="sex">Gender</label>
102 <input type='radio' name='sortby' value='ssn' id="ssn" <?php if ($parameters['sortby']=='ssn') {
103 echo "CHECKED";
104 } ?>>
105 <label for="ssn">SSN</label>
106 <br>
107 Limit search to first <input type='textbox' size='5' name='limit' id="limit" value='<?php echo $parameters['limit']; ?>'> records
108 <input type="button" name="do_search" id="do_search" value="Go">
109 </form>
111 <div id="thebiglist" style="height: 300px; overflow: auto; border: 1px solid blue;">
112 <form name="resolve" id="resolve" method="POST" action="dupcheck.php">
114 <?php
115 if ($parameters['go'] == "Go") {
116 // go and do the search
118 // counter that gathers duplicates into groups
119 $dupecount = 0;
121 // for EACH patient in OpenEMR find potential matches
122 $sqlstmt = "select id, pid, fname, lname, dob, sex, ss from patient_data";
123 switch ($parameters['sortby']) {
124 case 'dob':
125 $orderby = " ORDER BY dob";
126 break;
127 case 'sex':
128 $orderby = " ORDER BY sex";
129 break;
130 case 'ssn':
131 $orderby = " ORDER BY ss";
132 break;
133 case 'name':
134 default:
135 $orderby = " ORDER BY lname, fname";
136 break;
139 $sqlstmt .= $orderby;
140 if ($parameters['limit']) {
141 $sqlstmt .= " LIMIT 0,".$parameters['limit'];
144 $qResults = sqlStatement($sqlstmt);
145 while ($row = sqlFetchArray($qResults)) {
146 if ($dupelist[$row['id']] == 1) {
147 continue;
150 $sqlstmt = "select id, pid, fname, lname, dob, sex, ss ".
151 " from patient_data where ";
152 $sqland = "";
153 if ($parameters['match_name']) {
154 $sqlstmt .= $sqland . " fname='".$row['fname']."'";
155 $sqland = " AND ";
156 $sqlstmt .= $sqland . " lname='".$row['lname']."'";
159 if ($parameters['match_sex']) {
160 $sqlstmt .= $sqland . " sex='".$row['sex']."'";
161 $sqland = " AND ";
164 if ($parameters['match_ssn']) {
165 $sqlstmt .= $sqland . " ss='".$row['ss']."'";
166 $sqland = " AND ";
169 if ($parameters['match_dob']) {
170 $sqlstmt .= $sqland . " dob='".$row['dob']."'";
171 $sqland = " AND ";
174 $mResults = sqlStatement($sqlstmt);
176 if (! $mResults) {
177 continue;
180 if (sqlNumRows($mResults) <= 1) {
181 continue;
185 echo "<div class='match_block' style='padding: 5px 0px 5px 0px;' id='dupediv".$dupecount."'>";
186 echo "<table>";
188 echo "<tr class='onerow' id='".$row['id']."' oemrid='".$row['id']."' dupecount='".$dupecount."' title='Merge duplicates into this record'>";
189 echo "<td>".$row['lname'].", ".$row['fname']."</td>";
190 echo "<td>".$row['dob']."</td>";
191 echo "<td>".$row['sex']."</td>";
192 echo "<td>".$row['ss']."</td>";
193 echo "<td><input type='button' value=' ? ' class='moreinfo' oemrid='".$row['pid']."' title='More info'></td>";
194 echo "</tr>";
196 while ($mrow = sqlFetchArray($mResults)) {
197 if ($row['id'] == $mrow['id']) {
198 continue;
201 echo "<tr class='onerow' id='".$mrow['id']."' oemrid='".$mrow['id']."' dupecount='".$dupecount."' title='Merge duplicates into this record'>";
202 echo "<td>".$mrow['lname'].", ".$mrow['fname']."</td>";
203 echo "<td>".$mrow['dob']."</td>";
204 echo "<td>".$mrow['sex']."</td>";
205 echo "<td>".$mrow['ss']."</td>";
206 echo "<td><input type='button' value=' ? ' class='moreinfo' oemrid='".$mrow['pid']."' title='More info'></td>";
207 echo "</tr>";
208 // to keep the output clean let's not repeat IDs already tagged as dupes
209 $dupelist[$row['id']] = 1;
210 $dupelist[$mrow['id']] = 1;
213 $dupecount++;
215 echo "</table>";
216 echo "</div>\n";
221 </div> <!-- end the big list -->
222 <?php if ($dupecount > 0) : ?>
223 <div id="dupecounter" style='display:inline;'><?php echo $dupecount; ?></div>
224 &nbsp;duplicates found
225 <?php endif; ?>
226 </form>
228 </body>
230 <script language="javascript">
232 $(document).ready(function(){
234 // capture RETURN keypress
235 $("#limit").keypress(function(evt) { if (evt.keyCode == 13) $("#do_search").click(); });
237 // perform the database search for duplicates
238 $("#do_search").click(function() {
239 $("#thebiglist").html("<p style='margin:10px;'><img src='<?php echo $GLOBALS['webroot']; ?>/interface/pic/ajax-loader.gif'> Searching ...</p>");
240 $("#search_form").submit();
241 return true;
244 // pop up an OpenEMR window directly to the patient info
245 var moreinfoWin = null;
246 $(".moreinfo").click(function(evt) {
247 if (moreinfoWin) { moreinfoWin.close(); }
248 moreinfoWin = window.open("<?php echo $GLOBALS['webroot']; ?>/interface/patient_file/patient_file.php?set_pid="+$(this).attr("oemrid"), "moreinfo");
249 evt.stopPropagation();
252 // highlight the block of matching records
253 $(".match_block").mouseover(function() { $(this).toggleClass("highlight_block"); });
254 $(".match_block").mouseout(function() { $(this).toggleClass("highlight_block"); });
255 $(".onerow").mouseover(function() { $(this).toggleClass("highlight"); });
256 $(".onerow").mouseout(function() { $(this).toggleClass("highlight"); });
258 // begin the merge of a block into a single record
259 $(".onerow").click(function() {
260 var dupecount = $(this).attr("dupecount");
261 var masterid = $(this).attr("oemrid");
262 var newurl = "mergerecords.php?dupecount="+dupecount+"&masterid="+masterid;
263 $("[dupecount="+dupecount+"]").each(function (i) {
264 if (this.id != masterid) { newurl += "&otherid[]="+this.id; }
266 // open a new window and show the merge results
267 moreinfoWin = window.open(newurl, "mergewin");
271 function removedupe(dupeid) {
272 // remove the merged records from the list of duplicates
273 $("#dupediv"+dupeid).remove();
274 // reduce the duplicate counter
275 var dcounter = parseInt($("#dupecounter").html());
276 $("#dupecounter").html(dcounter-1);
279 </script>
281 </html>