internationalization bug fix - translate submit button
[openemr.git] / interface / reports / appt_encounter_report.php
blob76f2a7e38a44ead99021a909146f8019f23dff4a
1 <?php
2 // Copyright (C) 2005-2009 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 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 " &nbsp;" . xl('Totals for','','',' ') . $docrow['docname'] . "\n";
47 echo " </td>\n";
48 echo " <td align='right'>\n";
49 echo " &nbsp;" . $docrow['encounters'] . "&nbsp;\n";
50 echo " </td>\n";
51 echo " <td align='right'>\n";
52 echo " &nbsp;"; bucks($docrow['charges']); echo "&nbsp;\n";
53 echo " </td>\n";
54 echo " <td align='right'>\n";
55 echo " &nbsp;"; bucks($docrow['copays']); echo "&nbsp;\n";
56 echo " </td>\n";
57 echo " <td colspan='2'>\n";
58 echo " &nbsp;\n";
59 echo " </td>\n";
60 echo " </tr>\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.
80 $query = "( " .
81 "SELECT " .
82 "e.pc_eventDate, e.pc_startTime, " .
83 "fe.encounter, " .
84 "f.authorized, " .
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 ";
94 if ($form_to_date) {
95 $query .= "e.pc_eventDate >= '$form_from_date' AND e.pc_eventDate <= '$form_to_date' ";
96 } else {
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 != '?' " .
104 ") UNION ( " .
105 "SELECT " .
106 "e.pc_eventDate, e.pc_startTime, " .
107 "fe.encounter, " .
108 "f.authorized, " .
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 ";
119 if ($form_to_date) {
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' ";
122 } else {
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);
134 <html>
135 <head>
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 */
141 @media print {
142 #apptencreport_parameters {
143 visibility: hidden;
144 display: none;
146 #apptencreport_parameters_daterange {
147 visibility: visible;
148 display: inline;
152 /* specifically exclude some from the screen */
153 @media screen {
154 #apptencreport_parameters_daterange {
155 visibility: hidden;
156 display: none;
160 #apptencreport_parameters {
161 width: 100%;
162 margin: 10px;
163 text-align: center;
164 background-color: #ddf;
166 #apptencreport_parameters table {
167 text-align: center;
168 border: none;
169 width: 100%;
170 border-collapse: collapse;
172 #apptencreport_parameters table td {
173 padding: 3px;
176 #apptencreport_results {
177 width: 100%;
178 margin-top: 10px;
180 #apptencreport_results table {
181 border: 1px solid black;
182 width: 98%;
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 {
193 padding: 1px;
194 margin: 2px;
195 border-bottom: 1px solid #eee;
197 .apptencreport_totals td {
198 background-color: #77ff77;
199 font-weight: bold;
201 </style>
202 <title><?php xl('Appointments and Encounters','e'); ?></title>
203 </head>
205 <body class="body_top">
206 <center>
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)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
211 </div>
213 <div id="apptencreport_parameters">
214 <form method='post' action='appt_encounter_report.php'>
215 <table>
216 <tr>
217 <td>
218 <?php xl('Facility','e'); ?>:
219 <?php
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";
235 echo " </select>\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'); ?>'>
243 &nbsp;
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'); ?>'>
250 &nbsp;
251 <input type='checkbox' name='form_details'
252 value='1'<?php if ($_POST['form_details']) echo " checked"; ?>><?php xl('Details','e') ?>
253 &nbsp;
254 <input type='submit' name='form_search' value='<?php xl('Search','e'); ?>'>
255 &nbsp;
256 <input type='button' value='<?php xl('Print','e'); ?>' onclick='window.print()' />
257 </td>
258 </tr>
259 </table>
260 </div> <!-- end apptenc_report_parameters -->
262 <div id="apptencreport_results">
263 <table>
265 <thead>
266 <th> &nbsp;<?php xl('Practitioner','e'); ?> </th>
267 <th> &nbsp;<?php xl('Time','e'); ?> </th>
268 <th> &nbsp;<?php xl('Patient','e'); ?> </th>
269 <th> &nbsp;<?php xl('ID','e'); ?> </th>
270 <th> <?php xl('Chart','e'); ?>&nbsp; </th>
271 <th> <?php xl('Encounter','e'); ?>&nbsp; </th>
272 <th> <?php xl('Charges','e'); ?>&nbsp; </th>
273 <th> <?php xl('Copays','e'); ?>&nbsp; </th>
274 <th> <?php xl('Billed','e'); ?> </th>
275 <th> &nbsp;<?php xl('Error','e'); ?> </th>
276 </thead>
277 <tbody>
278 <?php
279 if ($res) {
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']) {
288 endDoctor($docrow);
291 $billed = "Y";
292 $errmsg = "";
293 $charges = 0;
294 $copays = 0;
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'])
307 $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";
319 } else {
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'] . "'";
331 } else {
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;
344 } // End IPPF stuff
346 } // end while
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 " .
352 "FROM lists AS l " .
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";
370 if (!$errmsg) {
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']) {
384 <tr>
385 <td>
386 &nbsp;<?php echo ($docname == $docrow['docname']) ? "" : $docname ?>
387 </td>
388 <td>
389 &nbsp;<?php
390 if ($form_to_date) {
391 echo $row['pc_eventDate'] . '<br>';
392 echo substr($row['pc_startTime'], 0, 5);
395 </td>
396 <td>
397 &nbsp;<?php echo $row['fname'] . " " . $row['lname'] ?>
398 </td>
399 <td>
400 &nbsp;<?php echo $row['pubpid'] ?>
401 </td>
402 <td align='right'>
403 <?php echo $row['pid'] ?>&nbsp;
404 </td>
405 <td align='right'>
406 <?php echo $encounter ?>&nbsp;
407 </td>
408 <td align='right'>
409 <?php bucks($charges) ?>&nbsp;
410 </td>
411 <td align='right'>
412 <?php bucks($copays) ?>&nbsp;
413 </td>
414 <td>
415 <?php echo $billed ?>
416 </td>
417 <td style='color:#cc0000'>
418 &nbsp;<?php echo xl($errmsg); ?>
419 </td>
420 </tr>
421 <?php
422 } // end of details line
424 $docrow['docname'] = $docname;
425 } // end of row
427 endDoctor($docrow);
429 echo " <tr class='apptencreport_totals'>\n";
430 echo " <td colspan='5'>\n";
431 echo " &nbsp;" . xl('Grand Totals') . "\n";
432 echo " </td>\n";
433 echo " <td align='right'>\n";
434 echo " &nbsp;" . $grand_total_encounters . "&nbsp;\n";
435 echo " </td>\n";
436 echo " <td align='right'>\n";
437 echo " &nbsp;"; bucks($grand_total_charges); echo "&nbsp;\n";
438 echo " </td>\n";
439 echo " <td align='right'>\n";
440 echo " &nbsp;"; bucks($grand_total_copays); echo "&nbsp;\n";
441 echo " </td>\n";
442 echo " <td colspan='2'>\n";
443 echo " &nbsp;\n";
444 echo " </td>\n";
445 echo " </tr>\n";
449 </tbody>
450 </table>
451 </div> <!-- end the apptenc_report_results -->
453 </form>
454 </center>
455 <script>
456 <?php if ($alertmsg) { echo " alert('$alertmsg');\n"; } ?>
457 </script>
458 </body>
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"});
468 </script>
470 </html>