6 * This report shows past encounters with filtering and sorting,
7 * Added filtering to show encounters not e-signed, encounters e-signed and forms e-signed.
10 * @link http://www.open-emr.org
11 * @author Rod Roark <rod@sunsetsystems.com>
12 * @author Terry Hill <terry@lilysystems.com>
13 * @author Brady Miller <brady.g.miller@gmail.com>
14 * @copyright Copyright (c) 2007-2016 Rod Roark <rod@sunsetsystems.com>
15 * @copyright Copyright (c) 2015 Terry Hill <terry@lillysystems.com>
16 * @copyright Copyright (c) 2017-2018 Brady Miller <brady.g.miller@gmail.com>
17 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
20 require_once("../globals.php");
21 require_once("$srcdir/forms.inc");
22 require_once("$srcdir/patient.inc");
23 require_once "$srcdir/options.inc.php";
25 use OpenEMR\Billing\BillingUtilities
;
26 use OpenEMR\Common\Csrf\CsrfUtils
;
27 use OpenEMR\Core\Header
;
30 if (!CsrfUtils
::verifyCsrfToken($_POST["csrf_token_form"])) {
31 CsrfUtils
::csrfNotVerified();
35 $alertmsg = ''; // not used yet but maybe later
37 // For each sorting option, specify the ORDER BY argument.
40 'doctor' => 'lower(u.lname), lower(u.fname), fe.date',
41 'patient' => 'lower(p.lname), lower(p.fname), fe.date',
42 'pubpid' => 'lower(p.pubpid), fe.date',
43 'time' => 'fe.date, lower(u.lname), lower(u.fname)',
44 'encounter' => 'fe.encounter, fe.date, lower(u.lname), lower(u.fname)',
47 function show_doc_total($lastdocname, $doc_encounters)
51 echo " <td class='detail'>" . text($lastdocname) . "</td>\n";
52 echo " <td class='detail' align='right'>" . text($doc_encounters) . "</td>\n";
57 $form_from_date = (isset($_POST['form_from_date'])) ?
DateToYYYYMMDD($_POST['form_from_date']) : date('Y-m-d');
58 $form_to_date = (isset($_POST['form_to_date'])) ?
DateToYYYYMMDD($_POST['form_to_date']) : date('Y-m-d');
59 $form_provider = $_POST['form_provider'] ??
null;
60 $form_facility = $_POST['form_facility'] ??
null;
61 $form_details = (!empty($_POST['form_details'])) ?
true : false;
62 $form_new_patients = (!empty($_POST['form_new_patients'])) ?
true : false;
63 $form_esigned = (!empty($_POST['form_esigned'])) ?
true : false;
64 $form_not_esigned = (!empty($_POST['form_not_esigned'])) ?
true : false;
65 $form_encounter_esigned = (!empty($_POST['form_encounter_esigned'])) ?
true : false;
67 $form_orderby = (!empty($_REQUEST['form_orderby']) && $ORDERHASH[$_REQUEST['form_orderby']]) ?
$_REQUEST['form_orderby'] : 'doctor';
68 $orderby = $ORDERHASH[$form_orderby];
74 if ($form_encounter_esigned) {
75 $esign_fields = ", es.table, es.tid ";
76 $esign_joins = "LEFT OUTER JOIN esign_signatures AS es ON es.tid = fe.encounter ";
80 $esign_fields = ", es.table, es.tid ";
81 $esign_joins = "LEFT OUTER JOIN esign_signatures AS es ON es.tid = fe.encounter ";
84 if ($form_not_esigned) {
85 $esign_fields = ", es.table, es.tid ";
86 $esign_joins = "LEFT JOIN esign_signatures AS es on es.tid = fe.encounter ";
89 $sqlBindArray = array();
92 "fe.encounter, fe.date, fe.reason, " .
93 "f.formdir, f.form_name, " .
94 "p.fname, p.mname, p.lname, p.pid, p.pubpid, " .
95 "u.lname AS ulname, u.fname AS ufname, u.mname AS umname " .
97 "FROM ( form_encounter AS fe, forms AS f ) " .
98 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
99 "LEFT JOIN users AS u ON u.id = fe.provider_id " .
101 "WHERE f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' ";
103 $query .= "AND fe.date >= ? AND fe.date <= ? ";
104 array_push($sqlBindArray, $form_from_date . ' 00:00:00', $form_to_date . ' 23:59:59');
106 $query .= "AND fe.date >= ? AND fe.date <= ? ";
107 array_push($sqlBindArray, $form_from_date . ' 00:00:00', $form_from_date . ' 23:59:59');
110 if ($form_provider) {
111 $query .= "AND fe.provider_id = ? ";
112 array_push($sqlBindArray, $form_provider);
115 if ($form_facility) {
116 $query .= "AND fe.facility_id = ? ";
117 array_push($sqlBindArray, $form_facility);
120 if ($form_new_patients) {
121 $query .= "AND fe.date = (SELECT MIN(fe2.date) FROM form_encounter AS fe2 WHERE fe2.pid = fe.pid) ";
124 if ($form_encounter_esigned) {
125 $query .= "AND es.tid = fe.encounter AND es.table = 'form_encounter' ";
129 $query .= "AND es.tid = fe.encounter ";
132 if ($form_not_esigned) {
133 $query .= "AND es.tid IS NULL ";
136 $query .= "ORDER BY $orderby";
138 $res = sqlStatement($query, $sqlBindArray);
142 <title
><?php
echo xlt('Encounters Report'); ?
></title
>
144 <?php Header
::setupHeader(['datetime-picker', 'report-helper']); ?
>
147 /* specifically include & exclude from printing */
153 #report_parameters_daterange {
157 #report_results table {
162 /* specifically exclude some from the screen */
164 #report_parameters_daterange {
173 oeFixedHeaderSetup(document
.getElementById('mymaintable'));
174 var win
= top
.printLogSetup ? top
: opener
.top
;
175 win
.printLogSetup(document
.getElementById('printbutton'));
177 $
('.datepicker').datetimepicker({
178 <?php
$datetimepicker_timepicker = false; ?
>
179 <?php
$datetimepicker_showseconds = false; ?
>
180 <?php
$datetimepicker_formatInput = true; ?
>
181 <?php
require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?
>
182 <?php
// can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
186 function dosort(orderby
) {
187 var f
= document
.forms
[0];
188 f
.form_orderby
.value
= orderby
;
193 function refreshme() {
194 document
.forms
[0].submit();
198 <body
class="body_top">
199 <!-- Required
for the popup date selectors
-->
200 <div id
="overDiv" style
="position:absolute; visibility:hidden; z-index:1000;"></div
>
202 <span
class='title'><?php
echo xlt('Report'); ?
> - <?php
echo xlt('Encounters'); ?
></span
>
204 <div id
="report_parameters_daterange">
205 <?php
echo text(oeFormatShortDate($form_from_date)) . " " . xlt('to{{Range}}') . " " . text(oeFormatShortDate($form_to_date)); ?
>
208 <form method
='post' name
='theform' id
='theform' action
='encounters_report.php' onsubmit
='return top.restoreSession()'>
209 <input type
="hidden" name
="csrf_token_form" value
="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
211 <div id
="report_parameters">
215 <div style
='float:left'>
219 <td
class='col-form-label'>
220 <?php
echo xlt('Facility'); ?
>:
223 <?php
dropdown_facility($form_facility, 'form_facility', true); ?
>
225 <td
class='col-form-label'>
226 <?php
echo xlt('Provider'); ?
>:
231 // Build a drop-down list of providers.
234 $query = "SELECT id, lname, fname FROM users WHERE " .
235 "authorized = 1 ORDER BY lname, fname"; //(CHEMED) facility filter
237 $ures = sqlStatement($query);
239 echo " <select name='form_provider' class='form-control'>\n";
240 echo " <option value=''>-- " . xlt('All') . " --\n";
242 while ($urow = sqlFetchArray($ures)) {
243 $provid = $urow['id'];
244 echo " <option value='" . attr($provid) . "'";
245 if (!empty($_POST['form_provider']) && ($provid == $_POST['form_provider'])) {
249 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
258 <td
class='col-form-label'>
259 <?php
echo xlt('From'); ?
>:
262 <input type
='text' class='datepicker form-control' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo attr(oeFormatShortDate($form_from_date)); ?>'>
264 <td
class='col-form-label'>
265 <?php
echo xlt('To{{Range}}'); ?
>:
268 <input type
='text' class='datepicker form-control' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo attr(oeFormatShortDate($form_to_date)); ?>'>
274 <div
class="checkbox">
275 <label
><input type
='checkbox' name
='form_details'<?php
echo ($form_details) ?
' checked' : ''; ?
>>
276 <?php
echo xlt('Details'); ?
></label
>
278 <div
class="checkbox">
279 <label
><input type
='checkbox' name
='form_new_patients' title
='<?php echo xla('First
-time visits only
'); ?>'<?php
echo ($form_new_patients) ?
' checked' : ''; ?
>>
280 <?php
echo xlt('New'); ?
></label
>
285 <div
class="checkbox">
286 <label
><input type
='checkbox' name
='form_esigned'<?php
echo ($form_esigned) ?
' checked' : ''; ?
>>
287 <?php
echo xlt('Forms Esigned'); ?
></label
>
289 <div
class="checkbox">
290 <label
><input type
='checkbox' name
='form_encounter_esigned'<?php
echo ($form_encounter_esigned) ?
' checked' : ''; ?
>>
291 <?php
echo xlt('Encounter Esigned'); ?
></label
>
293 <div
class="checkbox">
294 <label
><input type
='checkbox' name
='form_not_esigned'<?php
echo ($form_not_esigned) ?
' checked' : ''; ?
>>
295 <?php
echo xlt('Not Esigned'); ?
></label
>
304 <td
class='h-100' align
='left' valign
='middle'>
305 <table
class='w-100 h-100' style
='border-left:1px solid;'>
308 <div
class="text-center">
309 <div
class="btn-group" role
="group">
310 <a href
='#' class='btn btn-secondary btn-save' onclick
='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
311 <?php
echo xlt('Submit'); ?
>
313 <?php
if (!empty($_POST['form_refresh']) ||
!empty($_POST['form_orderby'])) { ?
>
314 <a href
='#' class='btn btn-secondary btn-print' id
='printbutton'>
315 <?php
echo xlt('Print'); ?
>
327 </div
> <!-- end report_parameters
-->
330 if (!empty($_POST['form_refresh']) ||
!empty($_POST['form_orderby'])) {
332 <div id
="report_results">
333 <table
class='table' id
='mymaintable'>
334 <thead
class='thead-light'>
335 <?php
if ($form_details) { ?
>
337 <a href
="nojs.php" onclick
="return dosort('doctor')"
338 <?php
echo ($form_orderby == "doctor") ?
" style=\"color: var(--success)\"" : ""; ?
>><?php
echo xlt('Provider'); ?
> </a
>
341 <a href
="nojs.php" onclick
="return dosort('time')"
342 <?php
echo ($form_orderby == "time") ?
" style=\"color: var(--success)\"" : ""; ?
>><?php
echo xlt('Date'); ?
></a
>
345 <a href
="nojs.php" onclick
="return dosort('patient')"
346 <?php
echo ($form_orderby == "patient") ?
" style=\"color: var(--success)\"" : ""; ?
>><?php
echo xlt('Patient'); ?
></a
>
349 <a href
="nojs.php" onclick
="return dosort('pubpid')"
350 <?php
echo ($form_orderby == "pubpid") ?
" style=\"color: var(--success)\"" : ""; ?
>><?php
echo xlt('ID'); ?
></a
>
353 <?php
echo xlt('Status'); ?
>
356 <?php
echo xlt('Encounter'); ?
>
359 <a href
="nojs.php" onclick
="return dosort('encounter')"
360 <?php
echo ($form_orderby == "encounter") ?
" style=\"color: var(--success)\"" : ""; ?
>><?php
echo xlt('Encounter Number'); ?
></a
>
363 <?php
echo xlt('Form'); ?
>
366 <?php
echo xlt('Coding'); ?
>
369 <th
><?php
echo xlt('Provider'); ?
></td
>
370 <th
><?php
echo xlt('Encounters'); ?
></td
>
378 while ($row = sqlFetchArray($res)) {
379 $patient_id = $row['pid'];
382 if (!empty($row['ulname']) ||
!empty($row['ufname'])) {
383 $docname = $row['ulname'];
384 if (!empty($row['ufname']) ||
!empty($row['umname'])) {
385 $docname .= ', ' . $row['ufname'] . ' ' . $row['umname'];
391 // Fetch all other forms for this encounter.
393 $encarr = getFormByEncounter(
396 "formdir, user, form_name, form_id"
399 foreach ($encarr as $enc) {
400 if ($enc['formdir'] == 'newpatient') {
405 $encnames .= '<br />';
408 $encnames .= text($enc['form_name']); // need to html escape it here for output below
412 // Fetch coding and compute billing status.
417 $billres = BillingUtilities
::getBillingByEncounter(
420 "code_type, code, code_text, billed"
423 foreach ($billres as $billrow) {
424 // $title = addslashes($billrow['code_text']);
425 if ($billrow['code_type'] != 'COPAY' && $billrow['code_type'] != 'TAX') {
426 $coded .= $billrow['code'] . ', ';
427 if ($billrow['billed']) {
435 $coded = substr($coded, 0, strlen($coded) - 2);
438 // Figure product sales into billing status.
439 $sres = sqlStatement("SELECT billed FROM drug_sales " .
440 "WHERE pid = ? AND encounter = ?", array($row['pid'], $row['encounter']));
441 while ($srow = sqlFetchArray($sres)) {
442 if ($srow['billed']) {
449 // Compute billing status.
450 if ($billed_count && $unbilled_count) {
451 $status = xl('Mixed');
452 } elseif ($billed_count) {
453 $status = xl('Closed');
454 } elseif ($unbilled_count) {
455 $status = xl('Open');
457 $status = xl('Empty');
460 <tr bgcolor
='<?php echo attr($bgcolor ?? ''); ?>'>
462 <?php
echo ($docname == $lastdocname) ?
"" : text($docname) ?
> 
;
465 <?php
echo text(oeFormatShortDate(substr($row['date'], 0, 10))) ?
> 
;
468 <?php
echo text($row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname']); ?
> 
;
471 <?php
echo text($row['pubpid']); ?
> 
;
474 <?php
echo text($status); ?
> 
;
477 <?php
echo text($row['reason']); ?
> 
;
480 <?php
echo text($row['encounter']); ?
> 
;
483 <?php
echo $encnames; //since this variable contains html, have already html escaped it above ?>
486 <?php
echo text($coded); ?
>
491 if ($docname != $lastdocname) {
492 show_doc_total($lastdocname, $doc_encounters);
499 $lastdocname = $docname;
502 if (!$form_details) {
503 show_doc_total($lastdocname, $doc_encounters);
509 </div
> <!-- end encresults
-->
512 <?php
echo xlt('Please input search criteria above, and click Submit to view results.'); ?
>
516 <input type
="hidden" name
="form_orderby" value
="<?php echo attr($form_orderby) ?>" />
517 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
523 <?php
if ($alertmsg) {
524 echo " alert(" . js_escape($alertmsg) . ");\n";