2 // Copyright (C) 2005-2009 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 include_once("../globals.php");
28 include_once("../../library/patient.inc");
29 include_once("../../custom/code_types.inc.php");
32 $alertmsg = ''; // not used yet but maybe later
33 $grand_total_charges = 0;
34 $grand_total_copays = 0;
35 $grand_total_encounters = 0;
37 function postError($msg) {
39 if ($errmsg) $errmsg .= '<br />';
43 function bucks($amount) {
44 if ($amount) printf("%.2f", $amount);
47 function endDoctor(&$docrow) {
48 global $grand_total_charges, $grand_total_copays, $grand_total_encounters;
49 if (!$docrow['docname']) return;
51 echo " <tr class='apptencreport_totals'>\n";
52 echo " <td colspan='5'>\n";
53 echo " " . xl('Totals for','','',' ') . $docrow['docname'] . "\n";
55 echo " <td align='right'>\n";
56 echo " " . $docrow['encounters'] . " \n";
58 echo " <td align='right'>\n";
59 echo " "; bucks($docrow['charges']); echo " \n";
61 echo " <td align='right'>\n";
62 echo " "; bucks($docrow['copays']); echo " \n";
64 echo " <td colspan='2'>\n";
69 $grand_total_charges +
= $docrow['charges'];
70 $grand_total_copays +
= $docrow['copays'];
71 $grand_total_encounters +
= $docrow['encounters'];
73 $docrow['charges'] = 0;
74 $docrow['copays'] = 0;
75 $docrow['encounters'] = 0;
78 $form_facility = isset($_POST['form_facility']) ?
$_POST['form_facility'] : '';
79 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
80 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
81 if ($_POST['form_search']) {
82 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
83 $form_to_date = fixDate($_POST['form_to_date'], "");
85 // MySQL doesn't grok full outer joins so we do it the hard way.
89 "e.pc_eventDate, e.pc_startTime, " .
90 "fe.encounter, fe.date AS encdate, " .
92 "p.fname, p.lname, p.pid, p.pubpid, " .
93 "CONCAT( u.lname, ', ', u.fname ) AS docname " .
94 "FROM openemr_postcalendar_events AS e " .
95 "LEFT OUTER JOIN form_encounter AS fe " .
96 "ON LEFT(fe.date, 10) = e.pc_eventDate AND fe.pid = e.pc_pid " .
97 "LEFT OUTER JOIN forms AS f ON f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
98 "LEFT OUTER JOIN patient_data AS p ON p.pid = e.pc_pid " .
99 // "LEFT OUTER JOIN users AS u ON BINARY u.username = BINARY f.user WHERE ";
100 "LEFT OUTER JOIN users AS u ON u.id = fe.provider_id WHERE ";
102 $query .= "e.pc_eventDate >= '$form_from_date' AND e.pc_eventDate <= '$form_to_date' ";
104 $query .= "e.pc_eventDate = '$form_from_date' ";
106 if ($form_facility !== '') {
107 $query .= "AND e.pc_facility = '$form_facility' ";
109 // $query .= "AND ( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
110 $query .= "AND e.pc_pid != '' AND e.pc_apptstatus != '?' " .
113 "e.pc_eventDate, e.pc_startTime, " .
114 "fe.encounter, fe.date AS encdate, " .
116 "p.fname, p.lname, p.pid, p.pubpid, " .
117 "CONCAT( u.lname, ', ', u.fname ) AS docname " .
118 "FROM form_encounter AS fe " .
119 "LEFT OUTER JOIN openemr_postcalendar_events AS e " .
120 "ON LEFT(fe.date, 10) = e.pc_eventDate AND fe.pid = e.pc_pid AND " .
121 // "( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
122 "e.pc_pid != '' AND e.pc_apptstatus != '?' " .
123 "LEFT OUTER JOIN forms AS f ON f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
124 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
126 "LEFT OUTER JOIN users AS u ON u.id = fe.provider_id WHERE ";
128 // $query .= "LEFT(fe.date, 10) >= '$form_from_date' AND LEFT(fe.date, 10) <= '$form_to_date' ";
129 $query .= "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' ";
131 // $query .= "LEFT(fe.date, 10) = '$form_from_date' ";
132 $query .= "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_from_date 23:59:59' ";
134 if ($form_facility !== '') {
135 $query .= "AND fe.facility_id = '$form_facility' ";
137 $query .= ") ORDER BY docname, pc_eventDate, pc_startTime";
139 $res = sqlStatement($query);
144 <?php
html_header_show();?
>
145 <link rel
="stylesheet" href
="<?php echo $css_header;?>" type
="text/css">
146 <style type
="text/css">
148 /* specifically include & exclude from printing */
150 #apptencreport_parameters {
154 #apptencreport_parameters_daterange {
160 /* specifically exclude some from the screen */
162 #apptencreport_parameters_daterange {
168 #apptencreport_parameters {
172 background
-color
: #ddf;
174 #apptencreport_parameters table {
178 border
-collapse
: collapse
;
180 #apptencreport_parameters table td {
184 #apptencreport_results {
188 #apptencreport_results table {
189 border
: 1px solid black
;
191 border
-collapse
: collapse
;
193 #apptencreport_results table thead {
194 display
: table
-header
-group
;
195 background
-color
: #ddd;
197 #apptencreport_results table th {
198 border
-bottom
: 1px solid black
;
200 #apptencreport_results table td {
203 border
-bottom
: 1px solid
#eee;
205 .apptencreport_totals td
{
206 background
-color
: #77ff77;
210 <title
><?php
xl('Appointments and Encounters','e'); ?
></title
>
213 <body
class="body_top">
216 <h2
><?php
xl('Appointments and Encounters','e'); ?
></h2
>
217 <div id
="apptencreport_parameters_daterange">
218 <?php
echo date("d F Y", strtotime($form_from_date)) ." to ". date("d F Y", strtotime($form_to_date)); ?
>
221 <div id
="apptencreport_parameters">
222 <form method
='post' action
='appt_encounter_report.php'>
226 <?php
xl('Facility','e'); ?
>:
228 // Build a drop-down list of facilities.
230 $query = "SELECT id, name FROM facility ORDER BY name";
231 $fres = sqlStatement($query);
232 echo " <select name='form_facility'>\n";
233 echo " <option value=''>-- " . xl('All') . " --\n";
234 while ($frow = sqlFetchArray($fres)) {
235 $facid = $frow['id'];
236 echo " <option value='$facid'";
237 if ($facid == $form_facility) echo " selected";
238 echo ">" . $frow['name'] . "\n";
240 echo " <option value='0'";
241 if ($form_facility === '0') echo " selected";
242 echo ">-- " . xl('Unspecified') . " --\n";
245 <?php
xl('DOS','e'); ?
>:
246 <input type
='text' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo $form_from_date; ?>'
247 title
='Date of appointments mm/dd/yyyy' >
248 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
249 id
='img_from_date' border
='0' alt
='[?]' style
='cursor:pointer'
250 title
='<?php xl('Click here to choose a date
','e
'); ?>'>
252 <?php
xl('to','e'); ?
>:
253 <input type
='text' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo $form_to_date; ?>'
254 title
='Optional end date mm/dd/yyyy' >
255 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
256 id
='img_to_date' border
='0' alt
='[?]' style
='cursor:pointer'
257 title
='<?php xl('Click here to choose a date
','e
'); ?>'>
259 <input type
='checkbox' name
='form_details'
260 value
='1'<?php
if ($_POST['form_details']) echo " checked"; ?
>><?php
xl('Details','e') ?
>
262 <input type
='submit' name
='form_search' value
='<?php xl('Search
','e
'); ?>'>
264 <input type
='button' value
='<?php xl('Print','e
'); ?>' onclick
='window.print()' />
268 </div
> <!-- end apptenc_report_parameters
-->
270 <div id
="apptencreport_results">
274 <th
>  
;<?php
xl('Practitioner','e'); ?
> </th
>
275 <th
>  
;<?php
xl('Time','e'); ?
> </th
>
276 <th
>  
;<?php
xl('Patient','e'); ?
> </th
>
277 <th
>  
;<?php
xl('ID','e'); ?
> </th
>
278 <th
> <?php
xl('Chart','e'); ?
> 
; </th
>
279 <th
> <?php
xl('Encounter','e'); ?
> 
; </th
>
280 <th
> <?php
xl('Charges','e'); ?
> 
; </th
>
281 <th
> <?php
xl('Copays','e'); ?
> 
; </th
>
282 <th
> <?php
xl('Billed','e'); ?
> </th
>
283 <th
>  
;<?php
xl('Error','e'); ?
> </th
>
288 $docrow = array('docname' => '', 'charges' => 0, 'copays' => 0, 'encounters' => 0);
290 while ($row = sqlFetchArray($res)) {
291 $patient_id = $row['pid'];
292 $encounter = $row['encounter'];
293 $docname = $row['docname'] ?
$row['docname'] : xl('Unknown');
295 if ($docname != $docrow['docname']) {
303 $gcac_related_visit = false;
305 // Scan the billing items for status and fee total.
307 $query = "SELECT code_type, code, modifier, authorized, billed, fee, justify " .
308 "FROM billing WHERE " .
309 "pid = '$patient_id' AND encounter = '$encounter' AND activity = 1";
310 $bres = sqlStatement($query);
312 while ($brow = sqlFetchArray($bres)) {
313 $code_type = $brow['code_type'];
314 if ($code_types[$code_type]['fee'] && !$brow['billed'])
316 if (!$GLOBALS['simplified_demographics'] && !$brow['authorized'])
317 postError(xl('Needs Auth'));
318 if ($code_types[$code_type]['just']) {
319 if (! $brow['justify']) postError(xl('Needs Justify'));
321 if ($code_type == 'COPAY') {
322 $copays -= $brow['fee'];
323 if ($brow['fee'] >= 0) postError(xl('Copay not positive'));
324 } else if ($code_types[$code_type]['fee']) {
325 $charges +
= $brow['fee'];
326 if ($brow['fee'] == 0 && !$GLOBALS['ippf_specific']) postError(xl('Missing Fee'));
328 if ($brow['fee'] != 0) postError(xl('Fee is not allowed'));
331 // Custom logic for IPPF to determine if a GCAC issue applies.
332 if ($GLOBALS['ippf_specific']) {
333 if (!empty($code_types[$code_type]['fee'])) {
334 $query = "SELECT related_code FROM codes WHERE code_type = '" .
335 $code_types[$code_type]['id'] . "' AND " .
336 "code = '" . $brow['code'] . "' AND ";
337 if ($brow['modifier']) {
338 $query .= "modifier = '" . $brow['modifier'] . "'";
340 $query .= "(modifier IS NULL OR modifier = '')";
342 $query .= " LIMIT 1";
343 $tmp = sqlQuery($query);
344 $relcodes = explode(';', $tmp['related_code']);
345 foreach ($relcodes as $codestring) {
346 if ($codestring === '') continue;
347 list($codetype, $code) = explode(':', $codestring);
348 if ($codetype !== 'IPPF') continue;
349 if (preg_match('/^25222/', $code)) $gcac_related_visit = true;
356 // More custom code for IPPF. Generates an error message if a
357 // GCAC issue is required but is not linked to this visit.
358 if (!$errmsg && $gcac_related_visit) {
359 $grow = sqlQuery("SELECT l.id, l.title, l.begdate, ie.pid " .
361 "LEFT JOIN issue_encounter AS ie ON ie.pid = l.pid AND " .
362 "ie.encounter = '$encounter' AND ie.list_id = l.id " .
363 "WHERE l.pid = '$patient_id' AND " .
364 "l.activity = 1 AND l.type = 'ippf_gcac' " .
365 "ORDER BY ie.pid DESC, l.begdate DESC LIMIT 1");
366 // Note that reverse-ordering by ie.pid is a trick for sorting
367 // issues linked to the encounter (non-null values) first.
368 if (empty($grow['pid'])) { // if there is no linked GCAC issue
369 if (empty($grow)) { // no GCAC issue exists
370 $errmsg = "GCAC issue does not exist";
372 else { // there is one but none is linked
373 $errmsg = "GCAC issue is not linked";
378 if (!$billed) postError($GLOBALS['simplified_demographics'] ?
379 xl('Not checked out') : xl('Not billed'));
380 if (!$encounter) postError(xl('No visit'));
382 if (! $charges) $billed = "";
384 $docrow['charges'] +
= $charges;
385 $docrow['copays'] +
= $copays;
386 if ($encounter) ++
$docrow['encounters'];
388 if ($_POST['form_details']) {
392  
;<?php
echo ($docname == $docrow['docname']) ?
"" : $docname ?
>
397 if (empty($row['pc_eventDate'])) {
398 echo substr($row['encdate'], 0, 10);
401 echo $row['pc_eventDate'] . ' ' . substr($row['pc_startTime'], 0, 5);
406  
;<?php
echo $row['fname'] . " " . $row['lname'] ?
>
409  
;<?php
echo $row['pubpid'] ?
>
412 <?php
echo $row['pid'] ?
> 
;
415 <?php
echo $encounter ?
> 
;
418 <?php
bucks($charges) ?
> 
;
421 <?php
bucks($copays) ?
> 
;
424 <?php
echo $billed ?
>
426 <td style
='color:#cc0000'>
427 <?php
echo $errmsg; ?
> 
;
431 } // end of details line
433 $docrow['docname'] = $docname;
438 echo " <tr class='apptencreport_totals'>\n";
439 echo " <td colspan='5'>\n";
440 echo " " . xl('Grand Totals') . "\n";
442 echo " <td align='right'>\n";
443 echo " " . $grand_total_encounters . " \n";
445 echo " <td align='right'>\n";
446 echo " "; bucks($grand_total_charges); echo " \n";
448 echo " <td align='right'>\n";
449 echo " "; bucks($grand_total_copays); echo " \n";
451 echo " <td colspan='2'>\n";
460 </div
> <!-- end the apptenc_report_results
-->
465 <?php
if ($alertmsg) { echo " alert('$alertmsg');\n"; } ?
>
469 <!-- stuff
for the popup calendar
-->
470 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
471 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
472 <script type
="text/javascript" src
="../../library/dynarch_calendar_en.js"></script
>
473 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
474 <script language
="Javascript">
475 Calendar
.setup({inputField
:"form_from_date", ifFormat
:"%Y-%m-%d", button
:"img_from_date"});
476 Calendar
.setup({inputField
:"form_to_date", ifFormat
:"%Y-%m-%d", button
:"img_to_date"});