patient_select.php

Updated - Ch, Nick, 19/07/2014 21:51

Μεταφόρτωση (17.484 KB)

 
1
<?php
2
/**
3
 * Patient selector screen.
4
 *
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>;.
15
 *
16
 * @package OpenEMR
17
 * @author  Brady Miller <brady@sparmy.com>
18
 * @link    http://www.open-emr.org
19
 */
20

    
21
//SANITIZE ALL ESCAPES
22
$sanitize_all_escapes=true;
23
//
24

    
25
//STOP FAKE REGISTER GLOBALS
26
$fake_register_globals=false;
27
//
28

    
29
require_once("../../globals.php");
30
require_once("$srcdir/patient.inc");
31
require_once("$srcdir/formdata.inc.php");
32

    
33
$fstart = isset($_REQUEST['fstart']) ? $_REQUEST['fstart'] : 0;
34
$popup  = empty($_REQUEST['popup']) ? 0 : 1;
35
$message = isset($_GET['message']) ? $_GET['message'] : "";
36
?>
37

    
38
<html>
39
<head>
40
<?php html_header_show();?>
41

    
42
<link rel=stylesheet href="<?php echo $css_header;?>" type="text/css">
43
<style>
44
form {
45
    padding: 0px;
46
    margin: 0px;
47
}
48
#searchCriteria {
49
    text-align: center;
50
    width: 100%;
51
    font-size: 0.8em;
52
    background-color: #ddddff;
53
    font-weight: bold;
54
    padding: 3px;
55
}
56
#searchResultsHeader { 
57
    width: 100%;
58
    background-color: lightgrey;
59
}
60
#searchResultsHeader table { 
61
    width: 96%;  /* not 100% because the 'searchResults' table has a scrollbar */
62
    border-collapse: collapse;
63
}
64
#searchResultsHeader th {
65
    font-size: 0.7em;
66
}
67
#searchResults {
68
    width: 100%;
69
    height: 80%;
70
    overflow: auto;
71
}
72

    
73
.srName { width: 12%; }
74
.srPhone { width: 11%; }
75
.srSS { width: 11%; }
76
.srDOB { width: 8%; }
77
.srID { width: 7%; }
78
.srPID { width: 7%; }
79
.srNumEnc { width: 11%; }
80
.srNumDays { width: 11%; }
81
.srDateLast { width: 11%; }
82
.srDateNext { width: 11%; }
83
.srMisc { width: 10%; }
84

    
85
#searchResults table {
86
    width: 100%;
87
    border-collapse: collapse;
88
    background-color: white;
89
}
90
#searchResults tr {
91
    cursor: hand;
92
    cursor: pointer;
93
}
94
#searchResults td {
95
    font-size: 0.7em;
96
    border-bottom: 1px solid #eee;
97
}
98
.oneResult { }
99
.billing { color: red; font-weight: bold; }
100
.highlight { 
101
    background-color: #336699;
102
    color: white;
103
}
104
</style>
105

    
106
<script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-1.2.2.min.js"></script>
107

    
108
<script language="JavaScript">
109

110
// This is called when forward or backward paging is done.
111
//
112
function submitList(offset) {
113
 var f = document.forms[0];
114
 var i = parseInt(f.fstart.value) + offset;
115
 if (i < 0) i = 0;
116
 f.fstart.value = i;
117
 top.restoreSession();
118
 f.submit();
119
}
120

    
121
</script>
122

    
123
</head>
124
<body class="body_top">
125

    
126
<form method='post' action='patient_select.php' name='theform' onsubmit='return top.restoreSession()'>
127
<input type='hidden' name='fstart'  value='<?php echo htmlspecialchars( $fstart, ENT_QUOTES); ?>' />
128

    
129
<?php
130
/* MA EL/LAK - AUTH Division
131
 * Summer School of Code 2014
132
 * OpenEMR Project
133
 *
134
 * @authors: Chatzakis Nick, Panagiotidis Xronis, Toloudis Dimitris
135
 * 
136
 * Description: Fix the proper view of date format as it is set at the global settings
137
 * Replace the static text "%m/%d/%Y" with $date_format_result
138
 */ 
139
 
