Changes to support electronic lab ordering and results.
[openemr.git] / interface / orders / single_order_results.php
blobed0360bebbfc45d1569c385722f8ea1d02bd1df7
1 <?php
2 /**
3 * Script to display results for a given procedure order.
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("../orders/lab_exchange_tools.php");
29 // Check authorization.
30 $thisauth = acl_check('patients', 'med');
31 if (!$thisauth) die(xl('Not authorized'));
33 $orderid = intval($_GET['orderid']);
35 function getListItem($listid, $value) {
36 $lrow = sqlQuery("SELECT title FROM list_options " .
37 "WHERE list_id = ? AND option_id = ?",
38 array($listid, $value));
39 $tmp = xl_list_label($lrow['title']);
40 if (empty($tmp)) $tmp = "($report_status)";
41 return $tmp;
44 function myCellText($s) {
45 if ($s === '') return '&nbsp;';
46 return text($s);
49 // Check if the given string already exists in the $aNotes array.
50 // If not, stores it as a new entry.
51 // Either way, returns the corresponding key which is a small integer.
52 function storeNote($s) {
53 global $aNotes;
54 $key = array_search($s, $aNotes);
55 if ($key !== FALSE) return $key;
56 $key = count($aNotes);
57 $aNotes[$key] = $s;
58 return $key;
61 if (!empty($_POST['form_sign_list'])) {
62 if (!acl_check('patients', 'sign')) {
63 die(xl('Not authorized to sign results'));
65 // When signing results we are careful to sign only those reports that were
66 // in the sending form. While this will usually be all the reports linked to
67 // the order it's possible for a new report to come in while viewing these,
68 // and it would be very bad to sign results that nobody has seen!
69 $arrSign = explode(',', $_POST['form_sign_list']);
70 foreach ($arrSign as $id) {
71 sqlStatement("UPDATE procedure_report SET " .
72 "review_status = 'reviewed' WHERE " .
73 "procedure_report_id = ?", array($id));
77 $orow = sqlQuery("SELECT " .
78 "po.procedure_order_id, po.date_ordered, po.diagnoses, " .
79 "po.order_status, po.specimen_type, " .
80 "pd.pubpid, pd.lname, pd.fname, pd.mname, " .
81 "pp.name AS labname, " .
82 "u.lname AS ulname, u.fname AS ufname, u.mname AS umname " .
83 "FROM procedure_order AS po " .
84 "LEFT JOIN patient_data AS pd ON pd.pid = po.patient_id " .
85 "LEFT JOIN procedure_providers AS pp ON pp.ppid = po.lab_id " .
86 "LEFT JOIN users AS u ON u.id = po.provider_id " .
87 "WHERE po.procedure_order_id = ?",
88 array($orderid));
90 <html>
92 <head>
93 <?php html_header_show(); ?>
95 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
96 <title><?php xl('Order Results','e'); ?></title>
98 <style>
100 body {
101 margin: 9pt;
102 font-family: sans-serif;
103 font-size: 1em;
106 tr.head { font-size:10pt; background-color:#cccccc; text-align:center; }
107 tr.detail { font-size:10pt; }
108 a, a:visited, a:hover { color:#0000cc; }
110 table {
111 border-style: solid;
112 border-width: 1px 0px 0px 1px;
113 border-color: black;
116 td, th {
117 border-style: solid;
118 border-width: 0px 1px 1px 0px;
119 border-color: black;
122 </style>
124 <script type="text/javascript" src="../../library/dialog.js"></script>
125 <script type="text/javascript" src="../../library/textformat.js"></script>
127 <script language="JavaScript">
129 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
131 </script>
133 </head>
135 <body>
136 <form method='post' action='single_order_results.php?orderid=<?php echo $orderid; ?>'>
138 <table width='100%' cellpadding='2' cellspacing='0'>
139 <tr bgcolor='#cccccc'>
140 <td width='5%' nowrap><?php echo xlt('Patient ID'); ?></td>
141 <td width='45%'><?php echo myCellText($orow['pubpid']); ?></td>
142 <td width='5%' nowrap><?php echo xlt('Order ID'); ?></td>
143 <td width='45%'><?php echo myCellText($orow['procedure_order_id']); ?></td>
144 </tr>
145 <tr bgcolor='#cccccc'>
146 <td nowrap><?php echo xlt('Patient Name'); ?></td>
147 <td><?php echo myCellText($orow['lname'] . ', ' . $orow['fname'] . ' ' . $orow['mname']); ?></td>
148 <td nowrap><?php echo xlt('Ordered By'); ?></td>
149 <td><?php echo myCellText($orow['ulname'] . ', ' . $orow['ufname'] . ' ' . $orow['umname']); ?></td>
150 </tr>
151 <tr bgcolor='#cccccc'>
152 <td nowrap><?php echo xlt('Order Date'); ?></td>
153 <td><?php echo myCellText(oeFormatShortDate($orow['date_ordered'])); ?></td>
154 <td nowrap><?php echo xlt('Print Date'); ?></td>
155 <td><?php echo oeFormatShortDate(date('Y-m-d')); ?></td>
156 </tr>
157 <tr bgcolor='#cccccc'>
158 <td nowrap><?php echo xlt('Order Status'); ?></td>
159 <td><?php echo myCellText($orow['order_status']); ?></td>
160 <td nowrap><?php echo xlt('Diagnoses'); ?></td>
161 <td><?php echo myCellText($orow['diagnoses']); ?></td>
162 </tr>
163 <tr bgcolor='#cccccc'>
164 <td nowrap><?php echo xlt('Lab'); ?></td>
165 <td><?php echo myCellText($orow['labname']); ?></td>
166 <td nowrap><?php echo xlt('Specimen Type'); ?></td>
167 <td><?php echo myCellText($orow['specimen_type']); ?></td>
168 </tr>
169 </table>
171 &nbsp;<br />
173 <table width='100%' cellpadding='2' cellspacing='0'>
175 <tr class='head'>
176 <td rowspan='2' valign='middle'><?php echo xlt('Ordered Procedure'); ?></td>
177 <td colspan='4'><?php echo xlt('Report'); ?></td>
178 <td colspan='7'><?php echo xlt('Results'); ?></td>
179 </tr>
181 <tr class='head'>
182 <td><?php echo xlt('Reported'); ?></td>
183 <td><?php echo xlt('Specimen'); ?></td>
184 <td><?php echo xlt('Status'); ?></td>
185 <td><?php echo xlt('Note'); ?></td>
186 <td><?php echo xlt('Code'); ?></td>
187 <td><?php echo xlt('Name'); ?></td>
188 <td><?php echo xlt('Abn'); ?></td>
189 <td><?php echo xlt('Value'); ?></td>
190 <td><?php echo xlt('Range'); ?></td>
191 <td><?php echo xlt('Units'); ?></td>
192 <td><?php echo xlt('Note'); ?></td>
193 </tr>
195 <?php
196 $query = "SELECT " .
197 "po.date_ordered, pc.procedure_order_seq, pc.procedure_code, " .
198 "pc.procedure_name, " .
199 "pr.procedure_report_id, pr.date_report, pr.date_collected, pr.specimen_num, " .
200 "pr.report_status, pr.review_status, pr.report_notes " .
201 "FROM procedure_order AS po " .
202 "JOIN procedure_order_code AS pc ON pc.procedure_order_id = po.procedure_order_id " .
203 "LEFT JOIN procedure_report AS pr ON pr.procedure_order_id = po.procedure_order_id AND " .
204 "pr.procedure_order_seq = pc.procedure_order_seq " .
205 "WHERE po.procedure_order_id = ? " .
206 "ORDER BY pc.procedure_order_seq, pr.procedure_report_id";
208 $res = sqlStatement($query, array($orderid));
210 $lastpoid = -1;
211 $lastpcid = -1;
212 $lastprid = -1;
213 $encount = 0;
214 $lino = 0;
215 $extra_html = '';
216 $aNotes = array();
217 $sign_list = '';
219 while ($row = sqlFetchArray($res)) {
220 $order_type_id = empty($row['order_type_id' ]) ? 0 : ($row['order_type_id' ] + 0);
221 $order_seq = empty($row['procedure_order_seq']) ? 0 : ($row['procedure_order_seq'] + 0);
222 $report_id = empty($row['procedure_report_id']) ? 0 : ($row['procedure_report_id'] + 0);
223 $procedure_code = empty($row['procedure_code' ]) ? '' : $row['procedure_code'];
224 $procedure_name = empty($row['procedure_name' ]) ? '' : $row['procedure_name'];
225 $date_report = empty($row['date_report' ]) ? '' : $row['date_report'];
226 $date_collected = empty($row['date_collected' ]) ? '' : substr($row['date_collected'], 0, 16);
227 $specimen_num = empty($row['specimen_num' ]) ? '' : $row['specimen_num'];
228 $report_status = empty($row['report_status' ]) ? '' : $row['report_status'];
229 $review_status = empty($row['review_status' ]) ? 'received' : $row['review_status'];
231 if ($review_status != 'reviewed') {
232 if ($sign_list) $sign_list .= ',';
233 $sign_list .= $report_id;
236 $report_noteid ='&nbsp;';
237 if (!empty($row['report_notes'])) {
238 $report_noteid = 1 + storeNote($row['report_notes']);
241 $query = "SELECT " .
242 "ps.result_code, ps.result_text, ps.abnormal, ps.result, " .
243 "ps.range, ps.result_status, ps.facility, ps.units, ps.comments " .
244 "FROM procedure_result AS ps " .
245 "WHERE ps.procedure_report_id = ? " .
246 "ORDER BY ps.result_code, ps.procedure_result_id";
248 $rres = sqlStatement($query, array($report_id));
249 $rrows = array();
250 while ($rrow = sqlFetchArray($rres)) {
251 $rrows[] = $rrow;
253 if (empty($rrows)) {
254 $rrows[0] = array('result_code' => '');
257 foreach ($rrows as $rrow) {
258 $result_code = empty($rrow['result_code' ]) ? '' : $rrow['result_code'];
259 $result_text = empty($rrow['result_text' ]) ? '' : $rrow['result_text'];
260 $result_abnormal = empty($rrow['abnormal' ]) ? '' : $rrow['abnormal'];
261 $result_result = empty($rrow['result' ]) ? '' : $rrow['result'];
262 $result_units = empty($rrow['units' ]) ? '' : $rrow['units'];
263 $result_facility = empty($rrow['facility' ]) ? '' : $rrow['facility'];
264 $result_comments = empty($rrow['comments' ]) ? '' : $rrow['comments'];
265 $result_range = empty($rrow['range' ]) ? '' : $rrow['range'];
266 $result_status = empty($rrow['result_status' ]) ? '' : $rrow['result_status'];
268 $result_comments = trim($result_comments);
269 $result_noteid = '&nbsp;';
270 if (!empty($result_comments)) {
271 $result_noteid = 1 + storeNote($result_comments);
274 if ($lastpoid != $order_id || $lastpcid != $order_seq) {
275 ++$encount;
277 $bgcolor = "#" . (($encount & 1) ? "ddddff" : "ffdddd");
279 echo " <tr class='detail' bgcolor='$bgcolor'>\n";
281 if ($lastpcid != $order_seq) {
282 $lastprid = -1; // force report fields on first line of each procedure
283 echo " <td>" . htmlentities("$procedure_code: $procedure_name") . "</td>\n";
285 else {
286 echo " <td style='background-color:transparent'>&nbsp;</td>";
289 // If this starts a new report or a new order, generate the report fields.
290 if ($report_id != $lastprid) {
291 echo " <td>";
292 echo myCellText(oeFormatShortDate($date_report));
293 echo "</td>\n";
295 echo " <td>";
296 echo myCellText($specimen_num);
297 echo "</td>\n";
299 echo " <td title='" . xla('Check mark indicates reviewed') . "'>";
300 echo myCellText(getListItem('proc_rep_status', $report_status));
301 if ($row['review_status'] == 'reviewed') {
302 echo " &#x2713;"; // unicode check mark character
304 echo "</td>\n";
306 echo " <td align='center'>";
307 echo $report_noteid;
308 echo "</td>\n";
310 else {
311 echo " <td colspan='4' style='background-color:transparent'>&nbsp;</td>\n";
314 if ($result_code !== '') {
315 echo " <td>";
316 echo myCellText($result_code);
317 echo "</td>\n";
318 echo " <td>";
319 echo myCellText($result_text);
320 echo "</td>\n";
321 echo " <td>";
322 echo myCellText(getListItem('proc_res_abnormal', $result_abnormal));
323 echo "</td>\n";
324 echo " <td>";
325 echo myCellText($result_result);
326 echo "</td>\n";
327 echo " <td>";
328 echo myCellText($result_range);
329 echo "</td>\n";
330 echo " <td>";
331 echo myCellText($result_units);
332 echo "</td>\n";
333 echo " <td align='center'>";
334 echo $result_noteid;
335 echo "</td>\n";
337 else {
338 echo " <td colspan='7' style='background-color:transparent'>&nbsp;</td>\n";
341 echo " </tr>\n";
343 $lastpoid = $order_id;
344 $lastpcid = $order_seq;
345 $lastprid = $report_id;
346 ++$lino;
351 </table>
353 &nbsp;<br />
354 <table width='100%' style='border-width:0px;'>
355 <tr>
356 <td style='border-width:0px;'>
357 <?php
358 if (!empty($aNotes)) {
359 echo "<table cellpadding='3' cellspacing='0'>\n";
360 echo " <tr bgcolor='#cccccc'>\n";
361 echo " <th align='center' colspan='2'>" . xlt('Notes') . "</th>\n";
362 echo " </tr>\n";
363 foreach ($aNotes as $key => $value) {
364 echo " <tr>\n";
365 echo " <td valign='top'>" . ($key + 1) . "</td>\n";
366 echo " <td>" . nl2br(text($value)) . "</td>\n";
367 echo " </tr>\n";
369 echo "</table>\n";
372 </td>
373 <td style='border-width:0px;' align='right' valign='top'>
374 <?php if ($sign_list) { ?>
375 <input type='hidden' name='form_sign_list' value='<?php echo attr($sign_list); ?>' />
376 <input type='submit' name='form_sign' value='<?php echo xla('Sign Results'); ?>'
377 title='<?php echo xla('Mark these reports as reviewed'); ?>' />
378 <?php } ?>
379 </td>
380 </tr>
381 </table>
383 </form>
384 </body>
385 </html>