minor clean up of prior commit
[openemr.git] / interface / reports / encounters_report.php
blob92910ce3cdd8b63e9a62d057c0f0be28ec9d237c
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 function dosort(orderby) {
179 var f = document.forms[0];
180 f.form_orderby.value = orderby;
181 f.submit();
182 return false;
185 function refreshme() {
186 document.forms[0].submit();
189 </script>
191 </head>
192 <body class="body_top">
193 <!-- Required for the popup date selectors -->
194 <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
196 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Encounters'); ?></span>
198 <div id="report_parameters_daterange">
199 <?php echo date("d F Y", strtotime($form_from_date)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
200 </div>
202 <form method='post' name='theform' id='theform' action='encounters_report.php'>
204 <div id="report_parameters">
205 <table>
206 <tr>
207 <td width='550px'>
208 <div style='float:left'>
210 <table class='text'>
211 <tr>
212 <td class='label'>
213 <?php echo xlt('Facility'); ?>:
214 </td>
215 <td>
216 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
217 </td>
218 <td class='label'>
219 <?php echo xlt('Provider'); ?>:
220 </td>
221 <td>
222 <?php
224 // Build a drop-down list of providers.
227 $query = "SELECT id, lname, fname FROM users WHERE ".
228 "authorized = 1 $provider_facility_filter ORDER BY lname, fname"; //(CHEMED) facility filter
230 $ures = sqlStatement($query);
232 echo " <select name='form_provider'>\n";
233 echo " <option value=''>-- " . xlt('All') . " --\n";
235 while ($urow = sqlFetchArray($ures)) {
236 $provid = $urow['id'];
237 echo " <option value='" . attr($provid) . "'";
238 if ($provid == $_POST['form_provider']) echo " selected";
239 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
242 echo " </select>\n";
245 </td>
246 <td>
247 <label><input type='checkbox' name='form_new_patients' title='First-time visits only'<?php if ($form_new_patients) echo ' checked'; ?>>
248 <?php echo xlt('New'); ?></label>
249 </td>
250 <td>
251 <label><input type='checkbox' name='form_esigned'<?php if ($form_esigned) echo ' checked'; ?>>
252 <?php echo xlt('Forms Esigned'); ?></label>
253 </td>
254 <td>
255 <label><input type='checkbox' name='form_encounter_esigned'<?php if ($form_encounter_esigned) echo ' checked'; ?>>
256 <?php echo xlt('Encounter Esigned'); ?></label>
257 </td>
258 </tr>
259 <tr>
260 <td class='label'>
261 <?php echo xlt('From'); ?>:
262 </td>
263 <td>
264 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo attr($form_from_date) ?>'
265 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
266 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
267 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
268 title='<?php echo xla('Click here to choose a date'); ?>'>
269 </td>
270 <td class='label'>
271 <?php echo xlt('To'); ?>:
272 </td>
273 <td>
274 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo attr($form_to_date) ?>'
275 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
276 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
277 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
278 title='<?php echo xla('Click here to choose a date'); ?>'>
279 </td>
280 <td>
281 <label><input type='checkbox' name='form_details'<?php if ($form_details) echo ' checked'; ?>>
282 <?php echo xlt('Details'); ?></label>
283 </td>
284 <td>
285 <label><input type='checkbox' name='form_not_esigned'<?php if ($form_not_esigned) echo ' checked'; ?>>
286 <?php echo xlt('Not Esigned'); ?></label>
287 </td>
288 </tr>
289 </table>
291 </div>
293 </td>
294 <td align='left' valign='middle' height="100%">
295 <table style='border-left:1px solid; width:100%; height:100%' >
296 <tr>
297 <td>
298 <div style='margin-left:15px'>
299 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
300 <span>
301 <?php echo xlt('Submit'); ?>
302 </span>
303 </a>
305 <?php if ($_POST['form_refresh'] || $_POST['form_orderby'] ) { ?>
306 <a href='#' class='css_button' onclick='window.print()'>
307 <span>
308 <?php echo xlt('Print'); ?>
309 </span>
310 </a>
311 <?php } ?>
312 </div>
313 </td>
314 </tr>
315 </table>
316 </td>
317 </tr>
318 </table>
320 </div> <!-- end report_parameters -->
322 <?php
323 if ($_POST['form_refresh'] || $_POST['form_orderby']) {
325 <div id="report_results">
326 <table>
328 <thead>
329 <?php if ($form_details) { ?>
330 <th>
331 <a href="nojs.php" onclick="return dosort('doctor')"
332 <?php if ($form_orderby == "doctor") echo " style=\"color:#00cc00\"" ?>><?php echo xlt('Provider'); ?> </a>
333 </th>
334 <th>
335 <a href="nojs.php" onclick="return dosort('time')"
336 <?php if ($form_orderby == "time") echo " style=\"color:#00cc00\"" ?>><?php echo xlt('Date'); ?></a>
337 </th>
338 <th>
339 <a href="nojs.php" onclick="return dosort('patient')"
340 <?php if ($form_orderby == "patient") echo " style=\"color:#00cc00\"" ?>><?php echo xlt('Patient'); ?></a>
341 </th>
342 <th>
343 <a href="nojs.php" onclick="return dosort('pubpid')"
344 <?php if ($form_orderby == "pubpid") echo " style=\"color:#00cc00\"" ?>><?php echo xlt('ID'); ?></a>
345 </th>
346 <th>
347 <?php echo xlt('Status'); ?>
348 </th>
349 <th>
350 <?php echo xlt('Encounter'); ?>
351 </th>
352 <th>
353 <a href="nojs.php" onclick="return dosort('encounter')"
354 <?php if ($form_orderby == "encounter") echo " style=\"color:#00cc00\"" ?>><?php echo xlt('Encounter Number'); ?></a>
355 </th>
356 <th>
357 <?php echo xlt('Form'); ?>
358 </th>
359 <th>
360 <?php echo xlt('Coding'); ?>
361 </th>
362 <?php } else { ?>
363 <th><?php echo xlt('Provider'); ?></td>
364 <th><?php echo xlt('Encounters'); ?></td>
365 <?php } ?>
366 </thead>
367 <tbody>
368 <?php
369 if ($res) {
370 $lastdocname = "";
371 $doc_encounters = 0;
372 while ($row = sqlFetchArray($res)) {
373 $patient_id = $row['pid'];
375 $docname = '';
376 if (!empty($row['ulname']) || !empty($row['ufname'])) {
377 $docname = $row['ulname'];
378 if (!empty($row['ufname']) || !empty($row['umname']))
379 $docname .= ', ' . $row['ufname'] . ' ' . $row['umname'];
382 $errmsg = "";
383 if ($form_details) {
384 // Fetch all other forms for this encounter.
385 $encnames = '';
386 $encarr = getFormByEncounter($patient_id, $row['encounter'],
387 "formdir, user, form_name, form_id");
388 if($encarr!='') {
389 foreach ($encarr as $enc) {
390 if ($enc['formdir'] == 'newpatient') continue;
391 if ($encnames) $encnames .= '<br />';
392 $encnames .= text($enc['form_name']); // need to html escape it here for output below
396 // Fetch coding and compute billing status.
397 $coded = "";
398 $billed_count = 0;
399 $unbilled_count = 0;
400 if ($billres = getBillingByEncounter($row['pid'], $row['encounter'],
401 "code_type, code, code_text, billed"))
403 foreach ($billres as $billrow) {
404 // $title = addslashes($billrow['code_text']);
405 if ($billrow['code_type'] != 'COPAY' && $billrow['code_type'] != 'TAX') {
406 $coded .= $billrow['code'] . ', ';
407 if ($billrow['billed']) ++$billed_count; else ++$unbilled_count;
410 $coded = substr($coded, 0, strlen($coded) - 2);
413 // Figure product sales into billing status.
414 $sres = sqlStatement("SELECT billed FROM drug_sales " .
415 "WHERE pid = '{$row['pid']}' AND encounter = '{$row['encounter']}'");
416 while ($srow = sqlFetchArray($sres)) {
417 if ($srow['billed']) ++$billed_count; else ++$unbilled_count;
420 // Compute billing status.
421 if ($billed_count && $unbilled_count) $status = xl('Mixed' );
422 else if ($billed_count ) $status = xl('Closed');
423 else if ($unbilled_count ) $status = xl('Open' );
424 else $status = xl('Empty' );
426 <tr bgcolor='<?php echo $bgcolor ?>'>
427 <td>
428 <?php echo ($docname == $lastdocname) ? "" : text($docname) ?>&nbsp;
429 </td>
430 <td>
431 <?php echo text(oeFormatShortDate(substr($row['date'], 0, 10))) ?>&nbsp;
432 </td>
433 <td>
434 <?php echo text($row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname']); ?>&nbsp;
435 </td>
436 <td>
437 <?php echo text($row['pubpid']); ?>&nbsp;
438 </td>
439 <td>
440 <?php echo text($status); ?>&nbsp;
441 </td>
442 <td>
443 <?php echo text($row['reason']); ?>&nbsp;
444 </td>
445 <td>
446 <?php echo text($row['encounter']); ?>&nbsp;
447 </td>
448 <td>
449 <?php echo $encnames; //since this variable contains html, have already html escaped it above ?>&nbsp;
450 </td>
451 <td>
452 <?php echo text($coded); ?>
453 </td>
454 </tr>
455 <?php
456 } else {
457 if ($docname != $lastdocname) {
458 show_doc_total($lastdocname, $doc_encounters);
459 $doc_encounters = 0;
461 ++$doc_encounters;
463 $lastdocname = $docname;
466 if (!$form_details) show_doc_total($lastdocname, $doc_encounters);
469 </tbody>
470 </table>
471 </div> <!-- end encresults -->
472 <?php } else { ?>
473 <div class='text'>
474 <?php echo xlt('Please input search criteria above, and click Submit to view results.' ); ?>
475 </div>
476 <?php } ?>
478 <input type="hidden" name="form_orderby" value="<?php echo attr($form_orderby) ?>" />
479 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
481 </form>
482 </body>
484 <script language='JavaScript'>
485 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
486 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
488 <?php if ($alertmsg) { echo " alert('$alertmsg');\n"; } ?>
490 </script>
492 </html>