ongoing internationalization of date widget
[openemr.git] / interface / reports / appt_encounter_report.php
blobb33febea0f035371124844a341a455cca8feb165
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
43 use OpenEMR\Core\Header;
45 require_once("../globals.php");
46 require_once("$srcdir/patient.inc");
47 require_once("../../custom/code_types.inc.php");
48 require_once("$srcdir/billing.inc");
50 use OpenEMR\Services\FacilityService;
52 $facilityService = new FacilityService();
54 $errmsg = "";
55 $alertmsg = ''; // not used yet but maybe later
56 $grand_total_charges = 0;
57 $grand_total_copays = 0;
58 $grand_total_encounters = 0;
60 function postError($msg)
62 global $errmsg;
63 if ($errmsg) {
64 $errmsg .= '<br />';
67 $errmsg .= $msg;
70 function bucks($amount)
72 if ($amount) {
73 echo oeFormatMoney($amount);
77 function endDoctor(&$docrow)
79 global $grand_total_charges, $grand_total_copays, $grand_total_encounters;
80 if (!$docrow['docname']) {
81 return;
84 echo " <tr class='report_totals'>\n";
85 echo " <td colspan='5'>\n";
86 echo " &nbsp;" . xl('Totals for', '', '', ' ') . $docrow['docname'] . "\n";
87 echo " </td>\n";
88 echo " <td align='right'>\n";
89 echo " &nbsp;" . $docrow['encounters'] . "&nbsp;\n";
90 echo " </td>\n";
91 echo " <td align='right'>\n";
92 echo " &nbsp;";
93 bucks($docrow['charges']);
94 echo "&nbsp;\n";
95 echo " </td>\n";
96 echo " <td align='right'>\n";
97 echo " &nbsp;";
98 bucks($docrow['copays']);
99 echo "&nbsp;\n";
100 echo " </td>\n";
101 echo " <td colspan='2'>\n";
102 echo " &nbsp;\n";
103 echo " </td>\n";
104 echo " </tr>\n";
106 $grand_total_charges += $docrow['charges'];
107 $grand_total_copays += $docrow['copays'];
108 $grand_total_encounters += $docrow['encounters'];
110 $docrow['charges'] = 0;
111 $docrow['copays'] = 0;
112 $docrow['encounters'] = 0;
115 $form_facility = isset($_POST['form_facility']) ? $_POST['form_facility'] : '';
116 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
117 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
118 if ($_POST['form_refresh']) {
119 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
120 $form_to_date = fixDate($_POST['form_to_date'], "");
122 // MySQL doesn't grok full outer joins so we do it the hard way.
124 $query = "( " .
125 "SELECT " .
126 "e.pc_eventDate, e.pc_startTime, " .
127 "fe.encounter, fe.date AS encdate, " .
128 "f.authorized, " .
129 "p.fname, p.lname, p.pid, p.pubpid, " .
130 "CONCAT( u.lname, ', ', u.fname ) AS docname " .
131 "FROM openemr_postcalendar_events AS e " .
132 "LEFT OUTER JOIN form_encounter AS fe " .
133 "ON fe.date = e.pc_eventDate AND fe.pid = e.pc_pid " .
134 "LEFT OUTER JOIN forms AS f ON f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
135 "LEFT OUTER JOIN patient_data AS p ON p.pid = e.pc_pid " .
136 // "LEFT OUTER JOIN users AS u ON BINARY u.username = BINARY f.user WHERE ";
137 "LEFT OUTER JOIN users AS u ON u.id = fe.provider_id WHERE ";
138 if ($form_to_date) {
139 $query .= "e.pc_eventDate >= '$form_from_date' AND e.pc_eventDate <= '$form_to_date' ";
140 } else {
141 $query .= "e.pc_eventDate = '$form_from_date' ";
144 if ($form_facility !== '') {
145 $query .= "AND e.pc_facility = '" . add_escape_custom($form_facility) . "' ";
148 // $query .= "AND ( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
149 $query .= "AND e.pc_pid != '' AND e.pc_apptstatus != '?' " .
150 ") UNION ( " .
151 "SELECT " .
152 "e.pc_eventDate, e.pc_startTime, " .
153 "fe.encounter, fe.date AS encdate, " .
154 "f.authorized, " .
155 "p.fname, p.lname, p.pid, p.pubpid, " .
156 "CONCAT( u.lname, ', ', u.fname ) AS docname " .
157 "FROM form_encounter AS fe " .
158 "LEFT OUTER JOIN openemr_postcalendar_events AS e " .
159 "ON fe.date = e.pc_eventDate AND fe.pid = e.pc_pid AND " .
160 // "( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
161 "e.pc_pid != '' AND e.pc_apptstatus != '?' " .
162 "LEFT OUTER JOIN forms AS f ON f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
163 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
164 // "LEFT OUTER JOIN users AS u ON BINARY u.username = BINARY f.user WHERE ";
165 "LEFT OUTER JOIN users AS u ON u.id = fe.provider_id WHERE ";
166 if ($form_to_date) {
167 // $query .= "LEFT(fe.date, 10) >= '$form_from_date' AND LEFT(fe.date, 10) <= '$form_to_date' ";
168 $query .= "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' ";
169 } else {
170 // $query .= "LEFT(fe.date, 10) = '$form_from_date' ";
171 $query .= "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_from_date 23:59:59' ";
174 if ($form_facility !== '') {
175 $query .= "AND fe.facility_id = '" . add_escape_custom($form_facility) . "' ";
178 $query .= ") ORDER BY docname, IFNULL(pc_eventDate, encdate), pc_startTime";
180 $res = sqlStatement($query);
183 <html>
184 <head>
186 <?php Header::setupHeader(['datetime-picker', 'report-helper']); ?>
188 <style type="text/css">
190 /* specifically include & exclude from printing */
191 @media print {
192 #report_parameters {
193 visibility: hidden;
194 display: none;
196 #report_parameters_daterange {
197 visibility: visible;
198 display: inline;
200 #report_results table {
201 margin-top: 0px;
205 /* specifically exclude some from the screen */
206 @media screen {
207 #report_parameters_daterange {
208 visibility: hidden;
209 display: none;
213 </style>
214 <title><?php xl('Appointments and Encounters', 'e'); ?></title>
216 <script LANGUAGE="JavaScript">
218 $(document).ready(function() {
219 oeFixedHeaderSetup(document.getElementById('mymaintable'));
220 var win = top.printLogSetup ? top : opener.top;
221 win.printLogSetup(document.getElementById('printbutton'));
223 $('.datepicker').datetimepicker({
224 <?php $datetimepicker_timepicker = false; ?>
225 <?php $datetimepicker_showseconds = false; ?>
226 <?php $datetimepicker_formatInput = false; ?>
227 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
228 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
232 </script>
234 </head>
236 <body class="body_top">
238 <span class='title'><?php xl('Report', 'e'); ?> - <?php xl('Appointments and Encounters', 'e'); ?></span>
240 <div id="report_parameters_daterange">
241 <?php echo date("d F Y", strtotime($form_from_date)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
242 </div>
244 <form method='post' id='theform' action='appt_encounter_report.php' onsubmit='return top.restoreSession()'>
246 <div id="report_parameters">
248 <table>
249 <tr>
250 <td width='630px'>
251 <div style='float:left'>
253 <table class='text'>
254 <tr>
255 <td class='control-label'>
256 <?php xl('Facility', 'e'); ?>:
257 </td>
258 <td>
259 <?php
260 // Build a drop-down list of facilities.
262 $fres = $facilityService->getAll();
263 echo " <select name='form_facility' class='form-control'>\n";
264 echo " <option value=''>-- " . xl('All Facilities') . " --\n";
265 foreach ($fres as $frow) {
266 $facid = $frow['id'];
267 echo " <option value='$facid'";
268 if ($facid == $form_facility) {
269 echo " selected";
272 echo ">" . htmlspecialchars($frow['name']) . "\n";
275 echo " <option value='0'";
276 if ($form_facility === '0') {
277 echo " selected";
280 echo ">-- " . xl('Unspecified') . " --\n";
281 echo " </select>\n";
283 </td>
284 <td class='control-label'>
285 <?php xl('DOS', 'e'); ?>:
286 </td>
287 <td>
288 <input type='text' class='datepicker form-control' name='form_from_date' id="form_from_date" size='10' value='<?php echo $form_from_date; ?>' >
289 </td>
290 <td class='control-label'>
291 <?php xl('To', 'e'); ?>:
292 </td>
293 <td>
294 <input type='text' class='datepicker form-control' name='form_to_date' id="form_to_date" size='10' value='<?php echo $form_to_date; ?>' >
295 </td>
296 </tr>
297 <tr>
298 <td>&nbsp;</td>
299 <td>
300 <div class="checkbox">
301 <label><input type='checkbox' name='form_details'
302 value='1'<?php if ($_POST['form_details']) {
303 echo " checked";
304 } ?>><?php xl('Details', 'e') ?></label>
305 </div>
306 </td>
307 </tr>
308 </table>
310 </div>
312 </td>
313 <td align='left' valign='middle' height="100%">
314 <table style='border-left:1px solid; width:100%; height:100%' >
315 <tr>
316 <td>
317 <div class="text-center">
318 <div class="btn-group" role="group">
319 <a href='#' class='btn btn-default btn-save' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
320 <?php echo xlt('Submit'); ?>
321 </a>
322 <?php if ($_POST['form_refresh']) { ?>
323 <a href='#' class='btn btn-default btn-print' id='printbutton'>
324 <?php echo xlt('Print'); ?>
325 </a>
326 <?php } ?>
327 </div>
328 </div>
329 </td>
330 </tr>
331 </table>
332 </td>
333 </tr>
334 </table>
336 </div> <!-- end apptenc_report_parameters -->
338 <?php
339 if ($_POST['form_refresh']) {
341 <div id="report_results">
342 <table id='mymaintable'>
344 <thead>
345 <th> &nbsp;<?php xl('Practitioner', 'e'); ?> </th>
346 <th> &nbsp;<?php xl('Date/Appt', 'e'); ?> </th>
347 <th> &nbsp;<?php xl('Patient', 'e'); ?> </th>
348 <th> &nbsp;<?php xl('ID', 'e'); ?> </th>
349 <th align='right'> <?php xl('Chart', 'e'); ?>&nbsp; </th>
350 <th align='right'> <?php xl('Encounter', 'e'); ?>&nbsp; </th>
351 <th align='right'> <?php xl('Charges', 'e'); ?>&nbsp; </th>
352 <th align='right'> <?php xl('Copays', 'e'); ?>&nbsp; </th>
353 <th> <?php xl('Billed', 'e'); ?> </th>
354 <th> &nbsp;<?php xl('Error', 'e'); ?> </th>
355 </thead>
356 <tbody>
357 <?php
358 if ($res) {
359 $docrow = array('docname' => '', 'charges' => 0, 'copays' => 0, 'encounters' => 0);
361 while ($row = sqlFetchArray($res)) {
362 $patient_id = $row['pid'];
363 $encounter = $row['encounter'];
364 $docname = $row['docname'] ? $row['docname'] : xl('Unknown');
366 if ($docname != $docrow['docname']) {
367 endDoctor($docrow);
370 $errmsg = "";
371 $billed = "Y";
372 $charges = 0;
373 $copays = 0;
374 $gcac_related_visit = false;
376 // Scan the billing items for status and fee total.
378 $query = "SELECT code_type, code, modifier, authorized, billed, fee, justify " .
379 "FROM billing WHERE " .
380 "pid = '$patient_id' AND encounter = '$encounter' AND activity = 1";
381 $bres = sqlStatement($query);
383 while ($brow = sqlFetchArray($bres)) {
384 $code_type = $brow['code_type'];
385 if ($code_types[$code_type]['fee'] && !$brow['billed']) {
386 $billed = "";
389 if (!$GLOBALS['simplified_demographics'] && !$brow['authorized']) {
390 postError(xl('Needs Auth'));
393 if ($code_types[$code_type]['just']) {
394 if (! $brow['justify']) {
395 postError(xl('Needs Justify'));
399 if ($code_types[$code_type]['fee']) {
400 $charges += $brow['fee'];
401 if ($brow['fee'] == 0 && !$GLOBALS['ippf_specific']) {
402 postError(xl('Missing Fee'));
404 } else {
405 if ($brow['fee'] != 0) {
406 postError(xl('Fee is not allowed'));
410 // Custom logic for IPPF to determine if a GCAC issue applies.
411 if ($GLOBALS['ippf_specific']) {
412 if (!empty($code_types[$code_type]['fee'])) {
413 $query = "SELECT related_code FROM codes WHERE code_type = '" .
414 $code_types[$code_type]['id'] . "' AND " .
415 "code = '" . $brow['code'] . "' AND ";
416 if ($brow['modifier']) {
417 $query .= "modifier = '" . $brow['modifier'] . "'";
418 } else {
419 $query .= "(modifier IS NULL OR modifier = '')";
422 $query .= " LIMIT 1";
423 $tmp = sqlQuery($query);
424 $relcodes = explode(';', $tmp['related_code']);
425 foreach ($relcodes as $codestring) {
426 if ($codestring === '') {
427 continue;
430 list($codetype, $code) = explode(':', $codestring);
431 if ($codetype !== 'IPPF') {
432 continue;
435 if (preg_match('/^25222/', $code)) {
436 $gcac_related_visit = true;
440 } // End IPPF stuff
441 } // end while
443 $copays -= getPatientCopay($patient_id, $encounter);
445 // The following is removed, perhaps temporarily, because gcac reporting
446 // no longer depends on gcac issues. -- Rod 2009-08-11
447 /******************************************************************
448 // More custom code for IPPF. Generates an error message if a
449 // GCAC issue is required but is not linked to this visit.
450 if (!$errmsg && $gcac_related_visit) {
451 $grow = sqlQuery("SELECT l.id, l.title, l.begdate, ie.pid " .
452 "FROM lists AS l " .
453 "LEFT JOIN issue_encounter AS ie ON ie.pid = l.pid AND " .
454 "ie.encounter = '$encounter' AND ie.list_id = l.id " .
455 "WHERE l.pid = '$patient_id' AND " .
456 "l.activity = 1 AND l.type = 'ippf_gcac' " .
457 "ORDER BY ie.pid DESC, l.begdate DESC LIMIT 1");
458 // Note that reverse-ordering by ie.pid is a trick for sorting
459 // issues linked to the encounter (non-null values) first.
460 if (empty($grow['pid'])) { // if there is no linked GCAC issue
461 if (empty($grow)) { // no GCAC issue exists
462 $errmsg = "GCAC issue does not exist";
464 else { // there is one but none is linked
465 $errmsg = "GCAC issue is not linked";
469 ******************************************************************/
470 if ($gcac_related_visit) {
471 $grow = sqlQuery("SELECT COUNT(*) AS count FROM forms " .
472 "WHERE pid = '$patient_id' AND encounter = '$encounter' AND " .
473 "deleted = 0 AND formdir = 'LBFgcac'");
474 if (empty($grow['count'])) { // if there is no gcac form
475 postError(xl('GCAC visit form is missing'));
477 } // end if
478 /*****************************************************************/
480 if (!$billed) {
481 postError($GLOBALS['simplified_demographics'] ?
482 xl('Not checked out') : xl('Not billed'));
485 if (!$encounter) {
486 postError(xl('No visit'));
489 if (! $charges) {
490 $billed = "";
493 $docrow['charges'] += $charges;
494 $docrow['copays'] += $copays;
495 if ($encounter) {
496 ++$docrow['encounters'];
499 if ($_POST['form_details']) {
501 <tr>
502 <td>
503 &nbsp;<?php echo ($docname == $docrow['docname']) ? "" : $docname ?>
504 </td>
505 <td>
506 &nbsp;<?php
507 /*****************************************************************
508 if ($form_to_date) {
509 echo $row['pc_eventDate'] . '<br>';
510 echo substr($row['pc_startTime'], 0, 5);
512 *****************************************************************/
513 if (empty($row['pc_eventDate'])) {
514 echo oeFormatShortDate(substr($row['encdate'], 0, 10));
515 } else {
516 echo oeFormatShortDate($row['pc_eventDate']) . ' ' . substr($row['pc_startTime'], 0, 5);
519 </td>
520 <td>
521 &nbsp;<?php echo $row['fname'] . " " . $row['lname'] ?>
522 </td>
523 <td>
524 &nbsp;<?php echo $row['pubpid'] ?>
525 </td>
526 <td align='right'>
527 <?php echo $row['pid'] ?>&nbsp;
528 </td>
529 <td align='right'>
530 <?php echo $encounter ?>&nbsp;
531 </td>
532 <td align='right'>
533 <?php bucks($charges) ?>&nbsp;
534 </td>
535 <td align='right'>
536 <?php bucks($copays) ?>&nbsp;
537 </td>
538 <td>
539 <?php echo $billed ?>
540 </td>
541 <td style='color:#cc0000'>
542 <?php echo $errmsg; ?>&nbsp;
543 </td>
544 </tr>
545 <?php
546 } // end of details line
548 $docrow['docname'] = $docname;
549 } // end of row
551 endDoctor($docrow);
553 echo " <tr class='report_totals'>\n";
554 echo " <td colspan='5'>\n";
555 echo " &nbsp;" . xl('Grand Totals') . "\n";
556 echo " </td>\n";
557 echo " <td align='right'>\n";
558 echo " &nbsp;" . $grand_total_encounters . "&nbsp;\n";
559 echo " </td>\n";
560 echo " <td align='right'>\n";
561 echo " &nbsp;";
562 bucks($grand_total_charges);
563 echo "&nbsp;\n";
564 echo " </td>\n";
565 echo " <td align='right'>\n";
566 echo " &nbsp;";
567 bucks($grand_total_copays);
568 echo "&nbsp;\n";
569 echo " </td>\n";
570 echo " <td colspan='2'>\n";
571 echo " &nbsp;\n";
572 echo " </td>\n";
573 echo " </tr>\n";
576 </tbody>
577 </table>
578 </div> <!-- end the apptenc_report_results -->
579 <?php } else { ?>
580 <div class='text'>
581 <?php echo xl('Please input search criteria above, and click Submit to view results.', 'e'); ?>
582 </div>
583 <?php } ?>
585 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
587 </form>
588 <script>
589 <?php if ($alertmsg) {
590 echo " alert('$alertmsg');\n";
591 } ?>
592 </script>
593 </body>
595 </html>