migrated ubiquitous libraries to composer autoloader (#421)
[openemr.git] / interface / reports / encounters_report.php
blob526d8fa9fc77dd11fa3c7fe2301e3650c6c39aac
1 <?php
2 /*
3 * Encounters report. (/interface/reports/encounters_report.php
4 *
5 * This report shows past encounters with filtering and sorting,
6 * Added filtering to show encounters not e-signed, encounters e-signed and forms e-signed.
7 *
8 * Copyright (C) 2015 Terry Hill <terry@lillysystems.com>
9 * Copyright (C) 2007-2016 Rod Roark <rod@sunsetsystems.com>
11 * LICENSE: This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 3
14 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
22 * @package OpenEMR
23 * @author Terry Hill <terry@lilysystems.com>
24 * @author Rod Roark <rod@sunsetsystems.com>
25 * @link http://www.open-emr.org
29 require_once("../globals.php");
30 require_once("$srcdir/forms.inc");
31 require_once("$srcdir/billing.inc");
32 require_once("$srcdir/patient.inc");
33 require_once("$srcdir/formatting.inc.php");
34 require_once "$srcdir/options.inc.php";
36 $alertmsg = ''; // not used yet but maybe later
38 // For each sorting option, specify the ORDER BY argument.
40 $ORDERHASH = array(
41 'doctor' => 'lower(u.lname), lower(u.fname), fe.date',
42 'patient' => 'lower(p.lname), lower(p.fname), fe.date',
43 'pubpid' => 'lower(p.pubpid), fe.date',
44 'time' => 'fe.date, lower(u.lname), lower(u.fname)',
45 'encounter' => 'fe.encounter, fe.date, lower(u.lname), lower(u.fname)',
48 function bucks($amount) {
49 if ($amount) printf("%.2f", $amount);
52 function show_doc_total($lastdocname, $doc_encounters) {
53 if ($lastdocname) {
54 echo " <tr>\n";
55 echo " <td class='detail'>$lastdocname</td>\n";
56 echo " <td class='detail' align='right'>$doc_encounters</td>\n";
57 echo " </tr>\n";
61 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
62 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
63 $form_provider = $_POST['form_provider'];
64 $form_facility = $_POST['form_facility'];
65 $form_details = $_POST['form_details'] ? true : false;
66 $form_new_patients = $_POST['form_new_patients'] ? true : false;
67 $form_esigned = $_POST['form_esigned'] ? true : false;
68 $form_not_esigned = $_POST['form_not_esigned'] ? true : false;
69 $form_encounter_esigned = $_POST['form_encounter_esigned'] ? true : false;
71 $form_orderby = $ORDERHASH[$_REQUEST['form_orderby']] ?
72 $_REQUEST['form_orderby'] : 'doctor';
73 $orderby = $ORDERHASH[$form_orderby];
75 // Get the info.
77 $esign_fields = '';
78 $esign_joins = '';
79 if ($form_encounter_esigned) {
80 $esign_fields = ", es.table, es.tid ";
81 $esign_joins = "LEFT OUTER JOIN esign_signatures AS es ON es.tid = fe.encounter ";
83 if ($form_esigned) {
84 $esign_fields = ", es.table, es.tid ";
85 $esign_joins = "LEFT OUTER JOIN esign_signatures AS es ON es.tid = fe.encounter ";
87 if ($form_not_esigned) {
88 $esign_fields = ", es.table, es.tid ";
89 $esign_joins = "LEFT JOIN esign_signatures AS es on es.tid = fe.encounter ";
92 $query = "SELECT " .
93 "fe.encounter, fe.date, fe.reason, " .
94 "f.formdir, f.form_name, " .
95 "p.fname, p.mname, p.lname, p.pid, p.pubpid, " .
96 "u.lname AS ulname, u.fname AS ufname, u.mname AS umname " .
97 "$esign_fields" .
98 "FROM ( form_encounter AS fe, forms AS f ) " .
99 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
100 "LEFT JOIN users AS u ON u.id = fe.provider_id " .
101 "$esign_joins" .
102 "WHERE f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' ";
103 if ($form_to_date) {
104 $query .= "AND fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' ";
105 } else {
106 $query .= "AND fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_from_date 23:59:59' ";
108 if ($form_provider) {
109 $query .= "AND fe.provider_id = '$form_provider' ";
111 if ($form_facility) {
112 $query .= "AND fe.facility_id = '$form_facility' ";
114 if ($form_new_patients) {
115 $query .= "AND fe.date = (SELECT MIN(fe2.date) FROM form_encounter AS fe2 WHERE fe2.pid = fe.pid) ";
117 if ($form_encounter_esigned) {
118 $query .= "AND es.tid = fe.encounter AND es.table = 'form_encounter' ";
120 if ($form_esigned) {
121 $query .= "AND es.tid = fe.encounter ";
123 if ($form_not_esigned) {
124 $query .= "AND es.tid IS NULL ";
126 $query .= "ORDER BY $orderby";
128 $res = sqlStatement($query);
130 <html>
131 <head>
132 <?php html_header_show();?>
133 <title><?php echo xlt('Encounters Report'); ?></title>
135 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
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 #report_parameters {
143 visibility: hidden;
144 display: none;
146 #report_parameters_daterange {
147 visibility: visible;
148 display: inline;
150 #report_results table {
151 margin-top: 0px;
155 /* specifically exclude some from the screen */
156 @media screen {
157 #report_parameters_daterange {
158 visibility: hidden;
159 display: none;
163 </style>
165 <script type="text/javascript" src="../../library/textformat.js"></script>
166 <script type="text/javascript" src="../../library/dialog.js?v=<?php echo $v_js_includes; ?>"></script>
167 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
168 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
169 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
170 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-9-1/index.js"></script>
171 <script type="text/javascript" src="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></script>
173 <script LANGUAGE="JavaScript">
175 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
177 $(document).ready(function() {
178 oeFixedHeaderSetup(document.getElementById('mymaintable'));
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 id='mymaintable'>
332 <thead>
333 <?php if ($form_details) { ?>
334 <th>
335 <a href="nojs.php" onclick="return dosort('doctor')"
336 <?php if ($form_orderby == "doctor") echo " style=\"color:#00cc00\"" ?>><?php echo xlt('Provider'); ?> </a>
337 </th>
338 <th>
339 <a href="nojs.php" onclick="return dosort('time')"
340 <?php if ($form_orderby == "time") echo " style=\"color:#00cc00\"" ?>><?php echo xlt('Date'); ?></a>
341 </th>
342 <th>
343 <a href="nojs.php" onclick="return dosort('patient')"
344 <?php if ($form_orderby == "patient") echo " style=\"color:#00cc00\"" ?>><?php echo xlt('Patient'); ?></a>
345 </th>
346 <th>
347 <a href="nojs.php" onclick="return dosort('pubpid')"
348 <?php if ($form_orderby == "pubpid") echo " style=\"color:#00cc00\"" ?>><?php echo xlt('ID'); ?></a>
349 </th>
350 <th>
351 <?php echo xlt('Status'); ?>
352 </th>
353 <th>
354 <?php echo xlt('Encounter'); ?>
355 </th>
356 <th>
357 <a href="nojs.php" onclick="return dosort('encounter')"
358 <?php if ($form_orderby == "encounter") echo " style=\"color:#00cc00\"" ?>><?php echo xlt('Encounter Number'); ?></a>
359 </th>
360 <th>
361 <?php echo xlt('Form'); ?>
362 </th>
363 <th>
364 <?php echo xlt('Coding'); ?>
365 </th>
366 <?php } else { ?>
367 <th><?php echo xlt('Provider'); ?></td>
368 <th><?php echo xlt('Encounters'); ?></td>
369 <?php } ?>
370 </thead>
371 <tbody>
372 <?php
373 if ($res) {
374 $lastdocname = "";
375 $doc_encounters = 0;
376 while ($row = sqlFetchArray($res)) {
377 $patient_id = $row['pid'];
379 $docname = '';
380 if (!empty($row['ulname']) || !empty($row['ufname'])) {
381 $docname = $row['ulname'];
382 if (!empty($row['ufname']) || !empty($row['umname']))
383 $docname .= ', ' . $row['ufname'] . ' ' . $row['umname'];
386 $errmsg = "";
387 if ($form_details) {
388 // Fetch all other forms for this encounter.
389 $encnames = '';
390 $encarr = getFormByEncounter($patient_id, $row['encounter'],
391 "formdir, user, form_name, form_id");
392 if($encarr!='') {
393 foreach ($encarr as $enc) {
394 if ($enc['formdir'] == 'newpatient') continue;
395 if ($encnames) $encnames .= '<br />';
396 $encnames .= text($enc['form_name']); // need to html escape it here for output below
400 // Fetch coding and compute billing status.
401 $coded = "";
402 $billed_count = 0;
403 $unbilled_count = 0;
404 if ($billres = getBillingByEncounter($row['pid'], $row['encounter'],
405 "code_type, code, code_text, billed"))
407 foreach ($billres as $billrow) {
408 // $title = addslashes($billrow['code_text']);
409 if ($billrow['code_type'] != 'COPAY' && $billrow['code_type'] != 'TAX') {
410 $coded .= $billrow['code'] . ', ';
411 if ($billrow['billed']) ++$billed_count; else ++$unbilled_count;
414 $coded = substr($coded, 0, strlen($coded) - 2);
417 // Figure product sales into billing status.
418 $sres = sqlStatement("SELECT billed FROM drug_sales " .
419 "WHERE pid = '{$row['pid']}' AND encounter = '{$row['encounter']}'");
420 while ($srow = sqlFetchArray($sres)) {
421 if ($srow['billed']) ++$billed_count; else ++$unbilled_count;
424 // Compute billing status.
425 if ($billed_count && $unbilled_count) $status = xl('Mixed' );
426 else if ($billed_count ) $status = xl('Closed');
427 else if ($unbilled_count ) $status = xl('Open' );
428 else $status = xl('Empty' );
430 <tr bgcolor='<?php echo $bgcolor ?>'>
431 <td>
432 <?php echo ($docname == $lastdocname) ? "" : text($docname) ?>&nbsp;
433 </td>
434 <td>
435 <?php echo text(oeFormatShortDate(substr($row['date'], 0, 10))) ?>&nbsp;
436 </td>
437 <td>
438 <?php echo text($row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname']); ?>&nbsp;
439 </td>
440 <td>
441 <?php echo text($row['pubpid']); ?>&nbsp;
442 </td>
443 <td>
444 <?php echo text($status); ?>&nbsp;
445 </td>
446 <td>
447 <?php echo text($row['reason']); ?>&nbsp;
448 </td>
449 <td>
450 <?php echo text($row['encounter']); ?>&nbsp;
451 </td>
452 <td>
453 <?php echo $encnames; //since this variable contains html, have already html escaped it above ?>&nbsp;
454 </td>
455 <td>
456 <?php echo text($coded); ?>
457 </td>
458 </tr>
459 <?php
460 } else {
461 if ($docname != $lastdocname) {
462 show_doc_total($lastdocname, $doc_encounters);
463 $doc_encounters = 0;
465 ++$doc_encounters;
467 $lastdocname = $docname;
470 if (!$form_details) show_doc_total($lastdocname, $doc_encounters);
473 </tbody>
474 </table>
475 </div> <!-- end encresults -->
476 <?php } else { ?>
477 <div class='text'>
478 <?php echo xlt('Please input search criteria above, and click Submit to view results.' ); ?>
479 </div>
480 <?php } ?>
482 <input type="hidden" name="form_orderby" value="<?php echo attr($form_orderby) ?>" />
483 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
485 </form>
486 </body>
488 <script language='JavaScript'>
489 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
490 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
492 <?php if ($alertmsg) { echo " alert('$alertmsg');\n"; } ?>
494 </script>
495 </html>