ongoing internationalization of date widget
[openemr.git] / interface / reports / encounters_report.php
blob721ba20b63ea210011ab5b8516a8b40d9b7006fc
1 <?php
2 /*
3 * Encounters report.
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.
8 * Copyright (C) 2015 Terry Hill <terry@lillysystems.com>
9 * Copyright (C) 2007-2016 Rod Roark <rod@sunsetsystems.com>
10 * Copyright (C) 2017 Brady Miller <brady.g.miller@gmail.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 * @author Brady Miller <brady.g.miller@gmail.com>
27 * @link http://www.open-emr.org
31 use OpenEMR\Core\Header;
33 require_once("../globals.php");
34 require_once("$srcdir/forms.inc");
35 require_once("$srcdir/billing.inc");
36 require_once("$srcdir/patient.inc");
37 require_once "$srcdir/options.inc.php";
39 $alertmsg = ''; // not used yet but maybe later
41 // For each sorting option, specify the ORDER BY argument.
43 $ORDERHASH = array(
44 'doctor' => 'lower(u.lname), lower(u.fname), fe.date',
45 'patient' => 'lower(p.lname), lower(p.fname), fe.date',
46 'pubpid' => 'lower(p.pubpid), fe.date',
47 'time' => 'fe.date, lower(u.lname), lower(u.fname)',
48 'encounter' => 'fe.encounter, fe.date, lower(u.lname), lower(u.fname)',
51 function bucks($amount)
53 if ($amount) {
54 printf("%.2f", $amount);
58 function show_doc_total($lastdocname, $doc_encounters)
60 if ($lastdocname) {
61 echo " <tr>\n";
62 echo " <td class='detail'>$lastdocname</td>\n";
63 echo " <td class='detail' align='right'>$doc_encounters</td>\n";
64 echo " </tr>\n";
68 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
69 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
70 $form_provider = $_POST['form_provider'];
71 $form_facility = $_POST['form_facility'];
72 $form_details = $_POST['form_details'] ? true : false;
73 $form_new_patients = $_POST['form_new_patients'] ? true : false;
74 $form_esigned = $_POST['form_esigned'] ? true : false;
75 $form_not_esigned = $_POST['form_not_esigned'] ? true : false;
76 $form_encounter_esigned = $_POST['form_encounter_esigned'] ? true : false;
78 $form_orderby = $ORDERHASH[$_REQUEST['form_orderby']] ?
79 $_REQUEST['form_orderby'] : 'doctor';
80 $orderby = $ORDERHASH[$form_orderby];
82 // Get the info.
84 $esign_fields = '';
85 $esign_joins = '';
86 if ($form_encounter_esigned) {
87 $esign_fields = ", es.table, es.tid ";
88 $esign_joins = "LEFT OUTER JOIN esign_signatures AS es ON es.tid = fe.encounter ";
91 if ($form_esigned) {
92 $esign_fields = ", es.table, es.tid ";
93 $esign_joins = "LEFT OUTER JOIN esign_signatures AS es ON es.tid = fe.encounter ";
96 if ($form_not_esigned) {
97 $esign_fields = ", es.table, es.tid ";
98 $esign_joins = "LEFT JOIN esign_signatures AS es on es.tid = fe.encounter ";
101 $query = "SELECT " .
102 "fe.encounter, fe.date, fe.reason, " .
103 "f.formdir, f.form_name, " .
104 "p.fname, p.mname, p.lname, p.pid, p.pubpid, " .
105 "u.lname AS ulname, u.fname AS ufname, u.mname AS umname " .
106 "$esign_fields" .
107 "FROM ( form_encounter AS fe, forms AS f ) " .
108 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
109 "LEFT JOIN users AS u ON u.id = fe.provider_id " .
110 "$esign_joins" .
111 "WHERE f.pid = fe.pid AND f.encounter = fe.encounter AND f.formdir = 'newpatient' ";
112 if ($form_to_date) {
113 $query .= "AND fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' ";
114 } else {
115 $query .= "AND fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_from_date 23:59:59' ";
118 if ($form_provider) {
119 $query .= "AND fe.provider_id = '$form_provider' ";
122 if ($form_facility) {
123 $query .= "AND fe.facility_id = '$form_facility' ";
126 if ($form_new_patients) {
127 $query .= "AND fe.date = (SELECT MIN(fe2.date) FROM form_encounter AS fe2 WHERE fe2.pid = fe.pid) ";
130 if ($form_encounter_esigned) {
131 $query .= "AND es.tid = fe.encounter AND es.table = 'form_encounter' ";
134 if ($form_esigned) {
135 $query .= "AND es.tid = fe.encounter ";
138 if ($form_not_esigned) {
139 $query .= "AND es.tid IS NULL ";
142 $query .= "ORDER BY $orderby";
144 $res = sqlStatement($query);
146 <html>
147 <head>
149 <title><?php echo xlt('Encounters Report'); ?></title>
151 <?php Header::setupHeader(['datetime-picker', 'report-helper']); ?>
153 <style type="text/css">
155 /* specifically include & exclude from printing */
156 @media print {
157 #report_parameters {
158 visibility: hidden;
159 display: none;
161 #report_parameters_daterange {
162 visibility: visible;
163 display: inline;
165 #report_results table {
166 margin-top: 0px;
170 /* specifically exclude some from the screen */
171 @media screen {
172 #report_parameters_daterange {
173 visibility: hidden;
174 display: none;
178 </style>
180 <script LANGUAGE="JavaScript">
182 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
184 $(document).ready(function() {
185 oeFixedHeaderSetup(document.getElementById('mymaintable'));
186 var win = top.printLogSetup ? top : opener.top;
187 win.printLogSetup(document.getElementById('printbutton'));
189 $('.datepicker').datetimepicker({
190 <?php $datetimepicker_timepicker = false; ?>
191 <?php $datetimepicker_showseconds = false; ?>
192 <?php $datetimepicker_formatInput = false; ?>
193 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
194 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
198 function dosort(orderby) {
199 var f = document.forms[0];
200 f.form_orderby.value = orderby;
201 f.submit();
202 return false;
205 function refreshme() {
206 document.forms[0].submit();
209 </script>
211 </head>
212 <body class="body_top">
213 <!-- Required for the popup date selectors -->
214 <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
216 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Encounters'); ?></span>
218 <div id="report_parameters_daterange">
219 <?php echo date("d F Y", strtotime($form_from_date)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
220 </div>
222 <form method='post' name='theform' id='theform' action='encounters_report.php' onsubmit='return top.restoreSession()'>
224 <div id="report_parameters">
225 <table>
226 <tr>
227 <td width='550px'>
228 <div style='float:left'>
230 <table class='text'>
231 <tr>
232 <td class='control-label'>
233 <?php echo xlt('Facility'); ?>:
234 </td>
235 <td>
236 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
237 </td>
238 <td class='control-label'>
239 <?php echo xlt('Provider'); ?>:
240 </td>
241 <td>
242 <?php
244 // Build a drop-down list of providers.
247 $query = "SELECT id, lname, fname FROM users WHERE ".
248 "authorized = 1 $provider_facility_filter ORDER BY lname, fname"; //(CHEMED) facility filter
250 $ures = sqlStatement($query);
252 echo " <select name='form_provider' class='form-control'>\n";
253 echo " <option value=''>-- " . xlt('All') . " --\n";
255 while ($urow = sqlFetchArray($ures)) {
256 $provid = $urow['id'];
257 echo " <option value='" . attr($provid) . "'";
258 if ($provid == $_POST['form_provider']) {
259 echo " selected";
262 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
265 echo " </select>\n";
268 </td>
269 </tr>
270 <tr>
271 <td class='control-label'>
272 <?php echo xlt('From'); ?>:
273 </td>
274 <td>
275 <input type='text' class='datepicker form-control' name='form_from_date' id="form_from_date" size='10' value='<?php echo attr($form_from_date) ?>'
276 title='yyyy-mm-dd'>
277 </td>
278 <td class='control-label'>
279 <?php echo xlt('To'); ?>:
280 </td>
281 <td>
282 <input type='text' class='datepicker form-control' name='form_to_date' id="form_to_date" size='10' value='<?php echo attr($form_to_date) ?>'
283 title='yyyy-mm-dd'>
284 </td>
285 </tr>
286 <tr>
287 <td></td>
288 <td>
289 <div class="checkbox">
290 <label><input type='checkbox' name='form_details'<?php if ($form_details) {
291 echo ' checked';
292 } ?>>
293 <?php echo xlt('Details'); ?></label>
294 </div>
295 <div class="checkbox">
296 <label><input type='checkbox' name='form_new_patients' title='<?php echo xla('First-time visits only'); ?>'<?php if ($form_new_patients) {
297 echo ' checked';
298 } ?>>
299 <?php echo xlt('New'); ?></label>
300 </div>
301 </td>
302 <td></td>
303 <td>
304 <div class="checkbox">
305 <label><input type='checkbox' name='form_esigned'<?php if ($form_esigned) {
306 echo ' checked';
307 } ?>>
308 <?php echo xlt('Forms Esigned'); ?></label>
309 </div>
310 <div class="checkbox">
311 <label><input type='checkbox' name='form_encounter_esigned'<?php if ($form_encounter_esigned) {
312 echo ' checked';
313 } ?>>
314 <?php echo xlt('Encounter Esigned'); ?></label>
315 </div>
316 <div class="checkbox">
317 <label><input type='checkbox' name='form_not_esigned'<?php if ($form_not_esigned) {
318 echo ' checked';
319 } ?>>
320 <?php echo xlt('Not Esigned'); ?></label>
321 </div>
322 </td>
323 </tr>
324 </table>
326 </div>
328 </td>
329 <td align='left' valign='middle' height="100%">
330 <table style='border-left:1px solid; width:100%; height:100%' >
331 <tr>
332 <td>
333 <div class="text-center">
334 <div class="btn-group" role="group">
335 <a href='#' class='btn btn-default btn-save' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
336 <?php echo xlt('Submit'); ?>
337 </a>
338 <?php if ($_POST['form_refresh'] || $_POST['form_orderby']) { ?>
339 <a href='#' class='btn btn-default btn-print' id='printbutton'>
340 <?php echo xlt('Print'); ?>
341 </a>
342 <?php } ?>
343 </div>
344 </div>
345 </td>
346 </tr>
347 </table>
348 </td>
349 </tr>
350 </table>
352 </div> <!-- end report_parameters -->
354 <?php
355 if ($_POST['form_refresh'] || $_POST['form_orderby']) {
357 <div id="report_results">
358 <table id='mymaintable'>
359 <thead>
360 <?php if ($form_details) { ?>
361 <th>
362 <a href="nojs.php" onclick="return dosort('doctor')"
363 <?php if ($form_orderby == "doctor") {
364 echo " style=\"color:#00cc00\"";
365 } ?>><?php echo xlt('Provider'); ?> </a>
366 </th>
367 <th>
368 <a href="nojs.php" onclick="return dosort('time')"
369 <?php if ($form_orderby == "time") {
370 echo " style=\"color:#00cc00\"";
371 } ?>><?php echo xlt('Date'); ?></a>
372 </th>
373 <th>
374 <a href="nojs.php" onclick="return dosort('patient')"
375 <?php if ($form_orderby == "patient") {
376 echo " style=\"color:#00cc00\"";
377 } ?>><?php echo xlt('Patient'); ?></a>
378 </th>
379 <th>
380 <a href="nojs.php" onclick="return dosort('pubpid')"
381 <?php if ($form_orderby == "pubpid") {
382 echo " style=\"color:#00cc00\"";
383 } ?>><?php echo xlt('ID'); ?></a>
384 </th>
385 <th>
386 <?php echo xlt('Status'); ?>
387 </th>
388 <th>
389 <?php echo xlt('Encounter'); ?>
390 </th>
391 <th>
392 <a href="nojs.php" onclick="return dosort('encounter')"
393 <?php if ($form_orderby == "encounter") {
394 echo " style=\"color:#00cc00\"";
395 } ?>><?php echo xlt('Encounter Number'); ?></a>
396 </th>
397 <th>
398 <?php echo xlt('Form'); ?>
399 </th>
400 <th>
401 <?php echo xlt('Coding'); ?>
402 </th>
403 <?php } else { ?>
404 <th><?php echo xlt('Provider'); ?></td>
405 <th><?php echo xlt('Encounters'); ?></td>
406 <?php } ?>
407 </thead>
408 <tbody>
409 <?php
410 if ($res) {
411 $lastdocname = "";
412 $doc_encounters = 0;
413 while ($row = sqlFetchArray($res)) {
414 $patient_id = $row['pid'];
416 $docname = '';
417 if (!empty($row['ulname']) || !empty($row['ufname'])) {
418 $docname = $row['ulname'];
419 if (!empty($row['ufname']) || !empty($row['umname'])) {
420 $docname .= ', ' . $row['ufname'] . ' ' . $row['umname'];
424 $errmsg = "";
425 if ($form_details) {
426 // Fetch all other forms for this encounter.
427 $encnames = '';
428 $encarr = getFormByEncounter(
429 $patient_id,
430 $row['encounter'],
431 "formdir, user, form_name, form_id"
433 if ($encarr!='') {
434 foreach ($encarr as $enc) {
435 if ($enc['formdir'] == 'newpatient') {
436 continue;
439 if ($encnames) {
440 $encnames .= '<br />';
443 $encnames .= text($enc['form_name']); // need to html escape it here for output below
447 // Fetch coding and compute billing status.
448 $coded = "";
449 $billed_count = 0;
450 $unbilled_count = 0;
451 if ($billres = getBillingByEncounter(
452 $row['pid'],
453 $row['encounter'],
454 "code_type, code, code_text, billed"
455 )) {
456 foreach ($billres as $billrow) {
457 // $title = addslashes($billrow['code_text']);
458 if ($billrow['code_type'] != 'COPAY' && $billrow['code_type'] != 'TAX') {
459 $coded .= $billrow['code'] . ', ';
460 if ($billrow['billed']) {
461 ++$billed_count;
462 } else {
463 ++$unbilled_count;
468 $coded = substr($coded, 0, strlen($coded) - 2);
471 // Figure product sales into billing status.
472 $sres = sqlStatement("SELECT billed FROM drug_sales " .
473 "WHERE pid = '{$row['pid']}' AND encounter = '{$row['encounter']}'");
474 while ($srow = sqlFetchArray($sres)) {
475 if ($srow['billed']) {
476 ++$billed_count;
477 } else {
478 ++$unbilled_count;
482 // Compute billing status.
483 if ($billed_count && $unbilled_count) {
484 $status = xl('Mixed');
485 } else if ($billed_count) {
486 $status = xl('Closed');
487 } else if ($unbilled_count) {
488 $status = xl('Open');
489 } else {
490 $status = xl('Empty');
493 <tr bgcolor='<?php echo $bgcolor ?>'>
494 <td>
495 <?php echo ($docname == $lastdocname) ? "" : text($docname) ?>&nbsp;
496 </td>
497 <td>
498 <?php echo text(oeFormatShortDate(substr($row['date'], 0, 10))) ?>&nbsp;
499 </td>
500 <td>
501 <?php echo text($row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname']); ?>&nbsp;
502 </td>
503 <td>
504 <?php echo text($row['pubpid']); ?>&nbsp;
505 </td>
506 <td>
507 <?php echo text($status); ?>&nbsp;
508 </td>
509 <td>
510 <?php echo text($row['reason']); ?>&nbsp;
511 </td>
512 <td>
513 <?php echo text($row['encounter']); ?>&nbsp;
514 </td>
515 <td>
516 <?php echo $encnames; //since this variable contains html, have already html escaped it above ?>&nbsp;
517 </td>
518 <td>
519 <?php echo text($coded); ?>
520 </td>
521 </tr>
522 <?php
523 } else {
524 if ($docname != $lastdocname) {
525 show_doc_total($lastdocname, $doc_encounters);
526 $doc_encounters = 0;
529 ++$doc_encounters;
532 $lastdocname = $docname;
535 if (!$form_details) {
536 show_doc_total($lastdocname, $doc_encounters);
540 </tbody>
541 </table>
542 </div> <!-- end encresults -->
543 <?php } else { ?>
544 <div class='text'>
545 <?php echo xlt('Please input search criteria above, and click Submit to view results.'); ?>
546 </div>
547 <?php } ?>
549 <input type="hidden" name="form_orderby" value="<?php echo attr($form_orderby) ?>" />
550 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
552 </form>
553 </body>
555 <script language='JavaScript'>
556 <?php if ($alertmsg) {
557 echo " alert('$alertmsg');\n";
558 } ?>
559 </script>
560 </html>