acknowledgments update
[openemr.git] / interface / orders / single_order_results.inc.php
bloba228849473082c6e51674329279ec2ea5dbefe6c
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");
27 function getListItem($listid, $value) {
28 $lrow = sqlQuery("SELECT title FROM list_options " .
29 "WHERE list_id = ? AND option_id = ?",
30 array($listid, $value));
31 $tmp = xl_list_label($lrow['title']);
32 if (empty($tmp)) $tmp = (($value === '') ? '' : "($value)");
33 return $tmp;
36 function myCellText($s) {
37 if ($s === '') return '&nbsp;';
38 return text($s);
41 // Check if the given string already exists in the $aNotes array.
42 // If not, stores it as a new entry.
43 // Either way, returns the corresponding key which is a small integer.
44 function storeNote($s) {
45 global $aNotes;
46 $key = array_search($s, $aNotes);
47 if ($key !== FALSE) return $key;
48 $key = count($aNotes);
49 $aNotes[$key] = $s;
50 return $key;
53 function generate_order_report($orderid, $input_form=false) {
54 global $aNotes;
56 // Check authorization.
57 $thisauth = acl_check('patients', 'med');
58 if (!$thisauth) return xl('Not authorized');
60 $orow = sqlQuery("SELECT " .
61 "po.procedure_order_id, po.date_ordered, " .
62 "po.order_status, po.specimen_type, " .
63 "pd.pubpid, pd.lname, pd.fname, pd.mname, " .
64 "fe.date, " .
65 "pp.name AS labname, " .
66 "u.lname AS ulname, u.fname AS ufname, u.mname AS umname " .
67 "FROM procedure_order AS po " .
68 "LEFT JOIN patient_data AS pd ON pd.pid = po.patient_id " .
69 "LEFT JOIN procedure_providers AS pp ON pp.ppid = po.lab_id " .
70 "LEFT JOIN users AS u ON u.id = po.provider_id " .
71 "LEFT JOIN form_encounter AS fe ON fe.pid = po.patient_id AND fe.encounter = po.encounter_id " .
72 "WHERE po.procedure_order_id = ?",
73 array($orderid));
76 <style>
78 .labres tr.head { font-size:10pt; background-color:#cccccc; text-align:center; }
79 .labres tr.detail { font-size:10pt; }
80 .labres a, .labres a:visited, .labres a:hover { color:#0000cc; }
82 .labres table {
83 border-style: solid;
84 border-width: 1px 0px 0px 1px;
85 border-color: black;
88 .labres td, .labres th {
89 border-style: solid;
90 border-width: 0px 1px 1px 0px;
91 border-color: black;
94 </style>
96 <?php if ($input_form) { ?>
97 <script type="text/javascript" src="../../library/dialog.js"></script>
98 <script type="text/javascript" src="../../library/textformat.js"></script>
99 <?php } // end if input form ?>
101 <?php if (empty($GLOBALS['PATIENT_REPORT_ACTIVE'])) { ?>
102 <script language="JavaScript">
103 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
104 // Called to show patient notes related to this order in the "other" frame.
105 function showpnotes(orderid) {
106 // Look for the top or bottom frame that contains this document, return if none.
107 var w;
108 for (w = window; w.name != 'RTop' && w.name != 'RBot'; w = w.parent) {
109 if (w.parent == w) return false;
111 var othername = (w.name == 'RTop') ? 'RBot' : 'RTop';
112 w.parent.left_nav.forceDual();
113 w.parent.left_nav.setRadio(othername, 'pno');
114 w.parent.left_nav.loadFrame('pno1', othername, 'patient_file/summary/pnotes_full.php?orderid=' + orderid);
115 return false;
117 </script>
118 <?php } // end if not patient report ?>
120 <?php if ($input_form) { ?>
121 <form method='post' action='single_order_results.php?orderid=<?php echo $orderid; ?>'>
122 <?php } // end if input form ?>
124 <div class='labres'>
126 <table width='100%' cellpadding='2' cellspacing='0'>
127 <tr bgcolor='#cccccc'>
128 <td width='5%' nowrap><?php echo xlt('Patient ID'); ?></td>
129 <td width='45%'><?php echo myCellText($orow['pubpid']); ?></td>
130 <td width='5%' nowrap><?php echo xlt('Order ID'); ?></td>
132 <td width='45%'>
133 <?php
134 echo " <a href='" . $GLOBALS['webroot'];
135 echo "/interface/orders/order_manifest.php?orderid=";
136 echo attr($orow['procedure_order_id']);
137 echo "' target='_blank' onclick='top.restoreSession()'>";
138 echo myCellText($orow['procedure_order_id']);
139 echo "</a>\n";
141 </td>
142 </tr>
143 <tr bgcolor='#cccccc'>
144 <td nowrap><?php echo xlt('Patient Name'); ?></td>
145 <td><?php echo myCellText($orow['lname'] . ', ' . $orow['fname'] . ' ' . $orow['mname']); ?></td>
146 <td nowrap><?php echo xlt('Ordered By'); ?></td>
147 <td><?php echo myCellText($orow['ulname'] . ', ' . $orow['ufname'] . ' ' . $orow['umname']); ?></td>
148 </tr>
149 <tr bgcolor='#cccccc'>
150 <td nowrap><?php echo xlt('Order Date'); ?></td>
151 <td><?php echo myCellText(oeFormatShortDate($orow['date_ordered'])); ?></td>
152 <td nowrap><?php echo xlt('Print Date'); ?></td>
153 <td><?php echo oeFormatShortDate(date('Y-m-d')); ?></td>
154 </tr>
155 <tr bgcolor='#cccccc'>
156 <td nowrap><?php echo xlt('Order Status'); ?></td>
157 <td><?php echo myCellText($orow['order_status']); ?></td>
158 <td nowrap><?php echo xlt('Encounter Date'); ?></td>
159 <td><?php echo myCellText(oeFormatShortDate(substr($orow['date'], 0, 10))); ?></td>
160 </tr>
161 <tr bgcolor='#cccccc'>
162 <td nowrap><?php echo xlt('Lab'); ?></td>
163 <td><?php echo myCellText($orow['labname']); ?></td>
164 <td nowrap><?php echo xlt('Specimen Type'); ?></td>
165 <td><?php echo myCellText($orow['specimen_type']); ?></td>
166 </tr>
167 </table>
169 &nbsp;<br />
171 <table width='100%' cellpadding='2' cellspacing='0'>
173 <tr class='head'>
174 <td rowspan='2' valign='middle'><?php echo xlt('Ordered Procedure'); ?></td>
175 <td colspan='4'><?php echo xlt('Report'); ?></td>
176 <td colspan='7'><?php echo xlt('Results'); ?></td>
177 </tr>
179 <tr class='head'>
180 <td><?php echo xlt('Reported'); ?></td>
181 <td><?php echo xlt('Specimen'); ?></td>
182 <td><?php echo xlt('Status'); ?></td>
183 <td><?php echo xlt('Note'); ?></td>
184 <td><?php echo xlt('Code'); ?></td>
185 <td><?php echo xlt('Name'); ?></td>
186 <td><?php echo xlt('Abn'); ?></td>
187 <td><?php echo xlt('Value'); ?></td>
188 <td><?php echo xlt('Range'); ?></td>
189 <td><?php echo xlt('Units'); ?></td>
190 <td><?php echo xlt('Note'); ?></td>
191 </tr>
193 <?php
194 $query = "SELECT " .
195 "po.date_ordered, pc.procedure_order_seq, pc.procedure_code, " .
196 "pc.procedure_name, " .
197 "pr.procedure_report_id, pr.date_report, pr.date_collected, pr.specimen_num, " .
198 "pr.report_status, pr.review_status, pr.report_notes " .
199 "FROM procedure_order AS po " .
200 "JOIN procedure_order_code AS pc ON pc.procedure_order_id = po.procedure_order_id " .
201 "LEFT JOIN procedure_report AS pr ON pr.procedure_order_id = po.procedure_order_id AND " .
202 "pr.procedure_order_seq = pc.procedure_order_seq " .
203 "WHERE po.procedure_order_id = ? " .
204 "ORDER BY pc.procedure_order_seq, pr.procedure_report_id";
206 $res = sqlStatement($query, array($orderid));
208 $lastpoid = -1;
209 $lastpcid = -1;
210 $lastprid = -1;
211 $encount = 0;
212 $lino = 0;
213 $extra_html = '';
214 $aNotes = array();
215 $sign_list = '';
217 while ($row = sqlFetchArray($res)) {
218 $order_type_id = empty($row['order_type_id' ]) ? 0 : ($row['order_type_id' ] + 0);
219 $order_seq = empty($row['procedure_order_seq']) ? 0 : ($row['procedure_order_seq'] + 0);
220 $report_id = empty($row['procedure_report_id']) ? 0 : ($row['procedure_report_id'] + 0);
221 $procedure_code = empty($row['procedure_code' ]) ? '' : $row['procedure_code'];
222 $procedure_name = empty($row['procedure_name' ]) ? '' : $row['procedure_name'];
223 $date_report = empty($row['date_report' ]) ? '' : $row['date_report'];
224 $date_collected = empty($row['date_collected' ]) ? '' : substr($row['date_collected'], 0, 16);
225 $specimen_num = empty($row['specimen_num' ]) ? '' : $row['specimen_num'];
226 $report_status = empty($row['report_status' ]) ? '' : $row['report_status'];
227 $review_status = empty($row['review_status' ]) ? 'received' : $row['review_status'];
229 if ($review_status != 'reviewed' && $report_id) {
230 if ($sign_list) $sign_list .= ',';
231 $sign_list .= $report_id;
234 $report_noteid ='';
235 if (!empty($row['report_notes'])) {
236 $report_noteid = 1 + storeNote($row['report_notes']);
239 $query = "SELECT " .
240 "ps.result_code, ps.result_text, ps.abnormal, ps.result, " .
241 "ps.range, ps.result_status, ps.facility, ps.units, ps.comments " .
242 "FROM procedure_result AS ps " .
243 "WHERE ps.procedure_report_id = ? " .
244 "ORDER BY ps.result_code, ps.procedure_result_id";
246 $rres = sqlStatement($query, array($report_id));
247 $rrows = array();
248 while ($rrow = sqlFetchArray($rres)) {
249 $rrows[] = $rrow;
251 if (empty($rrows)) {
252 $rrows[0] = array('result_code' => '');
255 foreach ($rrows as $rrow) {
256 $result_code = empty($rrow['result_code' ]) ? '' : $rrow['result_code'];
257 $result_text = empty($rrow['result_text' ]) ? '' : $rrow['result_text'];
258 $result_abnormal = empty($rrow['abnormal' ]) ? '' : $rrow['abnormal'];
259 $result_result = empty($rrow['result' ]) ? '' : $rrow['result'];
260 $result_units = empty($rrow['units' ]) ? '' : $rrow['units'];
261 $result_facility = empty($rrow['facility' ]) ? '' : $rrow['facility'];
262 $result_comments = empty($rrow['comments' ]) ? '' : $rrow['comments'];
263 $result_range = empty($rrow['range' ]) ? '' : $rrow['range'];
264 $result_status = empty($rrow['result_status' ]) ? '' : $rrow['result_status'];
266 $result_comments = trim($result_comments);
267 $result_noteid = '';
268 if (!empty($result_comments)) {
269 $result_noteid = 1 + storeNote($result_comments);
272 if ($lastpoid != $order_id || $lastpcid != $order_seq) {
273 ++$encount;
275 $bgcolor = "#" . (($encount & 1) ? "ddddff" : "ffdddd");
277 echo " <tr class='detail' bgcolor='$bgcolor'>\n";
279 if ($lastpcid != $order_seq) {
280 $lastprid = -1; // force report fields on first line of each procedure
281 echo " <td>" . text("$procedure_code: $procedure_name") . "</td>\n";
283 else {
284 echo " <td style='background-color:transparent'>&nbsp;</td>";
287 // If this starts a new report or a new order, generate the report fields.
288 if ($report_id != $lastprid) {
289 echo " <td>";
290 echo myCellText(oeFormatShortDate($date_report));
291 echo "</td>\n";
293 echo " <td>";
294 echo myCellText($specimen_num);
295 echo "</td>\n";
297 echo " <td title='" . xla('Check mark indicates reviewed') . "'>";
298 echo myCellText(getListItem('proc_rep_status', $report_status));
299 if ($row['review_status'] == 'reviewed') {
300 echo " &#x2713;"; // unicode check mark character
302 echo "</td>\n";
304 echo " <td align='center'>";
305 echo myCellText($report_noteid);
306 echo "</td>\n";
308 else {
309 echo " <td colspan='4' style='background-color:transparent'>&nbsp;</td>\n";
312 if ($result_code !== '') {
313 echo " <td>";
314 echo myCellText($result_code);
315 echo "</td>\n";
316 echo " <td>";
317 echo myCellText($result_text);
318 echo "</td>\n";
319 echo " <td>";
320 echo myCellText(getListItem('proc_res_abnormal', $result_abnormal));
321 echo "</td>\n";
322 echo " <td>";
323 echo myCellText($result_result);
324 echo "</td>\n";
325 echo " <td>";
326 echo myCellText($result_range);
327 echo "</td>\n";
328 echo " <td>";
329 echo myCellText($result_units);
330 echo "</td>\n";
331 echo " <td align='center'>";
332 echo myCellText($result_noteid);
333 echo "</td>\n";
335 else {
336 echo " <td colspan='7' style='background-color:transparent'>&nbsp;</td>\n";
339 echo " </tr>\n";
341 $lastpoid = $order_id;
342 $lastpcid = $order_seq;
343 $lastprid = $report_id;
344 ++$lino;
349 </table>
351 &nbsp;<br />
352 <table width='100%' style='border-width:0px;'>
353 <tr>
354 <td style='border-width:0px;'>
355 <?php
356 if (!empty($aNotes)) {
357 echo "<table cellpadding='3' cellspacing='0'>\n";
358 echo " <tr bgcolor='#cccccc'>\n";
359 echo " <th align='center' colspan='2'>" . xlt('Notes') . "</th>\n";
360 echo " </tr>\n";
361 foreach ($aNotes as $key => $value) {
362 echo " <tr>\n";
363 echo " <td valign='top'>" . ($key + 1) . "</td>\n";
364 echo " <td>" . nl2br(text($value)) . "</td>\n";
365 echo " </tr>\n";
367 echo "</table>\n";
370 </td>
371 <td style='border-width:0px;' align='right' valign='top'>
372 <?php if (empty($GLOBALS['PATIENT_REPORT_ACTIVE'])) { ?>
373 <input type='button' value='<?php echo xla('Related Patient Notes'); ?>'
374 onclick='showpnotes(<?php echo $orderid; ?>)' />
375 <?php } ?>
376 <?php if ($input_form && $sign_list) { ?>
377 &nbsp;
378 <input type='hidden' name='form_sign_list' value='<?php echo attr($sign_list); ?>' />
379 <input type='submit' name='form_sign' value='<?php echo xla('Sign Results'); ?>'
380 title='<?php echo xla('Mark these reports as reviewed'); ?>' />
381 <?php } ?>
382 </td>
383 </tr>
384 </table>
386 </div>
388 <?php if ($input_form) { ?>
389 </form>
390 <?php } // end if input form ?>
392 <?php
393 } // end function generate_order_report