Changes to support electronic lab ordering and results.
[openemr.git] / interface / orders / lab_exchange_tools.php
blob120519aac4fd142f402bddc19c2ab6385f0f9e48
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/sql.inc");
10 require_once("$srcdir/formdata.inc.php");
12 // Find and match the patient with the incoming lab report.
13 // return patient pid if matched else return false
14 function lab_exchange_match_patient($externalId, $firstName, $middleName, $lastName, $dob, $gender, $ssn, $address) {
15 $sql = "SELECT pid from patient_data WHERE ";
16 $where = "";
17 /*
18 // Search for pid and return if pid match with $externalId(from lab API)
19 if ($externalId != "") {
20 $where .= "pid = '".add_escape_custom($externalId)."' " ;
21 $res = sqlQuery($sql . $where);
22 if ($res['pid']) {
23 return $res['pid'];
25 else {
26 $where = "";
31 // If empty $externalId or externalId no matched
32 if (ereg_replace("[:space:]", "", $firstName) != "")
33 $where .= "fname = '".add_escape_custom($firstName)."' " ;
35 if (ereg_replace("[:space:]", "", $lastName) != "") {
36 if ($where != "") $where .= "AND ";
37 $where .= "lname = '".add_escape_custom($lastName)."' " ;
40 // if (ereg_replace("[:space:]", "", $middleName) != ""){
41 // if ($where != "") $where .= "AND ";
42 // $where .= "mname = '".add_escape_custom($middleName)."' " ;
43 // }
45 if (ereg_replace("[:space:]", "", $dob) != ""){
46 if ($where != "") $where .= "AND ";
47 $where .= "DOB = DATE_FORMAT('".add_escape_custom($dob)."', '%Y-%m-%d') " ;
50 if (ereg_replace("[:space:]", "", $gender) != "") {
51 if ($gender =="F") $sex = "Female";
52 if ($gender =="M") $sex = "Male";
54 if(isset($sex))
56 if ($where != "") $where .= "AND ";
57 $where .= "(sex = '".add_escape_custom($sex)."' OR sex = '" . add_escape_custom($gender) ."')" ;
61 if (ereg_replace("[:space:]", "", $ssn) != ""){
62 if ($where != "") $where .= "AND ";
63 // Change to xxx-xx-xxxx format.
64 $ss = substr($ssn,0,3)."-".substr($ssn,3,2)."-".substr($ssn,5);
65 $where .= "(ss = '".add_escape_custom($ssn)."' OR ss = '".add_escape_custom($ss)."' OR ss = '')";
68 if ($where == "") {
69 return false;
71 else {
72 $res = sqlQuery($sql . $where);
73 if ($res['pid']) {
74 return $res['pid'];
76 else {
77 return false;
82 /**
83 * identify the lab ordering provider and return the userid.
85 * parameters are populated from the lab result
87 * @param <type> $id
88 * @param <type> $lastName
89 * @param <type> $firstName
90 * @return <type> user.id
92 function lab_exchange_match_provider($id, $lastName, $firstName) {
93 $sql = "SELECT user_id from laboratory_providers WHERE ";
94 $where = "";
96 if (ereg_replace("[:space:]", "", $lastName) != "")
97 $where .= "provider_lname = '".add_escape_custom($lastName)."' " ;
99 if (ereg_replace("[:space:]", "", $firstName) != "") {
100 if ($where != "") $where .= "AND ";
101 $where .= "provider_fname = '".add_escape_custom($firstName)."' " ;
104 if (ereg_replace("[:space:]", "", $id) != "") {
105 if ($where != "") $where .= "AND ";
106 $where .= "provider_id = '".add_escape_custom($id)."' " ;
109 if ($where == "") {
110 return false;
112 else {
113 $res = sqlQuery($sql . $where);
114 if ($res['user_id']) {
115 // echo "found id: " . $res['user_id'];
116 return $res['user_id'];
118 else {
119 // echo "found no id using " . $lastName .", " . $firstName .", " . $id;
120 return false;
127 * identify the lab ordering provider and return the userid.
129 * parameters are populated from the lab result
131 * @param <type> $id
132 * @param <type> $lastName
133 * @param <type> $firstName
134 * @return <type> user.id if npi exists in users table; false if npi cannot be found
136 function lab_exchange_match_provider($npi)
138 $npi = trim($npi);
140 if(!empty($npi))
142 $sql = "SELECT id from users WHERE npi = " . $npi;
143 $res = sqlQuery($sql);
145 return isset($res['id']) ? $res['id'] : false;
149 * process the lab facility information
151 * @param <type> $facilities - potentially multiple facilities for performing lab info
152 * @return <type> facilityID
154 function processFacility($facilities)
156 // Loop through the facility
157 // There can be several facilities.
158 // Also there is no good place to store a reference to users.id for facility info lookup,
159 // so I'm concatenating the table id onto the lab id prior to the addition of a colon
161 $facilityId = null;
163 foreach ($facilities as $facility) {
164 // Access facility fields
165 $users_id = "";
167 if(!$users_id = getLabFacility($facility))
169 $users_id = addNewLabFacility($facility);
171 $facilityId[] = $facility->FacilityID . "_" . $users_id; //=>procedure_result.facility
175 if (count($facilityId) > 0) {
176 $str_facilityId = implode(":", $facilityId);
178 return $str_facilityId;
182 * @param <type> $facility
183 * @return <type> returns the user id for the lab facility record if it exists in the database, false otherwise.
185 function getLabFacility($facility)
187 $query = "select id from users where fname = '" . trim($facility->FacilityDirectorFirstName) . "' AND " .
188 "lname = '" . trim($facility->FacilityDirectorLastName) . "' AND " .
189 "street = '" . trim($facility->FacilityAddress) . "' AND " .
190 "city = '" . trim($facility->FacilityCity) . "' AND " .
191 "state = '" . trim($facility->FacilityState) . "' AND " .
192 "zip = " . trim($facility->FacilityZip) . " AND " .
193 "organization = '" . trim($facility->FacilityName) ."'";
195 $res = sqlStatement($query);
196 $result = sqlFetchArray($res);
198 return isset($result['id']) ? $result['id'] : false;
202 * @param <type> $facilityID
203 * @return <type> the result set, false if the input is malformed
205 function getFacilityInfo($facilityID)
207 // facility ID will be in the format XX_YY, where XX is the lab-assigned id, Y is the user.id record representing that lab facility, and the _ is a divider.
208 $facility = explode("_", $facilityID);
210 if(count($facility) > 1)
212 $query = "select
213 title,fname,lname,street,city,state,zip,organization,phone
214 from users where id = " . $facility[1];
216 $res = sqlStatement($query);
217 return sqlFetchArray($res);
219 return false;
223 * @param <type> $facility
224 * @return <type> returns the id
226 function addNewLabFacility($facility)
228 $query = "INSERT INTO users ( " .
229 "username, password, authorized, info, source, " .
230 "title, fname, lname, mname, " .
231 "federaltaxid, federaldrugid, upin, facility, see_auth, active, npi, taxonomy, " .
232 "specialty, organization, valedictory, assistant, billname, email, url, " .
233 "street, streetb, city, state, zip, " .
234 "street2, streetb2, city2, state2, zip2," .
235 "phone, phonew1, phonew2, phonecell, fax, notes, abook_type " .
236 ") VALUES ( " .
237 "'', " . // username
238 "'', " . // password
239 "0, " . // authorized
240 "'', " . // info
241 "NULL, " . // source
242 "'" . trim($facility->FacilityDirectorTitle) . "', " .
243 "'" . trim($facility->FacilityDirectorFirstName) . "', " .
244 "'" . trim($facility->FacilityDirectorLastName) . "', " .
245 "'', " .
246 "'', " .
247 "'', " . // federaldrugid
248 "'', " .
249 "'', " . // facility
250 "0, " . // see_auth
251 "1, " . // active
252 "'', " .
253 "'', " .
254 "'', " .
255 "'" . trim($facility->FacilityName) . "', " .
256 "'', " .
257 "'', " .
258 "'', " . // billname
259 "'', " .
260 "'', " .
261 "'" . trim($facility->FacilityAddress) . "', " .
262 "'', " .
263 "'" . trim($facility->FacilityCity) . "', " .
264 "'" . trim($facility->FacilityState) . "', " .
265 "'" . trim($facility->FacilityZip) . "', " .
266 "'', " .
267 "'', " .
268 "'', " .
269 "'', " .
270 "'', " .
271 "'" . trim($facility->FacilityPhone) . "', " .
272 "'', " .
273 "'', " .
274 "'', " .
275 "'', " .
276 "'', " .
277 "'ord_lab'" .
278 ")";
280 return sqlInsert($query);
283 function mapReportStatus($stat) {
284 $return_status = $stat;
286 // if($stat == "")
287 // $return_status = "unknown";
288 if($stat=="F" || $stat=="f")
289 $return_status = "final";
290 if($stat=="P" || $stat=="p")
291 $return_status = "prelim";
292 if($stat=="X" || $stat=="x")
293 $return_status = "cancel";
294 if($stat=="C" || $stat=="c")
295 $return_status = "correct";
297 return $return_status;
300 function mapResultStatus($stat) {
301 $return_status = $stat;
303 // if($stat == "")
304 // $return_status = "unknown";
305 if($stat=="F" || $stat=="f")
306 $return_status = "final";
307 if($stat=="P" || $stat=="p")
308 $return_status = "prelim";
309 if($stat=="X" || $stat=="x")
310 $return_status = "cancel";
311 if($stat=="C" || $stat=="c")
312 $return_status = "correct";
313 if($stat=="I" || $stat=="i")
314 $return_status = "incomplete";
316 return $return_status;
319 function mapAbnormalStatus($stat) {
320 $return_status = $stat;
322 // if($stat == "")
323 // $return_status = "unknown";
324 if($stat=="L" || $stat=="l")
325 $return_status = "low";
326 if($stat=="H" || $stat=="h")
327 $return_status = "high";
328 if($stat=="LL" || $stat=="ll")
329 $return_status = "low";
330 if($stat=="HH" || $stat=="hh")
331 $return_status = "high";
332 if($stat=="<")
333 $return_status = "low";
334 if($stat==">")
335 $return_status = "high";
336 if($stat=="A" || $stat=="a")
337 $return_status = "yes";
339 return $return_status;
342 function formatPhone($phone)
344 $phone = preg_replace("/[^0-9]/", "", $phone);
345 if(strlen($phone) == 7)
346 return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
347 elseif(strlen($phone) == 10)
348 return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
349 else
350 return $phone;