quick minor path updates (#1968)
[openemr.git] / interface / orders / lab_exchange.php
bloba5ec1e79658afefa773898b3abef6e812b2d5ad0
1 <?php
2 // Copyright (C) 2010 OpenEMR Support LLC
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
8 require_once("../globals.php");
9 require_once("$srcdir/lab_exchange_api.php");
10 require_once("lab_exchange_tools.php");
11 require_once("../main/messages/lab_results_messages.php");
13 $lab_query_report = "";
14 $lab_patient_success = array();
15 $lab_patient_errors = array();
16 $lab_provider_errors = array();
18 // Create the REST client
19 $client = new LabExchangeClient($GLOBALS['lab_exchange_siteid'], $GLOBALS['lab_exchange_token'], $GLOBALS['lab_exchange_endpoint']);
21 // Make the request
22 $response = $client->sendRequest("results", "GET");
23 $lab_query_report .= xl("Lab Query Status") . "<hr>";
25 // Check response for success or error
26 if ($response->IsError) {
27 $lab_query_report .= xl("Error retrieving results from Lab Exchange Network") . ": {$response->ErrorMessage} <br><br>";
28 } else {
29 $lab_query_report .= xl("Success retrieving results from Lab Exchange Network") . " <br><br>";
31 $resultSet = $response->ResponseXml;
33 foreach ($resultSet->LabResult as $labResult) {
34 $id = $labResult['id'];
35 $patient = $labResult->Patient;
36 $lastName = $patient->LastName;
37 $firstName = $patient->FirstName;
38 $middleName = $patient->MiddleName;
39 $dob = $patient->DOB;
40 $gender = $patient->Gender;
41 $externalId = $patient->ExternalId;
42 $ssn = $patient->SSN;
43 $address = $patient->Address;
44 $city = $patient->City;
45 $state = $patient->State;
46 $zip = $patient->Zip;
47 $homePhone = $patient->HomePhone;
49 // Match patient
50 $patient_id = lab_exchange_match_patient($externalId, $firstName, $middleName, $lastName, $dob, $gender, $ssn, $address);
53 if (!$patient_id) {
54 $lab_patient_errors[] = $labResult;
55 continue;
56 } else {
57 $lab_patient_success[] = $labResult;
60 // process facility information
61 $str_facilityId = processFacility($labResult->Facility);
63 echo "facility_ID = $str_facilityId<br>";
65 // Loop through all the Result Report
66 foreach ($labResult->ResultReport as $resultReport) {
67 // Access report fields
68 // ResultReport maps to procedure_order and procedure_report tables
69 $observationCode = $resultReport->ObservationCode; // => procedure_order.procedure_type_id
70 $observationText = $resultReport->ObservationText; // => This text should be the same as procedure_type.name
71 $observationDate = $resultReport->ObservationDateTime; // => procedure_report.date_collected
72 //$observationStatus = $resultReport->ObservationStatus; // => procedure_report.report_status
73 $observationStatus = $resultReport->OrderResultStatus; // => procedure_report.report_status
74 $controlId = $resultReport->ForeignAccId; // This is the CONTROL ID that is sent back
75 $orderingProviderId = $resultReport->OrderingProviderId; // =>procedure_order.provider_id But the ID here is NOT the same ID as OpenEMR. You have to match it correctly
76 $orderingProviderLastName = $resultReport->OrderingProviderLastName; // Use this to match the provider ID
77 $orderingProviderFirstName = $resultReport->OrderingProviderFirstName; // Use this to match the provider ID
80 // Match provider
81 $user_id = lab_exchange_match_provider($orderingProviderId);
83 if (!$user_id) {
84 $lab_provider_errors[] = $resultReport;
87 $date = '';
88 $date = substr($observationDate, 0, 8);
90 $check_type = sqlQuery("SELECT COUNT(*) AS count FROM procedure_type WHERE procedure_type_id = '" . add_escape_custom($observationCode) . "'");
92 if ($check_type['count'] <= 0) {
93 $sql_type_data =
94 "procedure_type_id = '" . add_escape_custom($observationCode) . "', " .
95 "name = '" . add_escape_custom($observationText) . "', " .
96 "procedure_type = 'res'";
98 $type_id = sqlInsert("INSERT INTO procedure_type SET $sql_type_data");
101 $check_order = sqlQuery("SELECT COUNT(*) AS count, procedure_order_id, provider_id, patient_id FROM procedure_order WHERE control_id = '" . add_escape_custom($controlId) . "' AND procedure_type_id = '" . add_escape_custom($observationCode) . "'");
103 if ($check_order['count'] <= 0) {
104 $sql_order_data =
105 "procedure_type_id = '" . add_escape_custom($observationCode) . "', " .
106 "provider_id = '" . add_escape_custom($user_id) . "', " .
107 "patient_id = '" . add_escape_custom($patient_id) . "', " .
108 "date_collected = DATE_FORMAT('" . add_escape_custom($observationDate . '00') . "', '%Y%m%d%H%i%s'), " .
109 "date_ordered = DATE_FORMAT('" . add_escape_custom($date) . "', '%Y%m%d'), " .
110 "order_priority = 'normal', " .
111 "order_status = 'complete', " .
112 "control_id = '" . add_escape_custom($controlId) . "'";
114 $order_id = sqlInsert("INSERT INTO procedure_order SET $sql_order_data");
115 } else {
116 $sql_order_data =
117 "provider_id = '" . add_escape_custom($user_id) . "', " .
118 "date_collected = DATE_FORMAT('" . add_escape_custom($observationDate . '00') . "', '%Y%m%d%H%i%s'), " .
119 "order_priority = 'normal', " .
120 "order_status = 'complete'";
122 if ($check_order['patient_id'] == "") {
123 $sql_order_data .=
124 ", patient_id = '" . add_escape_custom($patient_id) . "'";
125 } else {
126 $patient_id = $check_order['patient_id'];
129 if ($check_order['provider_id'] == "" or $check_order['provider_id'] <= 0) {
130 $sql_order_data .=
131 ", provider_id = '" . add_escape_custom($user_id) . "'";
132 } else {
133 $user_id = $check_order['provider_id'];
136 $order_id = $check_order['procedure_order_id'];
137 sqlStatement("UPDATE procedure_order SET $sql_order_data WHERE procedure_order_id = '" . add_escape_custom($order_id) . "'");
140 $report_status = mapReportStatus($observationStatus);
142 $check_report = sqlQuery("SELECT COUNT(*) AS count, procedure_report_id FROM procedure_report WHERE procedure_order_id = '" . add_escape_custom($order_id) . "'");
144 if ($check_report['count'] <= 0) {
145 $sql_report_data =
146 "procedure_order_id = '" . add_escape_custom($order_id) . "', " .
147 "date_collected = DATE_FORMAT('" . add_escape_custom($observationDate . "00") . "', '%Y%m%d%H%i%s'), " .
148 "source = '" . add_escape_custom($user_id) . "', " .
149 "date_report = DATE_FORMAT('" . add_escape_custom($date) . "', '%Y%m%d'), " .
150 "report_status = '" . add_escape_custom($report_status) . "', " .
151 "review_status = 'received'";
153 $report_id = sqlInsert("INSERT INTO procedure_report SET $sql_report_data");
154 } else {
155 $sql_report_data =
156 "date_collected = DATE_FORMAT('" . add_escape_custom($observationDate . "00") . "', '%Y%m%d%H%i%s'), " .
157 "source = '" . add_escape_custom($user_id) . "', " .
158 "report_status = '" . add_escape_custom($report_status) . "', " .
159 "review_status = 'received'";
161 $report_id = $check_report['procedure_report_id'];
162 sqlStatement("UPDATE procedure_report SET $sql_report_data WHERE procedure_report_id = '" . add_escape_custom($check_report['procedure_report_id']) . "' AND procedure_order_id = '" . add_escape_custom($order_id) . "'");
165 // Loop through all Results
166 // Result maps to procedure_report table
167 foreach ($resultReport->Result as $result) {
168 // Access result fields
169 $resultCode = $result->ObservationId; // => procedure_result.procedure_type_id
170 $resultCodeTex = $result->ObservationText; // => This text should be the same as procedure_type.name
171 $value = $result->Value; // => procedure_result.result
172 $unit = $result->Unit; // => procedure_result.units
173 $referenceRange = $result->ReferenceRange; //=> procedure_result.range
174 $abnormalFlag = $result->AbnormalFlag; // => procedure_result.abnormal
175 $resultStatus = $result->ResultStatus; // => procedure_result.result_status
176 $resultDateTime = $result->ResultDateTime; // => procedure_result.date
177 $comment = $result->CommentText; //=> procedure_result.comments
178 //Do something with result, ie put in DB
179 $check_type2 = sqlQuery("SELECT COUNT(*) AS count FROM procedure_type WHERE procedure_type_id = '" . add_escape_custom($resultCode) . "'");
180 if ($check_type2['count'] <= 0) {
181 $sql_type_data =
182 "procedure_type_id = '" . add_escape_custom($resultCode) . "', " .
183 "parent = '" . add_escape_custom($observationCode) . "', " .
184 "name = '" . add_escape_custom($resultCodeTex) . "', " .
185 "procedure_type = 'res'";
187 $type_id = sqlInsert("INSERT INTO procedure_type SET $sql_type_data");
190 $result_status = mapResultStatus($resultStatus);
192 $abnormalFlag = mapAbnormalStatus($abnormalFlag);
194 $check_result = sqlQuery("SELECT COUNT(*) AS count, procedure_result_id FROM procedure_result WHERE procedure_report_id = '" . add_escape_custom($report_id) . "' AND procedure_type_id = '" . add_escape_custom($resultCode) . "'");
196 if ($check_result['count'] <= 0) {
197 $sql_result_data =
198 "procedure_report_id = '" . add_escape_custom($report_id) . "', " .
199 "procedure_type_id = '" . add_escape_custom($resultCode) . "', " .
200 "date = DATE_FORMAT('" . add_escape_custom($resultDateTime . '00') . "', '%Y%m%d%H%i%s'), " .
201 "facility = '" . add_escape_custom($str_facilityId) . "', " .
202 "units = '" . add_escape_custom($unit) . "', " .
203 "result = '" . add_escape_custom($value) . "', " .
204 "`range` = '" . add_escape_custom($referenceRange) . "', " .
205 "abnormal = '" . add_escape_custom($abnormalFlag) . "', " .
206 "comments = '" . add_escape_custom($comment) . "', " .
207 "result_status = '" . add_escape_custom($result_status) . "'";
209 sqlInsert("INSERT INTO procedure_result SET $sql_result_data");
210 } else {
211 $sql_result_data =
212 "date = DATE_FORMAT('" . add_escape_custom($resultDateTime . '00') . "', '%Y%m%d%H%i%s'), " .
213 "facility = '" . add_escape_custom($str_facilityId) . "', " .
214 "units = '" . add_escape_custom($unit) . "', " .
215 "result = '" . add_escape_custom($value) . "', " .
216 "`range` = '" . add_escape_custom($referenceRange) . "', " .
217 "abnormal = '" . add_escape_custom($abnormalFlag) . "', " .
218 "comments = '" . add_escape_custom($comment) . "', " .
219 "result_status = '" . add_escape_custom($result_status) . "'";
221 sqlStatement("UPDATE procedure_result SET $sql_result_data WHERE procedure_result_id = '" . add_escape_custom($check_result['procedure_result_id']) . "'");
225 // Send a message regarding a report with pending review status.
226 //echo "Sending message for " . $patient_id . " ordered by " . $user_id . "<br>\n";
227 lab_results_messages($patient_id, $report_id, $user_id);
230 // Need to confirm that the lab result message has been received.
231 // This is the url of the confirm request.
232 $url = "confirm/" . $id;
233 // Make the confirmation request.
234 $response = $client->sendRequest($url, "POST");
235 // Check response for success or error.
236 if ($response->IsError) {
237 echo xl("Error confirming receipt of lab results") . ": {$response->ErrorMessage}<br>\n";
240 // else {
241 // echo xl("Success confirming receipt of lab result") . " <br>\n";
242 // echo $response->ResponseXml;
243 // }
246 // report on lab_patient_errors
247 $lab_query_report .= "<br>". xl("Provider Matching Errors") . ":<hr><table style=\"font-size:12px;\" cellpadding='3px'>";
249 if (count($lab_provider_errors) == 0) {
250 $lab_query_report .= "<tr><td>" .xl("No errors found"). "</td></tr>";
251 } else {
252 $lab_query_report .= "<tr><td>" .
253 xl("First Name") . "</td><td>" .
254 xl("Last Name") . "</td><td>" .
255 xl("NPI") . "</td><tr>";
257 foreach ($lab_provider_errors as $labResultReport) {
258 $lab_query_report .=
259 "<tr><td>{$labResultReport->OrderingProviderFirstName}</td>".
260 "<td>{$labResultReport->OrderingProviderLastName}</td>".
261 "<td>{$labResultReport->OrderingProviderId}</td></tr>";
265 $lab_query_report .= "</table>";
268 // report on lab_patient_errors
269 $lab_query_report .= "<br>". xl("Patient Lookup Errors") . ":<hr><table style=\"font-size:12px;\" cellpadding='3px'>";
271 if (count($lab_patient_errors) == 0) {
272 $lab_query_report .= "<tr><td>" .xl("No errors found"). "</td></tr>";
273 } else {
274 $lab_query_report .= "<tr><td>" .
275 xl("First Name") . "</td><td>" .
276 xl("Middle Name") . "</td><td>" .
277 xl("Last Name") . "</td><td>" .
278 xl("DOB") . "</td><td>" .
279 xl("Gender") . "</td><td>" .
280 xl("External Id") . "</td><td>" .
281 xl("SSN") . "</td><td>" .
282 xl("Address") . "</td><td>" .
283 xl("City") . "</td><td>".
284 xl("State") . "</td><td>" .
285 xl("Zip") . "</td><td>" .
286 xl("Home Phone") . "</td></tr>";
287 foreach ($lab_patient_errors as $labResult) {
288 $patient = $labResult->Patient;
289 $lab_query_report .= "<tr><td>{$patient->FirstName}</td>" .
290 "<td>{$patient->MiddleName}</td>" .
291 "<td>{$patient->LastName}</td>".
292 "<td>{$patient->DOB}</td>".
293 "<td>{$patient->Gender}</td>".
294 "<td>{$patient->ExternalId}</td>".
295 "<td>{$patient->SSN}</td>".
296 "<td>{$patient->Address}</td>".
297 "<td>{$patient->City}</td>".
298 "<td>{$patient->State}</td>".
299 "<td>{$patient->Zip}</td>".
300 "<td>{$patient->HomePhone}</td></tr>";
304 $lab_query_report .= "</table>";
306 // report on lab_patient_success
307 $lab_query_report .= "<br><br>". xl("New results from Lab Exchange") . ":<hr><table style=\"font-size:12px;\" >";
309 if (count($lab_patient_success) == 0) {
310 $lab_query_report .= "<tr><td>" . xl("No new results found") . "</td></tr>";
311 } else {
312 $lab_query_report .= "<tr><td>" .
313 xl("First Name") . "</td><td>" .
314 xl("Middle Name") . "</td><td>" .
315 xl("Last Name") . "</td><td>" .
316 xl("DOB") . "</td><td>" .
317 xl("Gender") . "</td><td>" .
318 xl("External Id") . "</td><td>" .
319 xl("SSN") . "</td><td>" .
320 xl("Address") . "</td><td>" .
321 xl("City") . "</td><td>" .
322 xl("State") . "</td><td>" .
323 xl("Zip") . "</td><td>" .
324 xl("Home Phone") . "</td></tr>";
325 foreach ($lab_patient_success as $labResult) {
326 $patient = $labResult->Patient;
327 $lab_query_report .= "<tr><td>{$patient->FirstName}</td>" .
328 "<td>{$patient->MiddleName}</td>" .
329 "<td>{$patient->LastName}</td>".
330 "<td>{$patient->DOB}</td>".
331 "<td>{$patient->Gender}</td>".
332 "<td>{$patient->ExternalId}</td>".
333 "<td>{$patient->SSN}</td>".
334 "<td>{$patient->Address}</td>".
335 "<td>{$patient->City}</td>".
336 "<td>{$patient->State}</td>".
337 "<td>{$patient->Zip}</td>".
338 "<td>{$patient->HomePhone}</td></tr>";
342 $lab_query_report .= "</table>";
345 <html>
346 <head>
348 <?php html_header_show(); ?>
349 <link rel="stylesheet" href="<?php echo $css_header; ?>" type="text/css">
350 <script type="text/javascript" src="../../../library/dialog.js?v=<?php echo $v_js_includes; ?>"></script>
351 <script type="text/javascript" src="../../../library/textformat.js"></script>
352 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/manual-added-packages/jquery-min-1-2-2/index.js"></script>
353 </head>
355 <body class="body_top">
356 <div id="lab_report">
357 <center>
358 <table style="width: 80%;"><tr><td><span class="title"><?php echo htmlspecialchars(xl('Lab Results Report'), ENT_NOQUOTES); ?></span></td></tr></table>
359 <br>
360 <table style="width: 90%; border: 1px solid black; font-size:12px;">
361 <tr><td><?php echo $lab_query_report; ?></td></tr>
362 </table>
363 </center>
364 </div>
365 </body>
366 </html>