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");
10 // Find and match the patient with the incoming lab report.
11 // return patient pid if matched else return false
12 function lab_exchange_match_patient($externalId, $firstName, $middleName, $lastName, $dob, $gender, $ssn, $address) {
13 $sql = "SELECT pid from patient_data WHERE ";
16 // Search for pid and return if pid match with $externalId(from lab API)
17 if ($externalId != "") {
18 $where .= "pid = '".add_escape_custom($externalId)."' " ;
19 $res = sqlQuery($sql . $where);
29 // If empty $externalId or externalId no matched
30 if (preg_replace("/[:space:]/", "", $firstName) != "")
31 $where .= "fname = '".add_escape_custom($firstName)."' " ;
33 if (preg_replace("/[:space:]/", "", $lastName) != "") {
34 if ($where != "") $where .= "AND ";
35 $where .= "lname = '".add_escape_custom($lastName)."' " ;
38 // if (ereg_replace("[:space:]", "", $middleName) != ""){
39 // if ($where != "") $where .= "AND ";
40 // $where .= "mname = '".add_escape_custom($middleName)."' " ;
43 if (preg_replace("/[:space:]/", "", $dob) != ""){
44 if ($where != "") $where .= "AND ";
45 $where .= "DOB = DATE_FORMAT('".add_escape_custom($dob)."', '%Y-%m-%d') " ;
48 if (preg_replace("/[:space:]/", "", $gender) != "") {
49 if ($gender =="F") $sex = "Female";
50 if ($gender =="M") $sex = "Male";
54 if ($where != "") $where .= "AND ";
55 $where .= "(sex = '".add_escape_custom($sex)."' OR sex = '" . add_escape_custom($gender) ."')" ;
59 if (preg_replace("/[:space:]/", "", $ssn) != ""){
60 if ($where != "") $where .= "AND ";
61 // Change to xxx-xx-xxxx format.
62 $ss = substr($ssn,0,3)."-".substr($ssn,3,2)."-".substr($ssn,5);
63 $where .= "(ss = '".add_escape_custom($ssn)."' OR ss = '".add_escape_custom($ss)."' OR ss = '')";
70 $res = sqlQuery($sql . $where);
81 * identify the lab ordering provider and return the userid.
83 * parameters are populated from the lab result
86 * @param <type> $lastName
87 * @param <type> $firstName
88 * @return <type> user.id
90 function lab_exchange_match_provider($id, $lastName, $firstName) {
91 $sql = "SELECT user_id from laboratory_providers WHERE ";
94 if (ereg_replace("[:space:]", "", $lastName) != "")
95 $where .= "provider_lname = '".add_escape_custom($lastName)."' " ;
97 if (ereg_replace("[:space:]", "", $firstName) != "") {
98 if ($where != "") $where .= "AND ";
99 $where .= "provider_fname = '".add_escape_custom($firstName)."' " ;
102 if (ereg_replace("[:space:]", "", $id) != "") {
103 if ($where != "") $where .= "AND ";
104 $where .= "provider_id = '".add_escape_custom($id)."' " ;
111 $res = sqlQuery($sql . $where);
112 if ($res['user_id']) {
113 // echo "found id: " . $res['user_id'];
114 return $res['user_id'];
117 // echo "found no id using " . $lastName .", " . $firstName .", " . $id;
125 * identify the lab ordering provider and return the userid.
127 * parameters are populated from the lab result
130 * @param <type> $lastName
131 * @param <type> $firstName
132 * @return <type> user.id if npi exists in users table; false if npi cannot be found
134 function lab_exchange_match_provider($npi)
140 $sql = "SELECT id from users WHERE npi = " . $npi;
141 $res = sqlQuery($sql);
143 return isset($res['id']) ?
$res['id'] : false;
147 * process the lab facility information
149 * @param <type> $facilities - potentially multiple facilities for performing lab info
150 * @return <type> facilityID
152 function processFacility($facilities)
154 // Loop through the facility
155 // There can be several facilities.
156 // Also there is no good place to store a reference to users.id for facility info lookup,
157 // so I'm concatenating the table id onto the lab id prior to the addition of a colon
161 foreach ($facilities as $facility) {
162 // Access facility fields
165 if(!$users_id = getLabFacility($facility))
167 $users_id = addNewLabFacility($facility);
169 $facilityId[] = $facility->FacilityID
. "_" . $users_id; //=>procedure_result.facility
173 if (count($facilityId) > 0) {
174 $str_facilityId = implode(":", $facilityId);
176 return $str_facilityId;
180 * @param <type> $facility
181 * @return <type> returns the user id for the lab facility record if it exists in the database, false otherwise.
183 function getLabFacility($facility)
185 $query = "select id from users where fname = '" . trim($facility->FacilityDirectorFirstName
) . "' AND " .
186 "lname = '" . trim($facility->FacilityDirectorLastName
) . "' AND " .
187 "street = '" . trim($facility->FacilityAddress
) . "' AND " .
188 "city = '" . trim($facility->FacilityCity
) . "' AND " .
189 "state = '" . trim($facility->FacilityState
) . "' AND " .
190 "zip = " . trim($facility->FacilityZip
) . " AND " .
191 "organization = '" . trim($facility->FacilityName
) ."'";
193 $res = sqlStatement($query);
194 $result = sqlFetchArray($res);
196 return isset($result['id']) ?
$result['id'] : false;
200 * @param <type> $facilityID
201 * @return <type> the result set, false if the input is malformed
203 function getFacilityInfo($facilityID)
205 // 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.
206 $facility = explode("_", $facilityID);
208 if(count($facility) > 1)
211 title,fname,lname,street,city,state,zip,organization,phone
212 from users where id = " . $facility[1];
214 $res = sqlStatement($query);
215 return sqlFetchArray($res);
221 * @param <type> $facility
222 * @return <type> returns the id
224 function addNewLabFacility($facility)
226 $query = "INSERT INTO users ( " .
227 "username, password, authorized, info, source, " .
228 "title, fname, lname, mname, " .
229 "federaltaxid, federaldrugid, upin, facility, see_auth, active, npi, taxonomy, " .
230 "specialty, organization, valedictory, assistant, billname, email, url, " .
231 "street, streetb, city, state, zip, " .
232 "street2, streetb2, city2, state2, zip2," .
233 "phone, phonew1, phonew2, phonecell, fax, notes, abook_type " .
237 "0, " . // authorized
240 "'" . trim($facility->FacilityDirectorTitle
) . "', " .
241 "'" . trim($facility->FacilityDirectorFirstName
) . "', " .
242 "'" . trim($facility->FacilityDirectorLastName
) . "', " .
245 "'', " . // federaldrugid
253 "'" . trim($facility->FacilityName
) . "', " .
259 "'" . trim($facility->FacilityAddress
) . "', " .
261 "'" . trim($facility->FacilityCity
) . "', " .
262 "'" . trim($facility->FacilityState
) . "', " .
263 "'" . trim($facility->FacilityZip
) . "', " .
269 "'" . trim($facility->FacilityPhone
) . "', " .
278 return sqlInsert($query);
281 function mapReportStatus($stat) {
282 $return_status = $stat;
285 // $return_status = "unknown";
286 if($stat=="F" ||
$stat=="f")
287 $return_status = "final";
288 if($stat=="P" ||
$stat=="p")
289 $return_status = "prelim";
290 if($stat=="X" ||
$stat=="x")
291 $return_status = "cancel";
292 if($stat=="C" ||
$stat=="c")
293 $return_status = "correct";
295 return $return_status;
298 function mapResultStatus($stat) {
299 $return_status = $stat;
302 // $return_status = "unknown";
303 if($stat=="F" ||
$stat=="f")
304 $return_status = "final";
305 if($stat=="P" ||
$stat=="p")
306 $return_status = "prelim";
307 if($stat=="X" ||
$stat=="x")
308 $return_status = "cancel";
309 if($stat=="C" ||
$stat=="c")
310 $return_status = "correct";
311 if($stat=="I" ||
$stat=="i")
312 $return_status = "incomplete";
314 return $return_status;
317 function mapAbnormalStatus($stat) {
318 $return_status = $stat;
321 // $return_status = "unknown";
322 if($stat=="L" ||
$stat=="l")
323 $return_status = "low";
324 if($stat=="H" ||
$stat=="h")
325 $return_status = "high";
326 if($stat=="LL" ||
$stat=="ll")
327 $return_status = "low";
328 if($stat=="HH" ||
$stat=="hh")
329 $return_status = "high";
331 $return_status = "low";
333 $return_status = "high";
334 if($stat=="A" ||
$stat=="a")
335 $return_status = "yes";
337 return $return_status;
340 function formatPhone($phone)
342 $phone = preg_replace("/[^0-9]/", "", $phone);
343 if(strlen($phone) == 7)
344 return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
345 elseif(strlen($phone) == 10)
346 return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);