new datepicker in encounter
[openemr.git] / interface / reports / appt_encounter_report.php
blob6a6aaed62cabad40dd5dcb0326c7c711c8afb34f
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("$srcdir/formatting.inc.php");
47 require_once("../../custom/code_types.inc.php");
48 require_once("$srcdir/billing.inc");
50 $errmsg = "";
51 $alertmsg = ''; // not used yet but maybe later
52 $grand_total_charges = 0;
53 $grand_total_copays = 0;
54 $grand_total_encounters = 0;
56 function postError($msg) {
57 global $errmsg;
58 if ($errmsg) $errmsg .= '<br />';
59 $errmsg .= $msg;
62 function bucks($amount) {
63 if ($amount) echo oeFormatMoney($amount);
66 function endDoctor(&$docrow) {
67 global $grand_total_charges, $grand_total_copays, $grand_total_encounters;
68 if (!$docrow['docname']) return;
70 echo " <tr class='report_totals'>\n";
71 echo " <td colspan='5'>\n";
72 echo " &nbsp;" . xl('Totals for','','',' ') . $docrow['docname'] . "\n";
73 echo " </td>\n";
74 echo " <td align='right'>\n";
75 echo " &nbsp;" . $docrow['encounters'] . "&nbsp;\n";
76 echo " </td>\n";
77 echo " <td align='right'>\n";
78 echo " &nbsp;"; bucks($docrow['charges']); echo "&nbsp;\n";
79 echo " </td>\n";
80 echo " <td align='right'>\n";
81 echo " &nbsp;"; bucks($docrow['copays']); echo "&nbsp;\n";
82 echo " </td>\n";
83 echo " <td colspan='2'>\n";
84 echo " &nbsp;\n";
85 echo " </td>\n";
86 echo " </tr>\n";
88 $grand_total_charges += $docrow['charges'];
89 $grand_total_copays += $docrow['copays'];
90 $grand_total_encounters += $docrow['encounters'];
92 $docrow['charges'] = 0;
93 $docrow['copays'] = 0;
94 $docrow['encounters'] = 0;
97 $form_facility = isset($_POST['form_facility']) ? $_POST['form_facility'] : '';
98 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
99 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
100 if ($_POST['form_refresh']) {
101 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
102 $form_to_date = fixDate($_POST['form_to_date'], "");
104 // MySQL doesn't grok full outer joins so we do it the hard way.
106 $query = "( " .
107 "SELECT " .
108 "e.pc_eventDate, e.pc_startTime, " .
109 "fe.encounter, fe.date AS encdate, " .
110 "f.authorized, " .
111 "p.fname, p.lname, p.pid, p.pubpid, " .
112 "CONCAT( u.lname, ', ', u.fname ) AS docname " .
113 "FROM openemr_postcalendar_events AS e " .
114 "LEFT OUTER JOIN form_encounter AS fe " .
115 "ON fe.date = e.pc_eventDate AND fe.pid = e.pc_pid " .
116 "LEFT OUTER JOIN forms AS f ON f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
117 "LEFT OUTER JOIN patient_data AS p ON p.pid = e.pc_pid " .
118 // "LEFT OUTER JOIN users AS u ON BINARY u.username = BINARY f.user WHERE ";
119 "LEFT OUTER JOIN users AS u ON u.id = fe.provider_id WHERE ";
120 if ($form_to_date) {
121 $query .= "e.pc_eventDate >= '$form_from_date' AND e.pc_eventDate <= '$form_to_date' ";
122 } else {
123 $query .= "e.pc_eventDate = '$form_from_date' ";
125 if ($form_facility !== '') {
126 $query .= "AND e.pc_facility = '" . add_escape_custom($form_facility) . "' ";
128 // $query .= "AND ( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
129 $query .= "AND e.pc_pid != '' AND e.pc_apptstatus != '?' " .
130 ") UNION ( " .
131 "SELECT " .
132 "e.pc_eventDate, e.pc_startTime, " .
133 "fe.encounter, fe.date AS encdate, " .
134 "f.authorized, " .
135 "p.fname, p.lname, p.pid, p.pubpid, " .
136 "CONCAT( u.lname, ', ', u.fname ) AS docname " .
137 "FROM form_encounter AS fe " .
138 "LEFT OUTER JOIN openemr_postcalendar_events AS e " .
139 "ON fe.date = e.pc_eventDate AND fe.pid = e.pc_pid AND " .
140 // "( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
141 "e.pc_pid != '' AND e.pc_apptstatus != '?' " .
142 "LEFT OUTER JOIN forms AS f ON f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
143 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
144 // "LEFT OUTER JOIN users AS u ON BINARY u.username = BINARY f.user WHERE ";
145 "LEFT OUTER JOIN users AS u ON u.id = fe.provider_id WHERE ";
146 if ($form_to_date) {
147 // $query .= "LEFT(fe.date, 10) >= '$form_from_date' AND LEFT(fe.date, 10) <= '$form_to_date' ";
148 $query .= "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' ";
149 } else {
150 // $query .= "LEFT(fe.date, 10) = '$form_from_date' ";
151 $query .= "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_from_date 23:59:59' ";
153 if ($form_facility !== '') {
154 $query .= "AND fe.facility_id = '" . add_escape_custom($form_facility) . "' ";
156 $query .= ") ORDER BY docname, IFNULL(pc_eventDate, encdate), pc_startTime";
158 $res = sqlStatement($query);
161 <html>
162 <head>
163 <?php html_header_show();?>
164 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
165 <link rel="stylesheet" href="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">
167 <style type="text/css">
169 /* specifically include & exclude from printing */
170 @media print {
171 #report_parameters {
172 visibility: hidden;
173 display: none;
175 #report_parameters_daterange {
176 visibility: visible;
177 display: inline;
179 #report_results table {
180 margin-top: 0px;
184 /* specifically exclude some from the screen */
185 @media screen {
186 #report_parameters_daterange {
187 visibility: hidden;
188 display: none;
192 </style>
193 <title><?php xl('Appointments and Encounters','e'); ?></title>
195 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-3-1-1/index.js"></script>
196 <script type="text/javascript" src="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></script>
197 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script>
199 <script LANGUAGE="JavaScript">
201 $(document).ready(function() {
202 oeFixedHeaderSetup(document.getElementById('mymaintable'));
203 var win = top.printLogSetup ? top : opener.top;
204 win.printLogSetup(document.getElementById('printbutton'));
206 $('.datepicker').datetimepicker({
207 <?php $datetimepicker_timepicker = false; ?>
208 <?php $datetimepicker_formatInput = false; ?>
209 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
210 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
214 </script>
216 </head>
218 <body class="body_top">
220 <span class='title'><?php xl('Report','e'); ?> - <?php xl('Appointments and Encounters','e'); ?></span>
222 <div id="report_parameters_daterange">
223 <?php echo date("d F Y", strtotime($form_from_date)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
224 </div>
226 <form method='post' id='theform' action='appt_encounter_report.php'>
228 <div id="report_parameters">
230 <table>
231 <tr>
232 <td width='630px'>
233 <div style='float:left'>
235 <table class='text'>
236 <tr>
237 <td class='label'>
238 <?php xl('Facility','e'); ?>:
239 </td>
240 <td>
241 <?php
242 // Build a drop-down list of facilities.
244 $query = "SELECT id, name FROM facility ORDER BY name";
245 $fres = sqlStatement($query);
246 echo " <select name='form_facility'>\n";
247 echo " <option value=''>-- " . xl('All Facilities', 'e') . " --\n";
248 while ($frow = sqlFetchArray($fres)) {
249 $facid = $frow['id'];
250 echo " <option value='$facid'";
251 if ($facid == $form_facility) echo " selected";
252 echo ">" . htmlspecialchars($frow['name']) . "\n";
254 echo " <option value='0'";
255 if ($form_facility === '0') echo " selected";
256 echo ">-- " . xl('Unspecified') . " --\n";
257 echo " </select>\n";
259 </td>
260 <td class='label'>
261 <?php xl('DOS','e'); ?>:
262 </td>
263 <td>
264 <input type='text' class='datepicker' name='form_from_date' id="form_from_date" size='10' value='<?php echo $form_from_date; ?>'
265 title='Date of appointments mm/dd/yyyy' >
266 </td>
267 <td class='label'>
268 <?php xl('To','e'); ?>:
269 </td>
270 <td>
271 <input type='text' class='datepicker' name='form_to_date' id="form_to_date" size='10' value='<?php echo $form_to_date; ?>'
272 title='Optional end date mm/dd/yyyy' >
273 </td>
274 </tr>
275 <tr>
276 <td>&nbsp;</td>
277 <td>
278 <input type='checkbox' name='form_details'
279 value='1'<?php if ($_POST['form_details']) echo " checked"; ?>><?php xl('Details','e') ?>
280 </td>
281 </tr>
282 </table>
284 </div>
286 </td>
287 <td align='left' valign='middle' height="100%">
288 <table style='border-left:1px solid; width:100%; height:100%' >
289 <tr>
290 <td>
291 <div style='margin-left:15px'>
292 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
293 <span>
294 <?php xl('Submit','e'); ?>
295 </span>
296 </a>
298 <?php if ($_POST['form_refresh']) { ?>
299 <a href='#' class='css_button' id='printbutton'>
300 <span>
301 <?php xl('Print','e'); ?>
302 </span>
303 </a>
304 <?php } ?>
305 </div>
306 </td>
307 </tr>
308 </table>
309 </td>
310 </tr>
311 </table>
313 </div> <!-- end apptenc_report_parameters -->
315 <?php
316 if ($_POST['form_refresh'] ) {
318 <div id="report_results">
319 <table id='mymaintable'>
321 <thead>
322 <th> &nbsp;<?php xl('Practitioner','e'); ?> </th>
323 <th> &nbsp;<?php xl('Date/Appt','e'); ?> </th>
324 <th> &nbsp;<?php xl('Patient','e'); ?> </th>
325 <th> &nbsp;<?php xl('ID','e'); ?> </th>
326 <th align='right'> <?php xl('Chart','e'); ?>&nbsp; </th>
327 <th align='right'> <?php xl('Encounter','e'); ?>&nbsp; </th>
328 <th align='right'> <?php xl('Charges','e'); ?>&nbsp; </th>
329 <th align='right'> <?php xl('Copays','e'); ?>&nbsp; </th>
330 <th> <?php xl('Billed','e'); ?> </th>
331 <th> &nbsp;<?php xl('Error','e'); ?> </th>
332 </thead>
333 <tbody>
334 <?php
335 if ($res) {
336 $docrow = array('docname' => '', 'charges' => 0, 'copays' => 0, 'encounters' => 0);
338 while ($row = sqlFetchArray($res)) {
339 $patient_id = $row['pid'];
340 $encounter = $row['encounter'];
341 $docname = $row['docname'] ? $row['docname'] : xl('Unknown');
343 if ($docname != $docrow['docname']) {
344 endDoctor($docrow);
347 $errmsg = "";
348 $billed = "Y";
349 $charges = 0;
350 $copays = 0;
351 $gcac_related_visit = false;
353 // Scan the billing items for status and fee total.
355 $query = "SELECT code_type, code, modifier, authorized, billed, fee, justify " .
356 "FROM billing WHERE " .
357 "pid = '$patient_id' AND encounter = '$encounter' AND activity = 1";
358 $bres = sqlStatement($query);
360 while ($brow = sqlFetchArray($bres)) {
361 $code_type = $brow['code_type'];
362 if ($code_types[$code_type]['fee'] && !$brow['billed'])
363 $billed = "";
364 if (!$GLOBALS['simplified_demographics'] && !$brow['authorized'])
365 postError(xl('Needs Auth'));
366 if ($code_types[$code_type]['just']) {
367 if (! $brow['justify']) postError(xl('Needs Justify'));
369 if ($code_types[$code_type]['fee']) {
370 $charges += $brow['fee'];
371 if ($brow['fee'] == 0 && !$GLOBALS['ippf_specific']) postError(xl('Missing Fee'));
372 } else {
373 if ($brow['fee'] != 0) postError(xl('Fee is not allowed'));
376 // Custom logic for IPPF to determine if a GCAC issue applies.
377 if ($GLOBALS['ippf_specific']) {
378 if (!empty($code_types[$code_type]['fee'])) {
379 $query = "SELECT related_code FROM codes WHERE code_type = '" .
380 $code_types[$code_type]['id'] . "' AND " .
381 "code = '" . $brow['code'] . "' AND ";
382 if ($brow['modifier']) {
383 $query .= "modifier = '" . $brow['modifier'] . "'";
384 } else {
385 $query .= "(modifier IS NULL OR modifier = '')";
387 $query .= " LIMIT 1";
388 $tmp = sqlQuery($query);
389 $relcodes = explode(';', $tmp['related_code']);
390 foreach ($relcodes as $codestring) {
391 if ($codestring === '') continue;
392 list($codetype, $code) = explode(':', $codestring);
393 if ($codetype !== 'IPPF') continue;
394 if (preg_match('/^25222/', $code)) $gcac_related_visit = true;
397 } // End IPPF stuff
399 } // end while
401 $copays -= getPatientCopay($patient_id,$encounter);
403 // The following is removed, perhaps temporarily, because gcac reporting
404 // no longer depends on gcac issues. -- Rod 2009-08-11
405 /******************************************************************
406 // More custom code for IPPF. Generates an error message if a
407 // GCAC issue is required but is not linked to this visit.
408 if (!$errmsg && $gcac_related_visit) {
409 $grow = sqlQuery("SELECT l.id, l.title, l.begdate, ie.pid " .
410 "FROM lists AS l " .
411 "LEFT JOIN issue_encounter AS ie ON ie.pid = l.pid AND " .
412 "ie.encounter = '$encounter' AND ie.list_id = l.id " .
413 "WHERE l.pid = '$patient_id' AND " .
414 "l.activity = 1 AND l.type = 'ippf_gcac' " .
415 "ORDER BY ie.pid DESC, l.begdate DESC LIMIT 1");
416 // Note that reverse-ordering by ie.pid is a trick for sorting
417 // issues linked to the encounter (non-null values) first.
418 if (empty($grow['pid'])) { // if there is no linked GCAC issue
419 if (empty($grow)) { // no GCAC issue exists
420 $errmsg = "GCAC issue does not exist";
422 else { // there is one but none is linked
423 $errmsg = "GCAC issue is not linked";
427 ******************************************************************/
428 if ($gcac_related_visit) {
429 $grow = sqlQuery("SELECT COUNT(*) AS count FROM forms " .
430 "WHERE pid = '$patient_id' AND encounter = '$encounter' AND " .
431 "deleted = 0 AND formdir = 'LBFgcac'");
432 if (empty($grow['count'])) { // if there is no gcac form
433 postError(xl('GCAC visit form is missing'));
435 } // end if
436 /*****************************************************************/
438 if (!$billed) postError($GLOBALS['simplified_demographics'] ?
439 xl('Not checked out') : xl('Not billed'));
440 if (!$encounter) postError(xl('No visit'));
442 if (! $charges) $billed = "";
444 $docrow['charges'] += $charges;
445 $docrow['copays'] += $copays;
446 if ($encounter) ++$docrow['encounters'];
448 if ($_POST['form_details']) {
450 <tr>
451 <td>
452 &nbsp;<?php echo ($docname == $docrow['docname']) ? "" : $docname ?>
453 </td>
454 <td>
455 &nbsp;<?php
456 /*****************************************************************
457 if ($form_to_date) {
458 echo $row['pc_eventDate'] . '<br>';
459 echo substr($row['pc_startTime'], 0, 5);
461 *****************************************************************/
462 if (empty($row['pc_eventDate'])) {
463 echo oeFormatShortDate(substr($row['encdate'], 0, 10));
465 else {
466 echo oeFormatShortDate($row['pc_eventDate']) . ' ' . substr($row['pc_startTime'], 0, 5);
469 </td>
470 <td>
471 &nbsp;<?php echo $row['fname'] . " " . $row['lname'] ?>
472 </td>
473 <td>
474 &nbsp;<?php echo $row['pubpid'] ?>
475 </td>
476 <td align='right'>
477 <?php echo $row['pid'] ?>&nbsp;
478 </td>
479 <td align='right'>
480 <?php echo $encounter ?>&nbsp;
481 </td>
482 <td align='right'>
483 <?php bucks($charges) ?>&nbsp;
484 </td>
485 <td align='right'>
486 <?php bucks($copays) ?>&nbsp;
487 </td>
488 <td>
489 <?php echo $billed ?>
490 </td>
491 <td style='color:#cc0000'>
492 <?php echo $errmsg; ?>&nbsp;
493 </td>
494 </tr>
495 <?php
496 } // end of details line
498 $docrow['docname'] = $docname;
499 } // end of row
501 endDoctor($docrow);
503 echo " <tr class='report_totals'>\n";
504 echo " <td colspan='5'>\n";
505 echo " &nbsp;" . xl('Grand Totals') . "\n";
506 echo " </td>\n";
507 echo " <td align='right'>\n";
508 echo " &nbsp;" . $grand_total_encounters . "&nbsp;\n";
509 echo " </td>\n";
510 echo " <td align='right'>\n";
511 echo " &nbsp;"; bucks($grand_total_charges); echo "&nbsp;\n";
512 echo " </td>\n";
513 echo " <td align='right'>\n";
514 echo " &nbsp;"; bucks($grand_total_copays); echo "&nbsp;\n";
515 echo " </td>\n";
516 echo " <td colspan='2'>\n";
517 echo " &nbsp;\n";
518 echo " </td>\n";
519 echo " </tr>\n";
523 </tbody>
524 </table>
525 </div> <!-- end the apptenc_report_results -->
526 <?php } else { ?>
527 <div class='text'>
528 <?php echo xl('Please input search criteria above, and click Submit to view results.', 'e' ); ?>
529 </div>
530 <?php } ?>
532 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
534 </form>
535 <script>
536 <?php if ($alertmsg) { echo " alert('$alertmsg');\n"; } ?>
537 </script>
538 </body>
540 </html>