3 // Copyright (C) 2010 OpenEMR Support LLC
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 require_once("../globals.php");
10 require_once("$srcdir/lab_exchange_api.php");
11 require_once("lab_exchange_match.php");
12 require_once("../main/messages/lab_results_messages.php");
13 include_once("$srcdir/formdata.inc.php");
16 // Create the REST client
17 $client = new LabExchangeClient($LAB_EXCHANGE_SITEID, $LAB_EXCHANGE_TOKEN, $LAB_EXCHANGE_ENDPOINT);
20 $response = $client->sendRequest("results", "GET");
22 // Check response for success or error
23 if($response->IsError
)
24 echo xl("Error getting lab results from Lab Exchange Network").": {$response->ErrorMessage}\n";
26 echo xl("Success getting lab results")." \n";
27 $resultSet = $response->ResponseXml
;
29 // For each lab result message
30 foreach($resultSet->LabResult
as $labResult) {
31 // Get the id for this message
32 $id = $labResult['id'];
33 // echo "ID:" . $id . "\n";
34 // Get the patient array
35 $patient = $labResult->Patient
;
36 // Access patient fields
37 $lastName = $patient->LastName
;
38 $firstName = $patient->FirstName
;
39 $middleName = $patient->MiddleName
;
41 $gender = $patient->Gender
;
42 $externalId = $patient->ExternalId
;
44 $address = $patient->Address
;
45 $city = $patient->City
;
46 $state = $patient->State
;
48 $homePhone = $patient->HomePhone
;
49 // Do something with patient, ie, find matching patient in DB
50 // echo "Patient:" . $lastName . "\n";
53 $patient_id = lab_exchange_match_patient($externalId, $firstName, $middleName, $lastName, $dob, $gender, $ssn, $address);
54 if (!$patient_id) continue;
56 // Loop through the facility
57 // There can be several facilities. You can either aggregate them with comma to fit into one field
60 foreach ($resultSet->Facility
as $facility){
61 // Access facility fields
62 $facilityId[] = $facility->FacilityID
; //=>procedure_result.facility
63 $facilityName = $facility->FacilityName
; //not mapped
66 if (count($facilityId) > 0) {
67 $str_facilityId = implode(":", $facilityId);
69 // Echo "facility:".$str_facilityId ."<br>";
71 // Loop through all the Result Report
72 foreach ($labResult->ResultReport
as $resultReport) {
74 // Access report fields
75 // ResultReport maps to procedure_order and procedure_report tables
76 $observationCode = $resultReport->ObservationCode
; // => procedure_order.procedure_type_id
77 $observationText = $resultReport->ObservationText
; // => This text should be the same as procedure_type.name
78 $observationDate = $resultReport->ObservationDateTime
; // => procedure_report.date_collected
79 //$observationStatus = $resultReport->ObservationStatus; // => procedure_report.report_status
80 $observationStatus = $resultReport->OrderResultStatus
; // => procedure_report.report_status
81 $controlId = $resultReport->ForeignAccId
; // This is the CONTROL ID that is sent back
82 $orderingProviderId = $resultReport->OrderingProviderId
; // =>procedure_order.provider_id But the ID here is NOT the same ID as OpenEMR. You have to match it correctly
83 $orderingProviderLastName = $resultReport->OrderingProviderLastName
; // Use this to match the provider ID
84 $orderingProviderFirstName = $resultReport->OrderingProviderFirstName
; // Use this to match the provider ID
86 // Do something with the report, ie put in DB
87 // echo $observationCode .":".$observationText.":".$observationStatus. "\n";
90 $user_id = lab_exchange_match_provider($orderingProviderLastName, $orderingProviderFirstName);
93 $date = substr($observationDate,0,8);
95 $check_type = sqlQuery("SELECT COUNT(*) AS count FROM procedure_type WHERE procedure_type_id = '".add_escape_custom($observationCode)."'");
97 if ($check_type['count'] <= 0) {
99 "procedure_type_id = '".add_escape_custom($observationCode)."', " .
100 "name = '".add_escape_custom($observationText)."', ".
101 "procedure_type = 'res'";
103 $type_id = sqlInsert("INSERT INTO procedure_type SET $sql_type_data");
106 $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)."'");
108 if ($check_order['count'] <= 0) {
110 "procedure_type_id = '".add_escape_custom($observationCode)."', " .
111 "provider_id = '".add_escape_custom($user_id)."', " .
112 "patient_id = '".add_escape_custom($patient_id)."', " .
113 "date_collected = DATE_FORMAT('".add_escape_custom($observationDate.'00')."', '%Y%m%d%H%i%s'), " .
114 "date_ordered = DATE_FORMAT('".add_escape_custom($date)."', '%Y%m%d'), " .
115 "order_priority = 'normal', " .
116 "order_status = 'complete', " .
117 "control_id = '".add_escape_custom($controlId)."'";
119 $order_id = sqlInsert("INSERT INTO procedure_order SET $sql_order_data");
123 "provider_id = '".add_escape_custom($user_id)."', " .
124 "date_collected = DATE_FORMAT('".add_escape_custom($observationDate.'00')."', '%Y%m%d%H%i%s'), " .
125 "order_priority = 'normal', " .
126 "order_status = 'complete'";
128 if ($check_order['patient_id'] == "") {
130 ", patient_id = '".add_escape_custom($patient_id)."'";
133 $patient_id = $check_order['patient_id'];
136 if ($check_order['provider_id'] == "" or $check_order['provider_id'] <= 0) {
138 ", provider_id = '".add_escape_custom($user_id)."'";
141 $user_id = $check_order['provider_id'];
144 $order_id = $check_order['procedure_order_id'];
145 sqlStatement("UPDATE procedure_order SET $sql_order_data WHERE procedure_order_id = '".add_escape_custom($order_id)."'");
148 $report_status = mapReportStatus($observationStatus);
150 $check_report = sqlQuery("SELECT COUNT(*) AS count, procedure_report_id FROM procedure_report WHERE procedure_order_id = '".add_escape_custom($order_id)."'");
152 if ($check_report['count'] <= 0) {
154 "procedure_order_id = '".add_escape_custom($order_id)."', ".
155 "date_collected = DATE_FORMAT('".add_escape_custom($observationDate."00")."', '%Y%m%d%H%i%s'), " .
156 "source = '".add_escape_custom($user_id)."', " .
157 "date_report = DATE_FORMAT('".add_escape_custom($date)."', '%Y%m%d'), " .
158 "report_status = '".add_escape_custom($report_status)."', ".
159 "review_status = 'received'";
161 $report_id = sqlInsert("INSERT INTO procedure_report SET $sql_report_data");
165 "date_collected = DATE_FORMAT('".add_escape_custom($observationDate."00")."', '%Y%m%d%H%i%s'), " .
166 "source = '".add_escape_custom($user_id)."', " .
167 "report_status = '".add_escape_custom($report_status)."', ".
168 "review_status = 'received'";
170 $report_id = $check_report['procedure_report_id'];
171 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)."'");
174 // Loop through all Results
175 // Result maps to procedure_report table
176 foreach ($resultReport->Result
as $result) {
177 // Access result fields
178 $resultCode = $result->ObservationId
; // => procedure_result.procedure_type_id
179 $resultCodeTex = $result->ObservationText
; // => This text should be the same as procedure_type.name
180 $value = $result->Value
; // => procedure_result.result
181 $unit = $result->Unit
; // => procedure_result.units
182 $referenceRange = $result->ReferenceRange
; //=> procedure_result.range
183 $abnormalFlag = $result->AbnormalFlag
; // => procedure_result.abnormal
184 $resultStatus = $result->ResultStatus
; // => procedure_result.result_status
185 $resultDateTime = $result->ResultDateTime
; // => procedure_result.date
186 $comment = $result->CommentText
; //=> procedure_result.comments
187 //Do something with result, ie put in DB
188 //echo $resultCode . ":" .$value . " " . $unit. "\n";
189 $check_type2 = sqlQuery("SELECT COUNT(*) AS count FROM procedure_type WHERE procedure_type_id = '".add_escape_custom($resultCode)."'");
190 if ($check_type2['count'] <= 0) {
192 "procedure_type_id = '".add_escape_custom($resultCode)."', " .
193 "parent = '".add_escape_custom($observationCode)."', " .
194 "name = '".add_escape_custom($resultCodeTex)."', ".
195 "procedure_type = 'res'";
197 $type_id = sqlInsert("INSERT INTO procedure_type SET $sql_type_data");
200 $result_status = mapResultStatus($resultStatus);
202 $abnormalFlag = mapAbnormalStatus($abnormalFlag);
204 $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)."'");
206 if ($check_result['count'] <= 0) {
209 "procedure_report_id = '".add_escape_custom($report_id)."', ".
210 "procedure_type_id = '".add_escape_custom($resultCode)."', ".
211 "date = DATE_FORMAT('".add_escape_custom($resultDateTime.'00')."', '%Y%m%d%H%i%s'), ".
212 "facility = '".add_escape_custom($str_facilityId)."', " .
213 "units = '".add_escape_custom($unit)."', ".
214 "result = '".add_escape_custom($value)."', ".
215 "`range` = '".add_escape_custom($referenceRange)."', ".
216 "abnormal = '".add_escape_custom($abnormalFlag)."', ".
217 "comments = '".add_escape_custom($comment)."', ".
218 "result_status = '".add_escape_custom($result_status)."'";
220 sqlInsert("INSERT INTO procedure_result SET $sql_result_data");
224 "date = DATE_FORMAT('".add_escape_custom($resultDateTime.'00')."', '%Y%m%d%H%i%s'), ".
225 "facility = '".add_escape_custom($str_facilityId)."', " .
226 "units = '".add_escape_custom($unit)."', ".
227 "result = '".add_escape_custom($value)."', ".
228 "`range` = '".add_escape_custom($referenceRange)."', ".
229 "abnormal = '".add_escape_custom($abnormalFlag)."', ".
230 "comments = '".add_escape_custom($comment)."', ".
231 "result_status = '".add_escape_custom($result_status)."'";
233 sqlStatement("UPDATE procedure_result SET $sql_result_data WHERE procedure_result_id = '".add_escape_custom($check_result['procedure_result_id'])."'");
238 // Send a message regarding a report with pending review status.
239 lab_results_messages($patient_id, $report_id, $user_id);
243 // Need to confirm that the lab result message has been received.
244 // This is the url of the confirm request.
245 $url = "confirm/" . $id;
246 // Make the confirmation request.
247 $response = $client->sendRequest($url, "POST");
248 // Check response for success or error.
249 if($response->IsError
)
250 echo xl("Error confirming receipt of lab results").": {$response->ErrorMessage}\n";
252 echo xl("Success confirming receipt of lab result")." \n";
253 echo $response->ResponseXml
;