Changes to the Clients>Clinical report
[openemr.git] / interface / reports / encounters_report.php
blobf12f1ada44f22d8f5e267a688038298c41ac0757
1 <?php
2 // Copyright (C) 2007-2010 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 shows past encounters with filtering and sorting.
11 require_once("../globals.php");
12 require_once("$srcdir/forms.inc");
13 require_once("$srcdir/billing.inc");
14 require_once("$srcdir/patient.inc");
15 require_once("$srcdir/formatting.inc.php");
16 require_once "$srcdir/options.inc.php";
17 require_once "$srcdir/formdata.inc.php";
19 $alertmsg = ''; // not used yet but maybe later
21 // For each sorting option, specify the ORDER BY argument.
23 $ORDERHASH = array(
24 'doctor' => 'lower(u.lname), lower(u.fname), fe.date',
25 'patient' => 'lower(p.lname), lower(p.fname), fe.date',
26 'pubpid' => 'lower(p.pubpid), fe.date',
27 'time' => 'fe.date, lower(u.lname), lower(u.fname)',
30 function bucks($amount) {
31 if ($amount) printf("%.2f", $amount);
34 function show_doc_total($lastdocname, $doc_encounters) {
35 if ($lastdocname) {
36 echo " <tr>\n";
37 echo " <td class='detail'>$lastdocname</td>\n";
38 echo " <td class='detail' align='right'>$doc_encounters</td>\n";
39 echo " </tr>\n";
43 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
44 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
45 $form_provider = $_POST['form_provider'];
46 $form_facility = $_POST['form_facility'];
47 $form_details = $_POST['form_details'] ? true : false;
48 $form_new_patients = $_POST['form_new_patients'] ? true : false;
50 $form_orderby = $ORDERHASH[$_REQUEST['form_orderby']] ?
51 $_REQUEST['form_orderby'] : 'doctor';
52 $orderby = $ORDERHASH[$form_orderby];
54 // Get the info.
56 $query = "SELECT " .
57 "fe.encounter, fe.date, fe.reason, " .
58 "f.formdir, f.form_name, " .
59 "p.fname, p.mname, p.lname, p.pid, p.pubpid, " .
60 "u.lname AS ulname, u.fname AS ufname, u.mname AS umname " .
61 "FROM ( form_encounter AS fe, forms AS f ) " .
62 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
63 "LEFT JOIN users AS u ON u.id = fe.provider_id " .
64 "WHERE f.encounter = fe.encounter AND f.formdir = 'newpatient' ";
65 if ($form_to_date) {
66 $query .= "AND fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' ";
67 } else {
68 $query .= "AND fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_from_date 23:59:59' ";
70 if ($form_provider) {
71 $query .= "AND fe.provider_id = '$form_provider' ";
73 if ($form_facility) {
74 $query .= "AND fe.facility_id = '$form_facility' ";
76 if ($form_new_patients) {
77 $query .= "AND fe.date = (SELECT MIN(fe2.date) FROM form_encounter AS fe2 WHERE fe2.pid = fe.pid) ";
79 $query .= "ORDER BY $orderby";
81 $res = sqlStatement($query);
83 <html>
84 <head>
85 <?php html_header_show();?>
86 <title><?php xl('Encounters Report','e'); ?></title>
88 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
90 <link rel=stylesheet href="<?php echo $css_header;?>" type="text/css">
91 <style type="text/css">
93 /* specifically include & exclude from printing */
94 @media print {
95 #report_parameters {
96 visibility: hidden;
97 display: none;
99 #report_parameters_daterange {
100 visibility: visible;
101 display: inline;
103 #report_results table {
104 margin-top: 0px;
108 /* specifically exclude some from the screen */
109 @media screen {
110 #report_parameters_daterange {
111 visibility: hidden;
112 display: none;
116 </style>
118 <script type="text/javascript" src="../../library/textformat.js"></script>
119 <script type="text/javascript" src="../../library/dialog.js"></script>
120 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
121 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
122 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
123 <script type="text/javascript" src="../../library/js/jquery.1.3.2.js"></script>
125 <script LANGUAGE="JavaScript">
127 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
129 function dosort(orderby) {
130 var f = document.forms[0];
131 f.form_orderby.value = orderby;
132 f.submit();
133 return false;
136 function refreshme() {
137 document.forms[0].submit();
140 </script>
142 </head>
143 <body class="body_top">
144 <!-- Required for the popup date selectors -->
145 <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
147 <span class='title'><?php xl('Report','e'); ?> - <?php xl('Encounters','e'); ?></span>
149 <div id="report_parameters_daterange">
150 <?php echo date("d F Y", strtotime($form_from_date)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
151 </div>
153 <form method='post' name='theform' id='theform' action='encounters_report.php'>
155 <div id="report_parameters">
156 <table>
157 <tr>
158 <td width='550px'>
159 <div style='float:left'>
161 <table class='text'>
162 <tr>
163 <td class='label'>
164 <?php xl('Facility','e'); ?>:
165 </td>
166 <td>
167 <?php dropdown_facility(strip_escape_custom($form_facility), 'form_facility', true); ?>
168 </td>
169 <td class='label'>
170 <?php xl('Provider','e'); ?>:
171 </td>
172 <td>
173 <?php
175 // Build a drop-down list of providers.
178 $query = "SELECT id, lname, fname FROM users WHERE ".
179 "authorized = 1 $provider_facility_filter ORDER BY lname, fname"; //(CHEMED) facility filter
181 $ures = sqlStatement($query);
183 echo " <select name='form_provider'>\n";
184 echo " <option value=''>-- " . xl('All') . " --\n";
186 while ($urow = sqlFetchArray($ures)) {
187 $provid = $urow['id'];
188 echo " <option value='$provid'";
189 if ($provid == $_POST['form_provider']) echo " selected";
190 echo ">" . $urow['lname'] . ", " . $urow['fname'] . "\n";
193 echo " </select>\n";
196 </td>
197 <td>
198 <input type='checkbox' name='form_new_patients' title='First-time visits only'<?php if ($form_new_patients) echo ' checked'; ?>>
199 <?php xl('New','e'); ?>
200 </td>
201 </tr>
202 <tr>
203 <td class='label'>
204 <?php xl('From','e'); ?>:
205 </td>
206 <td>
207 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo $form_from_date ?>'
208 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
209 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
210 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
211 title='<?php xl('Click here to choose a date','e'); ?>'>
212 </td>
213 <td class='label'>
214 <?php xl('To','e'); ?>:
215 </td>
216 <td>
217 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo $form_to_date ?>'
218 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
219 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
220 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
221 title='<?php xl('Click here to choose a date','e'); ?>'>
222 </td>
223 <td>
224 <input type='checkbox' name='form_details'<?php if ($form_details) echo ' checked'; ?>>
225 <?php xl('Details','e'); ?>
226 </td>
227 </tr>
228 </table>
230 </div>
232 </td>
233 <td align='left' valign='middle' height="100%">
234 <table style='border-left:1px solid; width:100%; height:100%' >
235 <tr>
236 <td>
237 <div style='margin-left:15px'>
238 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
239 <span>
240 <?php xl('Submit','e'); ?>
241 </span>
242 </a>
244 <?php if ($_POST['form_refresh'] || $_POST['form_orderby'] ) { ?>
245 <a href='#' class='css_button' onclick='window.print()'>
246 <span>
247 <?php xl('Print','e'); ?>
248 </span>
249 </a>
250 <?php } ?>
251 </div>
252 </td>
253 </tr>
254 </table>
255 </td>
256 </tr>
257 </table>
259 </div> <!-- end report_parameters -->
261 <?php
262 if ($_POST['form_refresh'] || $_POST['form_orderby']) {
264 <div id="report_results">
265 <table>
267 <thead>
268 <?php if ($form_details) { ?>
269 <th>
270 <a href="nojs.php" onclick="return dosort('doctor')"
271 <?php if ($form_orderby == "doctor") echo " style=\"color:#00cc00\"" ?>><?php xl('Provider','e'); ?> </a>
272 </th>
273 <th>
274 <a href="nojs.php" onclick="return dosort('time')"
275 <?php if ($form_orderby == "time") echo " style=\"color:#00cc00\"" ?>><?php xl('Date','e'); ?></a>
276 </th>
277 <th>
278 <a href="nojs.php" onclick="return dosort('patient')"
279 <?php if ($form_orderby == "patient") echo " style=\"color:#00cc00\"" ?>><?php xl('Patient','e'); ?></a>
280 </th>
281 <th>
282 <a href="nojs.php" onclick="return dosort('pubpid')"
283 <?php if ($form_orderby == "pubpid") echo " style=\"color:#00cc00\"" ?>><?php xl('ID','e'); ?></a>
284 </th>
285 <th>
286 <?php xl('Status','e'); ?>
287 </th>
288 <th>
289 <?php xl('Encounter','e'); ?>
290 </th>
291 <th>
292 <?php xl('Form','e'); ?>
293 </th>
294 <th>
295 <?php xl('Coding','e'); ?>
296 </th>
297 <?php } else { ?>
298 <th><?php xl('Provider','e'); ?></td>
299 <th><?php xl('Encounters','e'); ?></td>
300 <?php } ?>
301 </thead>
302 <tbody>
303 <?php
304 if ($res) {
305 $lastdocname = "";
306 $doc_encounters = 0;
307 while ($row = sqlFetchArray($res)) {
308 $patient_id = $row['pid'];
310 $docname = '';
311 if (!empty($row['ulname']) || !empty($row['ufname'])) {
312 $docname = $row['ulname'];
313 if (!empty($row['ufname']) || !empty($row['umname']))
314 $docname .= ', ' . $row['ufname'] . ' ' . $row['umname'];
317 $errmsg = "";
318 if ($form_details) {
319 // Fetch all other forms for this encounter.
320 $encnames = '';
321 $encarr = getFormByEncounter($patient_id, $row['encounter'],
322 "formdir, user, form_name, form_id");
323 if($encarr!='') {
324 foreach ($encarr as $enc) {
325 if ($enc['formdir'] == 'newpatient') continue;
326 if ($encnames) $encnames .= '<br />';
327 $encnames .= $enc['form_name'];
331 // Fetch coding and compute billing status.
332 $coded = "";
333 $billed_count = 0;
334 $unbilled_count = 0;
335 if ($billres = getBillingByEncounter($row['pid'], $row['encounter'],
336 "code_type, code, code_text, billed"))
338 foreach ($billres as $billrow) {
339 // $title = addslashes($billrow['code_text']);
340 if ($billrow['code_type'] != 'COPAY' && $billrow['code_type'] != 'TAX') {
341 $coded .= $billrow['code'] . ', ';
342 if ($billrow['billed']) ++$billed_count; else ++$unbilled_count;
345 $coded = substr($coded, 0, strlen($coded) - 2);
348 // Figure product sales into billing status.
349 $sres = sqlStatement("SELECT billed FROM drug_sales " .
350 "WHERE pid = '{$row['pid']}' AND encounter = '{$row['encounter']}'");
351 while ($srow = sqlFetchArray($sres)) {
352 if ($srow['billed']) ++$billed_count; else ++$unbilled_count;
355 // Compute billing status.
356 if ($billed_count && $unbilled_count) $status = xl('Mixed' );
357 else if ($billed_count ) $status = xl('Closed');
358 else if ($unbilled_count ) $status = xl('Open' );
359 else $status = xl('Empty' );
361 <tr bgcolor='<?php echo $bgcolor ?>'>
362 <td>
363 <?php echo ($docname == $lastdocname) ? "" : $docname ?>&nbsp;
364 </td>
365 <td>
366 <?php echo oeFormatShortDate(substr($row['date'], 0, 10)) ?>&nbsp;
367 </td>
368 <td>
369 <?php echo $row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname']; ?>&nbsp;
370 </td>
371 <td>
372 <?php echo $row['pubpid']; ?>&nbsp;
373 </td>
374 <td>
375 <?php echo $status; ?>&nbsp;
376 </td>
377 <td>
378 <?php echo $row['reason']; ?>&nbsp;
379 </td>
380 <td>
381 <?php echo $encnames; ?>&nbsp;
382 </td>
383 <td>
384 <?php echo $coded; ?>
385 </td>
386 </tr>
387 <?php
388 } else {
389 if ($docname != $lastdocname) {
390 show_doc_total($lastdocname, $doc_encounters);
391 $doc_encounters = 0;
393 ++$doc_encounters;
395 $lastdocname = $docname;
398 if (!$form_details) show_doc_total($lastdocname, $doc_encounters);
401 </tbody>
402 </table>
403 </div> <!-- end encresults -->
404 <?php } else { ?>
405 <div class='text'>
406 <?php echo xl('Please input search criteria above, and click Submit to view results.', 'e' ); ?>
407 </div>
408 <?php } ?>
410 <input type="hidden" name="form_orderby" value="<?php echo $form_orderby ?>" />
411 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
413 </form>
414 </body>
416 <script language='JavaScript'>
417 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
418 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
420 <?php if ($alertmsg) { echo " alert('$alertmsg');\n"; } ?>
422 </script>
424 </html>