140
// DatePatch -START-
141

    
142
// Get date_display_format - Global setting of date view
143
$query = "SELECT gl_value FROM globals WHERE gl_name = 'date_display_format'";
144
$state = sqlStatement($query);
145
while($row_state = sqlFetchArray($state))
146
{                
147
                if($row_state['gl_value'] == 0)
148
                {
149
                        $date_format_result = "%Y/%m/%d";        
150
                }
151
                else if($row_state['gl_value'] == 1) 
152
                { 
153
                        $date_format_result = "%m/%d/%Y"; 
154
                }
155
                else if($row_state['gl_value'] == 2)
156
                {
157
                        $date_format_result = "%d/%m/%Y";
158
                }
159
}
160
//                                   
161
// DatePatch -END- 
162
//
163

    
164
$MAXSHOW = 100; // maximum number of results to display at once
165

    
166
//the maximum number of patient records to display:
167
$sqllimit = $MAXSHOW;
168
$given = "*, DATE_FORMAT(DOB, '$date_format_result') as DOB_TS";
169
$orderby = "lname ASC, fname ASC";
170

    
171
$search_service_code = trim($_POST['search_service_code']);
172
echo "<input type='hidden' name='search_service_code' value='" .
173
  htmlspecialchars($search_service_code, ENT_QUOTES) . "' />\n";
174

    
175
if ($popup) {
176
  echo "<input type='hidden' name='popup' value='1' />\n";
177

    
178
  // Construct WHERE clause and save search parameters as form fields.
179
  $sqlBindArray = array();
180
  $where = "1 = 1";
181
  $fres = sqlStatement("SELECT * FROM layout_options " .
182
    "WHERE form_id = 'DEM' AND uor > 0 AND field_id != '' " .
183
    "ORDER BY group_name, seq");
184
  while ($frow = sqlFetchArray($fres)) {
185
    $field_id  = $frow['field_id'];
186
    if (strpos($field_id, 'em_') === 0) continue;
187
    $data_type = $frow['data_type'];
188
    if (!empty($_REQUEST[$field_id])) {
189
      $value = trim($_REQUEST[$field_id]);
190
      if ($field_id == 'pid') {
191
        $where .= " AND $field_id = ?";
192
        array_push($sqlBindArray,$value);
193
      }
194
      else if ($field_id == 'pubpid') {
195
        $where .= " AND $field_id LIKE ?";
196
        array_push($sqlBindArray,$value);
197
      }
198
      else {
199
        $where .= " AND $field_id LIKE ?";
200
        array_push($sqlBindArray,$value."%");
201
      }
202
      echo "<input type='hidden' name='" . htmlspecialchars( $field_id, ENT_QUOTES) .
203
        "' value='" . htmlspecialchars( $value, ENT_QUOTES) . "' />\n";
204
    }
205
  }
206

    
207
  // If a non-empty service code was given, then restrict to patients who
208
  // have been provided that service.  Since the code is used in a LIKE
209
  // clause, % and _ wildcards are supported.
210
  if ($search_service_code) {
211
    $where .=
212
      " AND ( SELECT COUNT(*) FROM billing AS b WHERE " .
213
      "b.pid = patient_data.pid AND " .
214
      "b.activity = 1 AND " .
215
      "b.code_type != 'COPAY' AND " .
216
      "b.code LIKE ? " .
217
      ") > 0";
218
    array_push($sqlBindArray, $search_service_code);
219
  }
220

    
221
  $sql = "SELECT $given FROM patient_data " .
222
    "WHERE $where ORDER BY $orderby LIMIT $fstart, $sqllimit";
223
  $rez = sqlStatement($sql,$sqlBindArray);
224
  $result = array();
225
  while ($row = sqlFetchArray($rez)) $result[] = $row;
226
  _set_patient_inc_count($sqllimit, count($result), $where, $sqlBindArray);
227
}
228
else {
229
  $patient = $_REQUEST['patient'];
230
  $findBy  = $_REQUEST['findBy'];
231
  $searchFields = $_REQUEST['searchFields'];
232

    
233
  echo "<input type='hidden' name='patient' value='" . htmlspecialchars( $patient, ENT_QUOTES) . "' />\n";
234
  echo "<input type='hidden' name='findBy'  value='" . htmlspecialchars( $findBy, ENT_QUOTES) . "' />\n";
235

    
236
  if ($findBy == "Last")
237
      $result = getPatientLnames("$patient", $given, $orderby, $sqllimit, $fstart);
238
  else if ($findBy == "ID")
239
      $result = getPatientId("$patient", $given, "id ASC, ".$orderby, $sqllimit, $fstart);
240
  else if ($findBy == "DOB")
241
      $result = getPatientDOB("$patient", $given, "DOB ASC, ".$orderby, $sqllimit, $fstart);
242
  else if ($findBy == "SSN")
243
      $result = getPatientSSN("$patient", $given, "ss ASC, ".$orderby, $sqllimit, $fstart);
244
  elseif ($findBy == "Phone")                  //(CHEMED) Search by phone number
245
      $result = getPatientPhone("$patient", $given, $orderby, $sqllimit, $fstart);
246
  else if ($findBy == "Any")
247
      $result = getByPatientDemographics("$patient", $given, $orderby, $sqllimit, $fstart);
248
  else if ($findBy == "Filter") {
249
    $result = getByPatientDemographicsFilter($searchFields, "$patient",
250
      $given, $orderby, $sqllimit, $fstart, $search_service_code);
251
  }
252
}
253
?>
254

    
255
</form>
256

    
257
<table border='0' cellpadding='5' cellspacing='0' width='100%'>
258
 <tr>
