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
44 require_once("../globals.php");
45 require_once("$srcdir/patient.inc");
46 require_once("../../custom/code_types.inc.php");
47 require_once("$srcdir/billing.inc");
50 $alertmsg = ''; // not used yet but maybe later
51 $grand_total_charges = 0;
52 $grand_total_copays = 0;
53 $grand_total_encounters = 0;
55 function postError($msg) {
57 if ($errmsg) $errmsg .= '<br />';
61 function bucks($amount) {
62 if ($amount) echo oeFormatMoney($amount);
65 function endDoctor(&$docrow) {
66 global $grand_total_charges, $grand_total_copays, $grand_total_encounters;
67 if (!$docrow['docname']) return;
69 echo " <tr class='report_totals'>\n";
70 echo " <td colspan='5'>\n";
71 echo " " . xl('Totals for','','',' ') . $docrow['docname'] . "\n";
73 echo " <td align='right'>\n";
74 echo " " . $docrow['encounters'] . " \n";
76 echo " <td align='right'>\n";
77 echo " "; bucks($docrow['charges']); echo " \n";
79 echo " <td align='right'>\n";
80 echo " "; bucks($docrow['copays']); echo " \n";
82 echo " <td colspan='2'>\n";
87 $grand_total_charges +
= $docrow['charges'];
88 $grand_total_copays +
= $docrow['copays'];
89 $grand_total_encounters +
= $docrow['encounters'];
91 $docrow['charges'] = 0;
92 $docrow['copays'] = 0;
93 $docrow['encounters'] = 0;
96 $form_facility = isset($_POST['form_facility']) ?
$_POST['form_facility'] : '';
97 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
98 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
99 if ($_POST['form_refresh']) {
100 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
101 $form_to_date = fixDate($_POST['form_to_date'], "");
103 // MySQL doesn't grok full outer joins so we do it the hard way.
107 "e.pc_eventDate, e.pc_startTime, " .
108 "fe.encounter, fe.date AS encdate, " .
110 "p.fname, p.lname, p.pid, p.pubpid, " .
111 "CONCAT( u.lname, ', ', u.fname ) AS docname " .
112 "FROM openemr_postcalendar_events AS e " .
113 "LEFT OUTER JOIN form_encounter AS fe " .
114 "ON fe.date = e.pc_eventDate AND fe.pid = e.pc_pid " .
115 "LEFT OUTER JOIN forms AS f ON f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
116 "LEFT OUTER JOIN patient_data AS p ON p.pid = e.pc_pid " .
117 // "LEFT OUTER JOIN users AS u ON BINARY u.username = BINARY f.user WHERE ";
118 "LEFT OUTER JOIN users AS u ON u.id = fe.provider_id WHERE ";
120 $query .= "e.pc_eventDate >= '$form_from_date' AND e.pc_eventDate <= '$form_to_date' ";
122 $query .= "e.pc_eventDate = '$form_from_date' ";
124 if ($form_facility !== '') {
125 $query .= "AND e.pc_facility = '" . add_escape_custom($form_facility) . "' ";
127 // $query .= "AND ( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
128 $query .= "AND e.pc_pid != '' AND e.pc_apptstatus != '?' " .
131 "e.pc_eventDate, e.pc_startTime, " .
132 "fe.encounter, fe.date AS encdate, " .
134 "p.fname, p.lname, p.pid, p.pubpid, " .
135 "CONCAT( u.lname, ', ', u.fname ) AS docname " .
136 "FROM form_encounter AS fe " .
137 "LEFT OUTER JOIN openemr_postcalendar_events AS e " .
138 "ON fe.date = e.pc_eventDate AND fe.pid = e.pc_pid AND " .
139 // "( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
140 "e.pc_pid != '' AND e.pc_apptstatus != '?' " .
141 "LEFT OUTER JOIN forms AS f ON f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
142 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
143 // "LEFT OUTER JOIN users AS u ON BINARY u.username = BINARY f.user WHERE ";
144 "LEFT OUTER JOIN users AS u ON u.id = fe.provider_id WHERE ";
146 // $query .= "LEFT(fe.date, 10) >= '$form_from_date' AND LEFT(fe.date, 10) <= '$form_to_date' ";
147 $query .= "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' ";
149 // $query .= "LEFT(fe.date, 10) = '$form_from_date' ";
150 $query .= "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_from_date 23:59:59' ";
152 if ($form_facility !== '') {
153 $query .= "AND fe.facility_id = '" . add_escape_custom($form_facility) . "' ";
155 $query .= ") ORDER BY docname, IFNULL(pc_eventDate, encdate), pc_startTime";
157 $res = sqlStatement($query);
162 <?php
html_header_show();?
>
163 <link rel
="stylesheet" href
="<?php echo $css_header;?>" type
="text/css">
164 <link rel
="stylesheet" href
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">
166 <style type
="text/css">
168 /* specifically include & exclude from printing */
174 #report_parameters_daterange {
178 #report_results table {
183 /* specifically exclude some from the screen */
185 #report_parameters_daterange {
192 <title
><?php
xl('Appointments and Encounters','e'); ?
></title
>
194 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-3-1-1/index.js"></script
>
195 <script type
="text/javascript" src
="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></script
>
196 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script
>
198 <script LANGUAGE
="JavaScript">
200 $
(document
).ready(function() {
201 oeFixedHeaderSetup(document
.getElementById('mymaintable'));
202 var win
= top
.printLogSetup ? top
: opener
.top
;
203 win
.printLogSetup(document
.getElementById('printbutton'));
205 $
('.datepicker').datetimepicker({
206 <?php
$datetimepicker_timepicker = false; ?
>
207 <?php
$datetimepicker_formatInput = false; ?
>
208 <?php
require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?
>
209 <?php
// can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
217 <body
class="body_top">
219 <span
class='title'><?php
xl('Report','e'); ?
> - <?php
xl('Appointments and Encounters','e'); ?
></span
>
221 <div id
="report_parameters_daterange">
222 <?php
echo date("d F Y", strtotime($form_from_date)) ." to ". date("d F Y", strtotime($form_to_date)); ?
>
225 <form method
='post' id
='theform' action
='appt_encounter_report.php'>
227 <div id
="report_parameters">
232 <div style
='float:left'>
237 <?php
xl('Facility','e'); ?
>:
241 // Build a drop-down list of facilities.
243 $query = "SELECT id, name FROM facility ORDER BY name";
244 $fres = sqlStatement($query);
245 echo " <select name='form_facility'>\n";
246 echo " <option value=''>-- " . xl('All Facilities', 'e') . " --\n";
247 while ($frow = sqlFetchArray($fres)) {
248 $facid = $frow['id'];
249 echo " <option value='$facid'";
250 if ($facid == $form_facility) echo " selected";
251 echo ">" . htmlspecialchars($frow['name']) . "\n";
253 echo " <option value='0'";
254 if ($form_facility === '0') echo " selected";
255 echo ">-- " . xl('Unspecified') . " --\n";
260 <?php
xl('DOS','e'); ?
>:
263 <input type
='text' class='datepicker' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo $form_from_date; ?>'
264 title
='Date of appointments mm/dd/yyyy' >
267 <?php
xl('To','e'); ?
>:
270 <input type
='text' class='datepicker' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo $form_to_date; ?>'
271 title
='Optional end date mm/dd/yyyy' >
277 <input type
='checkbox' name
='form_details'
278 value
='1'<?php
if ($_POST['form_details']) echo " checked"; ?
>><?php
xl('Details','e') ?
>
286 <td align
='left' valign
='middle' height
="100%">
287 <table style
='border-left:1px solid; width:100%; height:100%' >
290 <div style
='margin-left:15px'>
291 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
293 <?php
xl('Submit','e'); ?
>
297 <?php
if ($_POST['form_refresh']) { ?
>
298 <a href
='#' class='css_button' id
='printbutton'>
300 <?php
xl('Print','e'); ?
>
312 </div
> <!-- end apptenc_report_parameters
-->
315 if ($_POST['form_refresh'] ) {
317 <div id
="report_results">
318 <table id
='mymaintable'>
321 <th
>  
;<?php
xl('Practitioner','e'); ?
> </th
>
322 <th
>  
;<?php
xl('Date/Appt','e'); ?
> </th
>
323 <th
>  
;<?php
xl('Patient','e'); ?
> </th
>
324 <th
>  
;<?php
xl('ID','e'); ?
> </th
>
325 <th align
='right'> <?php
xl('Chart','e'); ?
> 
; </th
>
326 <th align
='right'> <?php
xl('Encounter','e'); ?
> 
; </th
>
327 <th align
='right'> <?php
xl('Charges','e'); ?
> 
; </th
>
328 <th align
='right'> <?php
xl('Copays','e'); ?
> 
; </th
>
329 <th
> <?php
xl('Billed','e'); ?
> </th
>
330 <th
>  
;<?php
xl('Error','e'); ?
> </th
>
335 $docrow = array('docname' => '', 'charges' => 0, 'copays' => 0, 'encounters' => 0);
337 while ($row = sqlFetchArray($res)) {
338 $patient_id = $row['pid'];
339 $encounter = $row['encounter'];
340 $docname = $row['docname'] ?
$row['docname'] : xl('Unknown');
342 if ($docname != $docrow['docname']) {
350 $gcac_related_visit = false;
352 // Scan the billing items for status and fee total.
354 $query = "SELECT code_type, code, modifier, authorized, billed, fee, justify " .
355 "FROM billing WHERE " .
356 "pid = '$patient_id' AND encounter = '$encounter' AND activity = 1";
357 $bres = sqlStatement($query);
359 while ($brow = sqlFetchArray($bres)) {
360 $code_type = $brow['code_type'];
361 if ($code_types[$code_type]['fee'] && !$brow['billed'])
363 if (!$GLOBALS['simplified_demographics'] && !$brow['authorized'])
364 postError(xl('Needs Auth'));
365 if ($code_types[$code_type]['just']) {
366 if (! $brow['justify']) postError(xl('Needs Justify'));
368 if ($code_types[$code_type]['fee']) {
369 $charges +
= $brow['fee'];
370 if ($brow['fee'] == 0 && !$GLOBALS['ippf_specific']) postError(xl('Missing Fee'));
372 if ($brow['fee'] != 0) postError(xl('Fee is not allowed'));
375 // Custom logic for IPPF to determine if a GCAC issue applies.
376 if ($GLOBALS['ippf_specific']) {
377 if (!empty($code_types[$code_type]['fee'])) {
378 $query = "SELECT related_code FROM codes WHERE code_type = '" .
379 $code_types[$code_type]['id'] . "' AND " .
380 "code = '" . $brow['code'] . "' AND ";
381 if ($brow['modifier']) {
382 $query .= "modifier = '" . $brow['modifier'] . "'";
384 $query .= "(modifier IS NULL OR modifier = '')";
386 $query .= " LIMIT 1";
387 $tmp = sqlQuery($query);
388 $relcodes = explode(';', $tmp['related_code']);
389 foreach ($relcodes as $codestring) {
390 if ($codestring === '') continue;
391 list($codetype, $code) = explode(':', $codestring);
392 if ($codetype !== 'IPPF') continue;
393 if (preg_match('/^25222/', $code)) $gcac_related_visit = true;
400 $copays -= getPatientCopay($patient_id,$encounter);
402 // The following is removed, perhaps temporarily, because gcac reporting
403 // no longer depends on gcac issues. -- Rod 2009-08-11
404 /******************************************************************
405 // More custom code for IPPF. Generates an error message if a
406 // GCAC issue is required but is not linked to this visit.
407 if (!$errmsg && $gcac_related_visit) {
408 $grow = sqlQuery("SELECT l.id, l.title, l.begdate, ie.pid " .
410 "LEFT JOIN issue_encounter AS ie ON ie.pid = l.pid AND " .
411 "ie.encounter = '$encounter' AND ie.list_id = l.id " .
412 "WHERE l.pid = '$patient_id' AND " .
413 "l.activity = 1 AND l.type = 'ippf_gcac' " .
414 "ORDER BY ie.pid DESC, l.begdate DESC LIMIT 1");
415 // Note that reverse-ordering by ie.pid is a trick for sorting
416 // issues linked to the encounter (non-null values) first.
417 if (empty($grow['pid'])) { // if there is no linked GCAC issue
418 if (empty($grow)) { // no GCAC issue exists
419 $errmsg = "GCAC issue does not exist";
421 else { // there is one but none is linked
422 $errmsg = "GCAC issue is not linked";
426 ******************************************************************/
427 if ($gcac_related_visit) {
428 $grow = sqlQuery("SELECT COUNT(*) AS count FROM forms " .
429 "WHERE pid = '$patient_id' AND encounter = '$encounter' AND " .
430 "deleted = 0 AND formdir = 'LBFgcac'");
431 if (empty($grow['count'])) { // if there is no gcac form
432 postError(xl('GCAC visit form is missing'));
435 /*****************************************************************/
437 if (!$billed) postError($GLOBALS['simplified_demographics'] ?
438 xl('Not checked out') : xl('Not billed'));
439 if (!$encounter) postError(xl('No visit'));
441 if (! $charges) $billed = "";
443 $docrow['charges'] +
= $charges;
444 $docrow['copays'] +
= $copays;
445 if ($encounter) ++
$docrow['encounters'];
447 if ($_POST['form_details']) {
451  
;<?php
echo ($docname == $docrow['docname']) ?
"" : $docname ?
>
455 /*****************************************************************
457 echo $row['pc_eventDate'] . '<br>';
458 echo substr($row['pc_startTime'], 0, 5);
460 *****************************************************************/
461 if (empty($row['pc_eventDate'])) {
462 echo oeFormatShortDate(substr($row['encdate'], 0, 10));
465 echo oeFormatShortDate($row['pc_eventDate']) . ' ' . substr($row['pc_startTime'], 0, 5);
470  
;<?php
echo $row['fname'] . " " . $row['lname'] ?
>
473  
;<?php
echo $row['pubpid'] ?
>
476 <?php
echo $row['pid'] ?
> 
;
479 <?php
echo $encounter ?
> 
;
482 <?php
bucks($charges) ?
> 
;
485 <?php
bucks($copays) ?
> 
;
488 <?php
echo $billed ?
>
490 <td style
='color:#cc0000'>
491 <?php
echo $errmsg; ?
> 
;
495 } // end of details line
497 $docrow['docname'] = $docname;
502 echo " <tr class='report_totals'>\n";
503 echo " <td colspan='5'>\n";
504 echo " " . xl('Grand Totals') . "\n";
506 echo " <td align='right'>\n";
507 echo " " . $grand_total_encounters . " \n";
509 echo " <td align='right'>\n";
510 echo " "; bucks($grand_total_charges); echo " \n";
512 echo " <td align='right'>\n";
513 echo " "; bucks($grand_total_copays); echo " \n";
515 echo " <td colspan='2'>\n";
524 </div
> <!-- end the apptenc_report_results
-->
527 <?php
echo xl('Please input search criteria above, and click Submit to view results.', 'e' ); ?
>
531 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
535 <?php
if ($alertmsg) { echo " alert('$alertmsg');\n"; } ?
>