QRDA import (#4646)
[openemr.git] / portal / get_lab_results.php
blobd13d1d5bdeaa0a55b001fe92d2554b46d070411b
1 <?php
3 /**
5 * portal/get_lab_results.php
7 * @package OpenEMR
8 * @link http://www.open-emr.org
9 * @author Cassian LUP <cassi.lup@gmail.com>
10 * @author Jerry Padgett <sjpadgett@gmail.com>
11 * @author Brady Miller <brady.g.miller@gmail.com>
12 * @copyright Copyright (C) 2011 Cassian LUP <cassi.lup@gmail.com>
13 * @copyright Copyright (C) 2016-2017 Jerry Padgett <sjpadgett@gmail.com>
14 * @copyright Copyright (c) 2019 Brady Miller <brady.g.miller@gmail.com>
15 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
18 require_once("verify_session.php");
19 require_once('../library/options.inc.php');
21 $selects =
22 "po.procedure_order_id, po.date_ordered, pc.procedure_order_seq, " .
23 "pt1.procedure_type_id AS order_type_id, pc.procedure_name, " .
24 "pr.procedure_report_id, pr.date_report, pr.date_collected, pr.specimen_num, " .
25 "pr.report_status, pr.review_status";
27 $joins =
28 "JOIN procedure_order_code AS pc ON pc.procedure_order_id = po.procedure_order_id " .
29 "LEFT JOIN procedure_type AS pt1 ON pt1.lab_id = po.lab_id AND pt1.procedure_code = pc.procedure_code " .
30 "LEFT JOIN procedure_report AS pr ON pr.procedure_order_id = po.procedure_order_id AND " .
31 "pr.procedure_order_seq = pc.procedure_order_seq";
33 $orderby =
34 "po.date_ordered, po.procedure_order_id, " .
35 "pc.procedure_order_seq, pr.procedure_report_id";
37 $where = "1 = 1";
39 $res = sqlStatement("SELECT $selects " .
40 "FROM procedure_order AS po $joins " .
41 "WHERE po.patient_id = ? AND $where " .
42 "ORDER BY $orderby", array($pid));
44 if (sqlNumRows($res) > 0) {
46 <table class="table table-striped table-sm table-bordered">
47 <tr class="header">
48 <th><?php echo xlt('Order Date'); ?></th>
49 <th><?php echo xlt('Order Name'); ?></th>
50 <th><?php echo xlt('Result Name'); ?></th>
51 <th><?php echo xlt('Abnormal'); ?></th>
52 <th><?php echo xlt('Value'); ?></th>
53 <th><?php echo xlt('Range'); ?></th>
54 <th><?php echo xlt('Units'); ?></th>
55 <th><?php echo xlt('Result Status'); ?></th>
56 <th><?php echo xlt('Report Status'); ?></th>
57 </tr>
58 <?php
59 $even = false;
61 while ($row = sqlFetchArray($res)) {
62 $order_type_id = empty($row['order_type_id' ]) ? 0 : ($row['order_type_id' ] + 0);
63 $report_id = empty($row['procedure_report_id']) ? 0 : ($row['procedure_report_id'] + 0);
65 $selects = "pt2.procedure_type, pt2.procedure_code, pt2.units AS pt2_units, " .
66 "pt2.range AS pt2_range, pt2.procedure_type_id AS procedure_type_id, " .
67 "pt2.name AS name, pt2.description, pt2.seq AS seq, " .
68 "ps.procedure_result_id, ps.result_code AS result_code, ps.result_text, ps.abnormal, ps.result, " .
69 "ps.range, ps.result_status, ps.facility, ps.comments, ps.units, ps.comments";
71 // procedure_type_id for order:
72 $pt2cond = "pt2.parent = '" . add_escape_custom($order_type_id) . "' AND " .
73 "(pt2.procedure_type LIKE 'res%' OR pt2.procedure_type LIKE 'rec%')";
75 // pr.procedure_report_id or 0 if none:
76 $pscond = "ps.procedure_report_id = '" . add_escape_custom($report_id) . "'";
78 $joincond = "ps.result_code = pt2.procedure_code";
80 // This union emulates a full outer join. The idea is to pick up all
81 // result types defined for this order type, as well as any actual
82 // results that do not have a matching result type.
83 $query = "(SELECT $selects FROM procedure_type AS pt2 " .
84 "LEFT JOIN procedure_result AS ps ON $pscond AND $joincond " .
85 "WHERE $pt2cond" .
86 ") UNION (" .
87 "SELECT $selects FROM procedure_result AS ps " .
88 "LEFT JOIN procedure_type AS pt2 ON $pt2cond AND $joincond " .
89 "WHERE $pscond) " .
90 "ORDER BY seq, name, procedure_type_id, result_code";
92 $rres = sqlStatement($query);
93 while ($rrow = sqlFetchArray($rres)) {
94 if ($even) {
95 $class = "class1_even";
96 $even = false;
97 } else {
98 $class = "class1_odd";
99 $even = true;
101 $date = explode('-', $row['date_ordered']);
102 echo "<tr class='" . $class . "'>";
103 echo "<td>" . text($date[1] . "/" . $date[2] . "/" . $date[0]) . "</td>";
104 echo "<td>" . text($row['procedure_name']) . "</td>";
105 echo "<td>" . text($rrow['name']) . "</td>";
106 echo "<td>" . generate_display_field(array('data_type' => '1', 'list_id' => 'proc_res_abnormal'), $rrow['abnormal']) . "</td>";
107 echo "<td>" . text($rrow['result']) . "</td>";
108 echo "<td>" . text($rrow['pt2_range']) . "</td>";
109 echo "<td>" . generate_display_field(array('data_type' => '1', 'list_id' => 'proc_unit'), $rrow['pt2_units']) . "</td>";
110 echo "<td>" . generate_display_field(array('data_type' => '1', 'list_id' => 'proc_res_status'), $rrow['result_status']) . "</td>";
111 echo "<td>" . generate_display_field(array('data_type' => '1', 'list_id' => 'proc_rep_status'), $row['report_status']) . "</td>";
112 echo "</tr>";
116 echo "</table>";
117 } else {
118 echo xlt("No Results");