Added a provider filter to Procedures / Electronic Reports.
[openemr.git] / interface / orders / list_reports.php
blobb44c0952c5d1087b778686e5bac7ee32024be66d
1 <?php
2 /**
3 * List procedure orders and reports, and fetch new reports and their results.
5 * Copyright (C) 2013 Rod Roark <rod@sunsetsystems.com>
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>.
18 * @package OpenEMR
19 * @author Rod Roark <rod@sunsetsystems.com>
22 $sanitize_all_escapes = true;
23 $fake_register_globals = false;
25 require_once("../globals.php");
26 require_once("$srcdir/acl.inc");
27 require_once("$srcdir/formdata.inc.php");
28 require_once("$srcdir/options.inc.php");
29 require_once("$srcdir/formatting.inc.php");
30 require_once("./receive_hl7_results.inc.php");
32 /**
33 * Get a list item title, translating if required.
35 * @param string $listid List identifier.
36 * @param string $value List item identifier.
37 * @return string The item's title.
39 function getListItem($listid, $value) {
40 $lrow = sqlQuery("SELECT title FROM list_options " .
41 "WHERE list_id = ? AND option_id = ?",
42 array($listid, $value));
43 $tmp = xl_list_label($lrow['title']);
44 if (empty($tmp)) $tmp = "($report_status)";
45 return $tmp;
48 /**
49 * Adapt text to be suitable as the contents of a table cell.
51 * @param string $s Input text.
52 * @return string Output text.
54 function myCellText($s) {
55 if ($s === '') return '&nbsp;';
56 return text($s);
59 // Check authorization.
60 $thisauth = acl_check('patients', 'med');
61 if (!$thisauth) die(xlt('Not authorized'));
63 <html>
64 <head>
65 <?php html_header_show();?>
67 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
68 <title><?php echo xlt('Procedure Orders and Reports'); ?></title>
70 <style>
72 tr.head { font-size:10pt; background-color:#cccccc; text-align:center; }
73 tr.detail { font-size:10pt; }
74 a, a:visited, a:hover { color:#0000cc; }
76 </style>
78 <style type="text/css">@import url(<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar.css);</style>
79 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar.js"></script>
80 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
81 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar_setup.js"></script>
83 <script type="text/javascript" src="../../library/dialog.js"></script>
84 <script type="text/javascript" src="../../library/textformat.js"></script>
86 <script language="JavaScript">
88 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
90 function openResults(orderid) {
91 top.restoreSession();
92 window.open('single_order_results.php?orderid=' + orderid);
95 </script>
97 </head>
99 <body class="body_top">
100 <form method='post' action='list_reports.php' enctype='multipart/form-data'
101 onsubmit='return validate(this)'>
103 <?php
104 $messages = array();
105 $errmsg = poll_hl7_results($messages);
106 foreach ($messages as $message) {
107 echo text($message) . "<br />\n";
109 if ($errmsg) {
110 echo "<font color='red'>" . text($errmsg) . "</font><br />\n";
113 // Process uploaded file if there is one.
114 if (!empty($_FILES['userfile']['name'])) { // if upload was attempted
115 if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
116 $hl7 = file_get_contents($_FILES['userfile']['tmp_name']);
117 $msg = receive_hl7_results($hl7);
118 $message = xl('Upload processed successfully');
119 if ($msg) {
120 $message = xl('Error processing upload') . ": " . $msg;
122 echo text($message) . "<br />\n";
124 else {
125 echo "<font color='red'>" . xlt('Upload failed!') . "</font><br />\n";
129 $form_from_date = empty($_POST['form_from_date']) ? '' : trim($_POST['form_from_date']);
130 $form_to_date = empty($_POST['form_to_date']) ? '' : trim($_POST['form_to_date']);
131 // if (empty($form_to_date)) $form_to_date = $form_from_date;
133 $form_reviewed = empty($_POST['form_reviewed']) ? 3 : intval($_POST['form_reviewed']);
135 $form_patient = !empty($_POST['form_patient']);
137 $form_provider = empty($_POST['form_provider']) ? 0 : intval($_POST['form_provider']);
140 <table width='100%'>
141 <tr>
142 <td class='text' align='center'>
143 &nbsp;<?php echo xlt('From'); ?>:
144 <input type='text' size='8' name='form_from_date' id='form_from_date'
145 value='<?php echo attr($form_from_date); ?>'
146 title='<?php echo xla('yyyy-mm-dd'); ?>'
147 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />
148 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
149 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
150 title='<?php echo xla('Click here to choose a date'); ?>' />
152 &nbsp;<?php echo xlt('To'); ?>:
153 <input type='text' size='8' name='form_to_date' id='form_to_date'
154 value='<?php echo attr($form_to_date); ?>'
155 title='<?php echo xla('yyyy-mm-dd'); ?>'
156 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />
157 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
158 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
159 title='<?php echo xla('Click here to choose a date'); ?>' />
161 &nbsp;
162 <select name='form_reviewed'>
163 <?php
164 foreach (array('1' => xl('All'), '2' => xl('Reviewed'), '3' => xl('Unreviewed'),
165 '4' => xl('Unreceived')) as $key => $value) {
166 echo "<option value='$key'";
167 if ($key == $form_reviewed) echo " selected";
168 echo ">" . text($value) . "</option>\n";
171 </select>
173 &nbsp;
174 <?php
175 generate_form_field(array('data_type' => 10, 'field_id' => 'provider',
176 'empty_title' => '-- All Providers --'), $form_provider);
178 </td>
179 </tr>
180 <tr>
181 <td class='text' align='center'>
182 <input type='checkbox' name='form_patient' value='1'
183 <?php if ($form_patient) echo 'checked '; ?>/>Current Patient Only
185 &nbsp;
186 <span title='<?php echo xla('You may optionally upload HL7 results from a file'); ?>'>
187 <?php echo xlt('Upload'); ?>:
188 <input type='hidden' name='MAX_FILE_SIZE' value='4000000' />
189 <input type='file' name='userfile' size='8' />
190 </span>
192 &nbsp;
193 <input type='submit' name='form_refresh' value=<?php echo xla('Submit'); ?>>
194 </td>
195 </tr>
196 </table>
198 <table width='100%' cellpadding='1' cellspacing='2'>
200 <tr class='head'>
201 <td colspan='2'><?php echo xlt('Patient' ); ?></td>
202 <td colspan='2'><?php echo xlt('Order' ); ?></td>
203 <td colspan='2'><?php echo xlt('Procedure'); ?></td>
204 <td colspan='2'><?php echo xlt('Report' ); ?></td>
205 </tr>
207 <tr class='head'>
208 <td><?php echo xlt('Name' ); ?></td>
209 <td><?php echo xlt('ID' ); ?></td>
210 <td><?php echo xlt('Date' ); ?></td>
211 <td><?php echo xlt('ID' ); ?></td>
212 <td><?php echo xlt('Code' ); ?></td>
213 <td><?php echo xlt('Description'); ?></td>
214 <td><?php echo xlt('Date' ); ?></td>
215 <td><?php echo xlt('Status' ); ?></td>
216 <!-- <td><?php echo xlt('Reviewed' ); ?></td> -->
217 </tr>
219 <?php
220 $selects =
221 "po.procedure_order_id, po.date_ordered, pc.procedure_order_seq, pc.procedure_code, " .
222 "pc.procedure_name, " .
223 "pr.procedure_report_id, pr.date_report, pr.report_status, pr.review_status";
225 $joins =
226 "LEFT JOIN procedure_report AS pr ON pr.procedure_order_id = po.procedure_order_id AND " .
227 "pr.procedure_order_seq = pc.procedure_order_seq";
229 $orderby =
230 "po.date_ordered, po.procedure_order_id, " .
231 "pc.procedure_order_seq, pr.procedure_report_id";
233 $where = "1 = 1";
234 $sqlBindArray = array();
236 if (!empty($form_from_date)) {
237 $where .= " AND po.date_ordered >= ?";
238 $sqlBindArray[] = $form_from_date;
240 if (!empty($form_to_date)) {
241 $where .= " AND po.date_ordered <= ?";
242 $sqlBindArray[] = $form_to_date;
245 if ($form_patient) {
246 $where .= " AND po.patient_id = ?";
247 $sqlBindArray[] = $pid;
250 if ($form_provider) {
251 $where .= " AND po.provider_id = ?";
252 $sqlBindArray[] = $form_provider;
255 if ($form_reviewed == 2) {
256 $where .= " AND pr.procedure_report_id IS NOT NULL AND pr.review_status = 'reviewed'";
258 else if ($form_reviewed == 3) {
259 $where .= " AND pr.procedure_report_id IS NOT NULL AND pr.review_status != 'reviewed'";
261 else if ($form_reviewed == 4) {
262 $where .= " AND pr.procedure_report_id IS NULL";
265 $query = "SELECT po.patient_id, " .
266 "pd.fname, pd.mname, pd.lname, pd.pubpid, $selects " .
267 "FROM procedure_order AS po " .
268 "LEFT JOIN procedure_order_code AS pc ON pc.procedure_order_id = po.procedure_order_id " .
269 "LEFT JOIN patient_data AS pd ON pd.pid = po.patient_id $joins " .
270 "WHERE $where " .
271 "ORDER BY pd.lname, pd.fname, pd.mname, po.patient_id, $orderby";
273 $res = sqlStatement($query, $sqlBindArray);
275 $lastptid = -1;
276 $lastpoid = -1;
277 $lastpcid = -1;
278 $encount = 0;
279 $lino = 0;
280 $extra_html = '';
282 while ($row = sqlFetchArray($res)) {
283 $patient_id = empty($row['patient_id' ]) ? 0 : ($row['patient_id' ] + 0);
284 $order_id = empty($row['procedure_order_id' ]) ? 0 : ($row['procedure_order_id' ] + 0);
285 $order_seq = empty($row['procedure_order_seq']) ? 0 : ($row['procedure_order_seq'] + 0);
286 $date_ordered = empty($row['date_ordered']) ? '' : $row['date_ordered'];
287 $procedure_code = empty($row['procedure_code']) ? '' : $row['procedure_code'];
288 $procedure_name = empty($row['procedure_name']) ? '' : $row['procedure_name'];
289 $report_id = empty($row['procedure_report_id']) ? 0 : ($row['procedure_report_id'] + 0);
290 $date_report = empty($row['date_report']) ? '' : $row['date_report'];
291 $report_status = empty($row['report_status']) ? '' : $row['report_status'];
292 $review_status = empty($row['review_status']) ? '' : $row['review_status'];
294 $ptname = $row['lname'];
295 if ($row['fname'] || $row['mname'])
296 $ptname .= ', ' . $row['fname'] . ' ' . $row['mname'];
298 if ($lastpoid != $order_id || $lastpcid != $order_seq) {
299 ++$encount;
301 $bgcolor = "#" . (($encount & 1) ? "ddddff" : "ffdddd");
303 echo " <tr class='detail' bgcolor='$bgcolor'>\n";
305 // Generate patient columns.
306 if ($lastptid != $patient_id) {
307 $lastpoid = -1;
308 echo " <td>" . text($ptname) . "</td>\n";
309 echo " <td>" . text($row['pubpid']) . "</td>\n";
311 else {
312 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
315 // Generate order columns.
316 if ($lastpoid != $order_id) {
317 $lastpcid = -1;
318 echo " <td><a href='javascript:openResults($order_id)'>";
319 echo text($date_ordered);
320 echo "</a></td>\n";
321 echo " <td>" . text($order_id) . "</td>\n";
323 else {
324 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
327 // Generate procedure columns.
328 if ($order_seq && $lastpcid != $order_seq) {
329 echo " <td>" . text($procedure_code) . "</td>\n";
330 echo " <td>" . text($procedure_name) . "</td>\n";
332 else {
333 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
336 // Generate report columns.
337 if ($report_id) {
338 echo " <td>" . text($date_report) . "</td>\n";
340 // echo " <td>" . text($report_status) . "</td>\n";
341 // echo " <td>" . text($review_status) . "</td>\n";
343 echo " <td title='" . xla('Check mark indicates reviewed') . "'>";
344 echo myCellText(getListItem('proc_rep_status', $report_status));
345 if ($review_status == 'reviewed') {
346 echo " &#x2713;"; // unicode check mark character
348 echo "</td>\n";
351 else {
352 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
355 echo " </tr>\n";
357 $lastptid = $patient_id;
358 $lastpoid = $order_id;
359 $lastpcid = $order_seq;
360 ++$lino;
364 </table>
366 <script language='JavaScript'>
368 // Initialize calendar widgets for "from" and "to" dates.
369 Calendar.setup({inputField:'form_from_date', ifFormat:'%Y-%m-%d',
370 button:'img_from_date'});
371 Calendar.setup({inputField:'form_to_date', ifFormat:'%Y-%m-%d',
372 button:'img_to_date'});
374 </script>
376 </form>
377 </body>
378 </html>