5 * This report shows past encounters with filtering and sorting,
6 * Added filtering to show encounters not e-signed, encounters e-signed and forms e-signed.
8 * Copyright (C) 2015 Terry Hill <terry@lillysystems.com>
9 * Copyright (C) 2007-2016 Rod Roark <rod@sunsetsystems.com>
10 * Copyright (C) 2017 Brady Miller <brady.g.miller@gmail.com>
12 * LICENSE: This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 3
15 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
24 * @author Terry Hill <terry@lilysystems.com>
25 * @author Rod Roark <rod@sunsetsystems.com>
26 * @author Brady Miller <brady.g.miller@gmail.com>
27 * @link http://www.open-emr.org
31 require_once("../globals.php");
32 require_once("$srcdir/forms.inc");
33 require_once("$srcdir/billing.inc");
34 require_once("$srcdir/patient.inc");
35 require_once("$srcdir/formatting.inc.php");
36 require_once "$srcdir/options.inc.php";
38 $alertmsg = ''; // not used yet but maybe later
40 // For each sorting option, specify the ORDER BY argument.
43 'doctor' => 'lower(u.lname), lower(u.fname), fe.date',
44 'patient' => 'lower(p.lname), lower(p.fname), fe.date',
45 'pubpid' => 'lower(p.pubpid), fe.date',
46 'time' => 'fe.date, lower(u.lname), lower(u.fname)',
47 'encounter' => 'fe.encounter, fe.date, lower(u.lname), lower(u.fname)',
50 function bucks($amount) {
51 if ($amount) printf("%.2f", $amount);
54 function show_doc_total($lastdocname, $doc_encounters) {
57 echo " <td class='detail'>$lastdocname</td>\n";
58 echo " <td class='detail' align='right'>$doc_encounters</td>\n";
63 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
64 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
65 $form_provider = $_POST['form_provider'];
66 $form_facility = $_POST['form_facility'];
67 $form_details = $_POST['form_details'] ?
true : false;
68 $form_new_patients = $_POST['form_new_patients'] ?
true : false;
69 $form_esigned = $_POST['form_esigned'] ?
true : false;
70 $form_not_esigned = $_POST['form_not_esigned'] ?
true : false;
71 $form_encounter_esigned = $_POST['form_encounter_esigned'] ?
true : false;
73 $form_orderby = $ORDERHASH[$_REQUEST['form_orderby']] ?
74 $_REQUEST['form_orderby'] : 'doctor';
75 $orderby = $ORDERHASH[$form_orderby];
81 if ($form_encounter_esigned) {
82 $esign_fields = ", es.table, es.tid ";
83 $esign_joins = "LEFT OUTER JOIN esign_signatures AS es ON es.tid = fe.encounter ";
86 $esign_fields = ", es.table, es.tid ";
87 $esign_joins = "LEFT OUTER JOIN esign_signatures AS es ON es.tid = fe.encounter ";
89 if ($form_not_esigned) {
90 $esign_fields = ", es.table, es.tid ";
91 $esign_joins = "LEFT JOIN esign_signatures AS es on es.tid = fe.encounter ";
95 "fe.encounter, fe.date, fe.reason, " .
96 "f.formdir, f.form_name, " .
97 "p.fname, p.mname, p.lname, p.pid, p.pubpid, " .
98 "u.lname AS ulname, u.fname AS ufname, u.mname AS umname " .
100 "FROM ( form_encounter AS fe, forms AS f ) " .
101 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
102 "LEFT JOIN users AS u ON u.id = fe.provider_id " .
104 "WHERE f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' ";
106 $query .= "AND fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' ";
108 $query .= "AND fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_from_date 23:59:59' ";
110 if ($form_provider) {
111 $query .= "AND fe.provider_id = '$form_provider' ";
113 if ($form_facility) {
114 $query .= "AND fe.facility_id = '$form_facility' ";
116 if ($form_new_patients) {
117 $query .= "AND fe.date = (SELECT MIN(fe2.date) FROM form_encounter AS fe2 WHERE fe2.pid = fe.pid) ";
119 if ($form_encounter_esigned) {
120 $query .= "AND es.tid = fe.encounter AND es.table = 'form_encounter' ";
123 $query .= "AND es.tid = fe.encounter ";
125 if ($form_not_esigned) {
126 $query .= "AND es.tid IS NULL ";
128 $query .= "ORDER BY $orderby";
130 $res = sqlStatement($query);
134 <?php
html_header_show();?
>
135 <title
><?php
echo xlt('Encounters Report'); ?
></title
>
137 <link rel
=stylesheet href
="<?php echo $css_header;?>" type
="text/css">
138 <link rel
="stylesheet" href
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">
140 <style type
="text/css">
142 /* specifically include & exclude from printing */
148 #report_parameters_daterange {
152 #report_results table {
157 /* specifically exclude some from the screen */
159 #report_parameters_daterange {
167 <script type
="text/javascript" src
="../../library/textformat.js?v=<?php echo $v_js_includes; ?>"></script
>
168 <script type
="text/javascript" src
="../../library/dialog.js?v=<?php echo $v_js_includes; ?>"></script
>
169 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-3-1-1/index.js"></script
>
170 <script type
="text/javascript" src
="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></script
>
171 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script
>
173 <script LANGUAGE
="JavaScript">
175 var mypcc
= '<?php echo $GLOBALS['phone_country_code
'] ?>';
177 $
(document
).ready(function() {
178 oeFixedHeaderSetup(document
.getElementById('mymaintable'));
179 var win
= top
.printLogSetup ? top
: opener
.top
;
180 win
.printLogSetup(document
.getElementById('printbutton'));
182 $
('.datepicker').datetimepicker({
183 <?php
$datetimepicker_timepicker = false; ?
>
184 <?php
$datetimepicker_formatInput = false; ?
>
185 <?php
require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?
>
186 <?php
// can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
190 function dosort(orderby
) {
191 var f
= document
.forms
[0];
192 f
.form_orderby
.value
= orderby
;
197 function refreshme() {
198 document
.forms
[0].submit();
204 <body
class="body_top">
205 <!-- Required
for the popup date selectors
-->
206 <div id
="overDiv" style
="position:absolute; visibility:hidden; z-index:1000;"></div
>
208 <span
class='title'><?php
echo xlt('Report'); ?
> - <?php
echo xlt('Encounters'); ?
></span
>
210 <div id
="report_parameters_daterange">
211 <?php
echo date("d F Y", strtotime($form_from_date)) ." to ". date("d F Y", strtotime($form_to_date)); ?
>
214 <form method
='post' name
='theform' id
='theform' action
='encounters_report.php'>
216 <div id
="report_parameters">
220 <div style
='float:left'>
225 <?php
echo xlt('Facility'); ?
>:
228 <?php
dropdown_facility($form_facility, 'form_facility', true); ?
>
231 <?php
echo xlt('Provider'); ?
>:
236 // Build a drop-down list of providers.
239 $query = "SELECT id, lname, fname FROM users WHERE ".
240 "authorized = 1 $provider_facility_filter ORDER BY lname, fname"; //(CHEMED) facility filter
242 $ures = sqlStatement($query);
244 echo " <select name='form_provider'>\n";
245 echo " <option value=''>-- " . xlt('All') . " --\n";
247 while ($urow = sqlFetchArray($ures)) {
248 $provid = $urow['id'];
249 echo " <option value='" . attr($provid) . "'";
250 if ($provid == $_POST['form_provider']) echo " selected";
251 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
259 <label
><input type
='checkbox' name
='form_new_patients' title
='First-time visits only'<?php
if ($form_new_patients) echo ' checked'; ?
>>
260 <?php
echo xlt('New'); ?
></label
>
263 <label
><input type
='checkbox' name
='form_esigned'<?php
if ($form_esigned) echo ' checked'; ?
>>
264 <?php
echo xlt('Forms Esigned'); ?
></label
>
267 <label
><input type
='checkbox' name
='form_encounter_esigned'<?php
if ($form_encounter_esigned) echo ' checked'; ?
>>
268 <?php
echo xlt('Encounter Esigned'); ?
></label
>
273 <?php
echo xlt('From'); ?
>:
276 <input type
='text' class='datepicker' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo attr($form_from_date) ?>'
280 <?php
echo xlt('To'); ?
>:
283 <input type
='text' class='datepicker' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo attr($form_to_date) ?>'
287 <label
><input type
='checkbox' name
='form_details'<?php
if ($form_details) echo ' checked'; ?
>>
288 <?php
echo xlt('Details'); ?
></label
>
291 <label
><input type
='checkbox' name
='form_not_esigned'<?php
if ($form_not_esigned) echo ' checked'; ?
>>
292 <?php
echo xlt('Not Esigned'); ?
></label
>
300 <td align
='left' valign
='middle' height
="100%">
301 <table style
='border-left:1px solid; width:100%; height:100%' >
304 <div style
='margin-left:15px'>
305 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
307 <?php
echo xlt('Submit'); ?
>
311 <?php
if ($_POST['form_refresh'] ||
$_POST['form_orderby'] ) { ?
>
312 <a href
='#' class='css_button' id
='printbutton'>
314 <?php
echo xlt('Print'); ?
>
326 </div
> <!-- end report_parameters
-->
329 if ($_POST['form_refresh'] ||
$_POST['form_orderby']) {
331 <div id
="report_results">
332 <table id
='mymaintable'>
334 <?php
if ($form_details) { ?
>
336 <a href
="nojs.php" onclick
="return dosort('doctor')"
337 <?php
if ($form_orderby == "doctor") echo " style=\"color:#00cc00\"" ?
>><?php
echo xlt('Provider'); ?
> </a
>
340 <a href
="nojs.php" onclick
="return dosort('time')"
341 <?php
if ($form_orderby == "time") echo " style=\"color:#00cc00\"" ?
>><?php
echo xlt('Date'); ?
></a
>
344 <a href
="nojs.php" onclick
="return dosort('patient')"
345 <?php
if ($form_orderby == "patient") echo " style=\"color:#00cc00\"" ?
>><?php
echo xlt('Patient'); ?
></a
>
348 <a href
="nojs.php" onclick
="return dosort('pubpid')"
349 <?php
if ($form_orderby == "pubpid") echo " style=\"color:#00cc00\"" ?
>><?php
echo xlt('ID'); ?
></a
>
352 <?php
echo xlt('Status'); ?
>
355 <?php
echo xlt('Encounter'); ?
>
358 <a href
="nojs.php" onclick
="return dosort('encounter')"
359 <?php
if ($form_orderby == "encounter") echo " style=\"color:#00cc00\"" ?
>><?php
echo xlt('Encounter Number'); ?
></a
>
362 <?php
echo xlt('Form'); ?
>
365 <?php
echo xlt('Coding'); ?
>
368 <th
><?php
echo xlt('Provider'); ?
></td
>
369 <th
><?php
echo xlt('Encounters'); ?
></td
>
377 while ($row = sqlFetchArray($res)) {
378 $patient_id = $row['pid'];
381 if (!empty($row['ulname']) ||
!empty($row['ufname'])) {
382 $docname = $row['ulname'];
383 if (!empty($row['ufname']) ||
!empty($row['umname']))
384 $docname .= ', ' . $row['ufname'] . ' ' . $row['umname'];
389 // Fetch all other forms for this encounter.
391 $encarr = getFormByEncounter($patient_id, $row['encounter'],
392 "formdir, user, form_name, form_id");
394 foreach ($encarr as $enc) {
395 if ($enc['formdir'] == 'newpatient') continue;
396 if ($encnames) $encnames .= '<br />';
397 $encnames .= text($enc['form_name']); // need to html escape it here for output below
401 // Fetch coding and compute billing status.
405 if ($billres = getBillingByEncounter($row['pid'], $row['encounter'],
406 "code_type, code, code_text, billed"))
408 foreach ($billres as $billrow) {
409 // $title = addslashes($billrow['code_text']);
410 if ($billrow['code_type'] != 'COPAY' && $billrow['code_type'] != 'TAX') {
411 $coded .= $billrow['code'] . ', ';
412 if ($billrow['billed']) ++
$billed_count; else ++
$unbilled_count;
415 $coded = substr($coded, 0, strlen($coded) - 2);
418 // Figure product sales into billing status.
419 $sres = sqlStatement("SELECT billed FROM drug_sales " .
420 "WHERE pid = '{$row['pid']}' AND encounter = '{$row['encounter']}'");
421 while ($srow = sqlFetchArray($sres)) {
422 if ($srow['billed']) ++
$billed_count; else ++
$unbilled_count;
425 // Compute billing status.
426 if ($billed_count && $unbilled_count) $status = xl('Mixed' );
427 else if ($billed_count ) $status = xl('Closed');
428 else if ($unbilled_count ) $status = xl('Open' );
429 else $status = xl('Empty' );
431 <tr bgcolor
='<?php echo $bgcolor ?>'>
433 <?php
echo ($docname == $lastdocname) ?
"" : text($docname) ?
> 
;
436 <?php
echo text(oeFormatShortDate(substr($row['date'], 0, 10))) ?
> 
;
439 <?php
echo text($row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname']); ?
> 
;
442 <?php
echo text($row['pubpid']); ?
> 
;
445 <?php
echo text($status); ?
> 
;
448 <?php
echo text($row['reason']); ?
> 
;
451 <?php
echo text($row['encounter']); ?
> 
;
454 <?php
echo $encnames; //since this variable contains html, have already html escaped it above ?>
457 <?php
echo text($coded); ?
>
462 if ($docname != $lastdocname) {
463 show_doc_total($lastdocname, $doc_encounters);
468 $lastdocname = $docname;
471 if (!$form_details) show_doc_total($lastdocname, $doc_encounters);
476 </div
> <!-- end encresults
-->
479 <?php
echo xlt('Please input search criteria above, and click Submit to view results.' ); ?
>
483 <input type
="hidden" name
="form_orderby" value
="<?php echo attr($form_orderby) ?>" />
484 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
489 <script language
='JavaScript'>
490 <?php
if ($alertmsg) { echo " alert('$alertmsg');\n"; } ?
>