Changes to support electronic lab ordering and results.
[openemr.git] / interface / orders / list_reports.php
blob1b60d44d234ba67c3b66c3938641f88c77399508
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 require_once("../globals.php");
23 require_once("$srcdir/acl.inc");
24 require_once("$srcdir/formdata.inc.php");
25 require_once("$srcdir/options.inc.php");
26 require_once("$srcdir/formatting.inc.php");
27 require_once("./receive_hl7_results.inc.php");
29 function getListItem($listid, $value) {
30 $lrow = sqlQuery("SELECT title FROM list_options " .
31 "WHERE list_id = ? AND option_id = ?",
32 array($listid, $value));
33 $tmp = xl_list_label($lrow['title']);
34 if (empty($tmp)) $tmp = "($report_status)";
35 return $tmp;
38 function myCellText($s) {
39 if ($s === '') return '&nbsp;';
40 return text($s);
43 // Check authorization.
44 $thisauth = acl_check('patients', 'med');
45 if (!$thisauth) die(xl('Not authorized'));
47 <html>
48 <head>
49 <?php html_header_show();?>
51 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
52 <title><?php xl('Procedure Orders and Reports','e'); ?></title>
54 <style>
56 tr.head { font-size:10pt; background-color:#cccccc; text-align:center; }
57 tr.detail { font-size:10pt; }
58 a, a:visited, a:hover { color:#0000cc; }
60 </style>
62 <style type="text/css">@import url(<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar.css);</style>
63 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar.js"></script>
64 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
65 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar_setup.js"></script>
67 <script type="text/javascript" src="../../library/dialog.js"></script>
68 <script type="text/javascript" src="../../library/textformat.js"></script>
70 <script language="JavaScript">
72 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
74 function openResults(orderid) {
75 top.restoreSession();
76 window.open('single_order_results.php?orderid=' + orderid);
79 </script>
81 </head>
83 <body class="body_top">
84 <form method='post' action='list_reports.php'
85 onsubmit='return validate(this)'>
87 <?php
88 $messages = array();
89 $errmsg = poll_hl7_results($messages);
90 foreach ($messages as $message) {
91 echo text($message) . "<br />\n";
93 if ($errmsg) {
94 echo "<font color='red'>" . text($errmsg) . "</font><br />\n";
97 $form_from_date = formData('form_from_date','P',true);
98 $form_to_date = formData('form_to_date','P',true);
99 if (empty($form_to_date)) $form_to_date = $form_from_date;
101 $form_reviewed = 0 + formData('form_reviewed');
102 if (!$form_reviewed) $form_reviewed = 3;
104 $form_patient = !empty($_POST['form_patient']);
107 <table>
108 <tr>
109 <td class='text'>
110 &nbsp;<?php xl('From','e'); ?>:
111 <input type='text' size='10' name='form_from_date' id='form_from_date'
112 value='<?php echo $form_from_date ?>'
113 title='<?php xl('yyyy-mm-dd','e'); ?>'
114 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />
115 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
116 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
117 title='<?php xl('Click here to choose a date','e'); ?>' />
119 &nbsp;<?php xl('To','e'); ?>:
120 <input type='text' size='10' name='form_to_date' id='form_to_date'
121 value='<?php echo $form_to_date ?>'
122 title='<?php xl('yyyy-mm-dd','e'); ?>'
123 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />
124 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
125 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
126 title='<?php xl('Click here to choose a date','e'); ?>' />
128 &nbsp;
129 <select name='form_reviewed'>
130 <?php
131 foreach (array('1' => xlt('All'), '2' => xlt('Reviewed'), '3' => xlt('Unreviewed'),
132 '4' => xlt('Unreceived')) as $key => $value) {
133 echo "<option value='$key'";
134 if ($key == $form_reviewed) echo " selected";
135 echo ">$value</option>\n";
138 </select>
140 &nbsp;
141 <input type='checkbox' name='form_patient' value='1'
142 <?php if ($form_patient) echo 'checked '; ?>/>Current Patient Only
144 &nbsp;
145 <input type='submit' name='form_refresh' value=<?php xl('Refresh','e'); ?>>
146 </td>
147 </tr>
148 </table>
150 <table width='100%' cellpadding='1' cellspacing='2'>
152 <tr class='head'>
153 <td colspan='2'><?php echo xlt('Patient' ); ?></td>
154 <td colspan='2'><?php echo xlt('Order' ); ?></td>
155 <td colspan='2'><?php echo xlt('Procedure'); ?></td>
156 <td colspan='2'><?php echo xlt('Report' ); ?></td>
157 </tr>
159 <tr class='head'>
160 <td><?php echo xlt('Name' ); ?></td>
161 <td><?php echo xlt('ID' ); ?></td>
162 <td><?php echo xlt('Date' ); ?></td>
163 <td><?php echo xlt('ID' ); ?></td>
164 <td><?php echo xlt('Code' ); ?></td>
165 <td><?php echo xlt('Description'); ?></td>
166 <td><?php echo xlt('Date' ); ?></td>
167 <td><?php echo xlt('Status' ); ?></td>
168 <!-- <td><?php echo xlt('Reviewed' ); ?></td> -->
169 </tr>
171 <?php
172 $selects =
173 "po.procedure_order_id, po.date_ordered, pc.procedure_order_seq, pc.procedure_code, " .
174 "pc.procedure_name, " .
175 "pr.procedure_report_id, pr.date_report, pr.report_status, pr.review_status";
177 $joins =
178 "LEFT JOIN procedure_report AS pr ON pr.procedure_order_id = po.procedure_order_id AND " .
179 "pr.procedure_order_seq = pc.procedure_order_seq";
181 $orderby =
182 "po.date_ordered, po.procedure_order_id, " .
183 "pc.procedure_order_seq, pr.procedure_report_id";
185 $where = "1 = 1";
186 if (!empty($form_from_date)) {
187 $where .= " AND po.date_ordered >= '$form_from_date'";
189 if (!empty($form_to_date)) {
190 $where .= " AND po.date_ordered <= '$form_to_date'";
193 if ($form_patient) {
194 $where .= " AND po.patient_id = '$pid'";
197 if ($form_reviewed == 2) {
198 $where .= " AND pr.procedure_report_id IS NOT NULL AND pr.review_status = 'reviewed'";
200 else if ($form_reviewed == 3) {
201 $where .= " AND pr.procedure_report_id IS NOT NULL AND pr.review_status != 'reviewed'";
203 else if ($form_reviewed == 4) {
204 $where .= " AND pr.procedure_report_id IS NULL";
207 $query = "SELECT po.patient_id, " .
208 "pd.fname, pd.mname, pd.lname, pd.pubpid, $selects " .
209 "FROM procedure_order AS po " .
210 "LEFT JOIN procedure_order_code AS pc ON pc.procedure_order_id = po.procedure_order_id " .
211 "LEFT JOIN patient_data AS pd ON pd.pid = po.patient_id $joins " .
212 "WHERE $where " .
213 "ORDER BY pd.lname, pd.fname, pd.mname, po.patient_id, $orderby";
215 $res = sqlStatement($query);
217 $lastptid = -1;
218 $lastpoid = -1;
219 $lastpcid = -1;
220 $encount = 0;
221 $lino = 0;
222 $extra_html = '';
224 while ($row = sqlFetchArray($res)) {
225 $patient_id = empty($row['patient_id' ]) ? 0 : ($row['patient_id' ] + 0);
226 $order_id = empty($row['procedure_order_id' ]) ? 0 : ($row['procedure_order_id' ] + 0);
227 $order_seq = empty($row['procedure_order_seq']) ? 0 : ($row['procedure_order_seq'] + 0);
228 $date_ordered = empty($row['date_ordered']) ? '' : $row['date_ordered'];
229 $procedure_code = empty($row['procedure_code']) ? '' : $row['procedure_code'];
230 $procedure_name = empty($row['procedure_name']) ? '' : $row['procedure_name'];
231 $report_id = empty($row['procedure_report_id']) ? 0 : ($row['procedure_report_id'] + 0);
232 $date_report = empty($row['date_report']) ? '' : $row['date_report'];
233 $report_status = empty($row['report_status']) ? '' : $row['report_status'];
234 $review_status = empty($row['review_status']) ? '' : $row['review_status'];
236 $ptname = $row['lname'];
237 if ($row['fname'] || $row['mname'])
238 $ptname .= ', ' . $row['fname'] . ' ' . $row['mname'];
240 if ($lastpoid != $order_id || $lastpcid != $order_seq) {
241 ++$encount;
243 $bgcolor = "#" . (($encount & 1) ? "ddddff" : "ffdddd");
245 echo " <tr class='detail' bgcolor='$bgcolor'>\n";
247 // Generate patient columns.
248 if ($lastptid != $patient_id) {
249 $lastpoid = -1;
250 echo " <td>" . text($ptname) . "</td>\n";
251 echo " <td>" . text($row['pubpid']) . "</td>\n";
253 else {
254 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
257 // Generate order columns.
258 if ($lastpoid != $order_id) {
259 $lastpcid = -1;
260 echo " <td><a href='javascript:openResults($order_id)'>";
261 echo text($date_ordered);
262 echo "</a></td>\n";
263 echo " <td>" . text($order_id) . "</td>\n";
265 else {
266 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
269 // Generate procedure columns.
270 if ($order_seq && $lastpcid != $order_seq) {
271 echo " <td>" . text($procedure_code) . "</td>\n";
272 echo " <td>" . text($procedure_name) . "</td>\n";
274 else {
275 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
278 // Generate report columns.
279 if ($report_id) {
280 echo " <td>" . text($date_report) . "</td>\n";
282 // echo " <td>" . text($report_status) . "</td>\n";
283 // echo " <td>" . text($review_status) . "</td>\n";
285 echo " <td title='" . xla('Check mark indicates reviewed') . "'>";
286 echo myCellText(getListItem('proc_rep_status', $report_status));
287 if ($review_status == 'reviewed') {
288 echo " &#x2713;"; // unicode check mark character
290 echo "</td>\n";
293 else {
294 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
297 echo " </tr>\n";
299 $lastptid = $patient_id;
300 $lastpoid = $order_id;
301 $lastpcid = $order_seq;
302 ++$lino;
306 </table>
308 <script language='JavaScript'>
310 // Initialize calendar widgets for "from" and "to" dates.
311 Calendar.setup({inputField:'form_from_date', ifFormat:'%Y-%m-%d',
312 button:'img_from_date'});
313 Calendar.setup({inputField:'form_to_date', ifFormat:'%Y-%m-%d',
314 button:'img_to_date'});
316 </script>
318 </form>
319 </body>
320 </html>