Fixed bug: https://sourceforge.net/p/openemr/bugs/416/
[openemr.git] / interface / reports / encounters_report.php
blob5ebf797d8062ad0af59e08cddb48a160545261f5
1 <?php
2 /*
3 * Encounters report. (/interface/reports/encounters_report.php
4 *
6 * This report shows past encounters with filtering and sorting,
7 * Added filtering to show encounters not e-signed, encounters e-signed and forms e-signed.
8 *
9 * Copyright (C) 2015 Terry Hill <terry@lillysystems.com>
10 * Copyright (C) 2007-2010 Rod Roark <rod@sunsetsystems.com>
12 * LICENSE: This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 3
15 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
23 * @package OpenEMR
24 * @author Terry Hill <terry@lilysystems.com>
25 * @author Rod Roark <rod@sunsetsystems.com>
26 * @link http://www.open-emr.org
30 require_once("../globals.php");
31 require_once("$srcdir/forms.inc");
32 require_once("$srcdir/billing.inc");
33 require_once("$srcdir/patient.inc");
34 require_once("$srcdir/formatting.inc.php");
35 require_once "$srcdir/options.inc.php";
36 require_once "$srcdir/formdata.inc.php";
38 $alertmsg = ''; // not used yet but maybe later
40 // For each sorting option, specify the ORDER BY argument.
42 $ORDERHASH = array(
43 'doctor' => 'lower(u.lname), lower(u.fname), fe.date',
44 'patient' => 'lower(p.lname), lower(p.fname), fe.date',
45 'pubpid' => 'lower(p.pubpid), fe.date',
46 'time' => 'fe.date, lower(u.lname), lower(u.fname)',
47 'encounter' => 'fe.encounter, fe.date, lower(u.lname), lower(u.fname)',
50 function bucks($amount) {
51 if ($amount) printf("%.2f", $amount);
54 function show_doc_total($lastdocname, $doc_encounters) {
55 if ($lastdocname) {
56 echo " <tr>\n";
57 echo " <td class='detail'>$lastdocname</td>\n";
58 echo " <td class='detail' align='right'>$doc_encounters</td>\n";
59 echo " </tr>\n";
63 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
64 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
65 $form_provider = $_POST['form_provider'];
66 $form_facility = $_POST['form_facility'];
67 $form_details = $_POST['form_details'] ? true : false;
68 $form_new_patients = $_POST['form_new_patients'] ? true : false;
69 $form_esigned = $_POST['form_esigned'] ? true : false;
70 $form_not_esigned = $_POST['form_not_esigned'] ? true : false;
71 $form_encounter_esigned = $_POST['form_encounter_esigned'] ? true : false;
73 $form_orderby = $ORDERHASH[$_REQUEST['form_orderby']] ?
74 $_REQUEST['form_orderby'] : 'doctor';
75 $orderby = $ORDERHASH[$form_orderby];
77 // Get the info.
79 $esign_fields = '';
80 $esign_joins = '';
81 if ($form_encounter_esigned) {
82 $esign_fields = ", es.table, es.tid ";
83 $esign_joins = "LEFT OUTER JOIN esign_signatures AS es ON es.tid = fe.encounter ";
85 if ($form_esigned) {
86 $esign_fields = ", es.table, es.tid ";
87 $esign_joins = "LEFT OUTER JOIN esign_signatures AS es ON es.tid = fe.encounter ";
89 if ($form_not_esigned) {
90 $esign_fields = ", es.table, es.tid ";
91 $esign_joins = "LEFT JOIN esign_signatures AS es on es.tid = fe.encounter ";
94 $query = "SELECT " .
95 "fe.encounter, fe.date, fe.reason, " .
96 "f.formdir, f.form_name, " .
97 "p.fname, p.mname, p.lname, p.pid, p.pubpid, " .
98 "u.lname AS ulname, u.fname AS ufname, u.mname AS umname " .
99 "$esign_fields" .
100 "FROM ( form_encounter AS fe, forms AS f ) " .
101 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
102 "LEFT JOIN users AS u ON u.id = fe.provider_id " .
103 "$esign_joins" .
104 "WHERE f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' ";
105 if ($form_to_date) {
106 $query .= "AND fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' ";
107 } else {
108 $query .= "AND fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_from_date 23:59:59' ";
110 if ($form_provider) {
111 $query .= "AND fe.provider_id = '$form_provider' ";
113 if ($form_facility) {
114 $query .= "AND fe.facility_id = '$form_facility' ";
116 if ($form_new_patients) {
117 $query .= "AND fe.date = (SELECT MIN(fe2.date) FROM form_encounter AS fe2 WHERE fe2.pid = fe.pid) ";
119 if ($form_encounter_esigned) {
120 $query .= "AND es.tid = fe.encounter AND es.table = 'form_encounter' ";
122 if ($form_esigned) {
123 $query .= "AND es.tid = fe.encounter ";
125 if ($form_not_esigned) {
126 $query .= "AND es.tid IS NULL ";
128 $query .= "ORDER BY $orderby";
130 $res = sqlStatement($query);
132 <html>
133 <head>
134 <?php html_header_show();?>
135 <title><?php echo xlt('Encounters Report'); ?></title>
137 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
139 <link rel=stylesheet href="<?php echo $css_header;?>" type="text/css">
140 <style type="text/css">
142 /* specifically include & exclude from printing */
143 @media print {
144 #report_parameters {
145 visibility: hidden;
146 display: none;
148 #report_parameters_daterange {
149 visibility: visible;
150 display: inline;
152 #report_results table {
153 margin-top: 0px;
157 /* specifically exclude some from the screen */
158 @media screen {
159 #report_parameters_daterange {
160 visibility: hidden;
161 display: none;
165 </style>
167 <script type="text/javascript" src="../../library/textformat.js"></script>
168 <script type="text/javascript" src="../../library/dialog.js"></script>
169 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
170 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
171 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
172 <script type="text/javascript" src="../../library/js/jquery.1.3.2.js"></script>
174 <script LANGUAGE="JavaScript">
176 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
178 $(document).ready(function() {
179 var win = top.printLogSetup ? top : opener.top;
180 win.printLogSetup(document.getElementById('printbutton'));
183 function dosort(orderby) {
184 var f = document.forms[0];
185 f.form_orderby.value = orderby;
186 f.submit();
187 return false;
190 function refreshme() {
191 document.forms[0].submit();
194 </script>
196 </head>
197 <body class="body_top">
198 <!-- Required for the popup date selectors -->
199 <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
201 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Encounters'); ?></span>
203 <div id="report_parameters_daterange">
204 <?php echo date("d F Y", strtotime($form_from_date)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
205 </div>
207 <form method='post' name='theform' id='theform' action='encounters_report.php'>
209 <div id="report_parameters">
210 <table>
211 <tr>
212 <td width='550px'>
213 <div style='float:left'>
215 <table class='text'>
216 <tr>
217 <td class='label'>
218 <?php echo xlt('Facility'); ?>:
219 </td>
220 <td>
221 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
222 </td>
223 <td class='label'>
224 <?php echo xlt('Provider'); ?>:
225 </td>
226 <td>
227 <?php
229 // Build a drop-down list of providers.
232 $query = "SELECT id, lname, fname FROM users WHERE ".
233 "authorized = 1 $provider_facility_filter ORDER BY lname, fname"; //(CHEMED) facility filter
235 $ures = sqlStatement($query);
237 echo " <select name='form_provider'>\n";
238 echo " <option value=''>-- " . xlt('All') . " --\n";
240 while ($urow = sqlFetchArray($ures)) {
241 $provid = $urow['id'];
242 echo " <option value='" . attr($provid) . "'";
243 if ($provid == $_POST['form_provider']) echo " selected";
244 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
247 echo " </select>\n";
250 </td>
251 <td>
252 <label><input type='checkbox' name='form_new_patients' title='First-time visits only'<?php if ($form_new_patients) echo ' checked'; ?>>
253 <?php echo xlt('New'); ?></label>
254 </td>
255 <td>
256 <label><input type='checkbox' name='form_esigned'<?php if ($form_esigned) echo ' checked'; ?>>
257 <?php echo xlt('Forms Esigned'); ?></label>
258 </td>
259 <td>
260 <label><input type='checkbox' name='form_encounter_esigned'<?php if ($form_encounter_esigned) echo ' checked'; ?>>
261 <?php echo xlt('Encounter Esigned'); ?></label>
262 </td>
263 </tr>
264 <tr>
265 <td class='label'>
266 <?php echo xlt('From'); ?>:
267 </td>
268 <td>
269 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo attr($form_from_date) ?>'
270 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
271 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
272 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
273 title='<?php echo xla('Click here to choose a date'); ?>'>
274 </td>
275 <td class='label'>
276 <?php echo xlt('To'); ?>:
277 </td>
278 <td>
279 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo attr($form_to_date) ?>'
280 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
281 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
282 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
283 title='<?php echo xla('Click here to choose a date'); ?>'>
284 </td>
285 <td>
286 <label><input type='checkbox' name='form_details'<?php if ($form_details) echo ' checked'; ?>>
287 <?php echo xlt('Details'); ?></label>
288 </td>
289 <td>
290 <label><input type='checkbox' name='form_not_esigned'<?php if ($form_not_esigned) echo ' checked'; ?>>
291 <?php echo xlt('Not Esigned'); ?></label>
292 </td>
293 </tr>
294 </table>
296 </div>
298 </td>
299 <td align='left' valign='middle' height="100%">
300 <table style='border-left:1px solid; width:100%; height:100%' >
301 <tr>
302 <td>
303 <div style='margin-left:15px'>
304 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
305 <span>
306 <?php echo xlt('Submit'); ?>
307 </span>
308 </a>
310 <?php if ($_POST['form_refresh'] || $_POST['form_orderby'] ) { ?>
311 <a href='#' class='css_button' id='printbutton'>
312 <span>
313 <?php echo xlt('Print'); ?>
314 </span>
315 </a>
316 <?php } ?>
317 </div>
318 </td>
319 </tr>
320 </table>
321 </td>
322 </tr>
323 </table>
325 </div> <!-- end report_parameters -->
327 <?php
328 if ($_POST['form_refresh'] || $_POST['form_orderby']) {
330 <div id="report_results">
331 <table>
333 <thead>
334 <?php if ($form_details) { ?>
335 <th>
336 <a href="nojs.php" onclick="return dosort('doctor')"
337 <?php if ($form_orderby == "doctor") echo " style=\"color:#00cc00\"" ?>><?php echo xlt('Provider'); ?> </a>
338 </th>
339 <th>
340 <a href="nojs.php" onclick="return dosort('time')"
341 <?php if ($form_orderby == "time") echo " style=\"color:#00cc00\"" ?>><?php echo xlt('Date'); ?></a>
342 </th>
343 <th>
344 <a href="nojs.php" onclick="return dosort('patient')"
345 <?php if ($form_orderby == "patient") echo " style=\"color:#00cc00\"" ?>><?php echo xlt('Patient'); ?></a>
346 </th>
347 <th>
348 <a href="nojs.php" onclick="return dosort('pubpid')"
349 <?php if ($form_orderby == "pubpid") echo " style=\"color:#00cc00\"" ?>><?php echo xlt('ID'); ?></a>
350 </th>
351 <th>
352 <?php echo xlt('Status'); ?>
353 </th>
354 <th>
355 <?php echo xlt('Encounter'); ?>
356 </th>
357 <th>
358 <a href="nojs.php" onclick="return dosort('encounter')"
359 <?php if ($form_orderby == "encounter") echo " style=\"color:#00cc00\"" ?>><?php echo xlt('Encounter Number'); ?></a>
360 </th>
361 <th>
362 <?php echo xlt('Form'); ?>
363 </th>
364 <th>
365 <?php echo xlt('Coding'); ?>
366 </th>
367 <?php } else { ?>
368 <th><?php echo xlt('Provider'); ?></td>
369 <th><?php echo xlt('Encounters'); ?></td>
370 <?php } ?>
371 </thead>
372 <tbody>
373 <?php
374 if ($res) {
375 $lastdocname = "";
376 $doc_encounters = 0;
377 while ($row = sqlFetchArray($res)) {
378 $patient_id = $row['pid'];
380 $docname = '';
381 if (!empty($row['ulname']) || !empty($row['ufname'])) {
382 $docname = $row['ulname'];
383 if (!empty($row['ufname']) || !empty($row['umname']))
384 $docname .= ', ' . $row['ufname'] . ' ' . $row['umname'];
387 $errmsg = "";
388 if ($form_details) {
389 // Fetch all other forms for this encounter.
390 $encnames = '';
391 $encarr = getFormByEncounter($patient_id, $row['encounter'],
392 "formdir, user, form_name, form_id");
393 if($encarr!='') {
394 foreach ($encarr as $enc) {
395 if ($enc['formdir'] == 'newpatient') continue;
396 if ($encnames) $encnames .= '<br />';
397 $encnames .= text($enc['form_name']); // need to html escape it here for output below
401 // Fetch coding and compute billing status.
402 $coded = "";
403 $billed_count = 0;
404 $unbilled_count = 0;
405 if ($billres = getBillingByEncounter($row['pid'], $row['encounter'],
406 "code_type, code, code_text, billed"))
408 foreach ($billres as $billrow) {
409 // $title = addslashes($billrow['code_text']);
410 if ($billrow['code_type'] != 'COPAY' && $billrow['code_type'] != 'TAX') {
411 $coded .= $billrow['code'] . ', ';
412 if ($billrow['billed']) ++$billed_count; else ++$unbilled_count;
415 $coded = substr($coded, 0, strlen($coded) - 2);
418 // Figure product sales into billing status.
419 $sres = sqlStatement("SELECT billed FROM drug_sales " .
420 "WHERE pid = '{$row['pid']}' AND encounter = '{$row['encounter']}'");
421 while ($srow = sqlFetchArray($sres)) {
422 if ($srow['billed']) ++$billed_count; else ++$unbilled_count;
425 // Compute billing status.
426 if ($billed_count && $unbilled_count) $status = xl('Mixed' );
427 else if ($billed_count ) $status = xl('Closed');
428 else if ($unbilled_count ) $status = xl('Open' );
429 else $status = xl('Empty' );
431 <tr bgcolor='<?php echo $bgcolor ?>'>
432 <td>
433 <?php echo ($docname == $lastdocname) ? "" : text($docname) ?>&nbsp;
434 </td>
435 <td>
436 <?php echo text(oeFormatShortDate(substr($row['date'], 0, 10))) ?>&nbsp;
437 </td>
438 <td>
439 <?php echo text($row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname']); ?>&nbsp;
440 </td>
441 <td>
442 <?php echo text($row['pubpid']); ?>&nbsp;
443 </td>
444 <td>
445 <?php echo text($status); ?>&nbsp;
446 </td>
447 <td>
448 <?php echo text($row['reason']); ?>&nbsp;
449 </td>
450 <td>
451 <?php echo text($row['encounter']); ?>&nbsp;
452 </td>
453 <td>
454 <?php echo $encnames; //since this variable contains html, have already html escaped it above ?>&nbsp;
455 </td>
456 <td>
457 <?php echo text($coded); ?>
458 </td>
459 </tr>
460 <?php
461 } else {
462 if ($docname != $lastdocname) {
463 show_doc_total($lastdocname, $doc_encounters);
464 $doc_encounters = 0;
466 ++$doc_encounters;
468 $lastdocname = $docname;
471 if (!$form_details) show_doc_total($lastdocname, $doc_encounters);
474 </tbody>
475 </table>
476 </div> <!-- end encresults -->
477 <?php } else { ?>
478 <div class='text'>
479 <?php echo xlt('Please input search criteria above, and click Submit to view results.' ); ?>
480 </div>
481 <?php } ?>
483 <input type="hidden" name="form_orderby" value="<?php echo attr($form_orderby) ?>" />
484 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
486 </form>
487 </body>
489 <script language='JavaScript'>
490 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
491 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
493 <?php if ($alertmsg) { echo " alert('$alertmsg');\n"; } ?>
495 </script>
497 </html>