259
  <td class='text'>
260
   <a href="./patient_select_help.php" target=_new onclick='top.restoreSession()'>[<?php echo htmlspecialchars( xl('Help'), ENT_NOQUOTES); ?>]&nbsp</a>
261
  </td>
262
  <td class='text' align='center'>
263
<?php if ($message) echo "<font color='red'><b>".htmlspecialchars( $message, ENT_NOQUOTES)."</b></font>\n"; ?>
264
  </td>
265
  <td class='text' align='right'>
266
<?php
267
// Show start and end row number, and number of rows, with paging links.
268
//
269
// $count = $fstart + $GLOBALS['PATIENT_INC_COUNT']; // Why did I do that???
270
$count = $GLOBALS['PATIENT_INC_COUNT'];
271
$fend = $fstart + $MAXSHOW;
272
if ($fend > $count) $fend = $count;
273
?>
274
<?php if ($fstart) { ?>
275
   <a href="javascript:submitList(-<?php echo $MAXSHOW ?>)">
276
    &lt;&lt;
277
   </a>
278
   &nbsp;&nbsp;
279
<?php } ?>
280
   <?php echo ($fstart + 1) . htmlspecialchars( " - $fend of $count", ENT_NOQUOTES); ?>
281
<?php if ($count > $fend) { ?>
282
   &nbsp;&nbsp;
283
   <a href="javascript:submitList(<?php echo $MAXSHOW ?>)">
284
    &gt;&gt;
285
   </a>
286
<?php } ?>
287
  </td>
288
 </tr>
