patient_select.php

Ch, Nick, 18/07/2014 16:38

Μεταφόρτωση (17.798 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

    
131
/* MA EL/LAK - AUTH Division
132
 * Summer School of Code 2014
133
 * OpenEMR Project
134
 *
135
 * @authors: Chatzakis Nick, Panagiotidis Xronis, Toloudis Dimitris 
136
 * 
137
 * Description: Fix the proper view of date format as it is set at the global settings
138
 * Replace the static text "%m/%d/%Y" with $date_format_result
139
 */ 
140
 
141
// DatePatch -START-
142
// Retrieve global database variables and connect to database
143
$db = new mysqli($host,$login,$pass, $dbase);       
144
if($db->connect_errno){    
145
    die("An error has occured! Please contact System Administrator!"); // Error report in case of error
146
}
147

    
148
// Get date_display_format - Global setting of date view
149
$query = "SELECT gl_value FROM globals WHERE gl_name = 'date_display_format'";
150
$result = $db->query($query) or die($db->error);
151

    
152
// Check the result and set the correct date format
153
if ($result->num_rows) 
154
{
155
        while ($row = $result->fetch_object()) {
156

    
157
                if($row->gl_value == 0)
158
                {
159
                        $date_format_result = "%Y/%m/%d";        
160
                }
161
                else if($row->gl_value == 1) 
162
                { 
163
                        $date_format_result = "%m/%d/%Y"; 
164
                }
165
                else if($row->gl_value == 2)
166
                {
167
                        $date_format_result = "%d/%m/%Y";
168
                }
169

    
170
    }
171
}
172

    
173
//
174
// DatePatch -END-
175
//
176

    
177
$MAXSHOW = 100; // maximum number of results to display at once
178

    
179
//the maximum number of patient records to display:
180
$sqllimit = $MAXSHOW;
181
$given = "*, DATE_FORMAT(DOB, '$date_format_result') as DOB_TS";
182
$orderby = "lname ASC, fname ASC";
183

    
184
$search_service_code = trim($_POST['search_service_code']);
185
echo "<input type='hidden' name='search_service_code' value='" .
186
  htmlspecialchars($search_service_code, ENT_QUOTES) . "' />\n";
187

    
188
if ($popup) {
189
  echo "<input type='hidden' name='popup' value='1' />\n";
190

    
191
  // Construct WHERE clause and save search parameters as form fields.
192
  $sqlBindArray = array();
193
  $where = "1 = 1";
194
  $fres = sqlStatement("SELECT * FROM layout_options " .
195
    "WHERE form_id = 'DEM' AND uor > 0 AND field_id != '' " .
196
    "ORDER BY group_name, seq");
197
  while ($frow = sqlFetchArray($fres)) {
198
    $field_id  = $frow['field_id'];
199
    if (strpos($field_id, 'em_') === 0) continue;
200
    $data_type = $frow['data_type'];
201
    if (!empty($_REQUEST[$field_id])) {
202
      $value = trim($_REQUEST[$field_id]);
203
      if ($field_id == 'pid') {
204
        $where .= " AND $field_id = ?";
205
        array_push($sqlBindArray,$value);
206
      }
207
      else if ($field_id == 'pubpid') {
208
        $where .= " AND $field_id LIKE ?";
209
        array_push($sqlBindArray,$value);
210
      }
211
      else {
212
        $where .= " AND $field_id LIKE ?";
213
        array_push($sqlBindArray,$value."%");
214
      }
215
      echo "<input type='hidden' name='" . htmlspecialchars( $field_id, ENT_QUOTES) .
216
        "' value='" . htmlspecialchars( $value, ENT_QUOTES) . "' />\n";
217
    }
218
  }
219

    
220
  // If a non-empty service code was given, then restrict to patients who
221
  // have been provided that service.  Since the code is used in a LIKE
222
  // clause, % and _ wildcards are supported.
223
  if ($search_service_code) {
224
    $where .=
225
      " AND ( SELECT COUNT(*) FROM billing AS b WHERE " .
226
      "b.pid = patient_data.pid AND " .
227
      "b.activity = 1 AND " .
228
      "b.code_type != 'COPAY' AND " .
229
      "b.code LIKE ? " .
230
      ") > 0";
231
    array_push($sqlBindArray, $search_service_code);
232
  }
233

    
234
  $sql = "SELECT $given FROM patient_data " .
235
    "WHERE $where ORDER BY $orderby LIMIT $fstart, $sqllimit";
236
  $rez = sqlStatement($sql,$sqlBindArray);
237
  $result = array();
238
  while ($row = sqlFetchArray($rez)) $result[] = $row;
239
  _set_patient_inc_count($sqllimit, count($result), $where, $sqlBindArray);
240
}
241
else {
242
  $patient = $_REQUEST['patient'];
243
  $findBy  = $_REQUEST['findBy'];
244
  $searchFields = $_REQUEST['searchFields'];
245

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

    
249
  if ($findBy == "Last")
250
      $result = getPatientLnames("$patient", $given, $orderby, $sqllimit, $fstart);
251
  else if ($findBy == "ID")
252
      $result = getPatientId("$patient", $given, "id ASC, ".$orderby, $sqllimit, $fstart);
253
  else if ($findBy == "DOB")
254
      $result = getPatientDOB("$patient", $given, "DOB ASC, ".$orderby, $sqllimit, $fstart);
255
  else if ($findBy == "SSN")
256
      $result = getPatientSSN("$patient", $given, "ss ASC, ".$orderby, $sqllimit, $fstart);
257
  elseif ($findBy == "Phone")                  //(CHEMED) Search by phone number
258
      $result = getPatientPhone("$patient", $given, $orderby, $sqllimit, $fstart);
259
  else if ($findBy == "Any")
260
      $result = getByPatientDemographics("$patient", $given, $orderby, $sqllimit, $fstart);
261
  else if ($findBy == "Filter") {
262
    $result = getByPatientDemographicsFilter($searchFields, "$patient",
263
      $given, $orderby, $sqllimit, $fstart, $search_service_code);
264
  }
265
}
266
?>
267

    
268
</form>
269

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

    
304
<div id="searchResultsHeader">
305
<table>
306
<tr>
307
<th class="srName"><?php echo htmlspecialchars( xl('Name'), ENT_NOQUOTES);?></th>
308
<th class="srPhone"><?php echo htmlspecialchars( xl('Phone'), ENT_NOQUOTES);?></th>
309
<th class="srSS"><?php echo htmlspecialchars( xl('SS'), ENT_NOQUOTES);?></th>
310
<th class="srDOB"><?php echo htmlspecialchars( xl('DOB'), ENT_NOQUOTES);?></th>
311
<th class="srID"><?php echo htmlspecialchars( xl('ID'), ENT_NOQUOTES);?></th>
312

    
313
<?php if (empty($GLOBALS['patient_search_results_style'])) { ?>
314
<th class="srPID"><?php echo htmlspecialchars( xl('PID'), ENT_NOQUOTES);?></th>
315
<th class="srNumEnc"><?php echo htmlspecialchars( xl('[Number Of Encounters]'), ENT_NOQUOTES);?></th>
316
<th class="srNumDays"><?php echo htmlspecialchars( xl('[Days Since Last Encounter]'), ENT_NOQUOTES);?></th>
317
<th class="srDateLast"><?php echo htmlspecialchars( xl('[Date of Last Encounter]'), ENT_NOQUOTES);?></th>
318
<th class="srDateNext">
319
<?php
320
$add_days = 90;
321
if (!$popup && preg_match('/^(\d+)\s*(.*)/',$patient,$matches) > 0) {
322
  $add_days = $matches[1];
323
  $patient = $matches[2];
324
}
325
?>
326
[<?php echo htmlspecialchars( $add_days, ENT_NOQUOTES);?> <?php echo htmlspecialchars( xl('Days From Last Encounter'), ENT_NOQUOTES); ?>]
327
</th>
328

    
329
<?php
330
}
331
else {
332
  // Alternate patient search results style; this gets address plus other
333
  // fields that are mandatory, up to a limit of 5.
334
  $extracols = array();
335
  $tres = sqlStatement("SELECT field_id, title FROM layout_options " .
336
    "WHERE form_id = 'DEM' AND ( uor > 1 AND field_id != '' " .
337
    "OR uor > 0 AND field_id = 'street' ) AND " .
338
    "field_id NOT LIKE '_name' AND " .
339
    "field_id NOT LIKE 'phone%' AND " .
340
    "field_id NOT LIKE 'title' AND " .
341
    "field_id NOT LIKE 'ss' AND " .
342
    "field_id NOT LIKE 'DOB' AND " .
343
    "field_id NOT LIKE 'pubpid' " .
344
    "ORDER BY group_name, seq LIMIT 5");
345
  while ($trow = sqlFetchArray($tres)) {
346
    $extracols[$trow['field_id']] = $trow['title'];
347
    echo "<th class='srMisc'>" . htmlspecialchars(xl($trow['title']), ENT_NOQUOTES) . "</th>\n";
348
  }
349
}
350
?>
351

    
352
</tr>
353
</table>
354
</div>
355

    
356
<div id="searchResults">
357

    
358
<table>
359
<tr>
360
<?php
361
if ($result) {
362
    foreach ($result as $iter) {
363
        echo "<tr class='oneresult' id='".htmlspecialchars( $iter['pid'], ENT_QUOTES)."'>";
364
        echo  "<td class='srName'>" . htmlspecialchars($iter['lname'] . ", " . $iter['fname']) . "</td>\n";
365
        //other phone number display setup for tooltip
366
        $phone_biz = '';
367
        if ($iter{"phone_biz"} != "") {
368
            $phone_biz = " [business phone ".$iter{"phone_biz"}."] ";
369
        }
370
        $phone_contact = '';
371
        if ($iter{"phone_contact"} != "") {
372
            $phone_contact = " [contact phone ".$iter{"phone_contact"}."] ";
373
        }
374
        $phone_cell = '';
375
        if ($iter{"phone_cell"} != "") {
376
            $phone_cell = " [cell phone ".$iter{"phone_cell"}."] ";
377
        }
378
        $all_other_phones = $phone_biz.$phone_contact.$phone_cell;
379
        if ($all_other_phones == '') {$all_other_phones = xl('No other phone numbers listed');}
380
        //end of phone number display setup, now display the phone number(s)
381
        echo "<td class='srPhone' title='".htmlspecialchars( $all_other_phones, ENT_QUOTES)."'>" .
382
            htmlspecialchars( $iter['phone_home'], ENT_NOQUOTES) . "</td>\n";
383
        
384
        echo "<td class='srSS'>" . htmlspecialchars( $iter['ss'], ENT_NOQUOTES) . "</td>";
385
        if ($iter{"DOB"} != "0000-00-00 00:00:00") {
386
            echo "<td class='srDOB'>" . htmlspecialchars( $iter['DOB_TS'], ENT_NOQUOTES) . "</td>";
387
        } else {
388
            echo "<td class='srDOB'>&nbsp;</td>";
389
        }
390
        
391
        echo "<td class='srID'>" . htmlspecialchars( $iter['pubpid'], ENT_NOQUOTES) . "</td>";
392

    
393
        if (empty($GLOBALS['patient_search_results_style'])) {
394

    
395
          echo "<td class='srPID'>" . htmlspecialchars( $iter['pid'], ENT_NOQUOTES) . "</td>";
396
          
397
          //setup for display of encounter date info
398
          $encounter_count = 0;
399
          $day_diff = ''; 
400
          $last_date_seen = ''; 
401
          $next_appt_date= ''; 
402
          $pid = '';
403

    
404
          // calculate date differences based on date of last encounter with billing entries
405
          $query = "select DATE_FORMAT(max(form_encounter.date),'$date_format_result') as mydate," .
406
                  " (to_days(current_date())-to_days(max(form_encounter.date))) as day_diff," .
407
                  " DATE_FORMAT(max(form_encounter.date) + interval " .
408
                  add_escape_custom($add_days) .
409
                  " day,'$date_format_result') as next_appt, dayname(max(form_encounter.date) + interval " .
410
                  add_escape_custom($add_days) .
411
                  " day) as next_appt_day from form_encounter " .
412
                  "join billing on billing.encounter = form_encounter.encounter and " .
413
                  "billing.pid = form_encounter.pid and billing.activity = 1 and " .
414
                  "billing.code_type not like 'COPAY' where ".
415
                  "form_encounter.pid = ?";
416
          $statement= sqlStatement($query, array($iter{"pid"}) );
417
          if ($results = sqlFetchArray($statement)) {
418
              $last_date_seen = $results['mydate']; 
419
              $day_diff = $results['day_diff'];
420
              $next_appt_date= $results['next_appt_day'].', '.$results['next_appt'];
421
          }
422
          // calculate date differences based on date of last encounter regardless of billing
423
          $query = "select DATE_FORMAT(max(form_encounter.date),'$date_format_result') as mydate," .
424
                  " (to_days(current_date())-to_days(max(form_encounter.date))) as day_diff," .
425
                  " DATE_FORMAT(max(form_encounter.date) + interval " .
426
                  add_escape_custom($add_days) .
427
                  " day,'$date_format_result') as next_appt, dayname(max(form_encounter.date) + interval " .
428
                  add_escape_custom($add_days) .
429
                  " day) as next_appt_day from form_encounter " .
430
                  " where form_encounter.pid = ?";
431
          $statement= sqlStatement($query, array($iter{"pid"}) );
432
          if ($results = sqlFetchArray($statement)) {
433
              $last_date_seen = $results['mydate']; 
434
              $day_diff = $results['day_diff'];
435
              $next_appt_date= $results['next_appt_day'].', '.$results['next_appt'];
436
          }
437

    
438
          //calculate count of encounters by distinct billing dates with cpt4
439
          //entries
440
          $query = "select count(distinct date) as encounter_count " .
441
                   " from billing ".
442
                   " where code_type not like 'COPAY' and activity = 1 " .
443
                   " and pid = ?";
444
          $statement= sqlStatement($query, array($iter{"pid"}) );
445
          if ($results = sqlFetchArray($statement)) {
446
              $encounter_count_billed = $results['encounter_count'];
447
          }
448
          // calculate count of encounters, regardless of billing
449
          $query = "select count(date) as encounter_count ".
450
                      " from form_encounter where ".
451
                      " pid = ?";
452
          $statement= sqlStatement($query, array($iter{"pid"}) );
453
          if ($results = sqlFetchArray($statement)) {
454
              $encounter_count = $results['encounter_count'];
455
          }
456
          echo "<td class='srNumEnc'>" . htmlspecialchars( $encounter_count, ENT_NOQUOTES) . "</td>\n";
457
          echo "<td class='srNumDay'>" . htmlspecialchars( $day_diff, ENT_NOQUOTES) . "</td>\n";
458
          echo "<td class='srDateLast'>" . htmlspecialchars( $last_date_seen, ENT_NOQUOTES) . "</td>\n";
459
          echo "<td class='srDateNext'>" . htmlspecialchars( $next_appt_date, ENT_NOQUOTES) . "</td>\n";
460
        }
461

    
462
        else { // alternate search results style
463
          foreach ($extracols as $field_id => $title) {
464
            echo "<td class='srMisc'>" . htmlspecialchars( $iter[$field_id], ENT_NOQUOTES) . "</td>\n";
465
          }
466
        }
467
    }
468
}
469
?>
470
</table>
471
</div>  <!-- end searchResults DIV -->
472

    
473
<script language="javascript">
474

475
// jQuery stuff to make the page a little easier to use
476

477
$(document).ready(function(){
478
    // $("#searchparm").focus();
479
    $(".oneresult").mouseover(function() { $(this).addClass("highlight"); });
480
    $(".oneresult").mouseout(function() { $(this).removeClass("highlight"); });
481
    $(".oneresult").click(function() { SelectPatient(this); });
482
    // $(".event").dblclick(function() { EditEvent(this); });
483
});
484

485
var SelectPatient = function (eObj) {
486
<?php 
487
// For the old layout we load a frameset that also sets up the new pid.
488
// The new layout loads just the demographics frame here, which in turn
489
// will set the pid and load all the other frames.
490
if ($GLOBALS['concurrent_layout']) {
491
    $newPage = "../../patient_file/summary/demographics.php?set_pid=";
492
    $target = "document";
493
}
494
else {
495
    $newPage = "../../patient_file/patient_file.php?set_pid=";
496
    $target = "top";
497
}
498
?>
499
    objID = eObj.id;
500
    var parts = objID.split("~");
501
    <?php if (!$popup) echo "top.restoreSession();\n"; ?>
502
    <?php if ($popup) echo "opener."; echo $target; ?>.location.href = '<?php echo $newPage; ?>' + parts[0];
503
    <?php if ($popup) echo "window.close();\n"; ?>
504
    return true;
505
}
506

    
507
</script>
508

    
509
</body>
510
</html>