Focus the search term on load
[openemr.git] / interface / orders / lab_exchange.php
blob530d0d22bfbda1429fe1044437bac5f2e7bf1470
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");
12 include_once("$srcdir/formdata.inc.php");
14 $lab_query_report = "";
15 $lab_patient_success = array();
16 $lab_patient_errors = array();
17 $lab_provider_errors = array();
19 // Create the REST client
20 $client = new LabExchangeClient($GLOBALS['lab_exchange_siteid'],$GLOBALS['lab_exchange_token'],$GLOBALS['lab_exchange_endpoint']);
22 // Make the request
23 $response = $client->sendRequest("results", "GET");
24 $lab_query_report .= xl("Lab Query Status") . "<hr>";
26 // Check response for success or error
27 if ($response->IsError)
28 $lab_query_report .= xl("Error retrieving results from Lab Exchange Network") . ": {$response->ErrorMessage} <br><br>";
29 else {
30 $lab_query_report .= xl("Success retrieving results from Lab Exchange Network") . " <br><br>";
32 $resultSet = $response->ResponseXml;
34 foreach ($resultSet->LabResult as $labResult) {
35 $id = $labResult['id'];
36 $patient = $labResult->Patient;
37 $lastName = $patient->LastName;
38 $firstName = $patient->FirstName;
39 $middleName = $patient->MiddleName;
40 $dob = $patient->DOB;
41 $gender = $patient->Gender;
42 $externalId = $patient->ExternalId;
43 $ssn = $patient->SSN;
44 $address = $patient->Address;
45 $city = $patient->City;
46 $state = $patient->State;
47 $zip = $patient->Zip;
48 $homePhone = $patient->HomePhone;
50 // Match patient
51 $patient_id = lab_exchange_match_patient($externalId, $firstName, $middleName, $lastName, $dob, $gender, $ssn, $address);
54 if (!$patient_id) {
55 $lab_patient_errors[] = $labResult;
56 continue;
58 else
60 $lab_patient_success[] = $labResult;
63 // process facility information
64 $str_facilityId = processFacility($labResult->Facility);
66 echo "facility_ID = $str_facilityId<br>";
68 // Loop through all the Result Report
69 foreach ($labResult->ResultReport as $resultReport) {
70 // Access report fields
71 // ResultReport maps to procedure_order and procedure_report tables
72 $observationCode = $resultReport->ObservationCode; // => procedure_order.procedure_type_id
73 $observationText = $resultReport->ObservationText; // => This text should be the same as procedure_type.name
74 $observationDate = $resultReport->ObservationDateTime; // => procedure_report.date_collected
75 //$observationStatus = $resultReport->ObservationStatus; // => procedure_report.report_status
76 $observationStatus = $resultReport->OrderResultStatus; // => procedure_report.report_status
77 $controlId = $resultReport->ForeignAccId; // This is the CONTROL ID that is sent back
78 $orderingProviderId = $resultReport->OrderingProviderId; // =>procedure_order.provider_id But the ID here is NOT the same ID as OpenEMR. You have to match it correctly
79 $orderingProviderLastName = $resultReport->OrderingProviderLastName; // Use this to match the provider ID
80 $orderingProviderFirstName = $resultReport->OrderingProviderFirstName; // Use this to match the provider ID
83 // Match provider
84 $user_id = lab_exchange_match_provider($orderingProviderId);
86 if(!$user_id)
88 $lab_provider_errors[] = $resultReport;
91 $date = '';
92 $date = substr($observationDate, 0, 8);
94 $check_type = sqlQuery("SELECT COUNT(*) AS count FROM procedure_type WHERE procedure_type_id = '" . add_escape_custom($observationCode) . "'");
96 if ($check_type['count'] <= 0) {
97 $sql_type_data =
98 "procedure_type_id = '" . add_escape_custom($observationCode) . "', " .
99 "name = '" . add_escape_custom($observationText) . "', " .
100 "procedure_type = 'res'";
102 $type_id = sqlInsert("INSERT INTO procedure_type SET $sql_type_data");
105 $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) . "'");
107 if ($check_order['count'] <= 0) {
108 $sql_order_data =
109 "procedure_type_id = '" . add_escape_custom($observationCode) . "', " .
110 "provider_id = '" . add_escape_custom($user_id) . "', " .
111 "patient_id = '" . add_escape_custom($patient_id) . "', " .
112 "date_collected = DATE_FORMAT('" . add_escape_custom($observationDate . '00') . "', '%Y%m%d%H%i%s'), " .
113 "date_ordered = DATE_FORMAT('" . add_escape_custom($date) . "', '%Y%m%d'), " .
114 "order_priority = 'normal', " .
115 "order_status = 'complete', " .
116 "control_id = '" . add_escape_custom($controlId) . "'";
118 $order_id = sqlInsert("INSERT INTO procedure_order SET $sql_order_data");
119 } else {
120 $sql_order_data =
121 "provider_id = '" . add_escape_custom($user_id) . "', " .
122 "date_collected = DATE_FORMAT('" . add_escape_custom($observationDate . '00') . "', '%Y%m%d%H%i%s'), " .
123 "order_priority = 'normal', " .
124 "order_status = 'complete'";
126 if ($check_order['patient_id'] == "") {
127 $sql_order_data .=
128 ", patient_id = '" . add_escape_custom($patient_id) . "'";
129 } else {
130 $patient_id = $check_order['patient_id'];
133 if ($check_order['provider_id'] == "" or $check_order['provider_id'] <= 0) {
134 $sql_order_data .=
135 ", provider_id = '" . add_escape_custom($user_id) . "'";
136 } else {
137 $user_id = $check_order['provider_id'];
140 $order_id = $check_order['procedure_order_id'];
141 sqlStatement("UPDATE procedure_order SET $sql_order_data WHERE procedure_order_id = '" . add_escape_custom($order_id) . "'");
144 $report_status = mapReportStatus($observationStatus);
146 $check_report = sqlQuery("SELECT COUNT(*) AS count, procedure_report_id FROM procedure_report WHERE procedure_order_id = '" . add_escape_custom($order_id) . "'");
148 if ($check_report['count'] <= 0) {
149 $sql_report_data =
150 "procedure_order_id = '" . add_escape_custom($order_id) . "', " .
151 "date_collected = DATE_FORMAT('" . add_escape_custom($observationDate . "00") . "', '%Y%m%d%H%i%s'), " .
152 "source = '" . add_escape_custom($user_id) . "', " .
153 "date_report = DATE_FORMAT('" . add_escape_custom($date) . "', '%Y%m%d'), " .
154 "report_status = '" . add_escape_custom($report_status) . "', " .
155 "review_status = 'received'";
157 $report_id = sqlInsert("INSERT INTO procedure_report SET $sql_report_data");
158 } else {
159 $sql_report_data =
160 "date_collected = DATE_FORMAT('" . add_escape_custom($observationDate . "00") . "', '%Y%m%d%H%i%s'), " .
161 "source = '" . add_escape_custom($user_id) . "', " .
162 "report_status = '" . add_escape_custom($report_status) . "', " .
163 "review_status = 'received'";
165 $report_id = $check_report['procedure_report_id'];
166 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) . "'");
169 // Loop through all Results
170 // Result maps to procedure_report table
171 foreach ($resultReport->Result as $result) {
172 // Access result fields
173 $resultCode = $result->ObservationId; // => procedure_result.procedure_type_id
174 $resultCodeTex = $result->ObservationText; // => This text should be the same as procedure_type.name
175 $value = $result->Value; // => procedure_result.result
176 $unit = $result->Unit; // => procedure_result.units
177 $referenceRange = $result->ReferenceRange; //=> procedure_result.range
178 $abnormalFlag = $result->AbnormalFlag; // => procedure_result.abnormal
179 $resultStatus = $result->ResultStatus; // => procedure_result.result_status
180 $resultDateTime = $result->ResultDateTime; // => procedure_result.date
181 $comment = $result->CommentText; //=> procedure_result.comments
182 //Do something with result, ie put in DB
183 $check_type2 = sqlQuery("SELECT COUNT(*) AS count FROM procedure_type WHERE procedure_type_id = '" . add_escape_custom($resultCode) . "'");
184 if ($check_type2['count'] <= 0) {
185 $sql_type_data =
186 "procedure_type_id = '" . add_escape_custom($resultCode) . "', " .
187 "parent = '" . add_escape_custom($observationCode) . "', " .
188 "name = '" . add_escape_custom($resultCodeTex) . "', " .
189 "procedure_type = 'res'";
191 $type_id = sqlInsert("INSERT INTO procedure_type SET $sql_type_data");
194 $result_status = mapResultStatus($resultStatus);
196 $abnormalFlag = mapAbnormalStatus($abnormalFlag);
198 $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) . "'");
200 if ($check_result['count'] <= 0) {
202 $sql_result_data =
203 "procedure_report_id = '" . add_escape_custom($report_id) . "', " .
204 "procedure_type_id = '" . add_escape_custom($resultCode) . "', " .
205 "date = DATE_FORMAT('" . add_escape_custom($resultDateTime . '00') . "', '%Y%m%d%H%i%s'), " .
206 "facility = '" . add_escape_custom($str_facilityId) . "', " .
207 "units = '" . add_escape_custom($unit) . "', " .
208 "result = '" . add_escape_custom($value) . "', " .
209 "`range` = '" . add_escape_custom($referenceRange) . "', " .
210 "abnormal = '" . add_escape_custom($abnormalFlag) . "', " .
211 "comments = '" . add_escape_custom($comment) . "', " .
212 "result_status = '" . add_escape_custom($result_status) . "'";
214 sqlInsert("INSERT INTO procedure_result SET $sql_result_data");
215 } else {
216 $sql_result_data =
217 "date = DATE_FORMAT('" . add_escape_custom($resultDateTime . '00') . "', '%Y%m%d%H%i%s'), " .
218 "facility = '" . add_escape_custom($str_facilityId) . "', " .
219 "units = '" . add_escape_custom($unit) . "', " .
220 "result = '" . add_escape_custom($value) . "', " .
221 "`range` = '" . add_escape_custom($referenceRange) . "', " .
222 "abnormal = '" . add_escape_custom($abnormalFlag) . "', " .
223 "comments = '" . add_escape_custom($comment) . "', " .
224 "result_status = '" . add_escape_custom($result_status) . "'";
226 sqlStatement("UPDATE procedure_result SET $sql_result_data WHERE procedure_result_id = '" . add_escape_custom($check_result['procedure_result_id']) . "'");
230 // Send a message regarding a report with pending review status.
231 //echo "Sending message for " . $patient_id . " ordered by " . $user_id . "<br>\n";
232 lab_results_messages($patient_id, $report_id, $user_id);
235 // Need to confirm that the lab result message has been received.
236 // This is the url of the confirm request.
237 $url = "confirm/" . $id;
238 // Make the confirmation request.
239 $response = $client->sendRequest($url, "POST");
240 // Check response for success or error.
241 if ($response->IsError)
242 echo xl("Error confirming receipt of lab results") . ": {$response->ErrorMessage}<br>\n";
243 // else {
244 // echo xl("Success confirming receipt of lab result") . " <br>\n";
245 // echo $response->ResponseXml;
246 // }
249 // report on lab_patient_errors
250 $lab_query_report .= "<br>". xl("Provider Matching Errors") . ":<hr><table style=\"font-size:12px;\" cellpadding='3px'>";
252 if(count($lab_provider_errors) == 0)
253 $lab_query_report .= "<tr><td>" .xl("No errors found"). "</td></tr>";
254 else
256 $lab_query_report .= "<tr><td>" .
257 xl("First Name") . "</td><td>" .
258 xl("Last Name") . "</td><td>" .
259 xl("NPI") . "</td><tr>";
261 foreach ($lab_provider_errors as $labResultReport)
263 $lab_query_report .=
264 "<tr><td>{$labResultReport->OrderingProviderFirstName}</td>".
265 "<td>{$labResultReport->OrderingProviderLastName}</td>".
266 "<td>{$labResultReport->OrderingProviderId}</td></tr>";
269 $lab_query_report .= "</table>";
272 // report on lab_patient_errors
273 $lab_query_report .= "<br>". xl("Patient Lookup Errors") . ":<hr><table style=\"font-size:12px;\" cellpadding='3px'>";
275 if(count($lab_patient_errors) == 0)
276 $lab_query_report .= "<tr><td>" .xl("No errors found"). "</td></tr>";
277 else
279 $lab_query_report .= "<tr><td>" .
280 xl("First Name") . "</td><td>" .
281 xl("Middle Name") . "</td><td>" .
282 xl("Last Name") . "</td><td>" .
283 xl("DOB") . "</td><td>" .
284 xl("Gender") . "</td><td>" .
285 xl("External Id") . "</td><td>" .
286 xl("SSN") . "</td><td>" .
287 xl("Address") . "</td><td>" .
288 xl("City") . "</td><td>".
289 xl("State") . "</td><td>" .
290 xl("Zip") . "</td><td>" .
291 xl("Home Phone") . "</td></tr>";
292 foreach ($lab_patient_errors as $labResult) {
293 $patient = $labResult->Patient;
294 $lab_query_report .= "<tr><td>{$patient->FirstName}</td>" .
295 "<td>{$patient->MiddleName}</td>" .
296 "<td>{$patient->LastName}</td>".
297 "<td>{$patient->DOB}</td>".
298 "<td>{$patient->Gender}</td>".
299 "<td>{$patient->ExternalId}</td>".
300 "<td>{$patient->SSN}</td>".
301 "<td>{$patient->Address}</td>".
302 "<td>{$patient->City}</td>".
303 "<td>{$patient->State}</td>".
304 "<td>{$patient->Zip}</td>".
305 "<td>{$patient->HomePhone}</td></tr>";
308 $lab_query_report .= "</table>";
310 // report on lab_patient_success
311 $lab_query_report .= "<br><br>". xl("New results from Lab Exchange") . ":<hr><table style=\"font-size:12px;\" >";
313 if(count($lab_patient_success) == 0)
314 $lab_query_report .= "<tr><td>" . xl("No new results found") . "</td></tr>";
315 else
317 $lab_query_report .= "<tr><td>" .
318 xl("First Name") . "</td><td>" .
319 xl("Middle Name") . "</td><td>" .
320 xl("Last Name") . "</td><td>" .
321 xl("DOB") . "</td><td>" .
322 xl("Gender") . "</td><td>" .
323 xl("External Id") . "</td><td>" .
324 xl("SSN") . "</td><td>" .
325 xl("Address") . "</td><td>" .
326 xl("City") . "</td><td>" .
327 xl("State") . "</td><td>" .
328 xl("Zip") . "</td><td>" .
329 xl("Home Phone") . "</td></tr>";
330 foreach ($lab_patient_success as $labResult) {
331 $patient = $labResult->Patient;
332 $lab_query_report .= "<tr><td>{$patient->FirstName}</td>" .
333 "<td>{$patient->MiddleName}</td>" .
334 "<td>{$patient->LastName}</td>".
335 "<td>{$patient->DOB}</td>".
336 "<td>{$patient->Gender}</td>".
337 "<td>{$patient->ExternalId}</td>".
338 "<td>{$patient->SSN}</td>".
339 "<td>{$patient->Address}</td>".
340 "<td>{$patient->City}</td>".
341 "<td>{$patient->State}</td>".
342 "<td>{$patient->Zip}</td>".
343 "<td>{$patient->HomePhone}</td></tr>";
346 $lab_query_report .= "</table>";
350 <html>
351 <head>
353 <?php html_header_show(); ?>
354 <link rel="stylesheet" href="<?php echo $css_header; ?>" type="text/css">
355 <script type="text/javascript" src="../../../library/dialog.js"></script>
356 <script type="text/javascript" src="../../../library/textformat.js"></script>
357 <script type="text/javascript" src="<?php echo $GLOBALS['webroot']; ?>/library/js/jquery.js"></script>
358 </head>
360 <body class="body_top">
361 <div id="lab_report">
362 <center>
363 <table style="width: 80%;"><tr><td><span class="title"><?php echo htmlspecialchars(xl('Lab Results Report'), ENT_NOQUOTES); ?></span></td></tr></table>
364 <br>
365 <table style="width: 90%; border: 1px solid black; font-size:12px;">
366 <tr><td><?php echo $lab_query_report; ?></td></tr>
367 </table>
368 </center>
369 </div>
370 </body>
371 </html>