Centralized formatting.inc.php include
[openemr.git] / interface / reports / appt_encounter_report.php
blobd8e7294d389a1398d96ccbb36d03cceed11222ad
1 <?php
2 /*
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
18 * forms.encounter
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>;.
36 * @package OpenEMR
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");
49 $errmsg = "";
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) {
56 global $errmsg;
57 if ($errmsg) $errmsg .= '<br />';
58 $errmsg .= $msg;
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 " &nbsp;" . xl('Totals for','','',' ') . $docrow['docname'] . "\n";
72 echo " </td>\n";
73 echo " <td align='right'>\n";
74 echo " &nbsp;" . $docrow['encounters'] . "&nbsp;\n";
75 echo " </td>\n";
76 echo " <td align='right'>\n";
77 echo " &nbsp;"; bucks($docrow['charges']); echo "&nbsp;\n";
78 echo " </td>\n";
79 echo " <td align='right'>\n";
80 echo " &nbsp;"; bucks($docrow['copays']); echo "&nbsp;\n";
81 echo " </td>\n";
82 echo " <td colspan='2'>\n";
83 echo " &nbsp;\n";
84 echo " </td>\n";
85 echo " </tr>\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.
105 $query = "( " .
106 "SELECT " .
107 "e.pc_eventDate, e.pc_startTime, " .
108 "fe.encounter, fe.date AS encdate, " .
109 "f.authorized, " .
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 ";
119 if ($form_to_date) {
120 $query .= "e.pc_eventDate >= '$form_from_date' AND e.pc_eventDate <= '$form_to_date' ";
121 } else {
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 != '?' " .
129 ") UNION ( " .
130 "SELECT " .
131 "e.pc_eventDate, e.pc_startTime, " .
132 "fe.encounter, fe.date AS encdate, " .
133 "f.authorized, " .
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 ";
145 if ($form_to_date) {
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' ";
148 } else {
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);
160 <html>
161 <head>
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 */
169 @media print {
170 #report_parameters {
171 visibility: hidden;
172 display: none;
174 #report_parameters_daterange {
175 visibility: visible;
176 display: inline;
178 #report_results table {
179 margin-top: 0px;
183 /* specifically exclude some from the screen */
184 @media screen {
185 #report_parameters_daterange {
186 visibility: hidden;
187 display: none;
191 </style>
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 ?>
213 </script>
215 </head>
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)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
223 </div>
225 <form method='post' id='theform' action='appt_encounter_report.php'>
227 <div id="report_parameters">
229 <table>
230 <tr>
231 <td width='630px'>
232 <div style='float:left'>
234 <table class='text'>
235 <tr>
236 <td class='label'>
237 <?php xl('Facility','e'); ?>:
238 </td>
239 <td>
240 <?php
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";
256 echo " </select>\n";
258 </td>
259 <td class='label'>
260 <?php xl('DOS','e'); ?>:
261 </td>
262 <td>
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' >
265 </td>
266 <td class='label'>
267 <?php xl('To','e'); ?>:
268 </td>
269 <td>
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' >
272 </td>
273 </tr>
274 <tr>
275 <td>&nbsp;</td>
276 <td>
277 <input type='checkbox' name='form_details'
278 value='1'<?php if ($_POST['form_details']) echo " checked"; ?>><?php xl('Details','e') ?>
279 </td>
280 </tr>
281 </table>
283 </div>
285 </td>
286 <td align='left' valign='middle' height="100%">
287 <table style='border-left:1px solid; width:100%; height:100%' >
288 <tr>
289 <td>
290 <div style='margin-left:15px'>
291 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
292 <span>
293 <?php xl('Submit','e'); ?>
294 </span>
295 </a>
297 <?php if ($_POST['form_refresh']) { ?>
298 <a href='#' class='css_button' id='printbutton'>
299 <span>
300 <?php xl('Print','e'); ?>
301 </span>
302 </a>
303 <?php } ?>
304 </div>
305 </td>
306 </tr>
307 </table>
308 </td>
309 </tr>
310 </table>
312 </div> <!-- end apptenc_report_parameters -->
314 <?php
315 if ($_POST['form_refresh'] ) {
317 <div id="report_results">
318 <table id='mymaintable'>
320 <thead>
321 <th> &nbsp;<?php xl('Practitioner','e'); ?> </th>
322 <th> &nbsp;<?php xl('Date/Appt','e'); ?> </th>
323 <th> &nbsp;<?php xl('Patient','e'); ?> </th>
324 <th> &nbsp;<?php xl('ID','e'); ?> </th>
325 <th align='right'> <?php xl('Chart','e'); ?>&nbsp; </th>
326 <th align='right'> <?php xl('Encounter','e'); ?>&nbsp; </th>
327 <th align='right'> <?php xl('Charges','e'); ?>&nbsp; </th>
328 <th align='right'> <?php xl('Copays','e'); ?>&nbsp; </th>
329 <th> <?php xl('Billed','e'); ?> </th>
330 <th> &nbsp;<?php xl('Error','e'); ?> </th>
331 </thead>
332 <tbody>
333 <?php
334 if ($res) {
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']) {
343 endDoctor($docrow);
346 $errmsg = "";
347 $billed = "Y";
348 $charges = 0;
349 $copays = 0;
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'])
362 $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'));
371 } else {
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'] . "'";
383 } else {
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;
396 } // End IPPF stuff
398 } // end while
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 " .
409 "FROM lists AS l " .
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'));
434 } // end if
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']) {
449 <tr>
450 <td>
451 &nbsp;<?php echo ($docname == $docrow['docname']) ? "" : $docname ?>
452 </td>
453 <td>
454 &nbsp;<?php
455 /*****************************************************************
456 if ($form_to_date) {
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));
464 else {
465 echo oeFormatShortDate($row['pc_eventDate']) . ' ' . substr($row['pc_startTime'], 0, 5);
468 </td>
469 <td>
470 &nbsp;<?php echo $row['fname'] . " " . $row['lname'] ?>
471 </td>
472 <td>
473 &nbsp;<?php echo $row['pubpid'] ?>
474 </td>
475 <td align='right'>
476 <?php echo $row['pid'] ?>&nbsp;
477 </td>
478 <td align='right'>
479 <?php echo $encounter ?>&nbsp;
480 </td>
481 <td align='right'>
482 <?php bucks($charges) ?>&nbsp;
483 </td>
484 <td align='right'>
485 <?php bucks($copays) ?>&nbsp;
486 </td>
487 <td>
488 <?php echo $billed ?>
489 </td>
490 <td style='color:#cc0000'>
491 <?php echo $errmsg; ?>&nbsp;
492 </td>
493 </tr>
494 <?php
495 } // end of details line
497 $docrow['docname'] = $docname;
498 } // end of row
500 endDoctor($docrow);
502 echo " <tr class='report_totals'>\n";
503 echo " <td colspan='5'>\n";
504 echo " &nbsp;" . xl('Grand Totals') . "\n";
505 echo " </td>\n";
506 echo " <td align='right'>\n";
507 echo " &nbsp;" . $grand_total_encounters . "&nbsp;\n";
508 echo " </td>\n";
509 echo " <td align='right'>\n";
510 echo " &nbsp;"; bucks($grand_total_charges); echo "&nbsp;\n";
511 echo " </td>\n";
512 echo " <td align='right'>\n";
513 echo " &nbsp;"; bucks($grand_total_copays); echo "&nbsp;\n";
514 echo " </td>\n";
515 echo " <td colspan='2'>\n";
516 echo " &nbsp;\n";
517 echo " </td>\n";
518 echo " </tr>\n";
522 </tbody>
523 </table>
524 </div> <!-- end the apptenc_report_results -->
525 <?php } else { ?>
526 <div class='text'>
527 <?php echo xl('Please input search criteria above, and click Submit to view results.', 'e' ); ?>
528 </div>
529 <?php } ?>
531 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
533 </form>
534 <script>
535 <?php if ($alertmsg) { echo " alert('$alertmsg');\n"; } ?>
536 </script>
537 </body>
539 </html>