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)
14 $sql = "SELECT pid from patient_data WHERE ";
17 // Search for pid and return if pid match with $externalId(from lab API)
18 if ($externalId != "") {
19 $where .= "pid = '".add_escape_custom($externalId)."' " ;
20 $res = sqlQuery($sql . $where);
30 // If empty $externalId or externalId no matched
31 if (preg_replace("/[:space:]/", "", $firstName) != "") {
32 $where .= "fname = '".add_escape_custom($firstName)."' " ;
35 if (preg_replace("/[:space:]/", "", $lastName) != "") {
40 $where .= "lname = '".add_escape_custom($lastName)."' " ;
43 // if (ereg_replace("[:space:]", "", $middleName) != ""){
44 // if ($where != "") $where .= "AND ";
45 // $where .= "mname = '".add_escape_custom($middleName)."' " ;
48 if (preg_replace("/[:space:]/", "", $dob) != "") {
53 $where .= "DOB = DATE_FORMAT('".add_escape_custom($dob)."', '%Y-%m-%d') " ;
56 if (preg_replace("/[:space:]/", "", $gender) != "") {
70 $where .= "(sex = '".add_escape_custom($sex)."' OR sex = '" . add_escape_custom($gender) ."')" ;
74 if (preg_replace("/[:space:]/", "", $ssn) != "") {
79 // Change to xxx-xx-xxxx format.
80 $ss = substr($ssn, 0, 3)."-".substr($ssn, 3, 2)."-".substr($ssn, 5);
81 $where .= "(ss = '".add_escape_custom($ssn)."' OR ss = '".add_escape_custom($ss)."' OR ss = '')";
87 $res = sqlQuery($sql . $where);
97 * identify the lab ordering provider and return the userid.
99 * parameters are populated from the lab result
102 * @param <type> $lastName
103 * @param <type> $firstName
104 * @return <type> user.id
106 function lab_exchange_match_provider($id, $lastName, $firstName) {
107 $sql = "SELECT user_id from laboratory_providers WHERE ";
110 if (ereg_replace("[:space:]", "", $lastName) != "")
111 $where .= "provider_lname = '".add_escape_custom($lastName)."' " ;
113 if (ereg_replace("[:space:]", "", $firstName) != "") {
114 if ($where != "") $where .= "AND ";
115 $where .= "provider_fname = '".add_escape_custom($firstName)."' " ;
118 if (ereg_replace("[:space:]", "", $id) != "") {
119 if ($where != "") $where .= "AND ";
120 $where .= "provider_id = '".add_escape_custom($id)."' " ;
127 $res = sqlQuery($sql . $where);
128 if ($res['user_id']) {
129 // echo "found id: " . $res['user_id'];
130 return $res['user_id'];
133 // echo "found no id using " . $lastName .", " . $firstName .", " . $id;
141 * identify the lab ordering provider and return the userid.
143 * parameters are populated from the lab result
146 * @param <type> $lastName
147 * @param <type> $firstName
148 * @return <type> user.id if npi exists in users table; false if npi cannot be found
150 function lab_exchange_match_provider($npi)
155 $sql = "SELECT id from users WHERE npi = " . $npi;
156 $res = sqlQuery($sql);
159 return isset($res['id']) ?
$res['id'] : false;
163 * process the lab facility information
165 * @param <type> $facilities - potentially multiple facilities for performing lab info
166 * @return <type> facilityID
168 function processFacility($facilities)
170 // Loop through the facility
171 // There can be several facilities.
172 // Also there is no good place to store a reference to users.id for facility info lookup,
173 // so I'm concatenating the table id onto the lab id prior to the addition of a colon
177 foreach ($facilities as $facility) {
178 // Access facility fields
181 if (!$users_id = getLabFacility($facility)) {
182 $users_id = addNewLabFacility($facility);
185 $facilityId[] = $facility->FacilityID
. "_" . $users_id; //=>procedure_result.facility
188 if (count($facilityId) > 0) {
189 $str_facilityId = implode(":", $facilityId);
192 return $str_facilityId;
196 * @param <type> $facility
197 * @return <type> returns the user id for the lab facility record if it exists in the database, false otherwise.
199 function getLabFacility($facility)
201 $query = "select id from users where fname = '" . trim($facility->FacilityDirectorFirstName
) . "' AND " .
202 "lname = '" . trim($facility->FacilityDirectorLastName
) . "' AND " .
203 "street = '" . trim($facility->FacilityAddress
) . "' AND " .
204 "city = '" . trim($facility->FacilityCity
) . "' AND " .
205 "state = '" . trim($facility->FacilityState
) . "' AND " .
206 "zip = " . trim($facility->FacilityZip
) . " AND " .
207 "organization = '" . trim($facility->FacilityName
) ."'";
209 $res = sqlStatement($query);
210 $result = sqlFetchArray($res);
212 return isset($result['id']) ?
$result['id'] : false;
216 * @param <type> $facilityID
217 * @return <type> the result set, false if the input is malformed
219 function getFacilityInfo($facilityID)
221 // 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.
222 $facility = explode("_", $facilityID);
224 if (count($facility) > 1) {
226 title,fname,lname,street,city,state,zip,organization,phone
227 from users where id = " . $facility[1];
229 $res = sqlStatement($query);
230 return sqlFetchArray($res);
237 * @param <type> $facility
238 * @return <type> returns the id
240 function addNewLabFacility($facility)
242 $query = "INSERT INTO users ( " .
243 "username, password, authorized, info, source, " .
244 "title, fname, lname, mname, " .
245 "federaltaxid, federaldrugid, upin, facility, see_auth, active, npi, taxonomy, " .
246 "specialty, organization, valedictory, assistant, billname, email, url, " .
247 "street, streetb, city, state, zip, " .
248 "street2, streetb2, city2, state2, zip2," .
249 "phone, phonew1, phonew2, phonecell, fax, notes, abook_type " .
253 "0, " . // authorized
256 "'" . trim($facility->FacilityDirectorTitle
) . "', " .
257 "'" . trim($facility->FacilityDirectorFirstName
) . "', " .
258 "'" . trim($facility->FacilityDirectorLastName
) . "', " .
261 "'', " . // federaldrugid
269 "'" . trim($facility->FacilityName
) . "', " .
275 "'" . trim($facility->FacilityAddress
) . "', " .
277 "'" . trim($facility->FacilityCity
) . "', " .
278 "'" . trim($facility->FacilityState
) . "', " .
279 "'" . trim($facility->FacilityZip
) . "', " .
285 "'" . trim($facility->FacilityPhone
) . "', " .
294 return sqlInsert($query);
297 function mapReportStatus($stat)
299 $return_status = $stat;
302 // $return_status = "unknown";
303 if ($stat=="F" ||
$stat=="f") {
304 $return_status = "final";
307 if ($stat=="P" ||
$stat=="p") {
308 $return_status = "prelim";
311 if ($stat=="X" ||
$stat=="x") {
312 $return_status = "cancel";
315 if ($stat=="C" ||
$stat=="c") {
316 $return_status = "correct";
319 return $return_status;
322 function mapResultStatus($stat)
324 $return_status = $stat;
327 // $return_status = "unknown";
328 if ($stat=="F" ||
$stat=="f") {
329 $return_status = "final";
332 if ($stat=="P" ||
$stat=="p") {
333 $return_status = "prelim";
336 if ($stat=="X" ||
$stat=="x") {
337 $return_status = "cancel";
340 if ($stat=="C" ||
$stat=="c") {
341 $return_status = "correct";
344 if ($stat=="I" ||
$stat=="i") {
345 $return_status = "incomplete";
348 return $return_status;
351 function mapAbnormalStatus($stat)
353 $return_status = $stat;
356 // $return_status = "unknown";
357 if ($stat=="L" ||
$stat=="l") {
358 $return_status = "low";
361 if ($stat=="H" ||
$stat=="h") {
362 $return_status = "high";
365 if ($stat=="LL" ||
$stat=="ll") {
366 $return_status = "low";
369 if ($stat=="HH" ||
$stat=="hh") {
370 $return_status = "high";
374 $return_status = "low";
378 $return_status = "high";
381 if ($stat=="A" ||
$stat=="a") {
382 $return_status = "yes";
385 return $return_status;
388 function formatPhone($phone)
390 $phone = preg_replace("/[^0-9]/", "", $phone);
391 if (strlen($phone) == 7) {
392 return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
393 } elseif (strlen($phone) == 10) {
394 return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);