Enhancements to Electronic Reports page: send unsent orders, open order summary.
[openemr.git] / interface / orders / list_reports.php
blob0f9a0d6c06fe60f62acdf560fda127c63591d5f6
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");
31 require_once("./gen_hl7_order.inc.php");
33 /**
34 * Get a list item title, translating if required.
36 * @param string $listid List identifier.
37 * @param string $value List item identifier.
38 * @return string The item's title.
40 function getListItem($listid, $value) {
41 $lrow = sqlQuery("SELECT title FROM list_options " .
42 "WHERE list_id = ? AND option_id = ?",
43 array($listid, $value));
44 $tmp = xl_list_label($lrow['title']);
45 if (empty($tmp)) $tmp = "($report_status)";
46 return $tmp;
49 /**
50 * Adapt text to be suitable as the contents of a table cell.
52 * @param string $s Input text.
53 * @return string Output text.
55 function myCellText($s) {
56 if ($s === '') return '&nbsp;';
57 return text($s);
60 // Check authorization.
61 $thisauth = acl_check('patients', 'med');
62 if (!$thisauth) die(xlt('Not authorized'));
64 $errmsg = '';
66 // Send selected unsent orders if requested. This does not support downloading
67 // very well as it will only send the first of those.
68 if ($_POST['form_xmit']) {
69 foreach ($_POST['form_cb'] as $formid) {
70 $row = sqlQuery("SELECT lab_id FROM procedure_order WHERE " .
71 "procedure_order_id = ?", array($formid));
72 $ppid = intval($row['lab_id']);
73 $hl7 = '';
74 $errmsg = gen_hl7_order($formid, $hl7);
75 if (empty($errmsg)) {
76 $errmsg = send_hl7_order($ppid, $hl7);
78 if ($errmsg) break;
79 sqlStatement("UPDATE procedure_order SET date_transmitted = NOW() WHERE " .
80 "procedure_order_id = ?", array($formid));
84 <html>
85 <head>
86 <?php html_header_show();?>
88 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
89 <title><?php echo xlt('Procedure Orders and Reports'); ?></title>
91 <style>
93 tr.head { font-size:10pt; background-color:#cccccc; text-align:center; }
94 tr.detail { font-size:10pt; }
95 a, a:visited, a:hover { color:#0000cc; }
97 </style>
99 <style type="text/css">@import url(<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar.css);</style>
100 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar.js"></script>
101 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
102 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar_setup.js"></script>
104 <script type="text/javascript" src="../../library/dialog.js"></script>
105 <script type="text/javascript" src="../../library/textformat.js"></script>
107 <script language="JavaScript">
109 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
111 function openResults(orderid) {
112 top.restoreSession();
113 // window.open('single_order_results.php?orderid=' + orderid);
114 document.location.href = 'single_order_results.php?orderid=' + orderid;
117 </script>
119 </head>
121 <body class="body_top">
122 <form method='post' action='list_reports.php' enctype='multipart/form-data'
123 onsubmit='return validate(this)'>
125 <?php
126 if ($errmsg) {
127 echo "<font color='red'>" . text($errmsg) . "</font><br />\n";
129 $messages = array();
130 $errmsg = poll_hl7_results($messages);
131 foreach ($messages as $message) {
132 echo text($message) . "<br />\n";
134 if ($errmsg) {
135 echo "<font color='red'>" . text($errmsg) . "</font><br />\n";
138 // Process uploaded file if there is one.
139 if (!empty($_FILES['userfile']['name'])) { // if upload was attempted
140 if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
141 $hl7 = file_get_contents($_FILES['userfile']['tmp_name']);
142 $msg = receive_hl7_results($hl7);
143 $message = xl('Upload processed successfully');
144 if ($msg) {
145 $message = xl('Error processing upload') . ": " . $msg;
147 echo text($message) . "<br />\n";
149 else {
150 echo "<font color='red'>" . xlt('Upload failed!') . "</font><br />\n";
154 $form_from_date = empty($_POST['form_from_date']) ? '' : trim($_POST['form_from_date']);
155 $form_to_date = empty($_POST['form_to_date']) ? '' : trim($_POST['form_to_date']);
156 // if (empty($form_to_date)) $form_to_date = $form_from_date;
158 $form_reviewed = empty($_POST['form_reviewed']) ? 3 : intval($_POST['form_reviewed']);
160 $form_patient = !empty($_POST['form_patient']);
162 $form_provider = empty($_POST['form_provider']) ? 0 : intval($_POST['form_provider']);
165 <table width='100%'>
166 <tr>
167 <td class='text' align='center'>
168 &nbsp;<?php echo xlt('From'); ?>:
169 <input type='text' size='8' name='form_from_date' id='form_from_date'
170 value='<?php echo attr($form_from_date); ?>'
171 title='<?php echo xla('yyyy-mm-dd'); ?>'
172 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />
173 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
174 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
175 title='<?php echo xla('Click here to choose a date'); ?>' />
177 &nbsp;<?php echo xlt('To'); ?>:
178 <input type='text' size='8' name='form_to_date' id='form_to_date'
179 value='<?php echo attr($form_to_date); ?>'
180 title='<?php echo xla('yyyy-mm-dd'); ?>'
181 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />
182 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
183 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
184 title='<?php echo xla('Click here to choose a date'); ?>' />
186 &nbsp;
187 <select name='form_reviewed'>
188 <?php
189 foreach (array(
190 '1' => xl('All'),
191 '2' => xl('Reviewed'),
192 '3' => xl('Received, not reviewed'),
193 '4' => xl('Sent, not received'),
194 '5' => xl('Not sent'),
195 ) as $key => $value) {
196 echo "<option value='$key'";
197 if ($key == $form_reviewed) echo " selected";
198 echo ">" . text($value) . "</option>\n";
201 </select>
203 &nbsp;
204 <?php
205 generate_form_field(array('data_type' => 10, 'field_id' => 'provider',
206 'empty_title' => '-- All Providers --'), $form_provider);
208 </td>
209 </tr>
210 <tr>
211 <td class='text' align='center'>
212 <input type='checkbox' name='form_patient' value='1'
213 <?php if ($form_patient) echo 'checked '; ?>/>Current Patient Only
215 &nbsp;
216 <span title='<?php echo xla('You may optionally upload HL7 results from a file'); ?>'>
217 <?php echo xlt('Upload'); ?>:
218 <input type='hidden' name='MAX_FILE_SIZE' value='4000000' />
219 <input type='file' name='userfile' size='8' />
220 </span>
222 &nbsp;
223 <input type='submit' name='form_refresh' value=<?php echo xla('Submit'); ?>>
224 </td>
225 </tr>
226 </table>
228 <table width='100%' cellpadding='1' cellspacing='2'>
230 <tr class='head'>
231 <td colspan='2'><?php echo xlt('Patient' ); ?></td>
232 <td colspan='2'><?php echo xlt('Order' ); ?></td>
233 <td colspan='2'><?php echo xlt('Procedure'); ?></td>
234 <td colspan='2'><?php echo xlt('Report' ); ?></td>
235 </tr>
237 <tr class='head'>
238 <td><?php echo xlt('Name' ); ?></td>
239 <td><?php echo xlt('ID' ); ?></td>
240 <td><?php echo xlt('Date' ); ?></td>
241 <td><?php echo xlt('ID' ); ?></td>
242 <td><?php echo xlt('Code' ); ?></td>
243 <td><?php echo xlt('Description'); ?></td>
244 <td><?php echo xlt('Date' ); ?></td>
245 <td><?php echo xlt('Status' ); ?></td>
246 <!-- <td><?php echo xlt('Reviewed' ); ?></td> -->
247 </tr>
249 <?php
250 $selects =
251 "po.patient_id, po.procedure_order_id, po.date_ordered, po.date_transmitted, " .
252 "pc.procedure_order_seq, pc.procedure_code, pc.procedure_name, " .
253 "pr.procedure_report_id, pr.date_report, pr.report_status, pr.review_status";
255 $joins =
256 "LEFT JOIN procedure_report AS pr ON pr.procedure_order_id = po.procedure_order_id AND " .
257 "pr.procedure_order_seq = pc.procedure_order_seq";
259 $orderby =
260 "po.date_ordered, po.procedure_order_id, " .
261 "pc.procedure_order_seq, pr.procedure_report_id";
263 $where = "1 = 1";
264 $sqlBindArray = array();
266 if (!empty($form_from_date)) {
267 $where .= " AND po.date_ordered >= ?";
268 $sqlBindArray[] = $form_from_date;
270 if (!empty($form_to_date)) {
271 $where .= " AND po.date_ordered <= ?";
272 $sqlBindArray[] = $form_to_date;
275 if ($form_patient) {
276 $where .= " AND po.patient_id = ?";
277 $sqlBindArray[] = $pid;
280 if ($form_provider) {
281 $where .= " AND po.provider_id = ?";
282 $sqlBindArray[] = $form_provider;
285 if ($form_reviewed == 2) {
286 $where .= " AND pr.procedure_report_id IS NOT NULL AND pr.review_status = 'reviewed'";
288 else if ($form_reviewed == 3) {
289 $where .= " AND pr.procedure_report_id IS NOT NULL AND pr.review_status != 'reviewed'";
291 else if ($form_reviewed == 4) {
292 $where .= " AND po.date_transmitted IS NOT NULL AND pr.procedure_report_id IS NULL";
294 else if ($form_reviewed == 5) {
295 $where .= " AND po.date_transmitted IS NULL AND pr.procedure_report_id IS NULL";
298 $query = "SELECT " .
299 "pd.fname, pd.mname, pd.lname, pd.pubpid, $selects " .
300 "FROM procedure_order AS po " .
301 "LEFT JOIN procedure_order_code AS pc ON pc.procedure_order_id = po.procedure_order_id " .
302 "LEFT JOIN patient_data AS pd ON pd.pid = po.patient_id $joins " .
303 "WHERE $where " .
304 "ORDER BY pd.lname, pd.fname, pd.mname, po.patient_id, $orderby";
306 $res = sqlStatement($query, $sqlBindArray);
308 $lastptid = -1;
309 $lastpoid = -1;
310 $lastpcid = -1;
311 $encount = 0;
312 $lino = 0;
313 $extra_html = '';
314 $num_checkboxes = 0;
316 while ($row = sqlFetchArray($res)) {
317 $patient_id = empty($row['patient_id' ]) ? 0 : ($row['patient_id' ] + 0);
318 $order_id = empty($row['procedure_order_id' ]) ? 0 : ($row['procedure_order_id' ] + 0);
319 $order_seq = empty($row['procedure_order_seq']) ? 0 : ($row['procedure_order_seq'] + 0);
320 $date_ordered = empty($row['date_ordered' ]) ? '' : $row['date_ordered'];
321 $date_transmitted = empty($row['date_transmitted' ]) ? '' : $row['date_transmitted'];
322 $procedure_code = empty($row['procedure_code' ]) ? '' : $row['procedure_code'];
323 $procedure_name = empty($row['procedure_name' ]) ? '' : $row['procedure_name'];
324 $report_id = empty($row['procedure_report_id']) ? 0 : ($row['procedure_report_id'] + 0);
325 $date_report = empty($row['date_report' ]) ? '' : $row['date_report'];
326 $report_status = empty($row['report_status' ]) ? '' : $row['report_status'];
327 $review_status = empty($row['review_status' ]) ? '' : $row['review_status'];
329 $ptname = $row['lname'];
330 if ($row['fname'] || $row['mname'])
331 $ptname .= ', ' . $row['fname'] . ' ' . $row['mname'];
333 if ($lastpoid != $order_id || $lastpcid != $order_seq) {
334 ++$encount;
336 $bgcolor = "#" . (($encount & 1) ? "ddddff" : "ffdddd");
338 echo " <tr class='detail' bgcolor='$bgcolor'>\n";
340 // Generate patient columns.
341 if ($lastptid != $patient_id) {
342 $lastpoid = -1;
343 echo " <td>" . text($ptname) . "</td>\n";
344 echo " <td>" . text($row['pubpid']) . "</td>\n";
346 else {
347 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
350 // Generate order columns.
351 if ($lastpoid != $order_id) {
352 $lastpcid = -1;
353 echo " <td>";
354 // Checkbox to support sending unsent orders, disabled if sent.
355 echo "<input type='checkbox' name='form_cb[$order_id]' value='$order_id' ";
356 if ($date_transmitted) {
357 echo "disabled";
358 } else {
359 echo "checked";
360 ++$num_checkboxes;
362 echo " />";
363 // Order date comes with a link to open results in the same frame.
364 echo "<a href='javascript:openResults($order_id)' ";
365 echo "title='" . xla('Click for results') . "'>";
366 echo text($date_ordered);
367 echo "</a></td>\n";
368 echo " <td>";
369 // Order ID comes with a link to open the manifest in a new window/tab.
370 echo "<a href='" . $GLOBALS['webroot'];
371 echo "/interface/orders/order_manifest.php?orderid=";
372 echo attr($order_id);
373 echo "' target='_blank' onclick='top.restoreSession()' ";
374 echo "title='" . xla('Click for order summary') . "'>";
375 echo text($order_id);
376 echo "</a></td>\n";
378 else {
379 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
382 // Generate procedure columns.
383 if ($order_seq && $lastpcid != $order_seq) {
384 echo " <td>" . text($procedure_code) . "</td>\n";
385 echo " <td>" . text($procedure_name) . "</td>\n";
387 else {
388 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
391 // Generate report columns.
392 if ($report_id) {
393 echo " <td>" . text($date_report) . "</td>\n";
394 echo " <td title='" . xla('Check mark indicates reviewed') . "'>";
395 echo myCellText(getListItem('proc_rep_status', $report_status));
396 if ($review_status == 'reviewed') {
397 echo " &#x2713;"; // unicode check mark character
399 echo "</td>\n";
401 else {
402 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
405 echo " </tr>\n";
407 $lastptid = $patient_id;
408 $lastpoid = $order_id;
409 $lastpcid = $order_seq;
410 ++$lino;
414 </table>
416 <?php if ($num_checkboxes) { ?>
417 <center><p>
418 <input type='submit' name='form_xmit' value='<?php echo xla('Transmit Selected Orders'); ?>' />
419 </p></center>
420 <?php } ?>
422 <script language='JavaScript'>
424 // Initialize calendar widgets for "from" and "to" dates.
425 Calendar.setup({inputField:'form_from_date', ifFormat:'%Y-%m-%d',
426 button:'img_from_date'});
427 Calendar.setup({inputField:'form_to_date', ifFormat:'%Y-%m-%d',
428 button:'img_to_date'});
430 </script>
432 </form>
433 </body>
434 </html>