2 // Copyright (C) 2005-2016 Rod Roark <rod@sunsetsystems.com>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This report cross-references appointments with encounters.
10 // For a given date, show a line for each appointment with the
11 // matching encounter, and also for each encounter that has no
12 // matching appointment. This helps to catch these errors:
14 // * Appointments with no encounter
15 // * Encounters with no appointment
16 // * Codes not justified
17 // * Codes not authorized
18 // * Procedure codes without a fee
19 // * Fees assigned to diagnoses (instead of procedures)
20 // * Encounters not billed
22 // For decent performance the following indexes are highly recommended:
23 // openemr_postcalendar_events.pc_eventDate
25 // billing.pid_encounter
27 require_once("../globals.php");
28 require_once("$srcdir/patient.inc");
29 require_once("$srcdir/formatting.inc.php");
30 require_once("../../custom/code_types.inc.php");
31 require_once("$srcdir/billing.inc");
34 $alertmsg = ''; // not used yet but maybe later
35 $grand_total_charges = 0;
36 $grand_total_copays = 0;
37 $grand_total_encounters = 0;
39 function postError($msg) {
41 if ($errmsg) $errmsg .= '<br />';
45 function bucks($amount) {
46 if ($amount) echo oeFormatMoney($amount);
49 function endDoctor(&$docrow) {
50 global $grand_total_charges, $grand_total_copays, $grand_total_encounters;
51 if (!$docrow['docname']) return;
53 echo " <tr class='report_totals'>\n";
54 echo " <td colspan='5'>\n";
55 echo " " . xl('Totals for','','',' ') . $docrow['docname'] . "\n";
57 echo " <td align='right'>\n";
58 echo " " . $docrow['encounters'] . " \n";
60 echo " <td align='right'>\n";
61 echo " "; bucks($docrow['charges']); echo " \n";
63 echo " <td align='right'>\n";
64 echo " "; bucks($docrow['copays']); echo " \n";
66 echo " <td colspan='2'>\n";
71 $grand_total_charges +
= $docrow['charges'];
72 $grand_total_copays +
= $docrow['copays'];
73 $grand_total_encounters +
= $docrow['encounters'];
75 $docrow['charges'] = 0;
76 $docrow['copays'] = 0;
77 $docrow['encounters'] = 0;
80 $form_facility = isset($_POST['form_facility']) ?
$_POST['form_facility'] : '';
81 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
82 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
83 if ($_POST['form_refresh']) {
84 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
85 $form_to_date = fixDate($_POST['form_to_date'], "");
87 // MySQL doesn't grok full outer joins so we do it the hard way.
91 "e.pc_eventDate, e.pc_startTime, " .
92 "fe.encounter, fe.date AS encdate, " .
94 "p.fname, p.lname, p.pid, p.pubpid, " .
95 "CONCAT( u.lname, ', ', u.fname ) AS docname " .
96 "FROM openemr_postcalendar_events AS e " .
97 "LEFT OUTER JOIN form_encounter AS fe " .
98 "ON fe.date = e.pc_eventDate AND fe.pid = e.pc_pid " .
99 "LEFT OUTER JOIN forms AS f ON f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
100 "LEFT OUTER JOIN patient_data AS p ON p.pid = e.pc_pid " .
101 // "LEFT OUTER JOIN users AS u ON BINARY u.username = BINARY f.user WHERE ";
102 "LEFT OUTER JOIN users AS u ON u.id = fe.provider_id WHERE ";
104 $query .= "e.pc_eventDate >= '$form_from_date' AND e.pc_eventDate <= '$form_to_date' ";
106 $query .= "e.pc_eventDate = '$form_from_date' ";
108 if ($form_facility !== '') {
109 $query .= "AND e.pc_facility = '" . add_escape_custom($form_facility) . "' ";
111 // $query .= "AND ( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
112 $query .= "AND e.pc_pid != '' AND e.pc_apptstatus != '?' " .
115 "e.pc_eventDate, e.pc_startTime, " .
116 "fe.encounter, fe.date AS encdate, " .
118 "p.fname, p.lname, p.pid, p.pubpid, " .
119 "CONCAT( u.lname, ', ', u.fname ) AS docname " .
120 "FROM form_encounter AS fe " .
121 "LEFT OUTER JOIN openemr_postcalendar_events AS e " .
122 "ON fe.date = e.pc_eventDate AND fe.pid = e.pc_pid AND " .
123 // "( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
124 "e.pc_pid != '' AND e.pc_apptstatus != '?' " .
125 "LEFT OUTER JOIN forms AS f ON f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
126 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
127 // "LEFT OUTER JOIN users AS u ON BINARY u.username = BINARY f.user WHERE ";
128 "LEFT OUTER JOIN users AS u ON u.id = fe.provider_id WHERE ";
130 // $query .= "LEFT(fe.date, 10) >= '$form_from_date' AND LEFT(fe.date, 10) <= '$form_to_date' ";
131 $query .= "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' ";
133 // $query .= "LEFT(fe.date, 10) = '$form_from_date' ";
134 $query .= "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_from_date 23:59:59' ";
136 if ($form_facility !== '') {
137 $query .= "AND fe.facility_id = '" . add_escape_custom($form_facility) . "' ";
139 $query .= ") ORDER BY docname, IFNULL(pc_eventDate, encdate), pc_startTime";
141 $res = sqlStatement($query);
146 <?php
html_header_show();?
>
147 <link rel
="stylesheet" href
="<?php echo $css_header;?>" type
="text/css">
148 <style type
="text/css">
150 /* specifically include & exclude from printing */
156 #report_parameters_daterange {
160 #report_results table {
165 /* specifically exclude some from the screen */
167 #report_parameters_daterange {
174 <title
><?php
xl('Appointments and Encounters','e'); ?
></title
>
176 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-9-1/index.js"></script
>
177 <script type
="text/javascript" src
="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></script
>
179 <script LANGUAGE
="JavaScript">
181 $
(document
).ready(function() {
182 oeFixedHeaderSetup(document
.getElementById('mymaintable'));
183 var win
= top
.printLogSetup ? top
: opener
.top
;
184 win
.printLogSetup(document
.getElementById('printbutton'));
191 <body
class="body_top">
193 <span
class='title'><?php
xl('Report','e'); ?
> - <?php
xl('Appointments and Encounters','e'); ?
></span
>
195 <div id
="report_parameters_daterange">
196 <?php
echo date("d F Y", strtotime($form_from_date)) ." to ". date("d F Y", strtotime($form_to_date)); ?
>
199 <form method
='post' id
='theform' action
='appt_encounter_report.php'>
201 <div id
="report_parameters">
206 <div style
='float:left'>
211 <?php
xl('Facility','e'); ?
>:
215 // Build a drop-down list of facilities.
217 $query = "SELECT id, name FROM facility ORDER BY name";
218 $fres = sqlStatement($query);
219 echo " <select name='form_facility'>\n";
220 echo " <option value=''>-- " . xl('All Facilities', 'e') . " --\n";
221 while ($frow = sqlFetchArray($fres)) {
222 $facid = $frow['id'];
223 echo " <option value='$facid'";
224 if ($facid == $form_facility) echo " selected";
225 echo ">" . htmlspecialchars($frow['name']) . "\n";
227 echo " <option value='0'";
228 if ($form_facility === '0') echo " selected";
229 echo ">-- " . xl('Unspecified') . " --\n";
234 <?php
xl('DOS','e'); ?
>:
237 <input type
='text' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo $form_from_date; ?>'
238 title
='Date of appointments mm/dd/yyyy' >
239 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
240 id
='img_from_date' border
='0' alt
='[?]' style
='cursor:pointer'
241 title
='<?php xl('Click here to choose a date
','e
'); ?>'>
244 <?php
xl('To','e'); ?
>:
247 <input type
='text' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo $form_to_date; ?>'
248 title
='Optional end date mm/dd/yyyy' >
249 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
250 id
='img_to_date' border
='0' alt
='[?]' style
='cursor:pointer'
251 title
='<?php xl('Click here to choose a date
','e
'); ?>'>
257 <input type
='checkbox' name
='form_details'
258 value
='1'<?php
if ($_POST['form_details']) echo " checked"; ?
>><?php
xl('Details','e') ?
>
266 <td align
='left' valign
='middle' height
="100%">
267 <table style
='border-left:1px solid; width:100%; height:100%' >
270 <div style
='margin-left:15px'>
271 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
273 <?php
xl('Submit','e'); ?
>
277 <?php
if ($_POST['form_refresh']) { ?
>
278 <a href
='#' class='css_button' id
='printbutton'>
280 <?php
xl('Print','e'); ?
>
292 </div
> <!-- end apptenc_report_parameters
-->
295 if ($_POST['form_refresh'] ) {
297 <div id
="report_results">
298 <table id
='mymaintable'>
301 <th
>  
;<?php
xl('Practitioner','e'); ?
> </th
>
302 <th
>  
;<?php
xl('Date/Appt','e'); ?
> </th
>
303 <th
>  
;<?php
xl('Patient','e'); ?
> </th
>
304 <th
>  
;<?php
xl('ID','e'); ?
> </th
>
305 <th align
='right'> <?php
xl('Chart','e'); ?
> 
; </th
>
306 <th align
='right'> <?php
xl('Encounter','e'); ?
> 
; </th
>
307 <th align
='right'> <?php
xl('Charges','e'); ?
> 
; </th
>
308 <th align
='right'> <?php
xl('Copays','e'); ?
> 
; </th
>
309 <th
> <?php
xl('Billed','e'); ?
> </th
>
310 <th
>  
;<?php
xl('Error','e'); ?
> </th
>
315 $docrow = array('docname' => '', 'charges' => 0, 'copays' => 0, 'encounters' => 0);
317 while ($row = sqlFetchArray($res)) {
318 $patient_id = $row['pid'];
319 $encounter = $row['encounter'];
320 $docname = $row['docname'] ?
$row['docname'] : xl('Unknown');
322 if ($docname != $docrow['docname']) {
330 $gcac_related_visit = false;
332 // Scan the billing items for status and fee total.
334 $query = "SELECT code_type, code, modifier, authorized, billed, fee, justify " .
335 "FROM billing WHERE " .
336 "pid = '$patient_id' AND encounter = '$encounter' AND activity = 1";
337 $bres = sqlStatement($query);
339 while ($brow = sqlFetchArray($bres)) {
340 $code_type = $brow['code_type'];
341 if ($code_types[$code_type]['fee'] && !$brow['billed'])
343 if (!$GLOBALS['simplified_demographics'] && !$brow['authorized'])
344 postError(xl('Needs Auth'));
345 if ($code_types[$code_type]['just']) {
346 if (! $brow['justify']) postError(xl('Needs Justify'));
348 if ($code_types[$code_type]['fee']) {
349 $charges +
= $brow['fee'];
350 if ($brow['fee'] == 0 && !$GLOBALS['ippf_specific']) postError(xl('Missing Fee'));
352 if ($brow['fee'] != 0) postError(xl('Fee is not allowed'));
355 // Custom logic for IPPF to determine if a GCAC issue applies.
356 if ($GLOBALS['ippf_specific']) {
357 if (!empty($code_types[$code_type]['fee'])) {
358 $query = "SELECT related_code FROM codes WHERE code_type = '" .
359 $code_types[$code_type]['id'] . "' AND " .
360 "code = '" . $brow['code'] . "' AND ";
361 if ($brow['modifier']) {
362 $query .= "modifier = '" . $brow['modifier'] . "'";
364 $query .= "(modifier IS NULL OR modifier = '')";
366 $query .= " LIMIT 1";
367 $tmp = sqlQuery($query);
368 $relcodes = explode(';', $tmp['related_code']);
369 foreach ($relcodes as $codestring) {
370 if ($codestring === '') continue;
371 list($codetype, $code) = explode(':', $codestring);
372 if ($codetype !== 'IPPF') continue;
373 if (preg_match('/^25222/', $code)) $gcac_related_visit = true;
380 $copays -= getPatientCopay($patient_id,$encounter);
382 // The following is removed, perhaps temporarily, because gcac reporting
383 // no longer depends on gcac issues. -- Rod 2009-08-11
384 /******************************************************************
385 // More custom code for IPPF. Generates an error message if a
386 // GCAC issue is required but is not linked to this visit.
387 if (!$errmsg && $gcac_related_visit) {
388 $grow = sqlQuery("SELECT l.id, l.title, l.begdate, ie.pid " .
390 "LEFT JOIN issue_encounter AS ie ON ie.pid = l.pid AND " .
391 "ie.encounter = '$encounter' AND ie.list_id = l.id " .
392 "WHERE l.pid = '$patient_id' AND " .
393 "l.activity = 1 AND l.type = 'ippf_gcac' " .
394 "ORDER BY ie.pid DESC, l.begdate DESC LIMIT 1");
395 // Note that reverse-ordering by ie.pid is a trick for sorting
396 // issues linked to the encounter (non-null values) first.
397 if (empty($grow['pid'])) { // if there is no linked GCAC issue
398 if (empty($grow)) { // no GCAC issue exists
399 $errmsg = "GCAC issue does not exist";
401 else { // there is one but none is linked
402 $errmsg = "GCAC issue is not linked";
406 ******************************************************************/
407 if ($gcac_related_visit) {
408 $grow = sqlQuery("SELECT COUNT(*) AS count FROM forms " .
409 "WHERE pid = '$patient_id' AND encounter = '$encounter' AND " .
410 "deleted = 0 AND formdir = 'LBFgcac'");
411 if (empty($grow['count'])) { // if there is no gcac form
412 postError(xl('GCAC visit form is missing'));
415 /*****************************************************************/
417 if (!$billed) postError($GLOBALS['simplified_demographics'] ?
418 xl('Not checked out') : xl('Not billed'));
419 if (!$encounter) postError(xl('No visit'));
421 if (! $charges) $billed = "";
423 $docrow['charges'] +
= $charges;
424 $docrow['copays'] +
= $copays;
425 if ($encounter) ++
$docrow['encounters'];
427 if ($_POST['form_details']) {
431  
;<?php
echo ($docname == $docrow['docname']) ?
"" : $docname ?
>
435 /*****************************************************************
437 echo $row['pc_eventDate'] . '<br>';
438 echo substr($row['pc_startTime'], 0, 5);
440 *****************************************************************/
441 if (empty($row['pc_eventDate'])) {
442 echo oeFormatShortDate(substr($row['encdate'], 0, 10));
445 echo oeFormatShortDate($row['pc_eventDate']) . ' ' . substr($row['pc_startTime'], 0, 5);
450  
;<?php
echo $row['fname'] . " " . $row['lname'] ?
>
453  
;<?php
echo $row['pubpid'] ?
>
456 <?php
echo $row['pid'] ?
> 
;
459 <?php
echo $encounter ?
> 
;
462 <?php
bucks($charges) ?
> 
;
465 <?php
bucks($copays) ?
> 
;
468 <?php
echo $billed ?
>
470 <td style
='color:#cc0000'>
471 <?php
echo $errmsg; ?
> 
;
475 } // end of details line
477 $docrow['docname'] = $docname;
482 echo " <tr class='report_totals'>\n";
483 echo " <td colspan='5'>\n";
484 echo " " . xl('Grand Totals') . "\n";
486 echo " <td align='right'>\n";
487 echo " " . $grand_total_encounters . " \n";
489 echo " <td align='right'>\n";
490 echo " "; bucks($grand_total_charges); echo " \n";
492 echo " <td align='right'>\n";
493 echo " "; bucks($grand_total_copays); echo " \n";
495 echo " <td colspan='2'>\n";
504 </div
> <!-- end the apptenc_report_results
-->
507 <?php
echo xl('Please input search criteria above, and click Submit to view results.', 'e' ); ?
>
511 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
515 <?php
if ($alertmsg) { echo " alert('$alertmsg');\n"; } ?
>
519 <!-- stuff
for the popup calendar
-->
520 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
521 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
522 <?php
include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?
>
523 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
525 <script language
="Javascript">
526 Calendar
.setup({inputField
:"form_from_date", ifFormat
:"%Y-%m-%d", button
:"img_from_date"});
527 Calendar
.setup({inputField
:"form_to_date", ifFormat
:"%Y-%m-%d", button
:"img_to_date"});