Highway to PSR2
[openemr.git] / interface / orders / lab_exchange_tools.php
blob714aeb8aebe7e48b60712d9e0a0d2f85ddf7234c
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");
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 ";
15 $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);
21 if ($res['pid']) {
22 return $res['pid'];
24 else {
25 $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) != "") {
36 if ($where != "") {
37 $where .= "AND ";
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)."' " ;
46 // }
48 if (preg_replace("/[:space:]/", "", $dob) != "") {
49 if ($where != "") {
50 $where .= "AND ";
53 $where .= "DOB = DATE_FORMAT('".add_escape_custom($dob)."', '%Y-%m-%d') " ;
56 if (preg_replace("/[:space:]/", "", $gender) != "") {
57 if ($gender =="F") {
58 $sex = "Female";
61 if ($gender =="M") {
62 $sex = "Male";
65 if (isset($sex)) {
66 if ($where != "") {
67 $where .= "AND ";
70 $where .= "(sex = '".add_escape_custom($sex)."' OR sex = '" . add_escape_custom($gender) ."')" ;
74 if (preg_replace("/[:space:]/", "", $ssn) != "") {
75 if ($where != "") {
76 $where .= "AND ";
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 = '')";
84 if ($where == "") {
85 return false;
86 } else {
87 $res = sqlQuery($sql . $where);
88 if ($res['pid']) {
89 return $res['pid'];
90 } else {
91 return false;
96 /**
97 * identify the lab ordering provider and return the userid.
99 * parameters are populated from the lab result
101 * @param <type> $id
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 ";
108 $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)."' " ;
123 if ($where == "") {
124 return false;
126 else {
127 $res = sqlQuery($sql . $where);
128 if ($res['user_id']) {
129 // echo "found id: " . $res['user_id'];
130 return $res['user_id'];
132 else {
133 // echo "found no id using " . $lastName .", " . $firstName .", " . $id;
134 return false;
141 * identify the lab ordering provider and return the userid.
143 * parameters are populated from the lab result
145 * @param <type> $id
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)
152 $npi = trim($npi);
154 if (!empty($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
175 $facilityId = null;
177 foreach ($facilities as $facility) {
178 // Access facility fields
179 $users_id = "";
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) {
225 $query = "select
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);
233 return false;
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 " .
250 ") VALUES ( " .
251 "'', " . // username
252 "'', " . // password
253 "0, " . // authorized
254 "'', " . // info
255 "NULL, " . // source
256 "'" . trim($facility->FacilityDirectorTitle) . "', " .
257 "'" . trim($facility->FacilityDirectorFirstName) . "', " .
258 "'" . trim($facility->FacilityDirectorLastName) . "', " .
259 "'', " .
260 "'', " .
261 "'', " . // federaldrugid
262 "'', " .
263 "'', " . // facility
264 "0, " . // see_auth
265 "1, " . // active
266 "'', " .
267 "'', " .
268 "'', " .
269 "'" . trim($facility->FacilityName) . "', " .
270 "'', " .
271 "'', " .
272 "'', " . // billname
273 "'', " .
274 "'', " .
275 "'" . trim($facility->FacilityAddress) . "', " .
276 "'', " .
277 "'" . trim($facility->FacilityCity) . "', " .
278 "'" . trim($facility->FacilityState) . "', " .
279 "'" . trim($facility->FacilityZip) . "', " .
280 "'', " .
281 "'', " .
282 "'', " .
283 "'', " .
284 "'', " .
285 "'" . trim($facility->FacilityPhone) . "', " .
286 "'', " .
287 "'', " .
288 "'', " .
289 "'', " .
290 "'', " .
291 "'ord_lab'" .
292 ")";
294 return sqlInsert($query);
297 function mapReportStatus($stat)
299 $return_status = $stat;
301 // if($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;
326 // if($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;
355 // if($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";
373 if ($stat=="<") {
374 $return_status = "low";
377 if ($stat==">") {
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);
395 } else {
396 return $phone;