3 * This report cross-references appointments with encounters.
4 * For a given date, show a line for each appointment with the
5 * matching encounter, and also for each encounter that has no
6 * matching appointment. This helps to catch these errors:
8 * * Appointments with no encounter
9 * * Encounters with no appointment
10 * * Codes not justified
11 * * Codes not authorized
12 * * Procedure codes without a fee
13 * * Fees assigned to diagnoses (instead of procedures)
14 * * Encounters not billed
16 * For decent performance the following indexes are highly recommended:
17 * openemr_postcalendar_events.pc_eventDate
19 * billing.pid_encounter
22 * Copyright (C) 2005-2016 Rod Roark <rod@sunsetsystems.com>
23 * Copyright (C) 2017 Brady Miller <brady.g.miller@gmail.com>
25 * LICENSE: This program is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU General Public License
27 * as published by the Free Software Foundation; either version 3
28 * of the License, or (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
37 * @author Rod Roark <rod@sunsetsystems.com>
38 * @author Brady Miller <brady.g.miller@gmail.com>
39 * @link http://www.open-emr.org
43 use OpenEMR\Core\Header
;
45 require_once("../globals.php");
46 require_once("$srcdir/patient.inc");
47 require_once("../../custom/code_types.inc.php");
48 require_once("$srcdir/billing.inc");
50 use OpenEMR\Services\FacilityService
;
52 $facilityService = new FacilityService();
55 $alertmsg = ''; // not used yet but maybe later
56 $grand_total_charges = 0;
57 $grand_total_copays = 0;
58 $grand_total_encounters = 0;
60 function postError($msg)
70 function bucks($amount)
73 echo oeFormatMoney($amount);
77 function endDoctor(&$docrow)
79 global $grand_total_charges, $grand_total_copays, $grand_total_encounters;
80 if (!$docrow['docname']) {
84 echo " <tr class='report_totals'>\n";
85 echo " <td colspan='5'>\n";
86 echo " " . xl('Totals for', '', '', ' ') . $docrow['docname'] . "\n";
88 echo " <td align='right'>\n";
89 echo " " . $docrow['encounters'] . " \n";
91 echo " <td align='right'>\n";
93 bucks($docrow['charges']);
96 echo " <td align='right'>\n";
98 bucks($docrow['copays']);
101 echo " <td colspan='2'>\n";
106 $grand_total_charges +
= $docrow['charges'];
107 $grand_total_copays +
= $docrow['copays'];
108 $grand_total_encounters +
= $docrow['encounters'];
110 $docrow['charges'] = 0;
111 $docrow['copays'] = 0;
112 $docrow['encounters'] = 0;
115 $form_facility = isset($_POST['form_facility']) ?
$_POST['form_facility'] : '';
116 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
117 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
118 if ($_POST['form_refresh']) {
119 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
120 $form_to_date = fixDate($_POST['form_to_date'], "");
122 // MySQL doesn't grok full outer joins so we do it the hard way.
126 "e.pc_eventDate, e.pc_startTime, " .
127 "fe.encounter, fe.date AS encdate, " .
129 "p.fname, p.lname, p.pid, p.pubpid, " .
130 "CONCAT( u.lname, ', ', u.fname ) AS docname " .
131 "FROM openemr_postcalendar_events AS e " .
132 "LEFT OUTER JOIN form_encounter AS fe " .
133 "ON fe.date = e.pc_eventDate AND fe.pid = e.pc_pid " .
134 "LEFT OUTER JOIN forms AS f ON f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
135 "LEFT OUTER JOIN patient_data AS p ON p.pid = e.pc_pid " .
136 // "LEFT OUTER JOIN users AS u ON BINARY u.username = BINARY f.user WHERE ";
137 "LEFT OUTER JOIN users AS u ON u.id = fe.provider_id WHERE ";
139 $query .= "e.pc_eventDate >= '$form_from_date' AND e.pc_eventDate <= '$form_to_date' ";
141 $query .= "e.pc_eventDate = '$form_from_date' ";
144 if ($form_facility !== '') {
145 $query .= "AND e.pc_facility = '" . add_escape_custom($form_facility) . "' ";
148 // $query .= "AND ( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
149 $query .= "AND e.pc_pid != '' AND e.pc_apptstatus != '?' " .
152 "e.pc_eventDate, e.pc_startTime, " .
153 "fe.encounter, fe.date AS encdate, " .
155 "p.fname, p.lname, p.pid, p.pubpid, " .
156 "CONCAT( u.lname, ', ', u.fname ) AS docname " .
157 "FROM form_encounter AS fe " .
158 "LEFT OUTER JOIN openemr_postcalendar_events AS e " .
159 "ON fe.date = e.pc_eventDate AND fe.pid = e.pc_pid AND " .
160 // "( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
161 "e.pc_pid != '' AND e.pc_apptstatus != '?' " .
162 "LEFT OUTER JOIN forms AS f ON f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
163 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
164 // "LEFT OUTER JOIN users AS u ON BINARY u.username = BINARY f.user WHERE ";
165 "LEFT OUTER JOIN users AS u ON u.id = fe.provider_id WHERE ";
167 // $query .= "LEFT(fe.date, 10) >= '$form_from_date' AND LEFT(fe.date, 10) <= '$form_to_date' ";
168 $query .= "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' ";
170 // $query .= "LEFT(fe.date, 10) = '$form_from_date' ";
171 $query .= "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_from_date 23:59:59' ";
174 if ($form_facility !== '') {
175 $query .= "AND fe.facility_id = '" . add_escape_custom($form_facility) . "' ";
178 $query .= ") ORDER BY docname, IFNULL(pc_eventDate, encdate), pc_startTime";
180 $res = sqlStatement($query);
186 <?php Header
::setupHeader(['datetime-picker', 'report-helper']); ?
>
188 <style type
="text/css">
190 /* specifically include & exclude from printing */
196 #report_parameters_daterange {
200 #report_results table {
205 /* specifically exclude some from the screen */
207 #report_parameters_daterange {
214 <title
><?php
xl('Appointments and Encounters', 'e'); ?
></title
>
216 <script LANGUAGE
="JavaScript">
218 $
(document
).ready(function() {
219 oeFixedHeaderSetup(document
.getElementById('mymaintable'));
220 var win
= top
.printLogSetup ? top
: opener
.top
;
221 win
.printLogSetup(document
.getElementById('printbutton'));
223 $
('.datepicker').datetimepicker({
224 <?php
$datetimepicker_timepicker = false; ?
>
225 <?php
$datetimepicker_showseconds = false; ?
>
226 <?php
$datetimepicker_formatInput = false; ?
>
227 <?php
require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?
>
228 <?php
// can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
236 <body
class="body_top">
238 <span
class='title'><?php
xl('Report', 'e'); ?
> - <?php
xl('Appointments and Encounters', 'e'); ?
></span
>
240 <div id
="report_parameters_daterange">
241 <?php
echo date("d F Y", strtotime($form_from_date)) ." to ". date("d F Y", strtotime($form_to_date)); ?
>
244 <form method
='post' id
='theform' action
='appt_encounter_report.php' onsubmit
='return top.restoreSession()'>
246 <div id
="report_parameters">
251 <div style
='float:left'>
255 <td
class='control-label'>
256 <?php
xl('Facility', 'e'); ?
>:
260 // Build a drop-down list of facilities.
262 $fres = $facilityService->getAll();
263 echo " <select name='form_facility' class='form-control'>\n";
264 echo " <option value=''>-- " . xl('All Facilities') . " --\n";
265 foreach ($fres as $frow) {
266 $facid = $frow['id'];
267 echo " <option value='$facid'";
268 if ($facid == $form_facility) {
272 echo ">" . htmlspecialchars($frow['name']) . "\n";
275 echo " <option value='0'";
276 if ($form_facility === '0') {
280 echo ">-- " . xl('Unspecified') . " --\n";
284 <td
class='control-label'>
285 <?php
xl('DOS', 'e'); ?
>:
288 <input type
='text' class='datepicker form-control' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo $form_from_date; ?>' >
290 <td
class='control-label'>
291 <?php
xl('To', 'e'); ?
>:
294 <input type
='text' class='datepicker form-control' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo $form_to_date; ?>' >
300 <div
class="checkbox">
301 <label
><input type
='checkbox' name
='form_details'
302 value
='1'<?php
if ($_POST['form_details']) {
304 } ?
>><?php
xl('Details', 'e') ?
></label
>
313 <td align
='left' valign
='middle' height
="100%">
314 <table style
='border-left:1px solid; width:100%; height:100%' >
317 <div
class="text-center">
318 <div
class="btn-group" role
="group">
319 <a href
='#' class='btn btn-default btn-save' onclick
='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
320 <?php
echo xlt('Submit'); ?
>
322 <?php
if ($_POST['form_refresh']) { ?
>
323 <a href
='#' class='btn btn-default btn-print' id
='printbutton'>
324 <?php
echo xlt('Print'); ?
>
336 </div
> <!-- end apptenc_report_parameters
-->
339 if ($_POST['form_refresh']) {
341 <div id
="report_results">
342 <table id
='mymaintable'>
345 <th
>  
;<?php
xl('Practitioner', 'e'); ?
> </th
>
346 <th
>  
;<?php
xl('Date/Appt', 'e'); ?
> </th
>
347 <th
>  
;<?php
xl('Patient', 'e'); ?
> </th
>
348 <th
>  
;<?php
xl('ID', 'e'); ?
> </th
>
349 <th align
='right'> <?php
xl('Chart', 'e'); ?
> 
; </th
>
350 <th align
='right'> <?php
xl('Encounter', 'e'); ?
> 
; </th
>
351 <th align
='right'> <?php
xl('Charges', 'e'); ?
> 
; </th
>
352 <th align
='right'> <?php
xl('Copays', 'e'); ?
> 
; </th
>
353 <th
> <?php
xl('Billed', 'e'); ?
> </th
>
354 <th
>  
;<?php
xl('Error', 'e'); ?
> </th
>
359 $docrow = array('docname' => '', 'charges' => 0, 'copays' => 0, 'encounters' => 0);
361 while ($row = sqlFetchArray($res)) {
362 $patient_id = $row['pid'];
363 $encounter = $row['encounter'];
364 $docname = $row['docname'] ?
$row['docname'] : xl('Unknown');
366 if ($docname != $docrow['docname']) {
374 $gcac_related_visit = false;
376 // Scan the billing items for status and fee total.
378 $query = "SELECT code_type, code, modifier, authorized, billed, fee, justify " .
379 "FROM billing WHERE " .
380 "pid = '$patient_id' AND encounter = '$encounter' AND activity = 1";
381 $bres = sqlStatement($query);
383 while ($brow = sqlFetchArray($bres)) {
384 $code_type = $brow['code_type'];
385 if ($code_types[$code_type]['fee'] && !$brow['billed']) {
389 if (!$GLOBALS['simplified_demographics'] && !$brow['authorized']) {
390 postError(xl('Needs Auth'));
393 if ($code_types[$code_type]['just']) {
394 if (! $brow['justify']) {
395 postError(xl('Needs Justify'));
399 if ($code_types[$code_type]['fee']) {
400 $charges +
= $brow['fee'];
401 if ($brow['fee'] == 0 && !$GLOBALS['ippf_specific']) {
402 postError(xl('Missing Fee'));
405 if ($brow['fee'] != 0) {
406 postError(xl('Fee is not allowed'));
410 // Custom logic for IPPF to determine if a GCAC issue applies.
411 if ($GLOBALS['ippf_specific']) {
412 if (!empty($code_types[$code_type]['fee'])) {
413 $query = "SELECT related_code FROM codes WHERE code_type = '" .
414 $code_types[$code_type]['id'] . "' AND " .
415 "code = '" . $brow['code'] . "' AND ";
416 if ($brow['modifier']) {
417 $query .= "modifier = '" . $brow['modifier'] . "'";
419 $query .= "(modifier IS NULL OR modifier = '')";
422 $query .= " LIMIT 1";
423 $tmp = sqlQuery($query);
424 $relcodes = explode(';', $tmp['related_code']);
425 foreach ($relcodes as $codestring) {
426 if ($codestring === '') {
430 list($codetype, $code) = explode(':', $codestring);
431 if ($codetype !== 'IPPF') {
435 if (preg_match('/^25222/', $code)) {
436 $gcac_related_visit = true;
443 $copays -= getPatientCopay($patient_id, $encounter);
445 // The following is removed, perhaps temporarily, because gcac reporting
446 // no longer depends on gcac issues. -- Rod 2009-08-11
447 /******************************************************************
448 // More custom code for IPPF. Generates an error message if a
449 // GCAC issue is required but is not linked to this visit.
450 if (!$errmsg && $gcac_related_visit) {
451 $grow = sqlQuery("SELECT l.id, l.title, l.begdate, ie.pid " .
453 "LEFT JOIN issue_encounter AS ie ON ie.pid = l.pid AND " .
454 "ie.encounter = '$encounter' AND ie.list_id = l.id " .
455 "WHERE l.pid = '$patient_id' AND " .
456 "l.activity = 1 AND l.type = 'ippf_gcac' " .
457 "ORDER BY ie.pid DESC, l.begdate DESC LIMIT 1");
458 // Note that reverse-ordering by ie.pid is a trick for sorting
459 // issues linked to the encounter (non-null values) first.
460 if (empty($grow['pid'])) { // if there is no linked GCAC issue
461 if (empty($grow)) { // no GCAC issue exists
462 $errmsg = "GCAC issue does not exist";
464 else { // there is one but none is linked
465 $errmsg = "GCAC issue is not linked";
469 ******************************************************************/
470 if ($gcac_related_visit) {
471 $grow = sqlQuery("SELECT COUNT(*) AS count FROM forms " .
472 "WHERE pid = '$patient_id' AND encounter = '$encounter' AND " .
473 "deleted = 0 AND formdir = 'LBFgcac'");
474 if (empty($grow['count'])) { // if there is no gcac form
475 postError(xl('GCAC visit form is missing'));
478 /*****************************************************************/
481 postError($GLOBALS['simplified_demographics'] ?
482 xl('Not checked out') : xl('Not billed'));
486 postError(xl('No visit'));
493 $docrow['charges'] +
= $charges;
494 $docrow['copays'] +
= $copays;
496 ++
$docrow['encounters'];
499 if ($_POST['form_details']) {
503  
;<?php
echo ($docname == $docrow['docname']) ?
"" : $docname ?
>
507 /*****************************************************************
509 echo $row['pc_eventDate'] . '<br>';
510 echo substr($row['pc_startTime'], 0, 5);
512 *****************************************************************/
513 if (empty($row['pc_eventDate'])) {
514 echo oeFormatShortDate(substr($row['encdate'], 0, 10));
516 echo oeFormatShortDate($row['pc_eventDate']) . ' ' . substr($row['pc_startTime'], 0, 5);
521  
;<?php
echo $row['fname'] . " " . $row['lname'] ?
>
524  
;<?php
echo $row['pubpid'] ?
>
527 <?php
echo $row['pid'] ?
> 
;
530 <?php
echo $encounter ?
> 
;
533 <?php
bucks($charges) ?
> 
;
536 <?php
bucks($copays) ?
> 
;
539 <?php
echo $billed ?
>
541 <td style
='color:#cc0000'>
542 <?php
echo $errmsg; ?
> 
;
546 } // end of details line
548 $docrow['docname'] = $docname;
553 echo " <tr class='report_totals'>\n";
554 echo " <td colspan='5'>\n";
555 echo " " . xl('Grand Totals') . "\n";
557 echo " <td align='right'>\n";
558 echo " " . $grand_total_encounters . " \n";
560 echo " <td align='right'>\n";
562 bucks($grand_total_charges);
565 echo " <td align='right'>\n";
567 bucks($grand_total_copays);
570 echo " <td colspan='2'>\n";
578 </div
> <!-- end the apptenc_report_results
-->
581 <?php
echo xl('Please input search criteria above, and click Submit to view results.', 'e'); ?
>
585 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
589 <?php
if ($alertmsg) {
590 echo " alert('$alertmsg');\n";