Moved diagnoses from orders to individual procedures.
[openemr.git] / interface / orders / single_order_results.php
bloba75c2aa07b20bc33b0bc924536a23233c09e8542
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 $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("../orders/lab_exchange_tools.php");
32 // Check authorization.
33 $thisauth = acl_check('patients', 'med');
34 if (!$thisauth) die(xl('Not authorized'));
36 $orderid = intval($_GET['orderid']);
38 function getListItem($listid, $value) {
39 $lrow = sqlQuery("SELECT title FROM list_options " .
40 "WHERE list_id = ? AND option_id = ?",
41 array($listid, $value));
42 $tmp = xl_list_label($lrow['title']);
43 if (empty($tmp)) $tmp = "($report_status)";
44 return $tmp;
47 function myCellText($s) {
48 if ($s === '') return '&nbsp;';
49 return text($s);
52 // Check if the given string already exists in the $aNotes array.
53 // If not, stores it as a new entry.
54 // Either way, returns the corresponding key which is a small integer.
55 function storeNote($s) {
56 global $aNotes;
57 $key = array_search($s, $aNotes);
58 if ($key !== FALSE) return $key;
59 $key = count($aNotes);
60 $aNotes[$key] = $s;
61 return $key;
64 if (!empty($_POST['form_sign_list'])) {
65 if (!acl_check('patients', 'sign')) {
66 die(xl('Not authorized to sign results'));
68 // When signing results we are careful to sign only those reports that were
69 // in the sending form. While this will usually be all the reports linked to
70 // the order it's possible for a new report to come in while viewing these,
71 // and it would be very bad to sign results that nobody has seen!
72 $arrSign = explode(',', $_POST['form_sign_list']);
73 foreach ($arrSign as $id) {
74 sqlStatement("UPDATE procedure_report SET " .
75 "review_status = 'reviewed' WHERE " .
76 "procedure_report_id = ?", array($id));
80 $orow = sqlQuery("SELECT " .
81 "po.procedure_order_id, po.date_ordered, " .
82 "po.order_status, po.specimen_type, " .
83 "pd.pubpid, pd.lname, pd.fname, pd.mname, " .
84 "fe.date, " .
85 "pp.name AS labname, " .
86 "u.lname AS ulname, u.fname AS ufname, u.mname AS umname " .
87 "FROM procedure_order AS po " .
88 "LEFT JOIN patient_data AS pd ON pd.pid = po.patient_id " .
89 "LEFT JOIN procedure_providers AS pp ON pp.ppid = po.lab_id " .
90 "LEFT JOIN users AS u ON u.id = po.provider_id " .
91 "LEFT JOIN form_encounter AS fe ON fe.pid = po.patient_id AND fe.encounter = po.encounter_id " .
92 "WHERE po.procedure_order_id = ?",
93 array($orderid));
95 <html>
97 <head>
98 <?php html_header_show(); ?>
100 <link rel="stylesheet" href='<?php echo $css_header; ?>' type='text/css'>
101 <title><?php echo xlt('Order Results'); ?></title>
103 <style>
105 body {
106 margin: 9pt;
107 font-family: sans-serif;
108 font-size: 1em;
111 tr.head { font-size:10pt; background-color:#cccccc; text-align:center; }
112 tr.detail { font-size:10pt; }
113 a, a:visited, a:hover { color:#0000cc; }
115 table {
116 border-style: solid;
117 border-width: 1px 0px 0px 1px;
118 border-color: black;
121 td, th {
122 border-style: solid;
123 border-width: 0px 1px 1px 0px;
124 border-color: black;
127 </style>
129 <script type="text/javascript" src="../../library/dialog.js"></script>
130 <script type="text/javascript" src="../../library/textformat.js"></script>
132 <script language="JavaScript">
134 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
136 </script>
138 </head>
140 <body>
141 <form method='post' action='single_order_results.php?orderid=<?php echo $orderid; ?>'>
143 <table width='100%' cellpadding='2' cellspacing='0'>
144 <tr bgcolor='#cccccc'>
145 <td width='5%' nowrap><?php echo xlt('Patient ID'); ?></td>
146 <td width='45%'><?php echo myCellText($orow['pubpid']); ?></td>
147 <td width='5%' nowrap><?php echo xlt('Order ID'); ?></td>
148 <td width='45%'><?php echo myCellText($orow['procedure_order_id']); ?></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') {
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, " .
248 "ps.range, ps.result_status, ps.facility, ps.units, ps.comments " .
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'];
273 $result_comments = trim($result_comments);
274 $result_noteid = '';
275 if (!empty($result_comments)) {
276 $result_noteid = 1 + storeNote($result_comments);
279 if ($lastpoid != $order_id || $lastpcid != $order_seq) {
280 ++$encount;
282 $bgcolor = "#" . (($encount & 1) ? "ddddff" : "ffdddd");
284 echo " <tr class='detail' bgcolor='$bgcolor'>\n";
286 if ($lastpcid != $order_seq) {
287 $lastprid = -1; // force report fields on first line of each procedure
288 echo " <td>" . text("$procedure_code: $procedure_name") . "</td>\n";
290 else {
291 echo " <td style='background-color:transparent'>&nbsp;</td>";
294 // If this starts a new report or a new order, generate the report fields.
295 if ($report_id != $lastprid) {
296 echo " <td>";
297 echo myCellText(oeFormatShortDate($date_report));
298 echo "</td>\n";
300 echo " <td>";
301 echo myCellText($specimen_num);
302 echo "</td>\n";
304 echo " <td title='" . xla('Check mark indicates reviewed') . "'>";
305 echo myCellText(getListItem('proc_rep_status', $report_status));
306 if ($row['review_status'] == 'reviewed') {
307 echo " &#x2713;"; // unicode check mark character
309 echo "</td>\n";
311 echo " <td align='center'>";
312 echo myCellText($report_noteid);
313 echo "</td>\n";
315 else {
316 echo " <td colspan='4' style='background-color:transparent'>&nbsp;</td>\n";
319 if ($result_code !== '') {
320 echo " <td>";
321 echo myCellText($result_code);
322 echo "</td>\n";
323 echo " <td>";
324 echo myCellText($result_text);
325 echo "</td>\n";
326 echo " <td>";
327 echo myCellText(getListItem('proc_res_abnormal', $result_abnormal));
328 echo "</td>\n";
329 echo " <td>";
330 echo myCellText($result_result);
331 echo "</td>\n";
332 echo " <td>";
333 echo myCellText($result_range);
334 echo "</td>\n";
335 echo " <td>";
336 echo myCellText($result_units);
337 echo "</td>\n";
338 echo " <td align='center'>";
339 echo myCellText($result_noteid);
340 echo "</td>\n";
342 else {
343 echo " <td colspan='7' style='background-color:transparent'>&nbsp;</td>\n";
346 echo " </tr>\n";
348 $lastpoid = $order_id;
349 $lastpcid = $order_seq;
350 $lastprid = $report_id;
351 ++$lino;
356 </table>
358 &nbsp;<br />
359 <table width='100%' style='border-width:0px;'>
360 <tr>
361 <td style='border-width:0px;'>
362 <?php
363 if (!empty($aNotes)) {
364 echo "<table cellpadding='3' cellspacing='0'>\n";
365 echo " <tr bgcolor='#cccccc'>\n";
366 echo " <th align='center' colspan='2'>" . xlt('Notes') . "</th>\n";
367 echo " </tr>\n";
368 foreach ($aNotes as $key => $value) {
369 echo " <tr>\n";
370 echo " <td valign='top'>" . ($key + 1) . "</td>\n";
371 echo " <td>" . nl2br(text($value)) . "</td>\n";
372 echo " </tr>\n";
374 echo "</table>\n";
377 </td>
378 <td style='border-width:0px;' align='right' valign='top'>
379 <?php if ($sign_list) { ?>
380 <input type='hidden' name='form_sign_list' value='<?php echo attr($sign_list); ?>' />
381 <input type='submit' name='form_sign' value='<?php echo xla('Sign Results'); ?>'
382 title='<?php echo xla('Mark these reports as reviewed'); ?>' />
383 <?php } ?>
384 </td>
385 </tr>
386 </table>
388 </form>
389 </body>
390 </html>