Fix for deleting payments via the Front Office Receipts report.
[openemr.git] / interface / orders / single_order_results.php
blob94ca84df1c79ee02a3e9258ab88ec8e68258be64
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, po.diagnoses, " .
82 "po.order_status, po.specimen_type, " .
83 "pd.pubpid, pd.lname, pd.fname, pd.mname, " .
84 "pp.name AS labname, " .
85 "u.lname AS ulname, u.fname AS ufname, u.mname AS umname " .
86 "FROM procedure_order AS po " .
87 "LEFT JOIN patient_data AS pd ON pd.pid = po.patient_id " .
88 "LEFT JOIN procedure_providers AS pp ON pp.ppid = po.lab_id " .
89 "LEFT JOIN users AS u ON u.id = po.provider_id " .
90 "WHERE po.procedure_order_id = ?",
91 array($orderid));
93 <html>
95 <head>
96 <?php html_header_show(); ?>
98 <link rel="stylesheet" href='<?php echo $css_header; ?>' type='text/css'>
99 <title><?php echo xlt('Order Results'); ?></title>
101 <style>
103 body {
104 margin: 9pt;
105 font-family: sans-serif;
106 font-size: 1em;
109 tr.head { font-size:10pt; background-color:#cccccc; text-align:center; }
110 tr.detail { font-size:10pt; }
111 a, a:visited, a:hover { color:#0000cc; }
113 table {
114 border-style: solid;
115 border-width: 1px 0px 0px 1px;
116 border-color: black;
119 td, th {
120 border-style: solid;
121 border-width: 0px 1px 1px 0px;
122 border-color: black;
125 </style>
127 <script type="text/javascript" src="../../library/dialog.js"></script>
128 <script type="text/javascript" src="../../library/textformat.js"></script>
130 <script language="JavaScript">
132 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
134 </script>
136 </head>
138 <body>
139 <form method='post' action='single_order_results.php?orderid=<?php echo $orderid; ?>'>
141 <table width='100%' cellpadding='2' cellspacing='0'>
142 <tr bgcolor='#cccccc'>
143 <td width='5%' nowrap><?php echo xlt('Patient ID'); ?></td>
144 <td width='45%'><?php echo myCellText($orow['pubpid']); ?></td>
145 <td width='5%' nowrap><?php echo xlt('Order ID'); ?></td>
146 <td width='45%'><?php echo myCellText($orow['procedure_order_id']); ?></td>
147 </tr>
148 <tr bgcolor='#cccccc'>
149 <td nowrap><?php echo xlt('Patient Name'); ?></td>
150 <td><?php echo myCellText($orow['lname'] . ', ' . $orow['fname'] . ' ' . $orow['mname']); ?></td>
151 <td nowrap><?php echo xlt('Ordered By'); ?></td>
152 <td><?php echo myCellText($orow['ulname'] . ', ' . $orow['ufname'] . ' ' . $orow['umname']); ?></td>
153 </tr>
154 <tr bgcolor='#cccccc'>
155 <td nowrap><?php echo xlt('Order Date'); ?></td>
156 <td><?php echo myCellText(oeFormatShortDate($orow['date_ordered'])); ?></td>
157 <td nowrap><?php echo xlt('Print Date'); ?></td>
158 <td><?php echo oeFormatShortDate(date('Y-m-d')); ?></td>
159 </tr>
160 <tr bgcolor='#cccccc'>
161 <td nowrap><?php echo xlt('Order Status'); ?></td>
162 <td><?php echo myCellText($orow['order_status']); ?></td>
163 <td nowrap><?php echo xlt('Diagnoses'); ?></td>
164 <td><?php echo myCellText($orow['diagnoses']); ?></td>
165 </tr>
166 <tr bgcolor='#cccccc'>
167 <td nowrap><?php echo xlt('Lab'); ?></td>
168 <td><?php echo myCellText($orow['labname']); ?></td>
169 <td nowrap><?php echo xlt('Specimen Type'); ?></td>
170 <td><?php echo myCellText($orow['specimen_type']); ?></td>
171 </tr>
172 </table>
174 &nbsp;<br />
176 <table width='100%' cellpadding='2' cellspacing='0'>
178 <tr class='head'>
179 <td rowspan='2' valign='middle'><?php echo xlt('Ordered Procedure'); ?></td>
180 <td colspan='4'><?php echo xlt('Report'); ?></td>
181 <td colspan='7'><?php echo xlt('Results'); ?></td>
182 </tr>
184 <tr class='head'>
185 <td><?php echo xlt('Reported'); ?></td>
186 <td><?php echo xlt('Specimen'); ?></td>
187 <td><?php echo xlt('Status'); ?></td>
188 <td><?php echo xlt('Note'); ?></td>
189 <td><?php echo xlt('Code'); ?></td>
190 <td><?php echo xlt('Name'); ?></td>
191 <td><?php echo xlt('Abn'); ?></td>
192 <td><?php echo xlt('Value'); ?></td>
193 <td><?php echo xlt('Range'); ?></td>
194 <td><?php echo xlt('Units'); ?></td>
195 <td><?php echo xlt('Note'); ?></td>
196 </tr>
198 <?php
199 $query = "SELECT " .
200 "po.date_ordered, pc.procedure_order_seq, pc.procedure_code, " .
201 "pc.procedure_name, " .
202 "pr.procedure_report_id, pr.date_report, pr.date_collected, pr.specimen_num, " .
203 "pr.report_status, pr.review_status, pr.report_notes " .
204 "FROM procedure_order AS po " .
205 "JOIN procedure_order_code AS pc ON pc.procedure_order_id = po.procedure_order_id " .
206 "LEFT JOIN procedure_report AS pr ON pr.procedure_order_id = po.procedure_order_id AND " .
207 "pr.procedure_order_seq = pc.procedure_order_seq " .
208 "WHERE po.procedure_order_id = ? " .
209 "ORDER BY pc.procedure_order_seq, pr.procedure_report_id";
211 $res = sqlStatement($query, array($orderid));
213 $lastpoid = -1;
214 $lastpcid = -1;
215 $lastprid = -1;
216 $encount = 0;
217 $lino = 0;
218 $extra_html = '';
219 $aNotes = array();
220 $sign_list = '';
222 while ($row = sqlFetchArray($res)) {
223 $order_type_id = empty($row['order_type_id' ]) ? 0 : ($row['order_type_id' ] + 0);
224 $order_seq = empty($row['procedure_order_seq']) ? 0 : ($row['procedure_order_seq'] + 0);
225 $report_id = empty($row['procedure_report_id']) ? 0 : ($row['procedure_report_id'] + 0);
226 $procedure_code = empty($row['procedure_code' ]) ? '' : $row['procedure_code'];
227 $procedure_name = empty($row['procedure_name' ]) ? '' : $row['procedure_name'];
228 $date_report = empty($row['date_report' ]) ? '' : $row['date_report'];
229 $date_collected = empty($row['date_collected' ]) ? '' : substr($row['date_collected'], 0, 16);
230 $specimen_num = empty($row['specimen_num' ]) ? '' : $row['specimen_num'];
231 $report_status = empty($row['report_status' ]) ? '' : $row['report_status'];
232 $review_status = empty($row['review_status' ]) ? 'received' : $row['review_status'];
234 if ($review_status != 'reviewed') {
235 if ($sign_list) $sign_list .= ',';
236 $sign_list .= $report_id;
239 $report_noteid ='';
240 if (!empty($row['report_notes'])) {
241 $report_noteid = 1 + storeNote($row['report_notes']);
244 $query = "SELECT " .
245 "ps.result_code, ps.result_text, ps.abnormal, ps.result, " .
246 "ps.range, ps.result_status, ps.facility, ps.units, ps.comments " .
247 "FROM procedure_result AS ps " .
248 "WHERE ps.procedure_report_id = ? " .
249 "ORDER BY ps.result_code, ps.procedure_result_id";
251 $rres = sqlStatement($query, array($report_id));
252 $rrows = array();
253 while ($rrow = sqlFetchArray($rres)) {
254 $rrows[] = $rrow;
256 if (empty($rrows)) {
257 $rrows[0] = array('result_code' => '');
260 foreach ($rrows as $rrow) {
261 $result_code = empty($rrow['result_code' ]) ? '' : $rrow['result_code'];
262 $result_text = empty($rrow['result_text' ]) ? '' : $rrow['result_text'];
263 $result_abnormal = empty($rrow['abnormal' ]) ? '' : $rrow['abnormal'];
264 $result_result = empty($rrow['result' ]) ? '' : $rrow['result'];
265 $result_units = empty($rrow['units' ]) ? '' : $rrow['units'];
266 $result_facility = empty($rrow['facility' ]) ? '' : $rrow['facility'];
267 $result_comments = empty($rrow['comments' ]) ? '' : $rrow['comments'];
268 $result_range = empty($rrow['range' ]) ? '' : $rrow['range'];
269 $result_status = empty($rrow['result_status' ]) ? '' : $rrow['result_status'];
271 $result_comments = trim($result_comments);
272 $result_noteid = '';
273 if (!empty($result_comments)) {
274 $result_noteid = 1 + storeNote($result_comments);
277 if ($lastpoid != $order_id || $lastpcid != $order_seq) {
278 ++$encount;
280 $bgcolor = "#" . (($encount & 1) ? "ddddff" : "ffdddd");
282 echo " <tr class='detail' bgcolor='$bgcolor'>\n";
284 if ($lastpcid != $order_seq) {
285 $lastprid = -1; // force report fields on first line of each procedure
286 echo " <td>" . text("$procedure_code: $procedure_name") . "</td>\n";
288 else {
289 echo " <td style='background-color:transparent'>&nbsp;</td>";
292 // If this starts a new report or a new order, generate the report fields.
293 if ($report_id != $lastprid) {
294 echo " <td>";
295 echo myCellText(oeFormatShortDate($date_report));
296 echo "</td>\n";
298 echo " <td>";
299 echo myCellText($specimen_num);
300 echo "</td>\n";
302 echo " <td title='" . xla('Check mark indicates reviewed') . "'>";
303 echo myCellText(getListItem('proc_rep_status', $report_status));
304 if ($row['review_status'] == 'reviewed') {
305 echo " &#x2713;"; // unicode check mark character
307 echo "</td>\n";
309 echo " <td align='center'>";
310 echo myCellText($report_noteid);
311 echo "</td>\n";
313 else {
314 echo " <td colspan='4' style='background-color:transparent'>&nbsp;</td>\n";
317 if ($result_code !== '') {
318 echo " <td>";
319 echo myCellText($result_code);
320 echo "</td>\n";
321 echo " <td>";
322 echo myCellText($result_text);
323 echo "</td>\n";
324 echo " <td>";
325 echo myCellText(getListItem('proc_res_abnormal', $result_abnormal));
326 echo "</td>\n";
327 echo " <td>";
328 echo myCellText($result_result);
329 echo "</td>\n";
330 echo " <td>";
331 echo myCellText($result_range);
332 echo "</td>\n";
333 echo " <td>";
334 echo myCellText($result_units);
335 echo "</td>\n";
336 echo " <td align='center'>";
337 echo myCellText($result_noteid);
338 echo "</td>\n";
340 else {
341 echo " <td colspan='7' style='background-color:transparent'>&nbsp;</td>\n";
344 echo " </tr>\n";
346 $lastpoid = $order_id;
347 $lastpcid = $order_seq;
348 $lastprid = $report_id;
349 ++$lino;
354 </table>
356 &nbsp;<br />
357 <table width='100%' style='border-width:0px;'>
358 <tr>
359 <td style='border-width:0px;'>
360 <?php
361 if (!empty($aNotes)) {
362 echo "<table cellpadding='3' cellspacing='0'>\n";
363 echo " <tr bgcolor='#cccccc'>\n";
364 echo " <th align='center' colspan='2'>" . xlt('Notes') . "</th>\n";
365 echo " </tr>\n";
366 foreach ($aNotes as $key => $value) {
367 echo " <tr>\n";
368 echo " <td valign='top'>" . ($key + 1) . "</td>\n";
369 echo " <td>" . nl2br(text($value)) . "</td>\n";
370 echo " </tr>\n";
372 echo "</table>\n";
375 </td>
376 <td style='border-width:0px;' align='right' valign='top'>
377 <?php if ($sign_list) { ?>
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 </form>
387 </body>
388 </html>