3 * Encounters report. (/interface/reports/encounters_report.php
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>
11 * LICENSE: This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 3
14 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
23 * @author Terry Hill <terry@lilysystems.com>
24 * @author Rod Roark <rod@sunsetsystems.com>
25 * @link http://www.open-emr.org
29 require_once("../globals.php");
30 require_once("$srcdir/forms.inc");
31 require_once("$srcdir/billing.inc");
32 require_once("$srcdir/patient.inc");
33 require_once("$srcdir/formatting.inc.php");
34 require_once "$srcdir/options.inc.php";
36 $alertmsg = ''; // not used yet but maybe later
38 // For each sorting option, specify the ORDER BY argument.
41 'doctor' => 'lower(u.lname), lower(u.fname), fe.date',
42 'patient' => 'lower(p.lname), lower(p.fname), fe.date',
43 'pubpid' => 'lower(p.pubpid), fe.date',
44 'time' => 'fe.date, lower(u.lname), lower(u.fname)',
45 'encounter' => 'fe.encounter, fe.date, lower(u.lname), lower(u.fname)',
48 function bucks($amount) {
49 if ($amount) printf("%.2f", $amount);
52 function show_doc_total($lastdocname, $doc_encounters) {
55 echo " <td class='detail'>$lastdocname</td>\n";
56 echo " <td class='detail' align='right'>$doc_encounters</td>\n";
61 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
62 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
63 $form_provider = $_POST['form_provider'];
64 $form_facility = $_POST['form_facility'];
65 $form_details = $_POST['form_details'] ?
true : false;
66 $form_new_patients = $_POST['form_new_patients'] ?
true : false;
67 $form_esigned = $_POST['form_esigned'] ?
true : false;
68 $form_not_esigned = $_POST['form_not_esigned'] ?
true : false;
69 $form_encounter_esigned = $_POST['form_encounter_esigned'] ?
true : false;
71 $form_orderby = $ORDERHASH[$_REQUEST['form_orderby']] ?
72 $_REQUEST['form_orderby'] : 'doctor';
73 $orderby = $ORDERHASH[$form_orderby];
79 if ($form_encounter_esigned) {
80 $esign_fields = ", es.table, es.tid ";
81 $esign_joins = "LEFT OUTER JOIN esign_signatures AS es ON es.tid = fe.encounter ";
84 $esign_fields = ", es.table, es.tid ";
85 $esign_joins = "LEFT OUTER JOIN esign_signatures AS es ON es.tid = fe.encounter ";
87 if ($form_not_esigned) {
88 $esign_fields = ", es.table, es.tid ";
89 $esign_joins = "LEFT JOIN esign_signatures AS es on es.tid = fe.encounter ";
93 "fe.encounter, fe.date, fe.reason, " .
94 "f.formdir, f.form_name, " .
95 "p.fname, p.mname, p.lname, p.pid, p.pubpid, " .
96 "u.lname AS ulname, u.fname AS ufname, u.mname AS umname " .
98 "FROM ( form_encounter AS fe, forms AS f ) " .
99 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
100 "LEFT JOIN users AS u ON u.id = fe.provider_id " .
102 "WHERE f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' ";
104 $query .= "AND fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' ";
106 $query .= "AND fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_from_date 23:59:59' ";
108 if ($form_provider) {
109 $query .= "AND fe.provider_id = '$form_provider' ";
111 if ($form_facility) {
112 $query .= "AND fe.facility_id = '$form_facility' ";
114 if ($form_new_patients) {
115 $query .= "AND fe.date = (SELECT MIN(fe2.date) FROM form_encounter AS fe2 WHERE fe2.pid = fe.pid) ";
117 if ($form_encounter_esigned) {
118 $query .= "AND es.tid = fe.encounter AND es.table = 'form_encounter' ";
121 $query .= "AND es.tid = fe.encounter ";
123 if ($form_not_esigned) {
124 $query .= "AND es.tid IS NULL ";
126 $query .= "ORDER BY $orderby";
128 $res = sqlStatement($query);
132 <?php
html_header_show();?
>
133 <title
><?php
echo xlt('Encounters Report'); ?
></title
>
135 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
137 <link rel
=stylesheet href
="<?php echo $css_header;?>" type
="text/css">
138 <style type
="text/css">
140 /* specifically include & exclude from printing */
146 #report_parameters_daterange {
150 #report_results table {
155 /* specifically exclude some from the screen */
157 #report_parameters_daterange {
165 <script type
="text/javascript" src
="../../library/textformat.js"></script
>
166 <script type
="text/javascript" src
="../../library/dialog.js?v=<?php echo $v_js_includes; ?>"></script
>
167 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
168 <?php
include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?
>
169 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
170 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-9-1/index.js"></script
>
171 <script type
="text/javascript" src
="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></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'));
183 function dosort(orderby
) {
184 var f
= document
.forms
[0];
185 f
.form_orderby
.value
= orderby
;
190 function refreshme() {
191 document
.forms
[0].submit();
197 <body
class="body_top">
198 <!-- Required
for the popup date selectors
-->
199 <div id
="overDiv" style
="position:absolute; visibility:hidden; z-index:1000;"></div
>
201 <span
class='title'><?php
echo xlt('Report'); ?
> - <?php
echo xlt('Encounters'); ?
></span
>
203 <div id
="report_parameters_daterange">
204 <?php
echo date("d F Y", strtotime($form_from_date)) ." to ". date("d F Y", strtotime($form_to_date)); ?
>
207 <form method
='post' name
='theform' id
='theform' action
='encounters_report.php'>
209 <div id
="report_parameters">
213 <div style
='float:left'>
218 <?php
echo xlt('Facility'); ?
>:
221 <?php
dropdown_facility($form_facility, 'form_facility', true); ?
>
224 <?php
echo xlt('Provider'); ?
>:
229 // Build a drop-down list of providers.
232 $query = "SELECT id, lname, fname FROM users WHERE ".
233 "authorized = 1 $provider_facility_filter ORDER BY lname, fname"; //(CHEMED) facility filter
235 $ures = sqlStatement($query);
237 echo " <select name='form_provider'>\n";
238 echo " <option value=''>-- " . xlt('All') . " --\n";
240 while ($urow = sqlFetchArray($ures)) {
241 $provid = $urow['id'];
242 echo " <option value='" . attr($provid) . "'";
243 if ($provid == $_POST['form_provider']) echo " selected";
244 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
252 <label
><input type
='checkbox' name
='form_new_patients' title
='First-time visits only'<?php
if ($form_new_patients) echo ' checked'; ?
>>
253 <?php
echo xlt('New'); ?
></label
>
256 <label
><input type
='checkbox' name
='form_esigned'<?php
if ($form_esigned) echo ' checked'; ?
>>
257 <?php
echo xlt('Forms Esigned'); ?
></label
>
260 <label
><input type
='checkbox' name
='form_encounter_esigned'<?php
if ($form_encounter_esigned) echo ' checked'; ?
>>
261 <?php
echo xlt('Encounter Esigned'); ?
></label
>
266 <?php
echo xlt('From'); ?
>:
269 <input type
='text' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo attr($form_from_date) ?>'
270 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
271 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
272 id
='img_from_date' border
='0' alt
='[?]' style
='cursor:pointer'
273 title
='<?php echo xla('Click here to choose a date
'); ?>'>
276 <?php
echo xlt('To'); ?
>:
279 <input type
='text' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo attr($form_to_date) ?>'
280 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
281 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
282 id
='img_to_date' border
='0' alt
='[?]' style
='cursor:pointer'
283 title
='<?php echo xla('Click here to choose a date
'); ?>'>
286 <label
><input type
='checkbox' name
='form_details'<?php
if ($form_details) echo ' checked'; ?
>>
287 <?php
echo xlt('Details'); ?
></label
>
290 <label
><input type
='checkbox' name
='form_not_esigned'<?php
if ($form_not_esigned) echo ' checked'; ?
>>
291 <?php
echo xlt('Not Esigned'); ?
></label
>
299 <td align
='left' valign
='middle' height
="100%">
300 <table style
='border-left:1px solid; width:100%; height:100%' >
303 <div style
='margin-left:15px'>
304 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
306 <?php
echo xlt('Submit'); ?
>
310 <?php
if ($_POST['form_refresh'] ||
$_POST['form_orderby'] ) { ?
>
311 <a href
='#' class='css_button' id
='printbutton'>
313 <?php
echo xlt('Print'); ?
>
325 </div
> <!-- end report_parameters
-->
328 if ($_POST['form_refresh'] ||
$_POST['form_orderby']) {
330 <div id
="report_results">
331 <table id
='mymaintable'>
333 <?php
if ($form_details) { ?
>
335 <a href
="nojs.php" onclick
="return dosort('doctor')"
336 <?php
if ($form_orderby == "doctor") echo " style=\"color:#00cc00\"" ?
>><?php
echo xlt('Provider'); ?
> </a
>
339 <a href
="nojs.php" onclick
="return dosort('time')"
340 <?php
if ($form_orderby == "time") echo " style=\"color:#00cc00\"" ?
>><?php
echo xlt('Date'); ?
></a
>
343 <a href
="nojs.php" onclick
="return dosort('patient')"
344 <?php
if ($form_orderby == "patient") echo " style=\"color:#00cc00\"" ?
>><?php
echo xlt('Patient'); ?
></a
>
347 <a href
="nojs.php" onclick
="return dosort('pubpid')"
348 <?php
if ($form_orderby == "pubpid") echo " style=\"color:#00cc00\"" ?
>><?php
echo xlt('ID'); ?
></a
>
351 <?php
echo xlt('Status'); ?
>
354 <?php
echo xlt('Encounter'); ?
>
357 <a href
="nojs.php" onclick
="return dosort('encounter')"
358 <?php
if ($form_orderby == "encounter") echo " style=\"color:#00cc00\"" ?
>><?php
echo xlt('Encounter Number'); ?
></a
>
361 <?php
echo xlt('Form'); ?
>
364 <?php
echo xlt('Coding'); ?
>
367 <th
><?php
echo xlt('Provider'); ?
></td
>
368 <th
><?php
echo xlt('Encounters'); ?
></td
>
376 while ($row = sqlFetchArray($res)) {
377 $patient_id = $row['pid'];
380 if (!empty($row['ulname']) ||
!empty($row['ufname'])) {
381 $docname = $row['ulname'];
382 if (!empty($row['ufname']) ||
!empty($row['umname']))
383 $docname .= ', ' . $row['ufname'] . ' ' . $row['umname'];
388 // Fetch all other forms for this encounter.
390 $encarr = getFormByEncounter($patient_id, $row['encounter'],
391 "formdir, user, form_name, form_id");
393 foreach ($encarr as $enc) {
394 if ($enc['formdir'] == 'newpatient') continue;
395 if ($encnames) $encnames .= '<br />';
396 $encnames .= text($enc['form_name']); // need to html escape it here for output below
400 // Fetch coding and compute billing status.
404 if ($billres = getBillingByEncounter($row['pid'], $row['encounter'],
405 "code_type, code, code_text, billed"))
407 foreach ($billres as $billrow) {
408 // $title = addslashes($billrow['code_text']);
409 if ($billrow['code_type'] != 'COPAY' && $billrow['code_type'] != 'TAX') {
410 $coded .= $billrow['code'] . ', ';
411 if ($billrow['billed']) ++
$billed_count; else ++
$unbilled_count;
414 $coded = substr($coded, 0, strlen($coded) - 2);
417 // Figure product sales into billing status.
418 $sres = sqlStatement("SELECT billed FROM drug_sales " .
419 "WHERE pid = '{$row['pid']}' AND encounter = '{$row['encounter']}'");
420 while ($srow = sqlFetchArray($sres)) {
421 if ($srow['billed']) ++
$billed_count; else ++
$unbilled_count;
424 // Compute billing status.
425 if ($billed_count && $unbilled_count) $status = xl('Mixed' );
426 else if ($billed_count ) $status = xl('Closed');
427 else if ($unbilled_count ) $status = xl('Open' );
428 else $status = xl('Empty' );
430 <tr bgcolor
='<?php echo $bgcolor ?>'>
432 <?php
echo ($docname == $lastdocname) ?
"" : text($docname) ?
> 
;
435 <?php
echo text(oeFormatShortDate(substr($row['date'], 0, 10))) ?
> 
;
438 <?php
echo text($row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname']); ?
> 
;
441 <?php
echo text($row['pubpid']); ?
> 
;
444 <?php
echo text($status); ?
> 
;
447 <?php
echo text($row['reason']); ?
> 
;
450 <?php
echo text($row['encounter']); ?
> 
;
453 <?php
echo $encnames; //since this variable contains html, have already html escaped it above ?>
456 <?php
echo text($coded); ?
>
461 if ($docname != $lastdocname) {
462 show_doc_total($lastdocname, $doc_encounters);
467 $lastdocname = $docname;
470 if (!$form_details) show_doc_total($lastdocname, $doc_encounters);
475 </div
> <!-- end encresults
-->
478 <?php
echo xlt('Please input search criteria above, and click Submit to view results.' ); ?
>
482 <input type
="hidden" name
="form_orderby" value
="<?php echo attr($form_orderby) ?>" />
483 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
488 <script language
='JavaScript'>
489 Calendar
.setup({inputField
:"form_from_date", ifFormat
:"%Y-%m-%d", button
:"img_from_date"});
490 Calendar
.setup({inputField
:"form_to_date", ifFormat
:"%Y-%m-%d", button
:"img_to_date"});
492 <?php
if ($alertmsg) { echo " alert('$alertmsg');\n"; } ?
>