Added support for documents embedded in HL7 OBX results, and some other e-labs improv...
[openemr.git] / interface / orders / single_order_results.inc.php
blob0a9e0da61cbbd00c06da76a4b1d7826274e93715
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("$srcdir/acl.inc");
23 require_once("$srcdir/formdata.inc.php");
24 require_once("$srcdir/options.inc.php");
25 require_once("$srcdir/formatting.inc.php");
26 require_once("$srcdir/classes/Document.class.php");
28 function getListItem($listid, $value) {
29 $lrow = sqlQuery("SELECT title FROM list_options " .
30 "WHERE list_id = ? AND option_id = ?",
31 array($listid, $value));
32 $tmp = xl_list_label($lrow['title']);
33 if (empty($tmp)) $tmp = (($value === '') ? '' : "($value)");
34 return $tmp;
37 function myCellText($s) {
38 $s = trim($s);
39 if ($s === '') return '&nbsp;';
40 return text($s);
43 // Check if the given string already exists in the $aNotes array.
44 // If not, stores it as a new entry.
45 // Either way, returns the corresponding key which is a small integer.
46 function storeNote($s) {
47 global $aNotes;
48 $key = array_search($s, $aNotes);
49 if ($key !== FALSE) return $key;
50 $key = count($aNotes);
51 $aNotes[$key] = $s;
52 return $key;
55 function generate_order_report($orderid, $input_form=false) {
56 global $aNotes;
58 // Check authorization.
59 $thisauth = acl_check('patients', 'med');
60 if (!$thisauth) return xl('Not authorized');
62 $orow = sqlQuery("SELECT " .
63 "po.procedure_order_id, po.date_ordered, " .
64 "po.order_status, po.specimen_type, po.patient_id, " .
65 "pd.pubpid, pd.lname, pd.fname, pd.mname, " .
66 "fe.date, " .
67 "pp.name AS labname, " .
68 "u.lname AS ulname, u.fname AS ufname, u.mname AS umname " .
69 "FROM procedure_order AS po " .
70 "LEFT JOIN patient_data AS pd ON pd.pid = po.patient_id " .
71 "LEFT JOIN procedure_providers AS pp ON pp.ppid = po.lab_id " .
72 "LEFT JOIN users AS u ON u.id = po.provider_id " .
73 "LEFT JOIN form_encounter AS fe ON fe.pid = po.patient_id AND fe.encounter = po.encounter_id " .
74 "WHERE po.procedure_order_id = ?",
75 array($orderid));
77 $patient_id = $orow['patient_id'];
80 <style>
82 .labres tr.head { font-size:10pt; background-color:#cccccc; text-align:center; }
83 .labres tr.detail { font-size:10pt; }
84 .labres a, .labres a:visited, .labres a:hover { color:#0000cc; }
86 .labres table {
87 border-style: solid;
88 border-width: 1px 0px 0px 1px;
89 border-color: black;
92 .labres td, .labres th {
93 border-style: solid;
94 border-width: 0px 1px 1px 0px;
95 border-color: black;
98 </style>
100 <?php if ($input_form) { ?>
101 <script type="text/javascript" src="../../library/dialog.js"></script>
102 <script type="text/javascript" src="../../library/textformat.js"></script>
103 <?php } // end if input form ?>
105 <?php if (empty($GLOBALS['PATIENT_REPORT_ACTIVE'])) { ?>
106 <script language="JavaScript">
107 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
108 // Called to show patient notes related to this order in the "other" frame.
109 function showpnotes(orderid) {
110 // Look for the top or bottom frame that contains this document, return if none.
111 var w;
112 for (w = window; w.name != 'RTop' && w.name != 'RBot'; w = w.parent) {
113 if (w.parent == w) return false;
115 var othername = (w.name == 'RTop') ? 'RBot' : 'RTop';
116 w.parent.left_nav.forceDual();
117 w.parent.left_nav.setRadio(othername, 'pno');
118 w.parent.left_nav.loadFrame('pno1', othername, 'patient_file/summary/pnotes_full.php?orderid=' + orderid);
119 return false;
121 </script>
122 <?php } // end if not patient report ?>
124 <?php if ($input_form) { ?>
125 <form method='post' action='single_order_results.php?orderid=<?php echo $orderid; ?>'>
126 <?php } // end if input form ?>
128 <div class='labres'>
130 <table width='100%' cellpadding='2' cellspacing='0'>
131 <tr bgcolor='#cccccc'>
132 <td width='5%' nowrap><?php echo xlt('Patient ID'); ?></td>
133 <td width='45%'><?php echo myCellText($orow['pubpid']); ?></td>
134 <td width='5%' nowrap><?php echo xlt('Order ID'); ?></td>
135 <td width='45%'>
136 <?php
137 if (empty($GLOBALS['PATIENT_REPORT_ACTIVE'])) {
138 echo " <a href='" . $GLOBALS['webroot'];
139 echo "/interface/orders/order_manifest.php?orderid=";
140 echo attr($orow['procedure_order_id']);
141 echo "' target='_blank' onclick='top.restoreSession()'>";
143 echo myCellText($orow['procedure_order_id']);
144 if (empty($GLOBALS['PATIENT_REPORT_ACTIVE'])) {
145 echo "</a>\n";
148 </td>
149 </tr>
150 <tr bgcolor='#cccccc'>
151 <td nowrap><?php echo xlt('Patient Name'); ?></td>
152 <td><?php echo myCellText($orow['lname'] . ', ' . $orow['fname'] . ' ' . $orow['mname']); ?></td>
153 <td nowrap><?php echo xlt('Ordered By'); ?></td>
154 <td><?php echo myCellText($orow['ulname'] . ', ' . $orow['ufname'] . ' ' . $orow['umname']); ?></td>
155 </tr>
156 <tr bgcolor='#cccccc'>
157 <td nowrap><?php echo xlt('Order Date'); ?></td>
158 <td><?php echo myCellText(oeFormatShortDate($orow['date_ordered'])); ?></td>
159 <td nowrap><?php echo xlt('Print Date'); ?></td>
160 <td><?php echo oeFormatShortDate(date('Y-m-d')); ?></td>
161 </tr>
162 <tr bgcolor='#cccccc'>
163 <td nowrap><?php echo xlt('Order Status'); ?></td>
164 <td><?php echo myCellText($orow['order_status']); ?></td>
165 <td nowrap><?php echo xlt('Encounter Date'); ?></td>
166 <td><?php echo myCellText(oeFormatShortDate(substr($orow['date'], 0, 10))); ?></td>
167 </tr>
168 <tr bgcolor='#cccccc'>
169 <td nowrap><?php echo xlt('Lab'); ?></td>
170 <td><?php echo myCellText($orow['labname']); ?></td>
171 <td nowrap><?php echo xlt('Specimen Type'); ?></td>
172 <td><?php echo myCellText($orow['specimen_type']); ?></td>
173 </tr>
174 </table>
176 &nbsp;<br />
178 <table width='100%' cellpadding='2' cellspacing='0'>
180 <tr class='head'>
181 <td rowspan='2' valign='middle'><?php echo xlt('Ordered Procedure'); ?></td>
182 <td colspan='4'><?php echo xlt('Report'); ?></td>
183 <td colspan='7'><?php echo xlt('Results'); ?></td>
184 </tr>
186 <tr class='head'>
187 <td><?php echo xlt('Reported'); ?></td>
188 <td><?php echo xlt('Specimen'); ?></td>
189 <td><?php echo xlt('Status'); ?></td>
190 <td><?php echo xlt('Note'); ?></td>
191 <td><?php echo xlt('Code'); ?></td>
192 <td><?php echo xlt('Name'); ?></td>
193 <td><?php echo xlt('Abn'); ?></td>
194 <td><?php echo xlt('Value'); ?></td>
195 <td><?php echo xlt('Range'); ?></td>
196 <td><?php echo xlt('Units'); ?></td>
197 <td><?php echo xlt('Note'); ?></td>
198 </tr>
200 <?php
201 $query = "SELECT " .
202 "po.date_ordered, pc.procedure_order_seq, pc.procedure_code, " .
203 "pc.procedure_name, " .
204 "pr.procedure_report_id, pr.date_report, pr.date_collected, pr.specimen_num, " .
205 "pr.report_status, pr.review_status, pr.report_notes " .
206 "FROM procedure_order AS po " .
207 "JOIN procedure_order_code AS pc ON pc.procedure_order_id = po.procedure_order_id " .
208 "LEFT JOIN procedure_report AS pr ON pr.procedure_order_id = po.procedure_order_id AND " .
209 "pr.procedure_order_seq = pc.procedure_order_seq " .
210 "WHERE po.procedure_order_id = ? " .
211 "ORDER BY pc.procedure_order_seq, pr.procedure_report_id";
213 $res = sqlStatement($query, array($orderid));
215 $lastpoid = -1;
216 $lastpcid = -1;
217 $lastprid = -1;
218 $encount = 0;
219 $lino = 0;
220 $extra_html = '';
221 $aNotes = array();
222 $sign_list = '';
224 while ($row = sqlFetchArray($res)) {
225 $order_type_id = empty($row['order_type_id' ]) ? 0 : ($row['order_type_id' ] + 0);
226 $order_seq = empty($row['procedure_order_seq']) ? 0 : ($row['procedure_order_seq'] + 0);
227 $report_id = empty($row['procedure_report_id']) ? 0 : ($row['procedure_report_id'] + 0);
228 $procedure_code = empty($row['procedure_code' ]) ? '' : $row['procedure_code'];
229 $procedure_name = empty($row['procedure_name' ]) ? '' : $row['procedure_name'];
230 $date_report = empty($row['date_report' ]) ? '' : $row['date_report'];
231 $date_collected = empty($row['date_collected' ]) ? '' : substr($row['date_collected'], 0, 16);
232 $specimen_num = empty($row['specimen_num' ]) ? '' : $row['specimen_num'];
233 $report_status = empty($row['report_status' ]) ? '' : $row['report_status'];
234 $review_status = empty($row['review_status' ]) ? 'received' : $row['review_status'];
236 if ($review_status != 'reviewed' && $report_id) {
237 if ($sign_list) $sign_list .= ',';
238 $sign_list .= $report_id;
241 $report_noteid ='';
242 if (!empty($row['report_notes'])) {
243 $report_noteid = 1 + storeNote($row['report_notes']);
246 $query = "SELECT " .
247 "ps.result_code, ps.result_text, ps.abnormal, ps.result, ps.range, " .
248 "ps.result_status, ps.facility, ps.units, ps.comments, ps.document_id " .
249 "FROM procedure_result AS ps " .
250 "WHERE ps.procedure_report_id = ? " .
251 "ORDER BY ps.result_code, ps.procedure_result_id";
253 $rres = sqlStatement($query, array($report_id));
254 $rrows = array();
255 while ($rrow = sqlFetchArray($rres)) {
256 $rrows[] = $rrow;
258 if (empty($rrows)) {
259 $rrows[0] = array('result_code' => '');
262 foreach ($rrows as $rrow) {
263 $result_code = empty($rrow['result_code' ]) ? '' : $rrow['result_code'];
264 $result_text = empty($rrow['result_text' ]) ? '' : $rrow['result_text'];
265 $result_abnormal = empty($rrow['abnormal' ]) ? '' : $rrow['abnormal'];
266 $result_result = empty($rrow['result' ]) ? '' : $rrow['result'];
267 $result_units = empty($rrow['units' ]) ? '' : $rrow['units'];
268 $result_facility = empty($rrow['facility' ]) ? '' : $rrow['facility'];
269 $result_comments = empty($rrow['comments' ]) ? '' : $rrow['comments'];
270 $result_range = empty($rrow['range' ]) ? '' : $rrow['range'];
271 $result_status = empty($rrow['result_status' ]) ? '' : $rrow['result_status'];
272 $result_document_id = empty($rrow['document_id' ]) ? '' : $rrow['document_id'];
274 $result_comments = trim($result_comments);
275 $result_noteid = '';
276 if (!empty($result_comments)) {
277 $result_noteid = 1 + storeNote($result_comments);
280 if ($lastpoid != $order_id || $lastpcid != $order_seq) {
281 ++$encount;
283 $bgcolor = "#" . (($encount & 1) ? "ddddff" : "ffdddd");
285 echo " <tr class='detail' bgcolor='$bgcolor'>\n";
287 if ($lastpcid != $order_seq) {
288 $lastprid = -1; // force report fields on first line of each procedure
289 echo " <td>" . text("$procedure_code: $procedure_name") . "</td>\n";
291 else {
292 echo " <td style='background-color:transparent'>&nbsp;</td>";
295 // If this starts a new report or a new order, generate the report fields.
296 if ($report_id != $lastprid) {
297 echo " <td>";
298 echo myCellText(oeFormatShortDate($date_report));
299 echo "</td>\n";
301 echo " <td>";
302 echo myCellText($specimen_num);
303 echo "</td>\n";
305 echo " <td title='" . xla('Check mark indicates reviewed') . "'>";
306 echo myCellText(getListItem('proc_rep_status', $report_status));
307 if ($row['review_status'] == 'reviewed') {
308 echo " &#x2713;"; // unicode check mark character
310 echo "</td>\n";
312 echo " <td align='center'>";
313 echo myCellText($report_noteid);
314 echo "</td>\n";
316 else {
317 echo " <td colspan='4' style='background-color:transparent'>&nbsp;</td>\n";
320 if ($result_code !== '' || $result_document_id) {
321 echo " <td>";
322 echo myCellText($result_code);
323 echo "</td>\n";
324 echo " <td>";
325 echo myCellText($result_text);
326 echo "</td>\n";
327 echo " <td>";
328 echo myCellText(getListItem('proc_res_abnormal', $result_abnormal));
329 echo "</td>\n";
331 if ($result_document_id) {
332 $d = new Document($result_document_id);
333 echo " <td colspan='3'>";
334 if (empty($GLOBALS['PATIENT_REPORT_ACTIVE'])) {
335 echo "<a href='" . $GLOBALS['webroot'] . "/controller.php?document";
336 echo "&retrieve&patient_id=$patient_id&document_id=$result_document_id' ";
337 echo "onclick='top.restoreSession()'>";
339 echo $d->get_url_file();
340 if (empty($GLOBALS['PATIENT_REPORT_ACTIVE'])) {
341 echo "</a>";
343 echo "</td>\n";
345 else {
346 echo " <td>";
347 echo myCellText($result_result);
348 echo "</td>\n";
349 echo " <td>";
350 echo myCellText($result_range);
351 echo "</td>\n";
352 echo " <td>";
353 echo myCellText($result_units);
354 echo "</td>\n";
356 echo " <td align='center'>";
357 echo myCellText($result_noteid);
358 echo "</td>\n";
360 else {
361 echo " <td colspan='7' style='background-color:transparent'>&nbsp;</td>\n";
364 echo " </tr>\n";
366 $lastpoid = $order_id;
367 $lastpcid = $order_seq;
368 $lastprid = $report_id;
369 ++$lino;
374 </table>
376 &nbsp;<br />
377 <table width='100%' style='border-width:0px;'>
378 <tr>
379 <td style='border-width:0px;'>
380 <?php
381 if (!empty($aNotes)) {
382 echo "<table cellpadding='3' cellspacing='0'>\n";
383 echo " <tr bgcolor='#cccccc'>\n";
384 echo " <th align='center' colspan='2'>" . xlt('Notes') . "</th>\n";
385 echo " </tr>\n";
386 foreach ($aNotes as $key => $value) {
387 echo " <tr>\n";
388 echo " <td valign='top'>" . ($key + 1) . "</td>\n";
389 echo " <td>" . nl2br(text($value)) . "</td>\n";
390 echo " </tr>\n";
392 echo "</table>\n";
395 </td>
396 <td style='border-width:0px;' align='right' valign='top'>
397 <?php if (empty($GLOBALS['PATIENT_REPORT_ACTIVE'])) { ?>
398 <input type='button' value='<?php echo xla('Related Patient Notes'); ?>'
399 onclick='showpnotes(<?php echo $orderid; ?>)' />
400 <?php } ?>
401 <?php if ($input_form && $sign_list) { ?>
402 &nbsp;
403 <input type='hidden' name='form_sign_list' value='<?php echo attr($sign_list); ?>' />
404 <input type='submit' name='form_sign' value='<?php echo xla('Sign Results'); ?>'
405 title='<?php echo xla('Mark these reports as reviewed'); ?>' />
406 <?php } ?>
407 </td>
408 </tr>
409 </table>
411 </div>
413 <?php if ($input_form) { ?>
414 </form>
415 <?php } // end if input form ?>
417 <?php
418 } // end function generate_order_report