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.php");
22 require_once("$srcdir/patient.inc.php");
23 require_once "$srcdir/options.inc.php";
25 use OpenEMR\Common\Acl\AclMain
;
26 use OpenEMR\Billing\BillingUtilities
;
27 use OpenEMR\Common\Csrf\CsrfUtils
;
28 use OpenEMR\Common\Twig\TwigContainer
;
29 use OpenEMR\Core\Header
;
31 if (!AclMain
::aclCheckCore('encounters', 'coding_a')) {
32 echo (new TwigContainer(null, $GLOBALS['kernel']))->getTwig()->render('core/unauthorized.html.twig', ['pageTitle' => xl("Encounters Report")]);
37 if (!CsrfUtils
::verifyCsrfToken($_POST["csrf_token_form"])) {
38 CsrfUtils
::csrfNotVerified();
42 $alertmsg = ''; // not used yet but maybe later
44 // For each sorting option, specify the ORDER BY argument.
47 'doctor' => 'lower(u.lname), lower(u.fname), fe.date',
48 'patient' => 'lower(p.lname), lower(p.fname), fe.date',
49 'pubpid' => 'lower(p.pubpid), fe.date',
50 'time' => 'fe.date, lower(u.lname), lower(u.fname)',
51 'encounter' => 'fe.encounter, fe.date, lower(u.lname), lower(u.fname)',
54 function show_doc_total($lastdocname, $doc_encounters)
58 echo " <td class='detail'>" . text($lastdocname) . "</td>\n";
59 echo " <td class='detail' align='right'>" . text($doc_encounters) . "</td>\n";
64 $form_from_date = (isset($_POST['form_from_date'])) ?
DateToYYYYMMDD($_POST['form_from_date']) : date('Y-m-d');
65 $form_to_date = (isset($_POST['form_to_date'])) ?
DateToYYYYMMDD($_POST['form_to_date']) : date('Y-m-d');
66 $form_provider = $_POST['form_provider'] ??
null;
67 $form_facility = $_POST['form_facility'] ??
null;
68 $form_details = (!empty($_POST['form_details'])) ?
true : false;
69 $form_new_patients = (!empty($_POST['form_new_patients'])) ?
true : false;
70 $form_esigned = (!empty($_POST['form_esigned'])) ?
true : false;
71 $form_not_esigned = (!empty($_POST['form_not_esigned'])) ?
true : false;
72 $form_encounter_esigned = (!empty($_POST['form_encounter_esigned'])) ?
true : false;
74 $form_orderby = (!empty($_REQUEST['form_orderby']) && $ORDERHASH[$_REQUEST['form_orderby']]) ?
$_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 ";
87 $esign_fields = ", es.table, es.tid ";
88 $esign_joins = "LEFT OUTER JOIN esign_signatures AS es ON es.tid = fe.encounter ";
91 if ($form_not_esigned) {
92 $esign_fields = ", es.table, es.tid ";
93 $esign_joins = "LEFT JOIN esign_signatures AS es on es.tid = fe.encounter ";
96 $sqlBindArray = array();
99 "fe.encounter, fe.date, fe.reason, " .
100 "f.formdir, f.form_name, " .
101 "p.fname, p.mname, p.lname, p.pid, p.pubpid, p.dob, " .
102 "u.lname AS ulname, u.fname AS ufname, u.mname AS umname " .
104 "FROM ( form_encounter AS fe, forms AS f ) " .
105 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
106 "LEFT JOIN users AS u ON u.id = fe.provider_id " .
108 "WHERE f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' ";
110 $query .= "AND fe.date >= ? AND fe.date <= ? ";
111 array_push($sqlBindArray, $form_from_date . ' 00:00:00', $form_to_date . ' 23:59:59');
113 $query .= "AND fe.date >= ? AND fe.date <= ? ";
114 array_push($sqlBindArray, $form_from_date . ' 00:00:00', $form_from_date . ' 23:59:59');
117 if ($form_provider) {
118 $query .= "AND fe.provider_id = ? ";
119 array_push($sqlBindArray, $form_provider);
122 if ($form_facility) {
123 $query .= "AND fe.facility_id = ? ";
124 array_push($sqlBindArray, $form_facility);
127 if ($form_new_patients) {
128 $query .= "AND fe.date = (SELECT MIN(fe2.date) FROM form_encounter AS fe2 WHERE fe2.pid = fe.pid) ";
131 if ($form_encounter_esigned) {
132 $query .= "AND es.tid = fe.encounter AND es.table = 'form_encounter' ";
136 $query .= "AND es.tid = fe.encounter ";
139 if ($form_not_esigned) {
140 $query .= "AND es.tid IS NULL ";
143 $query .= "ORDER BY $orderby";
145 $res = sqlStatement($query, $sqlBindArray);
149 <title
><?php
echo xlt('Encounters Report'); ?
></title
>
151 <?php Header
::setupHeader(['datetime-picker', 'report-helper']); ?
>
154 /* specifically include & exclude from printing */
160 #report_parameters_daterange {
164 #report_results table {
169 /* specifically exclude some from the screen */
171 #report_parameters_daterange {
180 oeFixedHeaderSetup(document
.getElementById('mymaintable'));
181 var win
= top
.printLogSetup ? top
: opener
.top
;
182 win
.printLogSetup(document
.getElementById('printbutton'));
184 $
('.datepicker').datetimepicker({
185 <?php
$datetimepicker_timepicker = false; ?
>
186 <?php
$datetimepicker_showseconds = false; ?
>
187 <?php
$datetimepicker_formatInput = true; ?
>
188 <?php
require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?
>
189 <?php
// can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
193 function dosort(orderby
) {
194 var f
= document
.forms
[0];
195 f
.form_orderby
.value
= orderby
;
200 function refreshme() {
201 document
.forms
[0].submit();
204 // Called to switch to the specified encounter having the specified DOS.
205 function toEncounter(newpid
, enc
) {
206 top
.restoreSession();
207 top
.RTop
.location
= "<?php echo $GLOBALS['webroot']; ?>/interface/patient_file/summary/demographics.php?set_pid=" +
encodeURIComponent(newpid
) +
"&set_encounterid=" +
encodeURIComponent(enc
);
212 <body
class="body_top">
213 <!-- Required
for the popup date selectors
-->
214 <div id
="overDiv" style
="position:absolute; visibility:hidden; z-index:1000;"></div
>
216 <span
class='title'><?php
echo xlt('Report'); ?
> - <?php
echo xlt('Encounters'); ?
></span
>
218 <div id
="report_parameters_daterange">
219 <?php
echo text(oeFormatShortDate($form_from_date)) . " " . xlt('to{{Range}}') . " " . text(oeFormatShortDate($form_to_date)); ?
>
222 <form method
='post' name
='theform' id
='theform' action
='encounters_report.php' onsubmit
='return top.restoreSession()'>
223 <input type
="hidden" name
="csrf_token_form" value
="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
225 <div id
="report_parameters">
229 <div style
='float:left'>
233 <td
class='col-form-label'>
234 <?php
echo xlt('Facility'); ?
>:
237 <?php
dropdown_facility($form_facility, 'form_facility', true); ?
>
239 <td
class='col-form-label'>
240 <?php
echo xlt('Provider'); ?
>:
245 // Build a drop-down list of providers.
248 $query = "SELECT id, lname, fname FROM users WHERE " .
249 "authorized = 1 ORDER BY lname, fname"; //(CHEMED) facility filter
251 $ures = sqlStatement($query);
253 echo " <select name='form_provider' class='form-control'>\n";
254 echo " <option value=''>-- " . xlt('All') . " --\n";
256 while ($urow = sqlFetchArray($ures)) {
257 $provid = $urow['id'];
258 echo " <option value='" . attr($provid) . "'";
259 if (!empty($_POST['form_provider']) && ($provid == $_POST['form_provider'])) {
263 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
272 <td
class='col-form-label'>
273 <?php
echo xlt('From'); ?
>:
276 <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)); ?>'>
278 <td
class='col-form-label'>
279 <?php
echo xlt('To{{Range}}'); ?
>:
282 <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)); ?>'>
288 <div
class="checkbox">
289 <label
><input type
='checkbox' name
='form_details'<?php
echo ($form_details) ?
' checked' : ''; ?
>>
290 <?php
echo xlt('Details'); ?
></label
>
292 <div
class="checkbox">
293 <label
><input type
='checkbox' name
='form_new_patients' title
='<?php echo xla('First
-time visits only
'); ?>'<?php
echo ($form_new_patients) ?
' checked' : ''; ?
>>
294 <?php
echo xlt('New'); ?
></label
>
299 <div
class="checkbox">
300 <label
><input type
='checkbox' name
='form_esigned'<?php
echo ($form_esigned) ?
' checked' : ''; ?
>>
301 <?php
echo xlt('Forms Esigned'); ?
></label
>
303 <div
class="checkbox">
304 <label
><input type
='checkbox' name
='form_encounter_esigned'<?php
echo ($form_encounter_esigned) ?
' checked' : ''; ?
>>
305 <?php
echo xlt('Encounter Esigned'); ?
></label
>
307 <div
class="checkbox">
308 <label
><input type
='checkbox' name
='form_not_esigned'<?php
echo ($form_not_esigned) ?
' checked' : ''; ?
>>
309 <?php
echo xlt('Not Esigned'); ?
></label
>
318 <td
class='h-100' align
='left' valign
='middle'>
319 <table
class='w-100 h-100' style
='border-left:1px solid;'>
322 <div
class="text-center">
323 <div
class="btn-group" role
="group">
324 <a href
='#' class='btn btn-secondary btn-save' onclick
='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
325 <?php
echo xlt('Submit'); ?
>
327 <?php
if (!empty($_POST['form_refresh']) ||
!empty($_POST['form_orderby'])) { ?
>
328 <a href
='#' class='btn btn-secondary btn-print' id
='printbutton'>
329 <?php
echo xlt('Print'); ?
>
341 </div
> <!-- end report_parameters
-->
344 if (!empty($_POST['form_refresh']) ||
!empty($_POST['form_orderby'])) {
346 <div id
="report_results">
347 <table
class='table' id
='mymaintable'>
348 <thead
class='thead-light'>
349 <?php
if ($form_details) { ?
>
351 <a href
="nojs.php" onclick
="return dosort('doctor')"
352 <?php
echo ($form_orderby == "doctor") ?
" style=\"color: var(--success)\"" : ""; ?
>><?php
echo xlt('Provider'); ?
> </a
>
355 <a href
="nojs.php" onclick
="return dosort('time')"
356 <?php
echo ($form_orderby == "time") ?
" style=\"color: var(--success)\"" : ""; ?
>><?php
echo xlt('Date'); ?
></a
>
359 <a href
="nojs.php" onclick
="return dosort('patient')"
360 <?php
echo ($form_orderby == "patient") ?
" style=\"color: var(--success)\"" : ""; ?
>><?php
echo xlt('Patient'); ?
></a
>
363 <a href
="nojs.php" onclick
="return dosort('pubpid')"
364 <?php
echo ($form_orderby == "pubpid") ?
" style=\"color: var(--success)\"" : ""; ?
>><?php
echo xlt('ID'); ?
></a
>
367 <?php
echo xlt('Status'); ?
>
370 <?php
echo xlt('Encounter'); ?
>
373 <a href
="nojs.php" onclick
="return dosort('encounter')"
374 <?php
echo ($form_orderby == "encounter") ?
" style=\"color: var(--success)\"" : ""; ?
>><?php
echo xlt('Encounter Number'); ?
></a
>
377 <?php
echo xlt('Form'); ?
>
380 <?php
echo xlt('Coding'); ?
>
383 <th
><?php
echo xlt('Provider'); ?
></td
>
384 <th
><?php
echo xlt('Encounters'); ?
></td
>
392 while ($row = sqlFetchArray($res)) {
393 $patient_id = $row['pid'];
396 if (!empty($row['ulname']) ||
!empty($row['ufname'])) {
397 $docname = $row['ulname'];
398 if (!empty($row['ufname']) ||
!empty($row['umname'])) {
399 $docname .= ', ' . $row['ufname'] . ' ' . $row['umname'];
405 // Fetch all other forms for this encounter.
407 $encarr = getFormByEncounter(
410 "formdir, user, form_name, form_id"
413 foreach ($encarr as $enc) {
414 if ($enc['formdir'] == 'newpatient') {
419 $encnames .= '<br />';
422 $encnames .= text($enc['form_name']); // need to html escape it here for output below
426 // Fetch coding and compute billing status.
431 $billres = BillingUtilities
::getBillingByEncounter(
434 "code_type, code, code_text, billed"
437 foreach ($billres as $billrow) {
438 // $title = addslashes($billrow['code_text']);
439 if ($billrow['code_type'] != 'COPAY' && $billrow['code_type'] != 'TAX') {
440 $coded .= $billrow['code'] . ', ';
441 if ($billrow['billed']) {
449 $coded = substr($coded, 0, strlen($coded) - 2);
452 // Figure product sales into billing status.
453 $sres = sqlStatement("SELECT billed FROM drug_sales " .
454 "WHERE pid = ? AND encounter = ?", array($row['pid'], $row['encounter']));
455 while ($srow = sqlFetchArray($sres)) {
456 if ($srow['billed']) {
463 // Compute billing status.
464 if ($billed_count && $unbilled_count) {
465 $status = xl('Mixed');
466 } elseif ($billed_count) {
467 $status = xl('Closed');
468 } elseif ($unbilled_count) {
469 $status = xl('Open');
471 $status = xl('Empty');
474 <tr bgcolor
='<?php echo attr($bgcolor ?? ''); ?>'>
476 <?php
echo ($docname == $lastdocname) ?
"" : text($docname) ?
> 
;
479 <?php
echo text(oeFormatShortDate(substr($row['date'], 0, 10))) ?
> 
;
482 <?php
echo text($row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname']); ?
> 
;
485 <?php
echo text($row['pubpid']); ?
> 
;
488 <?php
echo text($status); ?
> 
;
491 <?php
echo text($row['reason']); ?
> 
;
494 <?php
echo "<input type='button' class='btn btn-sm btn-secondary' value='" .
495 attr($row['encounter']) . "-" . attr($row['pid']) .
496 "' onClick='toEncounter(" . attr_js($row['pid']) . ", " . attr_js($row['encounter']) .
500 <?php
echo $encnames; //since this variable contains html, have already html escaped it above ?>
503 <?php
echo text($coded); ?
>
508 if ($docname != $lastdocname) {
509 show_doc_total($lastdocname, $doc_encounters);
516 $lastdocname = $docname;
519 if (!$form_details) {
520 show_doc_total($lastdocname, $doc_encounters);
526 </div
> <!-- end encresults
-->
529 <?php
echo xlt('Please input search criteria above, and click Submit to view results.'); ?
>
533 <input type
="hidden" name
="form_orderby" value
="<?php echo attr($form_orderby) ?>" />
534 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
540 <?php
if ($alertmsg) {
541 echo " alert(" . js_escape($alertmsg) . ");\n";