289
</table>
290

    
291
<div id="searchResultsHeader">
292
<table>
293
<tr>
294
<th class="srName"><?php echo htmlspecialchars( xl('Name'), ENT_NOQUOTES);?></th>
295
<th class="srPhone"><?php echo htmlspecialchars( xl('Phone'), ENT_NOQUOTES);?></th>
296
<th class="srSS"><?php echo htmlspecialchars( xl('SS'), ENT_NOQUOTES);?></th>
297
<th class="srDOB"><?php echo htmlspecialchars( xl('DOB'), ENT_NOQUOTES);?></th>
298
<th class="srID"><?php echo htmlspecialchars( xl('ID'), ENT_NOQUOTES);?></th>
299

    
300
<?php if (empty($GLOBALS['patient_search_results_style'])) { ?>
301
<th class="srPID"><?php echo htmlspecialchars( xl('PID'), ENT_NOQUOTES);?></th>
302
<th class="srNumEnc"><?php echo htmlspecialchars( xl('[Number Of Encounters]'), ENT_NOQUOTES);?></th>
303
<th class="srNumDays"><?php echo htmlspecialchars( xl('[Days Since Last Encounter]'), ENT_NOQUOTES);?></th>
304
<th class="srDateLast"><?php echo htmlspecialchars( xl('[Date of Last Encounter]'), ENT_NOQUOTES);?></th>
305
<th class="srDateNext">
306
<?php
307
$add_days = 90;
308
if (!$popup && preg_match('/^(\d+)\s*(.*)/',$patient,$matches) > 0) {
309
  $add_days = $matches[1];
310
  $patient = $matches[2];
311
}
312
?>
313
[<?php echo htmlspecialchars( $add_days, ENT_NOQUOTES);?> <?php echo htmlspecialchars( xl('Days From Last Encounter'), ENT_NOQUOTES); ?>]
314
</th>
315

    
316
<?php
317
}
318
else {
319
  // Alternate patient search results style; this gets address plus other
320
  // fields that are mandatory, up to a limit of 5.
321
  $extracols = array();
322
  $tres = sqlStatement("SELECT field_id, title FROM layout_options " .
323
    "WHERE form_id = 'DEM' AND ( uor > 1 AND field_id != '' " .
324
    "OR uor > 0 AND field_id = 'street' ) AND " .
325
    "field_id NOT LIKE '_name' AND " .
326
    "field_id NOT LIKE 'phone%' AND " .
327
    "field_id NOT LIKE 'title' AND " .
328
    "field_id NOT LIKE 'ss' AND " .
329
    "field_id NOT LIKE 'DOB' AND " .
330
    "field_id NOT LIKE 'pubpid' " .
331
    "ORDER BY group_name, seq LIMIT 5");
332
  while ($trow = sqlFetchArray($tres)) {
333
    $extracols[$trow['field_id']] = $trow['title'];
334
    echo "<th class='srMisc'>" . htmlspecialchars(xl($trow['title']), ENT_NOQUOTES) . "</th>\n";
335
  }
336
}
337
?>
338

    
339
</tr>
340
</table>
341
</div>
342

    
343
<div id="searchResults">
344

    
345
<table>
346
<tr>
347
<?php
348
if ($result) {
349
    foreach ($result as $iter) {
350
        echo "<tr class='oneresult' id='".htmlspecialchars( $iter['pid'], ENT_QUOTES)."'>";
351
        echo  "<td class='srName'>" . htmlspecialchars($iter['lname'] . ", " . $iter['fname']) . "</td>\n";
352
        //other phone number display setup for tooltip
353
        $phone_biz = '';
354
        if ($iter{"phone_biz"} != "") {
355
            $phone_biz = " [business phone ".$iter{"phone_biz"}."] ";
356
        }
357
        $phone_contact = '';
358
        if ($iter{"phone_contact"} != "") {
359
            $phone_contact = " [contact phone ".$iter{"phone_contact"}."] ";
360
        }
361
        $phone_cell = '';
362
        if ($iter{"phone_cell"} != "") {
363
            $phone_cell = " [cell phone ".$iter{"phone_cell"}."] ";
364
        }
365
        $all_other_phones = $phone_biz.$phone_contact.$phone_cell;
366
        if ($all_other_phones == '') {$all_other_phones = xl('No other phone numbers listed');}
367
        //end of phone number display setup, now display the phone number(s)
368
        echo "<td class='srPhone' title='".htmlspecialchars( $all_other_phones, ENT_QUOTES)."'>" .
369
            htmlspecialchars( $iter['phone_home'], ENT_NOQUOTES) . "</td>\n";
370
        
371
        echo "<td class='srSS'>" . htmlspecialchars( $iter['ss'], ENT_NOQUOTES) . "</td>";
372
        if ($iter{"DOB"} != "0000-00-00 00:00:00") {
373
            echo "<td class='srDOB'>" . htmlspecialchars( $iter['DOB_TS'], ENT_NOQUOTES) . "</td>";
374
        } else {
375
            echo "<td class='srDOB'>&nbsp;</td>";
376
        }
377
        
378
        echo "<td class='srID'>" . htmlspecialchars( $iter['pubpid'], ENT_NOQUOTES) . "</td>";
379

    
380
        if (empty($GLOBALS['patient_search_results_style'])) {
381

    
382
          echo "<td class='srPID'>" . htmlspecialchars( $iter['pid'], ENT_NOQUOTES) . "</td>";
383
          
384
          //setup for display of encounter date info
385
          $encounter_count = 0;
386
          $day_diff = ''; 
387
          $last_date_seen = ''; 
388
          $next_appt_date= ''; 
389
          $pid = '';
390

    
391
          // calculate date differences based on date of last encounter with billing entries
392
          $query = "select DATE_FORMAT(max(form_encounter.date),'$date_format_result') as mydate," .
393
                  " (to_days(current_date())-to_days(max(form_encounter.date))) as day_diff," .
394
                  " DATE_FORMAT(max(form_encounter.date) + interval " .
395
                  add_escape_custom($add_days) .
396
                  " day,'$date_format_result') as next_appt, dayname(max(form_encounter.date) + interval " .
397
                  add_escape_custom($add_days) .
398
                  " day) as next_appt_day from form_encounter " .
399
                  "join billing on billing.encounter = form_encounter.encounter and " .
400
                  "billing.pid = form_encounter.pid and billing.activity = 1 and " .
401
                  "billing.code_type not like 'COPAY' where ".
402
                  "form_encounter.pid = ?";
403
          $statement= sqlStatement($query, array($iter{"pid"}) );
404
          if ($results = sqlFetchArray($statement)) {
405
              $last_date_seen = $results['mydate']; 
406
              $day_diff = $results['day_diff'];
407
              $next_appt_date= $results['next_appt_day'].', '.$results['next_appt'];
408
          }
409
          // calculate date differences based on date of last encounter regardless of billing
410
          $query = "select DATE_FORMAT(max(form_encounter.date),'$date_format_result') as mydate," .
411
                  " (to_days(current_date())-to_days(max(form_encounter.date))) as day_diff," .
412
                  " DATE_FORMAT(max(form_encounter.date) + interval " .
413
                  add_escape_custom($add_days) .
414
                  " day,'$date_format_result') as next_appt, dayname(max(form_encounter.date) + interval " .
415
                  add_escape_custom($add_days) .
416
                  " day) as next_appt_day from form_encounter " .
417
                  " where form_encounter.pid = ?";
418
          $statement= sqlStatement($query, array($iter{"pid"}) );
419
          if ($results = sqlFetchArray($statement)) {
420
              $last_date_seen = $results['mydate']; 
421
              $day_diff = $results['day_diff'];
422
              $next_appt_date= $results['next_appt_day'].', '.$results['next_appt'];
423
          }
424

    
425
          //calculate count of encounters by distinct billing dates with cpt4
426
          //entries
427
          $query = "select count(distinct date) as encounter_count " .
428
                   " from billing ".
429
                   " where code_type not like 'COPAY' and activity = 1 " .
430
                   " and pid = ?";
431
          $statement= sqlStatement($query, array($iter{"pid"}) );
432
          if ($results = sqlFetchArray($statement)) {
433
              $encounter_count_billed = $results['encounter_count'];
434
          }
435
          // calculate count of encounters, regardless of billing
436
          $query = "select count(date) as encounter_count ".
437
                      " from form_encounter where ".
438
                      " pid = ?";
439
          $statement= sqlStatement($query, array($iter{"pid"}) );
440
          if ($results = sqlFetchArray($statement)) {
441
              $encounter_count = $results['encounter_count'];
442
          }
443
          echo "<td class='srNumEnc'>" . htmlspecialchars( $encounter_count, ENT_NOQUOTES) . "</td>\n";
444
          echo "<td class='srNumDay'>" . htmlspecialchars( $day_diff, ENT_NOQUOTES) . "</td>\n";
445
          echo "<td class='srDateLast'>" . htmlspecialchars( $last_date_seen, ENT_NOQUOTES) . "</td>\n";
446
          echo "<td class='srDateNext'>" . htmlspecialchars( $next_appt_date, ENT_NOQUOTES) . "</td>\n";
447
        }
448

    
449
        else { // alternate search results style
450
          foreach ($extracols as $field_id => $title) {
451
            echo "<td class='srMisc'>" . htmlspecialchars( $iter[$field_id], ENT_NOQUOTES) . "</td>\n";
452
          }
453
        }
454
    }
455
}
456
?>
457
</table>
458
</div>  <!-- end searchResults DIV -->
459

    
460
<script language="javascript">
461

