Added transmit timestamp to orders and logic to set and check it.
[openemr.git] / interface / orders / list_reports.php
blob76e39bc9f9b304a52c90f31444e69719bd97a7bc
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(
165 '1' => xl('All'),
166 '2' => xl('Reviewed'),
167 '3' => xl('Received, not reviewed'),
168 '4' => xl('Sent, not received'),
169 '5' => xl('Not sent'),
170 ) as $key => $value) {
171 echo "<option value='$key'";
172 if ($key == $form_reviewed) echo " selected";
173 echo ">" . text($value) . "</option>\n";
176 </select>
178 &nbsp;
179 <?php
180 generate_form_field(array('data_type' => 10, 'field_id' => 'provider',
181 'empty_title' => '-- All Providers --'), $form_provider);
183 </td>
184 </tr>
185 <tr>
186 <td class='text' align='center'>
187 <input type='checkbox' name='form_patient' value='1'
188 <?php if ($form_patient) echo 'checked '; ?>/>Current Patient Only
190 &nbsp;
191 <span title='<?php echo xla('You may optionally upload HL7 results from a file'); ?>'>
192 <?php echo xlt('Upload'); ?>:
193 <input type='hidden' name='MAX_FILE_SIZE' value='4000000' />
194 <input type='file' name='userfile' size='8' />
195 </span>
197 &nbsp;
198 <input type='submit' name='form_refresh' value=<?php echo xla('Submit'); ?>>
199 </td>
200 </tr>
201 </table>
203 <table width='100%' cellpadding='1' cellspacing='2'>
205 <tr class='head'>
206 <td colspan='2'><?php echo xlt('Patient' ); ?></td>
207 <td colspan='2'><?php echo xlt('Order' ); ?></td>
208 <td colspan='2'><?php echo xlt('Procedure'); ?></td>
209 <td colspan='2'><?php echo xlt('Report' ); ?></td>
210 </tr>
212 <tr class='head'>
213 <td><?php echo xlt('Name' ); ?></td>
214 <td><?php echo xlt('ID' ); ?></td>
215 <td><?php echo xlt('Date' ); ?></td>
216 <td><?php echo xlt('ID' ); ?></td>
217 <td><?php echo xlt('Code' ); ?></td>
218 <td><?php echo xlt('Description'); ?></td>
219 <td><?php echo xlt('Date' ); ?></td>
220 <td><?php echo xlt('Status' ); ?></td>
221 <!-- <td><?php echo xlt('Reviewed' ); ?></td> -->
222 </tr>
224 <?php
225 $selects =
226 "po.procedure_order_id, po.date_ordered, pc.procedure_order_seq, pc.procedure_code, " .
227 "pc.procedure_name, " .
228 "pr.procedure_report_id, pr.date_report, pr.report_status, pr.review_status";
230 $joins =
231 "LEFT JOIN procedure_report AS pr ON pr.procedure_order_id = po.procedure_order_id AND " .
232 "pr.procedure_order_seq = pc.procedure_order_seq";
234 $orderby =
235 "po.date_ordered, po.procedure_order_id, " .
236 "pc.procedure_order_seq, pr.procedure_report_id";
238 $where = "1 = 1";
239 $sqlBindArray = array();
241 if (!empty($form_from_date)) {
242 $where .= " AND po.date_ordered >= ?";
243 $sqlBindArray[] = $form_from_date;
245 if (!empty($form_to_date)) {
246 $where .= " AND po.date_ordered <= ?";
247 $sqlBindArray[] = $form_to_date;
250 if ($form_patient) {
251 $where .= " AND po.patient_id = ?";
252 $sqlBindArray[] = $pid;
255 if ($form_provider) {
256 $where .= " AND po.provider_id = ?";
257 $sqlBindArray[] = $form_provider;
260 if ($form_reviewed == 2) {
261 $where .= " AND pr.procedure_report_id IS NOT NULL AND pr.review_status = 'reviewed'";
263 else if ($form_reviewed == 3) {
264 $where .= " AND pr.procedure_report_id IS NOT NULL AND pr.review_status != 'reviewed'";
266 else if ($form_reviewed == 4) {
267 $where .= " AND po.date_transmitted IS NOT NULL AND pr.procedure_report_id IS NULL";
269 else if ($form_reviewed == 5) {
270 $where .= " AND po.date_transmitted IS NULL AND pr.procedure_report_id IS NULL";
273 $query = "SELECT po.patient_id, " .
274 "pd.fname, pd.mname, pd.lname, pd.pubpid, $selects " .
275 "FROM procedure_order AS po " .
276 "LEFT JOIN procedure_order_code AS pc ON pc.procedure_order_id = po.procedure_order_id " .
277 "LEFT JOIN patient_data AS pd ON pd.pid = po.patient_id $joins " .
278 "WHERE $where " .
279 "ORDER BY pd.lname, pd.fname, pd.mname, po.patient_id, $orderby";
281 $res = sqlStatement($query, $sqlBindArray);
283 $lastptid = -1;
284 $lastpoid = -1;
285 $lastpcid = -1;
286 $encount = 0;
287 $lino = 0;
288 $extra_html = '';
290 while ($row = sqlFetchArray($res)) {
291 $patient_id = empty($row['patient_id' ]) ? 0 : ($row['patient_id' ] + 0);
292 $order_id = empty($row['procedure_order_id' ]) ? 0 : ($row['procedure_order_id' ] + 0);
293 $order_seq = empty($row['procedure_order_seq']) ? 0 : ($row['procedure_order_seq'] + 0);
294 $date_ordered = empty($row['date_ordered']) ? '' : $row['date_ordered'];
295 $procedure_code = empty($row['procedure_code']) ? '' : $row['procedure_code'];
296 $procedure_name = empty($row['procedure_name']) ? '' : $row['procedure_name'];
297 $report_id = empty($row['procedure_report_id']) ? 0 : ($row['procedure_report_id'] + 0);
298 $date_report = empty($row['date_report']) ? '' : $row['date_report'];
299 $report_status = empty($row['report_status']) ? '' : $row['report_status'];
300 $review_status = empty($row['review_status']) ? '' : $row['review_status'];
302 $ptname = $row['lname'];
303 if ($row['fname'] || $row['mname'])
304 $ptname .= ', ' . $row['fname'] . ' ' . $row['mname'];
306 if ($lastpoid != $order_id || $lastpcid != $order_seq) {
307 ++$encount;
309 $bgcolor = "#" . (($encount & 1) ? "ddddff" : "ffdddd");
311 echo " <tr class='detail' bgcolor='$bgcolor'>\n";
313 // Generate patient columns.
314 if ($lastptid != $patient_id) {
315 $lastpoid = -1;
316 echo " <td>" . text($ptname) . "</td>\n";
317 echo " <td>" . text($row['pubpid']) . "</td>\n";
319 else {
320 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
323 // Generate order columns.
324 if ($lastpoid != $order_id) {
325 $lastpcid = -1;
326 echo " <td><a href='javascript:openResults($order_id)'>";
327 echo text($date_ordered);
328 echo "</a></td>\n";
329 echo " <td>" . text($order_id) . "</td>\n";
331 else {
332 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
335 // Generate procedure columns.
336 if ($order_seq && $lastpcid != $order_seq) {
337 echo " <td>" . text($procedure_code) . "</td>\n";
338 echo " <td>" . text($procedure_name) . "</td>\n";
340 else {
341 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
344 // Generate report columns.
345 if ($report_id) {
346 echo " <td>" . text($date_report) . "</td>\n";
348 // echo " <td>" . text($report_status) . "</td>\n";
349 // echo " <td>" . text($review_status) . "</td>\n";
351 echo " <td title='" . xla('Check mark indicates reviewed') . "'>";
352 echo myCellText(getListItem('proc_rep_status', $report_status));
353 if ($review_status == 'reviewed') {
354 echo " &#x2713;"; // unicode check mark character
356 echo "</td>\n";
359 else {
360 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
363 echo " </tr>\n";
365 $lastptid = $patient_id;
366 $lastpoid = $order_id;
367 $lastpcid = $order_seq;
368 ++$lino;
372 </table>
374 <script language='JavaScript'>
376 // Initialize calendar widgets for "from" and "to" dates.
377 Calendar.setup({inputField:'form_from_date', ifFormat:'%Y-%m-%d',
378 button:'img_from_date'});
379 Calendar.setup({inputField:'form_to_date', ifFormat:'%Y-%m-%d',
380 button:'img_to_date'});
382 </script>
384 </form>
385 </body>
386 </html>