ongoing new datepicker project
[openemr.git] / interface / orders / list_reports.php
blob3f1b1bcb49fba09483b454d8938b6d356f960441
1 <?php
2 /**
3 * List procedure orders and reports, and fetch new reports and their results.
5 * Copyright (C) 2013-2016 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/log.inc");
27 require_once("$srcdir/acl.inc");
28 require_once("$srcdir/patient.inc");
29 require_once("$srcdir/options.inc.php");
30 require_once("./receive_hl7_results.inc.php");
31 require_once("./gen_hl7_order.inc.php");
33 /**
34 * Get a list item title, translating if required.
36 * @param string $listid List identifier.
37 * @param string $value List item identifier.
38 * @return string The item's title.
40 function getListItem($listid, $value) {
41 $lrow = sqlQuery("SELECT title FROM list_options " .
42 "WHERE list_id = ? AND option_id = ? AND activity = 1",
43 array($listid, $value));
44 $tmp = xl_list_label($lrow['title']);
45 if (empty($tmp)) $tmp = (($value === '') ? '' : "($value)");
46 return $tmp;
49 /**
50 * Adapt text to be suitable as the contents of a table cell.
52 * @param string $s Input text.
53 * @return string Output text.
55 function myCellText($s) {
56 if ($s === '') return '&nbsp;';
57 return text($s);
60 // Check authorization.
61 $thisauth = acl_check('patients', 'med');
62 if (!$thisauth) die(xlt('Not authorized'));
64 $errmsg = '';
66 // Send selected unsent orders if requested. This does not support downloading
67 // very well as it will only send the first of those.
68 if ($_POST['form_xmit']) {
69 foreach ($_POST['form_cb'] as $formid) {
70 $row = sqlQuery("SELECT lab_id FROM procedure_order WHERE " .
71 "procedure_order_id = ?", array($formid));
72 $ppid = intval($row['lab_id']);
73 $hl7 = '';
74 $errmsg = gen_hl7_order($formid, $hl7);
75 if (empty($errmsg)) {
76 $errmsg = send_hl7_order($ppid, $hl7);
78 if ($errmsg) break;
79 sqlStatement("UPDATE procedure_order SET date_transmitted = NOW() WHERE " .
80 "procedure_order_id = ?", array($formid));
84 <html>
85 <head>
86 <?php html_header_show();?>
88 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
89 <title><?php echo xlt('Procedure Orders and Reports'); ?></title>
91 <style>
93 tr.head { font-size:10pt; background-color:#cccccc; text-align:center; }
94 tr.detail { font-size:10pt; }
95 a, a:visited, a:hover { color:#0000cc; }
97 </style>
99 <style type="text/css">@import url(<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar.css);</style>
100 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar.js"></script>
101 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
102 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar_setup.js"></script>
104 <script type="text/javascript" src="../../library/dialog.js?v=<?php echo $v_js_includes; ?>"></script>
105 <script type="text/javascript" src="../../library/textformat.js"></script>
107 <script language="JavaScript">
109 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
111 function openResults(orderid) {
112 top.restoreSession();
113 // Open results in a new window. The options parameter serves to defeat Firefox's
114 // "open windows in a new tab", which is what we want because the doc may want to
115 // see the results concurrently with other stuff like related patient notes.
116 // Opening in the other frame is not good enough because if they then do related
117 // patients notes it will wipe out this script's list. We need 3 viewports.
118 window.open('single_order_results.php?orderid=' + orderid, '_blank', 'toolbar=0,location=0,menubar=0,scrollbars=yes');
120 // To open results in the same frame:
121 // document.location.href = 'single_order_results.php?orderid=' + orderid;
123 // To open results in the "other" frame:
124 // var w = window;
125 // var othername = (w.name == 'RTop') ? 'RBot' : 'RTop';
126 // w.parent.left_nav.forceDual();
127 // w.parent.left_nav.loadFrame('ore1', othername, 'orders/single_order_results.php?orderid=' + orderid);
130 // Invokes the patient matching dialog.
131 // args is a PHP-serialized array of patient attributes.
132 // The dialog script will directly insert the selected pid value, or 0,
133 // into the value of the form field named "[select][$key]".
135 function openPtMatch(args) {
136 top.restoreSession();
137 window.open('patient_match_dialog.php?key=' + encodeURIComponent(args), '_blank', 'toolbar=0,location=0,menubar=0,scrollbars=yes');
140 function openPatient(pid) {
141 top.restoreSession();
142 document.location.href = "../patient_file/summary/demographics.php?set_pid=" + pid;
145 </script>
147 </head>
149 <body class="body_top">
150 <form method='post' action='list_reports.php' enctype='multipart/form-data'>
152 <!-- This might be set by the results window: -->
153 <input type='hidden' name='form_external_refresh' value='' />
155 <?php
156 if ($errmsg) {
157 echo "<font color='red'>" . text($errmsg) . "</font><br />\n";
160 $info = array('select' => array());
162 // We skip match/delete processing if this is just a refresh, because that
163 // might be a nasty surprise.
164 if (empty($_POST['form_external_refresh'])) {
165 // Get patient matching selections from this form if there are any.
166 if (is_array($_POST['select'])) {
167 foreach ($_POST['select'] as $selkey => $selval) {
168 $info['select'][$selkey] = $selval;
171 // Get file delete requests from this form if there are any.
172 if (is_array($_POST['delete'])) {
173 foreach ($_POST['delete'] as $delkey => $dummy) {
174 $info[$delkey] = array('delete' => true);
179 // Attempt to post any incoming results.
180 $errmsg = poll_hl7_results($info);
182 // echo "<!--\n"; // debugging
183 // print_r($info); // debugging
184 // echo "-->\n"; // debugging
186 // Display a row for each required patient matching decision or message.
187 $s = '';
188 $matchreqs = false;
189 $errors = false;
191 // Generate HTML to request patient matching.
192 if (is_array($info['match'])) {
193 foreach ($info['match'] as $matchkey => $matchval) {
194 $matchreqs = true;
195 $s .= " <tr class='detail' bgcolor='#ccccff'>\n";
196 $s .= " <td>&nbsp;</td>\n";
197 $s .= " <td>&nbsp;</td>\n";
198 $s .= " <td><a href='javascript:openPtMatch(\"" . addslashes($matchkey) . "\")'>";
199 $tmp = unserialize($matchkey);
200 $s .= xlt('Click to match patient') . ' "' . text($tmp['lname']) .
201 ', ' . text($tmp['fname']) . '"';
202 $s .= "</a>";
203 $s .= "</td>\n";
204 $s .= " <td style='width:1%'><input type='text' name='select[" .
205 attr($matchkey) . "]' size='3' value='' " .
206 "style='background-color:transparent' readonly /></td>\n";
207 $s .= " </tr>\n";
211 foreach ($info as $infokey => $infoval) {
212 if ($infokey == 'match' || $infokey == 'select') continue;
213 $count = 0;
214 if (is_array($infoval['mssgs'])) {
215 foreach ($infoval['mssgs'] as $message) {
216 $s .= " <tr class='detail' bgcolor='#ccccff'>\n";
217 if (substr($message, 0, 1) == '*') {
218 $errors = true;
219 // Error message starts with '*'
220 if (!$count++) {
221 $s .= " <td><input type='checkbox' name='delete[" . attr($infokey) . "]' value='1' /></td>\n";
222 $s .= " <td>" . text($infokey) . "</td>\n";
224 else {
225 $s .= " <td>&nbsp;</td>\n";
226 $s .= " <td>&nbsp;</td>\n";
228 $s .= " <td colspan='2' style='color:red'>". text(substr($message, 1)) . "</td>\n";
230 else {
231 // Informational message starts with '>'
232 $s .= " <td>&nbsp;</td>\n";
233 $s .= " <td>" . text($infokey) . "</td>\n";
234 $s .= " <td colspan='2' style='color:green'>". text(substr($message, 1)) . "</td>\n";
236 $s .= " </tr>\n";
240 if ($s) {
241 if ($matchreqs || $errors) {
242 echo "<p class='bold' style='color:#008800'>";
243 echo xlt('Incoming results requiring attention:');
244 echo "</p>\n";
246 echo "<table width='100%'>\n";
247 echo " <tr class='head'>\n";
248 echo " <td>" . xlt('Delete' ) . "</th>\n";
249 echo " <td>" . xlt('Lab/File') . "</th>\n";
250 echo " <td>" . xlt('Message' ) . "</th>\n";
251 echo " <td>" . xlt('Match' ) . "</th>\n";
252 echo " </tr>\n";
253 echo $s;
254 echo "</table>\n";
255 if ($matchreqs || $errors) {
256 echo "<p class='bold' style='color:#008800'>";
257 if ($matchreqs) {
258 echo xlt('Click where indicated above to match the patient.') . ' ';
259 echo xlt('After that the Match column will show the selected patient ID, or 0 to create.') . ' ';
260 echo xlt('If you do not select a match the patient will be created.') . ' ';
262 echo xlt('Checkboxes above indicate if you want to reject and delete the HL7 file.') . ' ';
263 echo xlt('When done, click Submit (below) to apply your choices.');
264 echo "</p>\n";
268 // If there was a fatal error display that.
269 if ($errmsg) {
270 echo "<font color='red'>" . text($errmsg) . "</font><br />\n";
273 // Upload support removed because it is awkward to handle and of dubious
274 // value. Note we can now get results from the local filesystem.
276 /*********************************************************************
277 // Process uploaded file if there is one.
278 if (!empty($_FILES['userfile']['name'])) { // if upload was attempted
279 if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
280 $hl7 = file_get_contents($_FILES['userfile']['tmp_name']);
281 $msg = receive_hl7_results($hl7);
282 $message = xl('Upload processed successfully');
283 if ($msg) {
284 $message = xl('Error processing upload') . ": " . $msg;
286 echo text($message) . "<br />\n";
288 else {
289 echo "<font color='red'>" . xlt('Upload failed!') . "</font><br />\n";
292 *********************************************************************/
294 $form_from_date = empty($_POST['form_from_date']) ? '' : trim($_POST['form_from_date']);
295 $form_to_date = empty($_POST['form_to_date']) ? '' : trim($_POST['form_to_date']);
296 // if (empty($form_to_date)) $form_to_date = $form_from_date;
298 $form_reviewed = empty($_POST['form_reviewed']) ? 3 : intval($_POST['form_reviewed']);
300 $form_patient = !empty($_POST['form_patient']);
302 $form_provider = empty($_POST['form_provider']) ? '' : intval($_POST['form_provider']);
305 <table width='100%'>
306 <tr>
307 <td class='text' align='center'>
308 &nbsp;<?php echo xlt('From'); ?>:
309 <input type='text' size='6' name='form_from_date' id='form_from_date'
310 value='<?php echo attr($form_from_date); ?>'
311 title='<?php echo xla('yyyy-mm-dd'); ?>'
312 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />
313 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
314 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
315 title='<?php echo xla('Click here to choose a date'); ?>' />
317 &nbsp;<?php echo xlt('To'); ?>:
318 <input type='text' size='6' name='form_to_date' id='form_to_date'
319 value='<?php echo attr($form_to_date); ?>'
320 title='<?php echo xla('yyyy-mm-dd'); ?>'
321 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />
322 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
323 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
324 title='<?php echo xla('Click here to choose a date'); ?>' />
326 &nbsp;
327 <input type='checkbox' name='form_patient' value='1'
328 <?php if ($form_patient) echo 'checked '; ?>/><?php echo xlt('Current Pt Only'); ?>
330 &nbsp;
331 <select name='form_reviewed'>
332 <?php
333 foreach (array(
334 '1' => xl('All'),
335 '2' => xl('Reviewed'),
336 '3' => xl('Received, unreviewed'),
337 '4' => xl('Sent, not received'),
338 '5' => xl('Not sent'),
339 ) as $key => $value) {
340 echo "<option value='$key'";
341 if ($key == $form_reviewed) echo " selected";
342 echo ">" . text($value) . "</option>\n";
345 </select>
347 &nbsp;
348 <?php
349 generate_form_field(array('data_type' => 10, 'field_id' => 'provider',
350 'empty_title' => '-- All Providers --'), $form_provider);
353 &nbsp;
354 <input type='submit' name='form_refresh' value=<?php echo xla('Submit'); ?>>
355 </td>
356 </tr>
357 </table>
359 <table width='100%' cellpadding='1' cellspacing='2'>
361 <tr class='head'>
362 <td colspan='2'><?php echo xlt('Patient' ); ?></td>
363 <td colspan='2'><?php echo xlt('Order' ); ?></td>
364 <td colspan='2'><?php echo xlt('Procedure'); ?></td>
365 <td colspan='2'><?php echo xlt('Report' ); ?></td>
366 </tr>
368 <tr class='head'>
369 <td><?php echo xlt('Name' ); ?></td>
370 <td><?php echo xlt('ID' ); ?></td>
371 <td><?php echo xlt('Date' ); ?></td>
372 <td><?php echo xlt('ID' ); ?></td>
373 <td><?php echo xlt('Code' ); ?></td>
374 <td><?php echo xlt('Description'); ?></td>
375 <td><?php echo xlt('Date' ); ?></td>
376 <td><?php echo xlt('Status' ); ?></td>
377 <!-- <td><?php echo xlt('Reviewed' ); ?></td> -->
378 </tr>
380 <?php
381 $selects =
382 "po.patient_id, po.procedure_order_id, po.date_ordered, po.date_transmitted, " .
383 "pc.procedure_order_seq, pc.procedure_code, pc.procedure_name, pc.do_not_send, " .
384 "pr.procedure_report_id, pr.date_report, pr.date_report_tz, pr.report_status, pr.review_status";
386 $joins =
387 "LEFT JOIN procedure_report AS pr ON pr.procedure_order_id = po.procedure_order_id AND " .
388 "pr.procedure_order_seq = pc.procedure_order_seq";
390 $orderby =
391 "po.date_ordered, po.procedure_order_id, " .
392 "pc.do_not_send, pc.procedure_order_seq, pr.procedure_report_id";
394 $where = "1 = 1";
395 $sqlBindArray = array();
397 if (!empty($form_from_date)) {
398 $where .= " AND po.date_ordered >= ?";
399 $sqlBindArray[] = $form_from_date;
401 if (!empty($form_to_date)) {
402 $where .= " AND po.date_ordered <= ?";
403 $sqlBindArray[] = $form_to_date;
406 if ($form_patient) {
407 $where .= " AND po.patient_id = ?";
408 $sqlBindArray[] = $pid;
411 if ($form_provider) {
412 $where .= " AND po.provider_id = ?";
413 $sqlBindArray[] = $form_provider;
416 if ($form_reviewed == 2) {
417 $where .= " AND pr.procedure_report_id IS NOT NULL AND pr.review_status = 'reviewed'";
419 else if ($form_reviewed == 3) {
420 $where .= " AND pr.procedure_report_id IS NOT NULL AND pr.review_status != 'reviewed'";
422 else if ($form_reviewed == 4) {
423 $where .= " AND po.date_transmitted IS NOT NULL AND pr.procedure_report_id IS NULL";
425 else if ($form_reviewed == 5) {
426 $where .= " AND po.date_transmitted IS NULL AND pr.procedure_report_id IS NULL";
429 $query = "SELECT " .
430 "pd.fname, pd.mname, pd.lname, pd.pubpid, $selects " .
431 "FROM procedure_order AS po " .
432 "LEFT JOIN procedure_order_code AS pc ON pc.procedure_order_id = po.procedure_order_id " .
433 "LEFT JOIN patient_data AS pd ON pd.pid = po.patient_id $joins " .
434 "WHERE $where " .
435 "ORDER BY pd.lname, pd.fname, pd.mname, po.patient_id, $orderby";
437 $res = sqlStatement($query, $sqlBindArray);
439 $lastptid = -1;
440 $lastpoid = -1;
441 $lastpcid = -1;
442 $encount = 0;
443 $lino = 0;
444 $extra_html = '';
445 $num_checkboxes = 0;
447 while ($row = sqlFetchArray($res)) {
448 $patient_id = empty($row['patient_id' ]) ? 0 : ($row['patient_id' ] + 0);
449 $order_id = empty($row['procedure_order_id' ]) ? 0 : ($row['procedure_order_id' ] + 0);
450 $order_seq = empty($row['procedure_order_seq']) ? 0 : ($row['procedure_order_seq'] + 0);
451 $date_ordered = empty($row['date_ordered' ]) ? '' : $row['date_ordered'];
452 $date_transmitted = empty($row['date_transmitted' ]) ? '' : $row['date_transmitted'];
453 $procedure_code = empty($row['procedure_code' ]) ? '' : $row['procedure_code'];
454 $procedure_name = empty($row['procedure_name' ]) ? '' : $row['procedure_name'];
455 $report_id = empty($row['procedure_report_id']) ? 0 : ($row['procedure_report_id'] + 0);
456 $date_report = empty($row['date_report' ]) ? '' : substr($row['date_report'], 0, 16);
457 $date_report_suf = empty($row['date_report_tz' ]) ? '' : (' ' . $row['date_report_tz' ]);
458 $report_status = empty($row['report_status' ]) ? '' : $row['report_status'];
459 $review_status = empty($row['review_status' ]) ? '' : $row['review_status'];
461 // Sendable procedures sort first, so this also applies to the order on an order ID change.
462 $sendable = isset($row['procedure_order_seq']) && $row['do_not_send'] == 0;
464 $ptname = $row['lname'];
465 if ($row['fname'] || $row['mname'])
466 $ptname .= ', ' . $row['fname'] . ' ' . $row['mname'];
468 if ($lastpoid != $order_id || $lastpcid != $order_seq) {
469 ++$encount;
471 $bgcolor = "#" . (($encount & 1) ? "ddddff" : "ffdddd");
473 echo " <tr class='detail' bgcolor='$bgcolor'>\n";
475 // Generate patient columns.
476 if ($lastptid != $patient_id) {
477 $lastpoid = -1;
478 echo " <td onclick='openPatient($patient_id)' style='cursor:pointer;color:blue'>";
479 echo text($ptname);
480 echo "</td>\n";
481 echo " <td>" . text($row['pubpid']) . "</td>\n";
483 else {
484 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
487 // Generate order columns.
488 if ($lastpoid != $order_id) {
489 $lastpcid = -1;
490 echo " <td>";
491 // Checkbox to support sending unsent orders, disabled if sent.
492 echo "<input type='checkbox' name='form_cb[$order_id]' value='$order_id' ";
493 if ($date_transmitted || !$sendable) {
494 echo "disabled";
495 } else {
496 echo "checked";
497 ++$num_checkboxes;
499 echo " />";
500 // Order date comes with a link to open results in the same frame.
501 echo "<a href='javascript:openResults($order_id)' ";
502 echo "title='" . xla('Click for results') . "'>";
503 echo text($date_ordered);
504 echo "</a></td>\n";
505 echo " <td>";
506 // Order ID comes with a link to open the manifest in a new window/tab.
507 echo "<a href='" . $GLOBALS['webroot'];
508 echo "/interface/orders/order_manifest.php?orderid=";
509 echo attr($order_id);
510 echo "' target='_blank' onclick='top.restoreSession()' ";
511 echo "title='" . xla('Click for order summary') . "'>";
512 echo text($order_id);
513 echo "</a></td>\n";
515 else {
516 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
519 // Generate procedure columns.
520 if ($order_seq && $lastpcid != $order_seq) {
521 if ($sendable) {
522 echo " <td>" . text($procedure_code) . "</td>\n";
523 echo " <td>" . text($procedure_name) . "</td>\n";
525 else {
526 echo " <td><strike>" . text($procedure_code) . "</strike></td>\n";
527 echo " <td><strike>" . text($procedure_name) . "</strike></td>\n";
530 else {
531 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
534 // Generate report columns.
535 if ($report_id) {
536 echo " <td>" . text($date_report . $date_report_suf) . "</td>\n";
537 echo " <td title='" . xla('Check mark indicates reviewed') . "'>";
538 echo myCellText(getListItem('proc_rep_status', $report_status));
539 if ($review_status == 'reviewed') {
540 echo " &#x2713;"; // unicode check mark character
542 echo "</td>\n";
544 else {
545 echo " <td colspan='2' style='background-color:transparent'>&nbsp;</td>";
548 echo " </tr>\n";
550 $lastptid = $patient_id;
551 $lastpoid = $order_id;
552 $lastpcid = $order_seq;
553 ++$lino;
557 </table>
559 <?php if ($num_checkboxes) { ?>
560 <center><p>
561 <input type='submit' name='form_xmit' value='<?php echo xla('Transmit Selected Orders'); ?>' />
562 </p></center>
563 <?php } ?>
565 <script language='JavaScript'>
567 // Initialize calendar widgets for "from" and "to" dates.
568 Calendar.setup({inputField:'form_from_date', ifFormat:'%Y-%m-%d',
569 button:'img_from_date'});
570 Calendar.setup({inputField:'form_to_date', ifFormat:'%Y-%m-%d',
571 button:'img_to_date'});
573 </script>
575 </form>
576 </body>
577 </html>