462
// jQuery stuff to make the page a little easier to use
463

464
$(document).ready(function(){
465
    // $("#searchparm").focus();
466
    $(".oneresult").mouseover(function() { $(this).addClass("highlight"); });
467
    $(".oneresult").mouseout(function() { $(this).removeClass("highlight"); });
468
    $(".oneresult").click(function() { SelectPatient(this); });
469
    // $(".event").dblclick(function() { EditEvent(this); });
470
});
471

472
var SelectPatient = function (eObj) {
473
<?php 
474
// For the old layout we load a frameset that also sets up the new pid.
475
// The new layout loads just the demographics frame here, which in turn
476
// will set the pid and load all the other frames.
477
if ($GLOBALS['concurrent_layout']) {
478
    $newPage = "../../patient_file/summary/demographics.php?set_pid=";
479
    $target = "document";
480
}
481
else {
482
    $newPage = "../../patient_file/patient_file.php?set_pid=";
483
    $target = "top";
484
}
485
?>
486
    objID = eObj.id;
487
    var parts = objID.split("~");
488
    <?php if (!$popup) echo "top.restoreSession();\n"; ?>
489
    <?php if ($popup) echo "opener."; echo $target; ?>.location.href = '<?php echo $newPage; ?>' + parts[0];
490
    <?php if ($popup) echo "window.close();\n"; ?>
491
    return true;
492
}
493

    
494
</script>
495

    
496
</body>
497
</html>