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");
31 $alertmsg = ''; // not used yet but maybe later
32 $grand_total_charges = 0;
33 $grand_total_copays = 0;
34 $grand_total_encounters = 0;
36 function bucks($amount) {
37 if ($amount) printf("%.2f", $amount);
40 function endDoctor(&$docrow) {
41 global $grand_total_charges, $grand_total_copays, $grand_total_encounters;
42 if (!$docrow['docname']) return;
44 echo " <tr class='apptencreport_totals'>\n";
45 echo " <td colspan='4'>\n";
46 echo " " . xl('Totals for','','',' ') . $docrow['docname'] . "\n";
48 echo " <td align='right'>\n";
49 echo " " . $docrow['encounters'] . " \n";
51 echo " <td align='right'>\n";
52 echo " "; bucks($docrow['charges']); echo " \n";
54 echo " <td align='right'>\n";
55 echo " "; bucks($docrow['copays']); echo " \n";
57 echo " <td colspan='2'>\n";
62 $grand_total_charges +
= $docrow['charges'];
63 $grand_total_copays +
= $docrow['copays'];
64 $grand_total_encounters +
= $docrow['encounters'];
66 $docrow['charges'] = 0;
67 $docrow['copays'] = 0;
68 $docrow['encounters'] = 0;
71 $form_facility = isset($_POST['form_facility']) ?
$_POST['form_facility'] : '';
72 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
73 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
74 if ($_POST['form_search']) {
75 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
76 $form_to_date = fixDate($_POST['form_to_date'], "");
78 // MySQL doesn't grok full outer joins so we do it the hard way.
82 "e.pc_eventDate, e.pc_startTime, " .
85 "p.fname, p.lname, p.pid, p.pubpid, " .
86 "CONCAT( u.lname, ', ', u.fname ) AS docname " .
87 "FROM openemr_postcalendar_events AS e " .
88 "LEFT OUTER JOIN form_encounter AS fe " .
89 "ON LEFT(fe.date, 10) = e.pc_eventDate AND fe.pid = e.pc_pid " .
90 "LEFT OUTER JOIN forms AS f ON f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
91 "LEFT OUTER JOIN patient_data AS p ON p.pid = e.pc_pid " .
92 // "LEFT OUTER JOIN users AS u ON u.id = e.pc_aid WHERE ";
93 "LEFT OUTER JOIN users AS u ON BINARY u.username = BINARY f.user WHERE ";
95 $query .= "e.pc_eventDate >= '$form_from_date' AND e.pc_eventDate <= '$form_to_date' ";
97 $query .= "e.pc_eventDate = '$form_from_date' ";
99 if ($form_facility !== '') {
100 $query .= "AND e.pc_facility = '$form_facility' ";
102 // $query .= "AND ( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
103 $query .= "AND e.pc_pid != '' AND e.pc_apptstatus != '?' " .
106 "e.pc_eventDate, e.pc_startTime, " .
109 "p.fname, p.lname, p.pid, p.pubpid, " .
110 "CONCAT( u.lname, ', ', u.fname ) AS docname " .
111 "FROM form_encounter AS fe " .
112 "LEFT OUTER JOIN openemr_postcalendar_events AS e " .
113 "ON LEFT(fe.date, 10) = e.pc_eventDate AND fe.pid = e.pc_pid AND " .
114 // "( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
115 "e.pc_pid != '' AND e.pc_apptstatus != '?' " .
116 "LEFT OUTER JOIN forms AS f ON f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
117 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
118 "LEFT OUTER JOIN users AS u ON BINARY u.username = BINARY f.user WHERE ";
120 // $query .= "LEFT(fe.date, 10) >= '$form_from_date' AND LEFT(fe.date, 10) <= '$form_to_date' ";
121 $query .= "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' ";
123 // $query .= "LEFT(fe.date, 10) = '$form_from_date' ";
124 $query .= "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_from_date 23:59:59' ";
126 if ($form_facility !== '') {
127 $query .= "AND fe.facility_id = '$form_facility' ";
129 $query .= ") ORDER BY docname, pc_eventDate, pc_startTime";
131 $res = sqlStatement($query);
136 <?php
html_header_show();?
>
137 <link rel
="stylesheet" href
="<?php echo $css_header;?>" type
="text/css">
138 <style type
="text/css">
140 /* specifically include & exclude from printing */
142 #apptencreport_parameters {
146 #apptencreport_parameters_daterange {
152 /* specifically exclude some from the screen */
154 #apptencreport_parameters_daterange {
160 #apptencreport_parameters {
164 background
-color
: #ddf;
166 #apptencreport_parameters table {
170 border
-collapse
: collapse
;
172 #apptencreport_parameters table td {
176 #apptencreport_results {
180 #apptencreport_results table {
181 border
: 1px solid black
;
183 border
-collapse
: collapse
;
185 #apptencreport_results table thead {
186 display
: table
-header
-group
;
187 background
-color
: #ddd;
189 #apptencreport_results table th {
190 border
-bottom
: 1px solid black
;
192 #apptencreport_results table td {
195 border
-bottom
: 1px solid
#eee;
197 .apptencreport_totals td
{
198 background
-color
: #77ff77;
202 <title
><?php
xl('Appointments and Encounters','e'); ?
></title
>
205 <body
class="body_top">
208 <h2
><?php
xl('Appointments and Encounters','e'); ?
></h2
>
209 <div id
="apptencreport_parameters_daterange">
210 <?php
echo date("d F Y", strtotime($form_from_date)) ." to ". date("d F Y", strtotime($form_to_date)); ?
>
213 <div id
="apptencreport_parameters">
214 <form method
='post' action
='appt_encounter_report.php'>
218 <?php
xl('Facility','e'); ?
>:
220 // Build a drop-down list of facilities.
222 $query = "SELECT id, name FROM facility ORDER BY name";
223 $fres = sqlStatement($query);
224 echo " <select name='form_facility'>\n";
225 echo " <option value=''>-- " . xl('All') . " --\n";
226 while ($frow = sqlFetchArray($fres)) {
227 $facid = $frow['id'];
228 echo " <option value='$facid'";
229 if ($facid == $form_facility) echo " selected";
230 echo ">" . $frow['name'] . "\n";
232 echo " <option value='0'";
233 if ($form_facility === '0') echo " selected";
234 echo ">-- " . xl('Unspecified') . " --\n";
237 <?php
xl('DOS','e'); ?
>:
238 <input type
='text' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo $form_from_date; ?>'
239 title
='Date of appointments mm/dd/yyyy' >
240 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
241 id
='img_from_date' border
='0' alt
='[?]' style
='cursor:pointer'
242 title
='<?php xl('Click here to choose a date
','e
'); ?>'>
244 <?php
xl('to','e'); ?
>:
245 <input type
='text' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo $form_to_date; ?>'
246 title
='Optional end date mm/dd/yyyy' >
247 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
248 id
='img_to_date' border
='0' alt
='[?]' style
='cursor:pointer'
249 title
='<?php xl('Click here to choose a date
','e
'); ?>'>
251 <input type
='checkbox' name
='form_details'
252 value
='1'<?php
if ($_POST['form_details']) echo " checked"; ?
>><?php
xl('Details','e') ?
>
254 <input type
='submit' name
='form_search' value
='<?php xl('Search
','e
'); ?>'>
256 <input type
='button' value
='<?php xl('Print','e
'); ?>' onclick
='window.print()' />
260 </div
> <!-- end apptenc_report_parameters
-->
262 <div id
="apptencreport_results">
266 <th
>  
;<?php
xl('Practitioner','e'); ?
> </th
>
267 <th
>  
;<?php
xl('Time','e'); ?
> </th
>
268 <th
>  
;<?php
xl('Patient','e'); ?
> </th
>
269 <th
>  
;<?php
xl('ID','e'); ?
> </th
>
270 <th
> <?php
xl('Chart','e'); ?
> 
; </th
>
271 <th
> <?php
xl('Encounter','e'); ?
> 
; </th
>
272 <th
> <?php
xl('Charges','e'); ?
> 
; </th
>
273 <th
> <?php
xl('Copays','e'); ?
> 
; </th
>
274 <th
> <?php
xl('Billed','e'); ?
> </th
>
275 <th
>  
;<?php
xl('Error','e'); ?
> </th
>
280 $docrow = array('docname' => '', 'charges' => 0, 'copays' => 0, 'encounters' => 0);
282 while ($row = sqlFetchArray($res)) {
283 $patient_id = $row['pid'];
284 $encounter = $row['encounter'];
285 $docname = $row['docname'] ?
$row['docname'] : 'Unknown';
287 if ($docname != $docrow['docname']) {
295 $gcac_related_visit = false;
297 // Scan the billing items for status and fee total.
299 $query = "SELECT code_type, code, modifier, authorized, billed, fee, justify " .
300 "FROM billing WHERE " .
301 "pid = '$patient_id' AND encounter = '$encounter' AND activity = 1";
302 $bres = sqlStatement($query);
304 while ($brow = sqlFetchArray($bres)) {
305 $code_type = $brow['code_type'];
306 if ($code_types[$code_type]['fee'] && !$brow['billed'])
308 if (!$GLOBALS['simplified_demographics'] && !$brow['authorized'])
309 $errmsg = "Needs Auth";
310 if ($code_types[$code_type]['just']) {
311 if (! $brow['justify']) $errmsg = "Needs Justify";
313 if ($code_type == 'COPAY') {
314 $copays -= $brow['fee'];
315 if ($brow['fee'] >= 0) $errmsg = "Copay not positive";
316 } else if ($code_types[$code_type]['fee']) {
317 $charges +
= $brow['fee'];
318 if ($brow['fee'] == 0 && !$GLOBALS['ippf_specific']) $errmsg = "Missing Fee";
320 if ($brow['fee'] != 0) $errmsg = "Fee is not allowed";
323 // Custom logic for IPPF to determine if a GCAC issue applies.
324 if ($GLOBALS['ippf_specific']) {
325 if (!empty($code_types[$code_type]['fee'])) {
326 $query = "SELECT related_code FROM codes WHERE code_type = '" .
327 $code_types[$code_type]['id'] . "' AND " .
328 "code = '" . $brow['code'] . "' AND ";
329 if ($brow['modifier']) {
330 $query .= "modifier = '" . $brow['modifier'] . "'";
332 $query .= "(modifier IS NULL OR modifier = '')";
334 $query .= " LIMIT 1";
335 $tmp = sqlQuery($query);
336 $relcodes = explode(';', $tmp['related_code']);
337 foreach ($relcodes as $codestring) {
338 if ($codestring === '') continue;
339 list($codetype, $code) = explode(':', $codestring);
340 if ($codetype !== 'IPPF') continue;
341 if (preg_match('/^25222/', $code)) $gcac_related_visit = true;
348 // More custom code for IPPF. Generates an error message if a
349 // GCAC issue is required but is not linked to this visit.
350 if (!$errmsg && $gcac_related_visit) {
351 $grow = sqlQuery("SELECT l.id, l.title, l.begdate, ie.pid " .
353 "LEFT JOIN issue_encounter AS ie ON ie.pid = l.pid AND " .
354 "ie.encounter = '$encounter' AND ie.list_id = l.id " .
355 "WHERE l.pid = '$patient_id' AND " .
356 "l.activity = 1 AND l.type = 'ippf_gcac' " .
357 "ORDER BY ie.pid DESC, l.begdate DESC LIMIT 1");
358 // Note that reverse-ordering by ie.pid is a trick for sorting
359 // issues linked to the encounter (non-null values) first.
360 if (empty($grow['pid'])) { // if there is no linked GCAC issue
361 if (empty($grow)) { // no GCAC issue exists
362 $errmsg = "GCAC issue does not exist";
364 else { // there is one but none is linked
365 $errmsg = "GCAC issue is not linked";
371 if (!$billed) $errmsg = $GLOBALS['simplified_demographics'] ?
372 "Not checked out" : "Not billed";
373 if (!$encounter) $errmsg = "No visit";
376 if (! $charges) $billed = "";
378 $docrow['charges'] +
= $charges;
379 $docrow['copays'] +
= $copays;
380 if ($encounter) ++
$docrow['encounters'];
382 if ($_POST['form_details']) {
386  
;<?php
echo ($docname == $docrow['docname']) ?
"" : $docname ?
>
391 echo $row['pc_eventDate'] . '<br>';
392 echo substr($row['pc_startTime'], 0, 5);
397  
;<?php
echo $row['fname'] . " " . $row['lname'] ?
>
400  
;<?php
echo $row['pubpid'] ?
>
403 <?php
echo $row['pid'] ?
> 
;
406 <?php
echo $encounter ?
> 
;
409 <?php
bucks($charges) ?
> 
;
412 <?php
bucks($copays) ?
> 
;
415 <?php
echo $billed ?
>
417 <td style
='color:#cc0000'>
418  
;<?php
echo xl($errmsg); ?
>
422 } // end of details line
424 $docrow['docname'] = $docname;
429 echo " <tr class='apptencreport_totals'>\n";
430 echo " <td colspan='5'>\n";
431 echo " " . xl('Grand Totals') . "\n";
433 echo " <td align='right'>\n";
434 echo " " . $grand_total_encounters . " \n";
436 echo " <td align='right'>\n";
437 echo " "; bucks($grand_total_charges); echo " \n";
439 echo " <td align='right'>\n";
440 echo " "; bucks($grand_total_copays); echo " \n";
442 echo " <td colspan='2'>\n";
451 </div
> <!-- end the apptenc_report_results
-->
456 <?php
if ($alertmsg) { echo " alert('$alertmsg');\n"; } ?
>
460 <!-- stuff
for the popup calendar
-->
461 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
462 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
463 <script type
="text/javascript" src
="../../library/dynarch_calendar_en.js"></script
>
464 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
465 <script language
="Javascript">
466 Calendar
.setup({inputField
:"form_from_date", ifFormat
:"%Y-%m-%d", button
:"img_from_date"});
467 Calendar
.setup({inputField
:"form_to_date", ifFormat
:"%Y-%m-%d", button
:"img_to_date"});