Phyaura Calendar speed inhancement
[openemr.git] / interface / main / finder / patient_select.php
blobc2bf9d8be6e55a4a1521987b86c392a4d7bcd398
1 <?php
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; either version 2
5 // of the License, or (at your option) any later version.
7 //SANITIZE ALL ESCAPES
8 $sanitize_all_escapes=true;
9 //
11 //STOP FAKE REGISTER GLOBALS
12 $fake_register_globals=false;
15 include_once("../../globals.php");
16 include_once("$srcdir/patient.inc");
17 include_once("$srcdir/formdata.inc.php");
19 $fstart = $_REQUEST['fstart'] + 0;
20 $popup = empty($_REQUEST['popup']) ? 0 : 1;
21 $message = $_GET['message'];
24 <html>
25 <head>
26 <?php html_header_show();?>
28 <link rel=stylesheet href="<?php echo $css_header;?>" type="text/css">
29 <style>
30 form {
31 padding: 0px;
32 margin: 0px;
34 #searchCriteria {
35 text-align: center;
36 width: 100%;
37 font-size: 0.8em;
38 background-color: #ddddff;
39 font-weight: bold;
40 padding: 3px;
42 #searchResultsHeader {
43 width: 100%;
44 background-color: lightgrey;
46 #searchResultsHeader table {
47 width: 96%; /* not 100% because the 'searchResults' table has a scrollbar */
48 border-collapse: collapse;
50 #searchResultsHeader th {
51 font-size: 0.7em;
53 #searchResults {
54 width: 100%;
55 height: 80%;
56 overflow: auto;
59 .srName { width: 12%; }
60 .srPhone { width: 11%; }
61 .srSS { width: 11%; }
62 .srDOB { width: 8%; }
63 .srID { width: 7%; }
64 .srPID { width: 7%; }
65 .srNumEnc { width: 11%; }
66 .srNumDays { width: 11%; }
67 .srDateLast { width: 11%; }
68 .srDateNext { width: 11%; }
69 .srMisc { width: 10%; }
71 #searchResults table {
72 width: 100%;
73 border-collapse: collapse;
74 background-color: white;
76 #searchResults tr {
77 cursor: hand;
78 cursor: pointer;
80 #searchResults td {
81 font-size: 0.7em;
82 border-bottom: 1px solid #eee;
84 .oneResult { }
85 .billing { color: red; font-weight: bold; }
86 .highlight {
87 background-color: #336699;
88 color: white;
90 </style>
92 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-1.2.2.min.js"></script>
94 <script language="JavaScript">
96 // This is called when forward or backward paging is done.
98 function submitList(offset) {
99 var f = document.forms[0];
100 var i = parseInt(f.fstart.value) + offset;
101 if (i < 0) i = 0;
102 f.fstart.value = i;
103 f.submit();
106 </script>
108 </head>
109 <body class="body_top">
111 <form method='post' action='patient_select.php' name='theform'>
112 <input type='hidden' name='fstart' value='<?php echo htmlspecialchars( $fstart, ENT_QUOTES); ?>' />
114 <?php
115 $MAXSHOW = 100; // maximum number of results to display at once
117 //the maximum number of patient records to display:
118 $sqllimit = $MAXSHOW;
119 $given = "*, DATE_FORMAT(DOB,'%m/%d/%Y') as DOB_TS";
120 $orderby = "lname ASC, fname ASC";
122 if ($popup) {
123 echo "<input type='hidden' name='popup' value='1' />\n";
125 // Construct WHERE clause and save search parameters as form fields.
126 $sqlBindArray = array();
127 $where = "1 = 1";
128 $fres = sqlStatement("SELECT * FROM layout_options " .
129 "WHERE form_id = 'DEM' AND uor > 0 AND field_id != '' " .
130 "ORDER BY group_name, seq");
131 while ($frow = sqlFetchArray($fres)) {
132 $field_id = $frow['field_id'];
133 if (strpos($field_id, 'em_') === 0) continue;
134 $data_type = $frow['data_type'];
135 if (!empty($_REQUEST[$field_id])) {
136 $value = trim($_REQUEST[$field_id]);
137 if ($field_id == 'pid') {
138 $where .= " AND $field_id = ?";
139 array_push($sqlBindArray,$value);
141 else if ($field_id == 'pubpid') {
142 $where .= " AND $field_id LIKE ?";
143 array_push($sqlBindArray,$value);
145 else {
146 $where .= " AND $field_id LIKE ?";
147 array_push($sqlBindArray,$value."%");
149 echo "<input type='hidden' name='" . htmlspecialchars( $field_id, ENT_QUOTES) .
150 "' value='" . htmlspecialchars( $value, ENT_QUOTES) . "' />\n";
154 $sql = "SELECT $given FROM patient_data " .
155 "WHERE $where ORDER BY $orderby LIMIT $fstart, $sqllimit";
156 $rez = sqlStatement($sql,$sqlBindArray);
157 $result = array();
158 while ($row = sqlFetchArray($rez)) $result[] = $row;
159 _set_patient_inc_count($sqllimit, count($result), $where, $sqlBindArray);
161 else {
162 $patient = $_REQUEST['patient'];
163 $findBy = $_REQUEST['findBy'];
164 $searchFields = $_REQUEST['searchFields'];
166 echo "<input type='hidden' name='patient' value='" . htmlspecialchars( $patient, ENT_QUOTES) . "' />\n";
167 echo "<input type='hidden' name='findBy' value='" . htmlspecialchars( $findBy, ENT_QUOTES) . "' />\n";
169 if ($findBy == "Last")
170 $result = getPatientLnames("$patient", $given, $orderby, $sqllimit, $fstart);
171 else if ($findBy == "ID")
172 $result = getPatientId("$patient", $given, "id ASC, ".$orderby, $sqllimit, $fstart);
173 else if ($findBy == "DOB")
174 $result = getPatientDOB("$patient", $given, "DOB ASC, ".$orderby, $sqllimit, $fstart);
175 else if ($findBy == "SSN")
176 $result = getPatientSSN("$patient", $given, "ss ASC, ".$orderby, $sqllimit, $fstart);
177 elseif ($findBy == "Phone") //(CHEMED) Search by phone number
178 $result = getPatientPhone("$patient", $given, $orderby, $sqllimit, $fstart);
179 else if ($findBy == "Any")
180 $result = getByPatientDemographics("$patient", $given, $orderby, $sqllimit, $fstart);
181 else if ($findBy == "Filter") {
182 $result = getByPatientDemographicsFilter($searchFields, "$patient", $given, $orderby, $sqllimit, $fstart);
187 </form>
189 <table border='0' cellpadding='5' cellspacing='0' width='100%'>
190 <tr>
191 <td class='text'>
192 <a href="./patient_select_help.php" target=_new>[<?php echo htmlspecialchars( xl('Help'), ENT_NOQUOTES); ?>]&nbsp</a>
193 </td>
194 <td class='text' align='center'>
195 <?php if ($message) echo "<font color='red'><b>".htmlspecialchars( $message, ENT_NOQUOTES)."</b></font>\n"; ?>
196 </td>
197 <td class='text' align='right'>
198 <?php
199 // Show start and end row number, and number of rows, with paging links.
201 // $count = $fstart + $GLOBALS['PATIENT_INC_COUNT']; // Why did I do that???
202 $count = $GLOBALS['PATIENT_INC_COUNT'];
203 $fend = $fstart + $MAXSHOW;
204 if ($fend > $count) $fend = $count;
206 <?php if ($fstart) { ?>
207 <a href="javascript:submitList(-<?php echo $MAXSHOW ?>)">
208 &lt;&lt;
209 </a>
210 &nbsp;&nbsp;
211 <?php } ?>
212 <?php echo ($fstart + 1) . htmlspecialchars( " - $fend of $count", ENT_NOQUOTES); ?>
213 <?php if ($count > $fend) { ?>
214 &nbsp;&nbsp;
215 <a href="javascript:submitList(<?php echo $MAXSHOW ?>)">
216 &gt;&gt;
217 </a>
218 <?php } ?>
219 </td>
220 </tr>
221 </table>
223 <div id="searchResultsHeader">
224 <table>
225 <tr>
226 <th class="srName"><?php echo htmlspecialchars( xl('Name'), ENT_NOQUOTES);?></th>
227 <th class="srPhone"><?php echo htmlspecialchars( xl('Phone'), ENT_NOQUOTES);?></th>
228 <th class="srSS"><?php echo htmlspecialchars( xl('SS'), ENT_NOQUOTES);?></th>
229 <th class="srDOB"><?php echo htmlspecialchars( xl('DOB'), ENT_NOQUOTES);?></th>
230 <th class="srID"><?php echo htmlspecialchars( xl('ID'), ENT_NOQUOTES);?></th>
232 <?php if (empty($GLOBALS['patient_search_results_style'])) { ?>
233 <th class="srPID"><?php echo htmlspecialchars( xl('PID'), ENT_NOQUOTES);?></th>
234 <th class="srNumEnc"><?php echo htmlspecialchars( xl('[Number Of Encounters]'), ENT_NOQUOTES);?></th>
235 <th class="srNumDays"><?php echo htmlspecialchars( xl('[Days Since Last Encounter]'), ENT_NOQUOTES);?></th>
236 <th class="srDateLast"><?php echo htmlspecialchars( xl('[Date of Last Encounter]'), ENT_NOQUOTES);?></th>
237 <th class="srDateNext">
238 <?php
239 $add_days = 90;
240 if (!$popup && preg_match('/^(\d+)\s*(.*)/',$patient,$matches) > 0) {
241 $add_days = $matches[1];
242 $patient = $matches[2];
245 [<?php echo htmlspecialchars( $add_days, ENT_NOQUOTES);?> <?php echo htmlspecialchars( xl('Days From Last Encounter'), ENT_NOQUOTES); ?>]
246 </th>
248 <?php
250 else {
251 // Alternate patient search results style; this gets address plus other
252 // fields that are mandatory, up to a limit of 5.
253 $extracols = array();
254 $tres = sqlStatement("SELECT field_id, title FROM layout_options " .
255 "WHERE form_id = 'DEM' AND ( uor > 1 AND field_id != '' " .
256 "OR uor > 0 AND field_id = 'street' ) AND " .
257 "field_id NOT LIKE '_name' AND " .
258 "field_id NOT LIKE 'phone%' AND " .
259 "field_id NOT LIKE 'title' AND " .
260 "field_id NOT LIKE 'ss' AND " .
261 "field_id NOT LIKE 'DOB' AND " .
262 "field_id NOT LIKE 'pubpid' " .
263 "ORDER BY group_name, seq LIMIT 5");
264 while ($trow = sqlFetchArray($tres)) {
265 $extracols[$trow['field_id']] = $trow['title'];
266 echo "<th class='srMisc'>" . htmlspecialchars(xl($trow['title']), ENT_NOQUOTES) . "</th>\n";
271 </tr>
272 </table>
273 </div>
275 <div id="searchResults">
277 <table>
278 <tr>
279 <?php
280 if ($result) {
281 foreach ($result as $iter) {
282 echo "<tr class='oneresult' id='".htmlspecialchars( $iter['pid'], ENT_QUOTES)."'>";
283 echo "<td class='srName'>" . htmlspecialchars($iter['lname'] . ", " . $iter['fname']) . "</td>\n";
284 //other phone number display setup for tooltip
285 $phone_biz = '';
286 if ($iter{"phone_biz"} != "") {
287 $phone_biz = " [business phone ".$iter{"phone_biz"}."] ";
289 $phone_contact = '';
290 if ($iter{"phone_contact"} != "") {
291 $phone_contact = " [contact phone ".$iter{"phone_contact"}."] ";
293 $phone_cell = '';
294 if ($iter{"phone_cell"} != "") {
295 $phone_cell = " [cell phone ".$iter{"phone_cell"}."] ";
297 $all_other_phones = $phone_biz.$phone_contact.$phone_cell;
298 if ($all_other_phones == '') {$all_other_phones = xl('No other phone numbers listed');}
299 //end of phone number display setup, now display the phone number(s)
300 echo "<td class='srPhone' title='".htmlspecialchars( $all_other_phones, ENT_QUOTES)."'>" .
301 htmlspecialchars( $iter['phone_home'], ENT_NOQUOTES) . "</td>\n";
303 echo "<td class='srSS'>" . htmlspecialchars( $iter['ss'], ENT_NOQUOTES) . "</td>";
304 if ($iter{"DOB"} != "0000-00-00 00:00:00") {
305 echo "<td class='srDOB'>" . htmlspecialchars( $iter['DOB_TS'], ENT_NOQUOTES) . "</td>";
306 } else {
307 echo "<td class='srDOB'>&nbsp;</td>";
310 echo "<td class='srID'>" . htmlspecialchars( $iter['pubpid'], ENT_NOQUOTES) . "</td>";
312 if (empty($GLOBALS['patient_search_results_style'])) {
314 echo "<td class='srPID'>" . htmlspecialchars( $iter['pid'], ENT_NOQUOTES) . "</td>";
316 //setup for display of encounter date info
317 $encounter_count = 0;
318 $day_diff = '';
319 $last_date_seen = '';
320 $next_appt_date= '';
321 $pid = '';
323 // calculate date differences based on date of last encounter with billing entries
324 $query = "select DATE_FORMAT(max(form_encounter.date),'%m/%d/%y') as mydate," .
325 " (to_days(current_date())-to_days(max(form_encounter.date))) as day_diff," .
326 " DATE_FORMAT(max(form_encounter.date) + interval " .
327 add_escape_custom($add_days) .
328 " day,'%m/%d/%y') as next_appt, dayname(max(form_encounter.date) + interval " .
329 add_escape_custom($add_days) .
330 " day) as next_appt_day from form_encounter " .
331 "join billing on billing.encounter = form_encounter.encounter and " .
332 "billing.pid = form_encounter.pid and billing.activity = 1 and " .
333 "billing.code_type not like 'COPAY' where ".
334 "form_encounter.pid = ?";
335 $statement= sqlStatement($query, array($iter{"pid"}) );
336 if ($results = sqlFetchArray($statement)) {
337 $last_date_seen = $results['mydate'];
338 $day_diff = $results['day_diff'];
339 $next_appt_date= $results['next_appt_day'].', '.$results['next_appt'];
341 // calculate date differences based on date of last encounter regardless of billing
342 $query = "select DATE_FORMAT(max(form_encounter.date),'%m/%d/%y') as mydate," .
343 " (to_days(current_date())-to_days(max(form_encounter.date))) as day_diff," .
344 " DATE_FORMAT(max(form_encounter.date) + interval " .
345 add_escape_custom($add_days) .
346 " day,'%m/%d/%y') as next_appt, dayname(max(form_encounter.date) + interval " .
347 add_escape_custom($add_days) .
348 " day) as next_appt_day from form_encounter " .
349 " where form_encounter.pid = ?";
350 $statement= sqlStatement($query, array($iter{"pid"}) );
351 if ($results = sqlFetchArray($statement)) {
352 $last_date_seen = $results['mydate'];
353 $day_diff = $results['day_diff'];
354 $next_appt_date= $results['next_appt_day'].', '.$results['next_appt'];
357 //calculate count of encounters by distinct billing dates with cpt4
358 //entries
359 $query = "select count(distinct date) as encounter_count " .
360 " from billing ".
361 " where code_type not like 'COPAY' and activity = 1 " .
362 " and pid = ?";
363 $statement= sqlStatement($query, array($iter{"pid"}) );
364 if ($results = sqlFetchArray($statement)) {
365 $encounter_count_billed = $results['encounter_count'];
367 // calculate count of encounters, regardless of billing
368 $query = "select count(date) as encounter_count ".
369 " from form_encounter where ".
370 " pid = ?";
371 $statement= sqlStatement($query, array($iter{"pid"}) );
372 if ($results = sqlFetchArray($statement)) {
373 $encounter_count = $results['encounter_count'];
375 echo "<td class='srNumEnc'>" . htmlspecialchars( $encounter_count, ENT_NOQUOTES) . "</td>\n";
376 echo "<td class='srNumDay'>" . htmlspecialchars( $day_diff, ENT_NOQUOTES) . "</td>\n";
377 echo "<td class='srDateLast'>" . htmlspecialchars( $last_date_seen, ENT_NOQUOTES) . "</td>\n";
378 echo "<td class='srDateNext'>" . htmlspecialchars( $next_appt_date, ENT_NOQUOTES) . "</td>\n";
381 else { // alternate search results style
382 foreach ($extracols as $field_id => $title) {
383 echo "<td class='srMisc'>" . htmlspecialchars( $iter[$field_id], ENT_NOQUOTES) . "</td>\n";
389 </table>
390 </div> <!-- end searchResults DIV -->
392 <script language="javascript">
394 // jQuery stuff to make the page a little easier to use
396 $(document).ready(function(){
397 // $("#searchparm").focus();
398 $(".oneresult").mouseover(function() { $(this).addClass("highlight"); });
399 $(".oneresult").mouseout(function() { $(this).removeClass("highlight"); });
400 $(".oneresult").click(function() { SelectPatient(this); });
401 // $(".event").dblclick(function() { EditEvent(this); });
404 var SelectPatient = function (eObj) {
405 <?php
406 // For the old layout we load a frameset that also sets up the new pid.
407 // The new layout loads just the demographics frame here, which in turn
408 // will set the pid and load all the other frames.
409 if ($GLOBALS['concurrent_layout']) {
410 $newPage = "../../patient_file/summary/demographics.php?set_pid=";
411 $target = "document";
413 else {
414 $newPage = "../../patient_file/patient_file.php?set_pid=";
415 $target = "top";
418 objID = eObj.id;
419 var parts = objID.split("~");
420 <?php if ($popup) echo "opener."; echo $target; ?>.location.href = '<?php echo $newPage; ?>' + parts[0];
421 <?php if ($popup) echo "window.close();\n"; ?>
422 return true;
425 </script>
427 </body>
428 </html>