minor change for prior commit
[openemr.git] / interface / reports / appt_encounter_report.php
blob2d88b17ac55a1c3686c2fd6a7c77f0c4f6cf54f2
1 <?php
2 // Copyright (C) 2005-2016 Rod Roark <rod@sunsetsystems.com>
3 //
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
24 // forms.encounter
25 // billing.pid_encounter
27 require_once("../globals.php");
28 require_once("$srcdir/patient.inc");
29 require_once("$srcdir/formatting.inc.php");
30 require_once("../../custom/code_types.inc.php");
31 require_once("$srcdir/billing.inc");
33 $errmsg = "";
34 $alertmsg = ''; // not used yet but maybe later
35 $grand_total_charges = 0;
36 $grand_total_copays = 0;
37 $grand_total_encounters = 0;
39 function postError($msg) {
40 global $errmsg;
41 if ($errmsg) $errmsg .= '<br />';
42 $errmsg .= $msg;
45 function bucks($amount) {
46 if ($amount) echo oeFormatMoney($amount);
49 function endDoctor(&$docrow) {
50 global $grand_total_charges, $grand_total_copays, $grand_total_encounters;
51 if (!$docrow['docname']) return;
53 echo " <tr class='report_totals'>\n";
54 echo " <td colspan='5'>\n";
55 echo " &nbsp;" . xl('Totals for','','',' ') . $docrow['docname'] . "\n";
56 echo " </td>\n";
57 echo " <td align='right'>\n";
58 echo " &nbsp;" . $docrow['encounters'] . "&nbsp;\n";
59 echo " </td>\n";
60 echo " <td align='right'>\n";
61 echo " &nbsp;"; bucks($docrow['charges']); echo "&nbsp;\n";
62 echo " </td>\n";
63 echo " <td align='right'>\n";
64 echo " &nbsp;"; bucks($docrow['copays']); echo "&nbsp;\n";
65 echo " </td>\n";
66 echo " <td colspan='2'>\n";
67 echo " &nbsp;\n";
68 echo " </td>\n";
69 echo " </tr>\n";
71 $grand_total_charges += $docrow['charges'];
72 $grand_total_copays += $docrow['copays'];
73 $grand_total_encounters += $docrow['encounters'];
75 $docrow['charges'] = 0;
76 $docrow['copays'] = 0;
77 $docrow['encounters'] = 0;
80 $form_facility = isset($_POST['form_facility']) ? $_POST['form_facility'] : '';
81 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
82 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
83 if ($_POST['form_refresh']) {
84 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
85 $form_to_date = fixDate($_POST['form_to_date'], "");
87 // MySQL doesn't grok full outer joins so we do it the hard way.
89 $query = "( " .
90 "SELECT " .
91 "e.pc_eventDate, e.pc_startTime, " .
92 "fe.encounter, fe.date AS encdate, " .
93 "f.authorized, " .
94 "p.fname, p.lname, p.pid, p.pubpid, " .
95 "CONCAT( u.lname, ', ', u.fname ) AS docname " .
96 "FROM openemr_postcalendar_events AS e " .
97 "LEFT OUTER JOIN form_encounter AS fe " .
98 "ON fe.date = e.pc_eventDate AND fe.pid = e.pc_pid " .
99 "LEFT OUTER JOIN forms AS f ON f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
100 "LEFT OUTER JOIN patient_data AS p ON p.pid = e.pc_pid " .
101 // "LEFT OUTER JOIN users AS u ON BINARY u.username = BINARY f.user WHERE ";
102 "LEFT OUTER JOIN users AS u ON u.id = fe.provider_id WHERE ";
103 if ($form_to_date) {
104 $query .= "e.pc_eventDate >= '$form_from_date' AND e.pc_eventDate <= '$form_to_date' ";
105 } else {
106 $query .= "e.pc_eventDate = '$form_from_date' ";
108 if ($form_facility !== '') {
109 $query .= "AND e.pc_facility = '" . add_escape_custom($form_facility) . "' ";
111 // $query .= "AND ( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
112 $query .= "AND e.pc_pid != '' AND e.pc_apptstatus != '?' " .
113 ") UNION ( " .
114 "SELECT " .
115 "e.pc_eventDate, e.pc_startTime, " .
116 "fe.encounter, fe.date AS encdate, " .
117 "f.authorized, " .
118 "p.fname, p.lname, p.pid, p.pubpid, " .
119 "CONCAT( u.lname, ', ', u.fname ) AS docname " .
120 "FROM form_encounter AS fe " .
121 "LEFT OUTER JOIN openemr_postcalendar_events AS e " .
122 "ON fe.date = e.pc_eventDate AND fe.pid = e.pc_pid AND " .
123 // "( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
124 "e.pc_pid != '' AND e.pc_apptstatus != '?' " .
125 "LEFT OUTER JOIN forms AS f ON f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
126 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
127 // "LEFT OUTER JOIN users AS u ON BINARY u.username = BINARY f.user WHERE ";
128 "LEFT OUTER JOIN users AS u ON u.id = fe.provider_id WHERE ";
129 if ($form_to_date) {
130 // $query .= "LEFT(fe.date, 10) >= '$form_from_date' AND LEFT(fe.date, 10) <= '$form_to_date' ";
131 $query .= "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' ";
132 } else {
133 // $query .= "LEFT(fe.date, 10) = '$form_from_date' ";
134 $query .= "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_from_date 23:59:59' ";
136 if ($form_facility !== '') {
137 $query .= "AND fe.facility_id = '" . add_escape_custom($form_facility) . "' ";
139 $query .= ") ORDER BY docname, IFNULL(pc_eventDate, encdate), pc_startTime";
141 $res = sqlStatement($query);
144 <html>
145 <head>
146 <?php html_header_show();?>
147 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
148 <style type="text/css">
150 /* specifically include & exclude from printing */
151 @media print {
152 #report_parameters {
153 visibility: hidden;
154 display: none;
156 #report_parameters_daterange {
157 visibility: visible;
158 display: inline;
160 #report_results table {
161 margin-top: 0px;
165 /* specifically exclude some from the screen */
166 @media screen {
167 #report_parameters_daterange {
168 visibility: hidden;
169 display: none;
173 </style>
174 <title><?php xl('Appointments and Encounters','e'); ?></title>
176 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-9-1/index.js"></script>
177 <script type="text/javascript" src="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></script>
179 <script LANGUAGE="JavaScript">
181 $(document).ready(function() {
182 oeFixedHeaderSetup(document.getElementById('mymaintable'));
183 var win = top.printLogSetup ? top : opener.top;
184 win.printLogSetup(document.getElementById('printbutton'));
187 </script>
189 </head>
191 <body class="body_top">
193 <span class='title'><?php xl('Report','e'); ?> - <?php xl('Appointments and Encounters','e'); ?></span>
195 <div id="report_parameters_daterange">
196 <?php echo date("d F Y", strtotime($form_from_date)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
197 </div>
199 <form method='post' id='theform' action='appt_encounter_report.php'>
201 <div id="report_parameters">
203 <table>
204 <tr>
205 <td width='630px'>
206 <div style='float:left'>
208 <table class='text'>
209 <tr>
210 <td class='label'>
211 <?php xl('Facility','e'); ?>:
212 </td>
213 <td>
214 <?php
215 // Build a drop-down list of facilities.
217 $query = "SELECT id, name FROM facility ORDER BY name";
218 $fres = sqlStatement($query);
219 echo " <select name='form_facility'>\n";
220 echo " <option value=''>-- " . xl('All Facilities', 'e') . " --\n";
221 while ($frow = sqlFetchArray($fres)) {
222 $facid = $frow['id'];
223 echo " <option value='$facid'";
224 if ($facid == $form_facility) echo " selected";
225 echo ">" . htmlspecialchars($frow['name']) . "\n";
227 echo " <option value='0'";
228 if ($form_facility === '0') echo " selected";
229 echo ">-- " . xl('Unspecified') . " --\n";
230 echo " </select>\n";
232 </td>
233 <td class='label'>
234 <?php xl('DOS','e'); ?>:
235 </td>
236 <td>
237 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo $form_from_date; ?>'
238 title='Date of appointments mm/dd/yyyy' >
239 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
240 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
241 title='<?php xl('Click here to choose a date','e'); ?>'>
242 </td>
243 <td class='label'>
244 <?php xl('To','e'); ?>:
245 </td>
246 <td>
247 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo $form_to_date; ?>'
248 title='Optional end date mm/dd/yyyy' >
249 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
250 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
251 title='<?php xl('Click here to choose a date','e'); ?>'>
252 </td>
253 </tr>
254 <tr>
255 <td>&nbsp;</td>
256 <td>
257 <input type='checkbox' name='form_details'
258 value='1'<?php if ($_POST['form_details']) echo " checked"; ?>><?php xl('Details','e') ?>
259 </td>
260 </tr>
261 </table>
263 </div>
265 </td>
266 <td align='left' valign='middle' height="100%">
267 <table style='border-left:1px solid; width:100%; height:100%' >
268 <tr>
269 <td>
270 <div style='margin-left:15px'>
271 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
272 <span>
273 <?php xl('Submit','e'); ?>
274 </span>
275 </a>
277 <?php if ($_POST['form_refresh']) { ?>
278 <a href='#' class='css_button' id='printbutton'>
279 <span>
280 <?php xl('Print','e'); ?>
281 </span>
282 </a>
283 <?php } ?>
284 </div>
285 </td>
286 </tr>
287 </table>
288 </td>
289 </tr>
290 </table>
292 </div> <!-- end apptenc_report_parameters -->
294 <?php
295 if ($_POST['form_refresh'] ) {
297 <div id="report_results">
298 <table id='mymaintable'>
300 <thead>
301 <th> &nbsp;<?php xl('Practitioner','e'); ?> </th>
302 <th> &nbsp;<?php xl('Date/Appt','e'); ?> </th>
303 <th> &nbsp;<?php xl('Patient','e'); ?> </th>
304 <th> &nbsp;<?php xl('ID','e'); ?> </th>
305 <th align='right'> <?php xl('Chart','e'); ?>&nbsp; </th>
306 <th align='right'> <?php xl('Encounter','e'); ?>&nbsp; </th>
307 <th align='right'> <?php xl('Charges','e'); ?>&nbsp; </th>
308 <th align='right'> <?php xl('Copays','e'); ?>&nbsp; </th>
309 <th> <?php xl('Billed','e'); ?> </th>
310 <th> &nbsp;<?php xl('Error','e'); ?> </th>
311 </thead>
312 <tbody>
313 <?php
314 if ($res) {
315 $docrow = array('docname' => '', 'charges' => 0, 'copays' => 0, 'encounters' => 0);
317 while ($row = sqlFetchArray($res)) {
318 $patient_id = $row['pid'];
319 $encounter = $row['encounter'];
320 $docname = $row['docname'] ? $row['docname'] : xl('Unknown');
322 if ($docname != $docrow['docname']) {
323 endDoctor($docrow);
326 $errmsg = "";
327 $billed = "Y";
328 $charges = 0;
329 $copays = 0;
330 $gcac_related_visit = false;
332 // Scan the billing items for status and fee total.
334 $query = "SELECT code_type, code, modifier, authorized, billed, fee, justify " .
335 "FROM billing WHERE " .
336 "pid = '$patient_id' AND encounter = '$encounter' AND activity = 1";
337 $bres = sqlStatement($query);
339 while ($brow = sqlFetchArray($bres)) {
340 $code_type = $brow['code_type'];
341 if ($code_types[$code_type]['fee'] && !$brow['billed'])
342 $billed = "";
343 if (!$GLOBALS['simplified_demographics'] && !$brow['authorized'])
344 postError(xl('Needs Auth'));
345 if ($code_types[$code_type]['just']) {
346 if (! $brow['justify']) postError(xl('Needs Justify'));
348 if ($code_types[$code_type]['fee']) {
349 $charges += $brow['fee'];
350 if ($brow['fee'] == 0 && !$GLOBALS['ippf_specific']) postError(xl('Missing Fee'));
351 } else {
352 if ($brow['fee'] != 0) postError(xl('Fee is not allowed'));
355 // Custom logic for IPPF to determine if a GCAC issue applies.
356 if ($GLOBALS['ippf_specific']) {
357 if (!empty($code_types[$code_type]['fee'])) {
358 $query = "SELECT related_code FROM codes WHERE code_type = '" .
359 $code_types[$code_type]['id'] . "' AND " .
360 "code = '" . $brow['code'] . "' AND ";
361 if ($brow['modifier']) {
362 $query .= "modifier = '" . $brow['modifier'] . "'";
363 } else {
364 $query .= "(modifier IS NULL OR modifier = '')";
366 $query .= " LIMIT 1";
367 $tmp = sqlQuery($query);
368 $relcodes = explode(';', $tmp['related_code']);
369 foreach ($relcodes as $codestring) {
370 if ($codestring === '') continue;
371 list($codetype, $code) = explode(':', $codestring);
372 if ($codetype !== 'IPPF') continue;
373 if (preg_match('/^25222/', $code)) $gcac_related_visit = true;
376 } // End IPPF stuff
378 } // end while
380 $copays -= getPatientCopay($patient_id,$encounter);
382 // The following is removed, perhaps temporarily, because gcac reporting
383 // no longer depends on gcac issues. -- Rod 2009-08-11
384 /******************************************************************
385 // More custom code for IPPF. Generates an error message if a
386 // GCAC issue is required but is not linked to this visit.
387 if (!$errmsg && $gcac_related_visit) {
388 $grow = sqlQuery("SELECT l.id, l.title, l.begdate, ie.pid " .
389 "FROM lists AS l " .
390 "LEFT JOIN issue_encounter AS ie ON ie.pid = l.pid AND " .
391 "ie.encounter = '$encounter' AND ie.list_id = l.id " .
392 "WHERE l.pid = '$patient_id' AND " .
393 "l.activity = 1 AND l.type = 'ippf_gcac' " .
394 "ORDER BY ie.pid DESC, l.begdate DESC LIMIT 1");
395 // Note that reverse-ordering by ie.pid is a trick for sorting
396 // issues linked to the encounter (non-null values) first.
397 if (empty($grow['pid'])) { // if there is no linked GCAC issue
398 if (empty($grow)) { // no GCAC issue exists
399 $errmsg = "GCAC issue does not exist";
401 else { // there is one but none is linked
402 $errmsg = "GCAC issue is not linked";
406 ******************************************************************/
407 if ($gcac_related_visit) {
408 $grow = sqlQuery("SELECT COUNT(*) AS count FROM forms " .
409 "WHERE pid = '$patient_id' AND encounter = '$encounter' AND " .
410 "deleted = 0 AND formdir = 'LBFgcac'");
411 if (empty($grow['count'])) { // if there is no gcac form
412 postError(xl('GCAC visit form is missing'));
414 } // end if
415 /*****************************************************************/
417 if (!$billed) postError($GLOBALS['simplified_demographics'] ?
418 xl('Not checked out') : xl('Not billed'));
419 if (!$encounter) postError(xl('No visit'));
421 if (! $charges) $billed = "";
423 $docrow['charges'] += $charges;
424 $docrow['copays'] += $copays;
425 if ($encounter) ++$docrow['encounters'];
427 if ($_POST['form_details']) {
429 <tr>
430 <td>
431 &nbsp;<?php echo ($docname == $docrow['docname']) ? "" : $docname ?>
432 </td>
433 <td>
434 &nbsp;<?php
435 /*****************************************************************
436 if ($form_to_date) {
437 echo $row['pc_eventDate'] . '<br>';
438 echo substr($row['pc_startTime'], 0, 5);
440 *****************************************************************/
441 if (empty($row['pc_eventDate'])) {
442 echo oeFormatShortDate(substr($row['encdate'], 0, 10));
444 else {
445 echo oeFormatShortDate($row['pc_eventDate']) . ' ' . substr($row['pc_startTime'], 0, 5);
448 </td>
449 <td>
450 &nbsp;<?php echo $row['fname'] . " " . $row['lname'] ?>
451 </td>
452 <td>
453 &nbsp;<?php echo $row['pubpid'] ?>
454 </td>
455 <td align='right'>
456 <?php echo $row['pid'] ?>&nbsp;
457 </td>
458 <td align='right'>
459 <?php echo $encounter ?>&nbsp;
460 </td>
461 <td align='right'>
462 <?php bucks($charges) ?>&nbsp;
463 </td>
464 <td align='right'>
465 <?php bucks($copays) ?>&nbsp;
466 </td>
467 <td>
468 <?php echo $billed ?>
469 </td>
470 <td style='color:#cc0000'>
471 <?php echo $errmsg; ?>&nbsp;
472 </td>
473 </tr>
474 <?php
475 } // end of details line
477 $docrow['docname'] = $docname;
478 } // end of row
480 endDoctor($docrow);
482 echo " <tr class='report_totals'>\n";
483 echo " <td colspan='5'>\n";
484 echo " &nbsp;" . xl('Grand Totals') . "\n";
485 echo " </td>\n";
486 echo " <td align='right'>\n";
487 echo " &nbsp;" . $grand_total_encounters . "&nbsp;\n";
488 echo " </td>\n";
489 echo " <td align='right'>\n";
490 echo " &nbsp;"; bucks($grand_total_charges); echo "&nbsp;\n";
491 echo " </td>\n";
492 echo " <td align='right'>\n";
493 echo " &nbsp;"; bucks($grand_total_copays); echo "&nbsp;\n";
494 echo " </td>\n";
495 echo " <td colspan='2'>\n";
496 echo " &nbsp;\n";
497 echo " </td>\n";
498 echo " </tr>\n";
502 </tbody>
503 </table>
504 </div> <!-- end the apptenc_report_results -->
505 <?php } else { ?>
506 <div class='text'>
507 <?php echo xl('Please input search criteria above, and click Submit to view results.', 'e' ); ?>
508 </div>
509 <?php } ?>
511 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
513 </form>
514 <script>
515 <?php if ($alertmsg) { echo " alert('$alertmsg');\n"; } ?>
516 </script>
517 </body>
519 <!-- stuff for the popup calendar -->
520 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
521 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
522 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
523 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
525 <script language="Javascript">
526 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
527 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
528 </script>
530 </html>