3 * Patient selector screen.
5 * LICENSE: This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
17 * @author Brady Miller <brady@sparmy.com>
18 * @link http://www.open-emr.org
21 //SANITIZE ALL ESCAPES
22 $sanitize_all_escapes=true;
25 //STOP FAKE REGISTER GLOBALS
26 $fake_register_globals=false;
29 require_once("../../globals.php");
30 require_once("$srcdir/patient.inc");
31 require_once("$srcdir/options.inc.php");
32 require_once("$srcdir/report_database.inc");
34 $fstart = isset($_REQUEST['fstart']) ?
$_REQUEST['fstart'] : 0;
35 $popup = empty($_REQUEST['popup']) ?
0 : 1;
36 $message = isset($_GET['message']) ?
$_GET['message'] : "";
37 $from_page = isset($_REQUEST['from_page']) ?
$_REQUEST['from_page'] : "";
43 <?php
html_header_show();?
>
44 <script type
="text/javascript" src
="<?php echo $webroot ?>/interface/main/tabs/js/include_opener.js"></script
>
46 <link rel
=stylesheet href
="<?php echo $css_header;?>" type
="text/css">
56 background
-color
: #ddddff;
60 #searchResultsHeader {
62 background
-color
: lightgrey
;
64 #searchResultsHeader table {
65 width
: 96%
; /* not 100% because the 'searchResults' table has a scrollbar */
66 border
-collapse
: collapse
;
68 #searchResultsHeader th {
77 .srName
{ width
: 12%
; }
78 .srGender
{ width
: 5%
; }
79 .srPhone
{ width
: 11%
; }
84 .srNumEnc
{ width
: 11%
; }
85 .srNumDays
{ width
: 11%
; }
86 .srDateLast
{ width
: 11%
; }
87 .srDateNext
{ width
: 11%
; }
88 .srMisc
{ width
: 10%
; }
90 #searchResults table {
92 border
-collapse
: collapse
;
93 background
-color
: white
;
101 border
-bottom
: 1px solid
#eee;
104 .billing
{ color
: red
; font
-weight
: bold
; }
106 background
-color
: #336699;
111 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-2-2/index.js"></script
>
113 <?php
if ($popup) { ?
>
114 <script type
="text/javascript" src
="../../../library/topdialog.js"></script
>
117 <script language
="JavaScript">
118 <?php
if ($popup) require($GLOBALS['srcdir'] . "/restoreSession.php"); ?
>
119 // This is called when forward or backward paging is done.
121 function submitList(offset
) {
122 var f
= document
.forms
[0];
123 var i
= parseInt(f
.fstart
.value
) + offset
;
126 top
.restoreSession();
133 <body
class="body_top">
135 <form method
='post' action
='patient_select.php' name
='theform' onsubmit
='return top.restoreSession()'>
136 <input type
='hidden' name
='fstart' value
='<?php echo htmlspecialchars( $fstart, ENT_QUOTES); ?>' />
139 $MAXSHOW = 100; // maximum number of results to display at once
141 //the maximum number of patient records to display:
142 $sqllimit = $MAXSHOW;
143 $given = "*, DATE_FORMAT(DOB,'%m/%d/%Y') as DOB_TS";
144 $orderby = "lname ASC, fname ASC";
146 $search_service_code = trim($_POST['search_service_code']);
147 echo "<input type='hidden' name='search_service_code' value='" .
148 htmlspecialchars($search_service_code, ENT_QUOTES
) . "' />\n";
151 echo "<input type='hidden' name='popup' value='1' />\n";
153 // Construct WHERE clause and save search parameters as form fields.
154 $sqlBindArray = array();
156 $fres = sqlStatement("SELECT * FROM layout_options " .
157 "WHERE form_id = 'DEM' AND uor > 0 AND field_id != '' " .
158 "ORDER BY group_name, seq");
159 while ($frow = sqlFetchArray($fres)) {
160 $field_id = $frow['field_id'];
161 if (strpos($field_id, 'em_') === 0) continue;
162 $data_type = $frow['data_type'];
163 if (!empty($_REQUEST[$field_id])) {
164 $value = trim($_REQUEST[$field_id]);
165 if ($field_id == 'pid') {
166 $where .= " AND $field_id = ?";
167 array_push($sqlBindArray,$value);
169 else if ($field_id == 'pubpid') {
170 $where .= " AND $field_id LIKE ?";
171 array_push($sqlBindArray,$value);
174 $where .= " AND $field_id LIKE ?";
175 array_push($sqlBindArray,$value."%");
177 echo "<input type='hidden' name='" . htmlspecialchars( $field_id, ENT_QUOTES
) .
178 "' value='" . htmlspecialchars( $value, ENT_QUOTES
) . "' />\n";
182 // If a non-empty service code was given, then restrict to patients who
183 // have been provided that service. Since the code is used in a LIKE
184 // clause, % and _ wildcards are supported.
185 if ($search_service_code) {
187 " AND ( SELECT COUNT(*) FROM billing AS b WHERE " .
188 "b.pid = patient_data.pid AND " .
189 "b.activity = 1 AND " .
190 "b.code_type != 'COPAY' AND " .
193 array_push($sqlBindArray, $search_service_code);
196 $sql = "SELECT $given FROM patient_data " .
197 "WHERE $where ORDER BY $orderby LIMIT $fstart, $sqllimit";
198 $rez = sqlStatement($sql,$sqlBindArray);
200 while ($row = sqlFetchArray($rez)) $result[] = $row;
201 _set_patient_inc_count($sqllimit, count($result), $where, $sqlBindArray);
203 else if ($from_page == "cdr_report") {
204 // Collect setting from cdr report
205 echo "<input type='hidden' name='from_page' value='$from_page' />\n";
206 $report_id = isset($_REQUEST['report_id']) ?
$_REQUEST['report_id'] : 0;
207 echo "<input type='hidden' name='report_id' value='".$report_id."' />\n";
208 $itemized_test_id = isset($_REQUEST['itemized_test_id']) ?
$_REQUEST['itemized_test_id'] : 0;
209 echo "<input type='hidden' name='itemized_test_id' value='".$itemized_test_id."' />\n";
210 $numerator_label = isset($_REQUEST['numerator_label']) ?
$_REQUEST['numerator_label'] : '';
211 echo "<input type='hidden' name='numerator_label' value='".$numerator_label."' />\n";
212 $pass_id = isset($_REQUEST['pass_id']) ?
$_REQUEST['pass_id'] : "all";
213 echo "<input type='hidden' name='pass_id' value='".$pass_id."' />\n";
214 $print_patients = isset($_REQUEST['print_patients'])?
$_REQUEST['print_patients'] : 0;
215 echo "<input type='hidden' name='print_patients' value='".$print_patients."' />\n";
217 // Collect patient listing from cdr report
218 if ($print_patients) {
219 // collect entire listing for printing
220 $result = collectItemizedPatientsCdrReport($report_id,$itemized_test_id,$pass_id,$numerator_label);
221 $GLOBALS['PATIENT_INC_COUNT'] = count($result);
222 $MAXSHOW = $GLOBALS['PATIENT_INC_COUNT'];
225 // collect the total listing count
226 $GLOBALS['PATIENT_INC_COUNT'] = collectItemizedPatientsCdrReport($report_id,$itemized_test_id,$pass_id,$numerator_label,true);
227 // then just collect applicable list for pagination
228 $result = collectItemizedPatientsCdrReport($report_id,$itemized_test_id,$pass_id,$numerator_label,false,$sqllimit,$fstart);
232 $patient = $_REQUEST['patient'];
233 $findBy = $_REQUEST['findBy'];
234 $searchFields = $_REQUEST['searchFields'];
236 echo "<input type='hidden' name='patient' value='" . htmlspecialchars( $patient, ENT_QUOTES
) . "' />\n";
237 echo "<input type='hidden' name='findBy' value='" . htmlspecialchars( $findBy, ENT_QUOTES
) . "' />\n";
239 if ($findBy == "Last")
240 $result = getPatientLnames("$patient", $given, $orderby, $sqllimit, $fstart);
241 else if ($findBy == "ID")
242 $result = getPatientId("$patient", $given, "id ASC, ".$orderby, $sqllimit, $fstart);
243 else if ($findBy == "DOB")
244 $result = getPatientDOB("$patient", $given, "DOB ASC, ".$orderby, $sqllimit, $fstart);
245 else if ($findBy == "SSN")
246 $result = getPatientSSN("$patient", $given, "ss ASC, ".$orderby, $sqllimit, $fstart);
247 elseif ($findBy == "Phone") //(CHEMED) Search by phone number
248 $result = getPatientPhone("$patient", $given, $orderby, $sqllimit, $fstart);
249 else if ($findBy == "Any")
250 $result = getByPatientDemographics("$patient", $given, $orderby, $sqllimit, $fstart);
251 else if ($findBy == "Filter") {
252 $result = getByPatientDemographicsFilter($searchFields, "$patient",
253 $given, $orderby, $sqllimit, $fstart, $search_service_code);
260 <table border
='0' cellpadding
='5' cellspacing
='0' width
='100%'>
263 <?php
if ($from_page == "cdr_report") { ?
>
264 <a href
='../../reports/cqm.php?report_id=<?php echo attr($report_id) ?>' class='css_button' onclick
='top.restoreSession()'><span
><?php
echo xlt("Return To Report Results"); ?
></span
></a
>
266 <a href
="./patient_select_help.php" target
=_new onclick
='top.restoreSession()'>[<?php
echo htmlspecialchars( xl('Help'), ENT_NOQUOTES
); ?
>] 
</a
>
269 <td
class='text' align
='center'>
270 <?php
if ($message) echo "<font color='red'><b>".htmlspecialchars( $message, ENT_NOQUOTES
)."</b></font>\n"; ?
>
273 <?php
if ($from_page == "cdr_report") { ?
>
274 <?php
echo "<a href='patient_select.php?from_page=cdr_report&pass_id=".attr($pass_id)."&report_id=".attr($report_id)."&itemized_test_id=".attr($itemized_test_id)."&numerator_label=".urlencode(attr($row['numerator_label']))."&print_patients=1' class='css_button' onclick='top.restoreSession()'><span>".xlt("Print Entire Listing")."</span></a>"; ?
>
277 <td
class='text' align
='right'>
279 // Show start and end row number, and number of rows, with paging links.
281 // $count = $fstart + $GLOBALS['PATIENT_INC_COUNT']; // Why did I do that???
282 $count = $GLOBALS['PATIENT_INC_COUNT'];
283 $fend = $fstart +
$MAXSHOW;
284 if ($fend > $count) $fend = $count;
286 <?php
if ($fstart) { ?
>
287 <a href
="javascript:submitList(-<?php echo $MAXSHOW ?>)">
293 $countStatement = " - " . $fend . " " . xl('of') . " " . $count;
294 echo ($fstart +
1) . htmlspecialchars( $countStatement, ENT_NOQUOTES
);
296 <?php
if ($count > $fend) { ?
>
298 <a href
="javascript:submitList(<?php echo $MAXSHOW ?>)">
305 <?php
if ($from_page == "cdr_report") {
306 echo "<td colspan='6' class='text'>";
308 if ($pass_id == "fail") {
309 echo xlt("Failed Patients");
311 else if ($pass_id == "pass") {
312 echo xlt("Passed Patients");
314 else if ($pass_id == "exclude") {
315 echo xlt("Excluded Patients");
317 else { // $pass_id == "all"
318 echo xlt("All Patients");
322 echo collectItemizedRuleDisplayTitle($report_id,$itemized_test_id,$numerator_label);
328 <div id
="searchResultsHeader">
331 <th
class="srName"><?php
echo htmlspecialchars( xl('Name'), ENT_NOQUOTES
);?
></th
>
332 <th
class="srGender"><?php
echo htmlspecialchars( xl('Sex'), ENT_NOQUOTES
);?
></th
>
333 <th
class="srPhone"><?php
echo htmlspecialchars( xl('Phone'), ENT_NOQUOTES
);?
></th
>
334 <th
class="srSS"><?php
echo htmlspecialchars( xl('SS'), ENT_NOQUOTES
);?
></th
>
335 <th
class="srDOB"><?php
echo htmlspecialchars( xl('DOB'), ENT_NOQUOTES
);?
></th
>
336 <th
class="srID"><?php
echo htmlspecialchars( xl('ID'), ENT_NOQUOTES
);?
></th
>
338 <?php
if (empty($GLOBALS['patient_search_results_style'])) { ?
>
339 <th
class="srPID"><?php
echo htmlspecialchars( xl('PID'), ENT_NOQUOTES
);?
></th
>
340 <th
class="srNumEnc"><?php
echo htmlspecialchars( xl('[Number Of Encounters]'), ENT_NOQUOTES
);?
></th
>
341 <th
class="srNumDays"><?php
echo htmlspecialchars( xl('[Days Since Last Encounter]'), ENT_NOQUOTES
);?
></th
>
342 <th
class="srDateLast"><?php
echo htmlspecialchars( xl('[Date of Last Encounter]'), ENT_NOQUOTES
);?
></th
>
343 <th
class="srDateNext">
346 if (!$popup && preg_match('/^(\d+)\s*(.*)/',$patient,$matches) > 0) {
347 $add_days = $matches[1];
348 $patient = $matches[2];
351 [<?php
echo htmlspecialchars( $add_days, ENT_NOQUOTES
);?
> <?php
echo htmlspecialchars( xl('Days From Last Encounter'), ENT_NOQUOTES
); ?
>]
357 // Alternate patient search results style; this gets address plus other
358 // fields that are mandatory, up to a limit of 5.
359 $extracols = array();
360 $tres = sqlStatement("SELECT * FROM layout_options " .
361 "WHERE form_id = 'DEM' AND ( uor > 1 AND field_id != '' " .
362 "OR uor > 0 AND field_id = 'street' ) AND " .
363 "field_id NOT LIKE '_name' AND " .
364 "field_id NOT LIKE 'phone%' AND " .
365 "field_id NOT LIKE 'title' AND " .
366 "field_id NOT LIKE 'ss' AND " .
367 "field_id NOT LIKE 'DOB' AND " .
368 "field_id NOT LIKE 'pubpid' " .
369 "ORDER BY group_name, seq LIMIT 5");
370 while ($trow = sqlFetchArray($tres)) {
371 $extracols[$trow['field_id']] = $trow;
372 echo "<th class='srMisc'>" . htmlspecialchars(xl($trow['title']), ENT_NOQUOTES
) . "</th>\n";
381 <div id
="searchResults">
387 foreach ($result as $iter) {
388 echo "<tr class='oneresult' id='".htmlspecialchars( $iter['pid'], ENT_QUOTES
)."'>";
389 echo "<td class='srName'>" . htmlspecialchars($iter['lname'] . ", " . $iter['fname']) . "</td>\n";
390 echo "<td class='srGender'>" . text(getListItemTitle("sex",$iter['sex'])) . "</td>\n";
391 //other phone number display setup for tooltip
393 if ($iter{"phone_biz"} != "") {
394 $phone_biz = " [business phone ".$iter{"phone_biz"}."] ";
397 if ($iter{"phone_contact"} != "") {
398 $phone_contact = " [contact phone ".$iter{"phone_contact"}."] ";
401 if ($iter{"phone_cell"} != "") {
402 $phone_cell = " [cell phone ".$iter{"phone_cell"}."] ";
404 $all_other_phones = $phone_biz.$phone_contact.$phone_cell;
405 if ($all_other_phones == '') {$all_other_phones = xl('No other phone numbers listed');}
406 //end of phone number display setup, now display the phone number(s)
407 echo "<td class='srPhone' title='".htmlspecialchars( $all_other_phones, ENT_QUOTES
)."'>" .
408 htmlspecialchars( $iter['phone_home'], ENT_NOQUOTES
) . "</td>\n";
410 echo "<td class='srSS'>" . htmlspecialchars( $iter['ss'], ENT_NOQUOTES
) . "</td>";
411 if ($iter{"DOB"} != "0000-00-00 00:00:00") {
412 echo "<td class='srDOB'>" . htmlspecialchars( $iter['DOB_TS'], ENT_NOQUOTES
) . "</td>";
414 echo "<td class='srDOB'> </td>";
417 echo "<td class='srID'>" . htmlspecialchars( $iter['pubpid'], ENT_NOQUOTES
) . "</td>";
419 if (empty($GLOBALS['patient_search_results_style'])) {
421 echo "<td class='srPID'>" . htmlspecialchars( $iter['pid'], ENT_NOQUOTES
) . "</td>";
423 //setup for display of encounter date info
424 $encounter_count = 0;
426 $last_date_seen = '';
430 // calculate date differences based on date of last encounter with billing entries
431 $query = "select DATE_FORMAT(max(form_encounter.date),'%m/%d/%y') as mydate," .
432 " (to_days(current_date())-to_days(max(form_encounter.date))) as day_diff," .
433 " DATE_FORMAT(max(form_encounter.date) + interval " .
434 add_escape_custom($add_days) .
435 " day,'%m/%d/%y') as next_appt, dayname(max(form_encounter.date) + interval " .
436 add_escape_custom($add_days) .
437 " day) as next_appt_day from form_encounter " .
438 "join billing on billing.encounter = form_encounter.encounter and " .
439 "billing.pid = form_encounter.pid and billing.activity = 1 and " .
440 "billing.code_type not like 'COPAY' where ".
441 "form_encounter.pid = ?";
442 $statement= sqlStatement($query, array($iter{"pid"}) );
443 if ($results = sqlFetchArray($statement)) {
444 $last_date_seen = $results['mydate'];
445 $day_diff = $results['day_diff'];
446 $next_appt_date= xl($results['next_appt_day']).', '.$results['next_appt'];
448 // calculate date differences based on date of last encounter regardless of billing
449 $query = "select DATE_FORMAT(max(form_encounter.date),'%m/%d/%y') as mydate," .
450 " (to_days(current_date())-to_days(max(form_encounter.date))) as day_diff," .
451 " DATE_FORMAT(max(form_encounter.date) + interval " .
452 add_escape_custom($add_days) .
453 " day,'%m/%d/%y') as next_appt, dayname(max(form_encounter.date) + interval " .
454 add_escape_custom($add_days) .
455 " day) as next_appt_day from form_encounter " .
456 " where form_encounter.pid = ?";
457 $statement= sqlStatement($query, array($iter{"pid"}) );
458 if ($results = sqlFetchArray($statement)) {
459 $last_date_seen = $results['mydate'];
460 $day_diff = $results['day_diff'];
461 $next_appt_date= xl($results['next_appt_day']).', '.$results['next_appt'];
464 //calculate count of encounters by distinct billing dates with cpt4
466 $query = "select count(distinct date) as encounter_count " .
468 " where code_type not like 'COPAY' and activity = 1 " .
470 $statement= sqlStatement($query, array($iter{"pid"}) );
471 if ($results = sqlFetchArray($statement)) {
472 $encounter_count_billed = $results['encounter_count'];
474 // calculate count of encounters, regardless of billing
475 $query = "select count(date) as encounter_count ".
476 " from form_encounter where ".
478 $statement= sqlStatement($query, array($iter{"pid"}) );
479 if ($results = sqlFetchArray($statement)) {
480 $encounter_count = $results['encounter_count'];
482 echo "<td class='srNumEnc'>" . htmlspecialchars( $encounter_count, ENT_NOQUOTES
) . "</td>\n";
483 echo "<td class='srNumDay'>" . htmlspecialchars( $day_diff, ENT_NOQUOTES
) . "</td>\n";
484 echo "<td class='srDateLast'>" . htmlspecialchars( $last_date_seen, ENT_NOQUOTES
) . "</td>\n";
485 echo "<td class='srDateNext'>" . htmlspecialchars( $next_appt_date, ENT_NOQUOTES
) . "</td>\n";
488 else { // alternate search results style
489 foreach ($extracols as $field_id => $frow) {
490 echo "<td class='srMisc'>";
491 echo generate_display_field($frow, $iter[$field_id]);
500 </div
> <!-- end searchResults DIV
-->
502 <script language
="javascript">
504 // jQuery stuff to make the page a little easier to use
506 $
(document
).ready(function(){
507 // $("#searchparm").focus();
508 $
(".oneresult").mouseover(function() { $
(this
).addClass("highlight"); });
509 $
(".oneresult").mouseout(function() { $
(this
).removeClass("highlight"); });
510 $
(".oneresult").click(function() { SelectPatient(this
); });
511 // $(".event").dblclick(function() { EditEvent(this); });
512 <?php
if($print_patients) { ?
>
513 var win
= top
.printLogPrint ? top
: opener
.top
;
514 win
.printLogPrint(window
);
518 var SelectPatient
= function (eObj
) {
520 // The layout loads just the demographics frame here, which in turn
521 // will set the pid and load all the other frames.
522 $newPage = "../../patient_file/summary/demographics.php?set_pid=";
523 $target = "document";
526 var parts
= objID
.split("~");
527 <?php
if (!$popup) echo "top.restoreSession();\n"; ?
>
528 <?php
if ($popup) echo "opener."; echo $target; ?
>.location
.href
= '<?php echo $newPage; ?>' + parts
[0];
529 <?php
if ($popup) echo "window.close();\n"; ?
>