PageRenderTime 25ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/interface/orders/lab_exchange_tools.php

https://github.com/md-tech/openemr
PHP | 353 lines | 206 code | 35 blank | 112 comment | 63 complexity | 9b36396903405208eb1151323091836d MD5 | raw file
  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.
  7. require_once("../globals.php");
  8. require_once("$srcdir/sql.inc");
  9. require_once("$srcdir/formdata.inc.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 ";
  14. $where = "";
  15. /*
  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);
  20. if ($res['pid']) {
  21. return $res['pid'];
  22. }
  23. else {
  24. $where = "";
  25. }
  26. }
  27. */
  28. // If empty $externalId or externalId no matched
  29. if (ereg_replace("[:space:]", "", $firstName) != "")
  30. $where .= "fname = '".add_escape_custom($firstName)."' " ;
  31. if (ereg_replace("[:space:]", "", $lastName) != "") {
  32. if ($where != "") $where .= "AND ";
  33. $where .= "lname = '".add_escape_custom($lastName)."' " ;
  34. }
  35. // if (ereg_replace("[:space:]", "", $middleName) != ""){
  36. // if ($where != "") $where .= "AND ";
  37. // $where .= "mname = '".add_escape_custom($middleName)."' " ;
  38. // }
  39. if (ereg_replace("[:space:]", "", $dob) != ""){
  40. if ($where != "") $where .= "AND ";
  41. $where .= "DOB = DATE_FORMAT('".add_escape_custom($dob)."', '%Y-%m-%d') " ;
  42. }
  43. if (ereg_replace("[:space:]", "", $gender) != "") {
  44. if ($gender =="F") $sex = "Female";
  45. if ($gender =="M") $sex = "Male";
  46. if(isset($sex))
  47. {
  48. if ($where != "") $where .= "AND ";
  49. $where .= "(sex = '".add_escape_custom($sex)."' OR sex = '" . add_escape_custom($gender) ."')" ;
  50. }
  51. }
  52. if (ereg_replace("[:space:]", "", $ssn) != ""){
  53. if ($where != "") $where .= "AND ";
  54. // Change to xxx-xx-xxxx format.
  55. $ss = substr($ssn,0,3)."-".substr($ssn,3,2)."-".substr($ssn,5);
  56. $where .= "(ss = '".add_escape_custom($ssn)."' OR ss = '".add_escape_custom($ss)."' OR ss = '')";
  57. }
  58. if ($where == "") {
  59. return false;
  60. }
  61. else {
  62. $res = sqlQuery($sql . $where);
  63. if ($res['pid']) {
  64. return $res['pid'];
  65. }
  66. else {
  67. return false;
  68. }
  69. }
  70. }
  71. /**
  72. * identify the lab ordering provider and return the userid.
  73. *
  74. * parameters are populated from the lab result
  75. *
  76. * @param <type> $id
  77. * @param <type> $lastName
  78. * @param <type> $firstName
  79. * @return <type> user.id
  80. *
  81. function lab_exchange_match_provider($id, $lastName, $firstName) {
  82. $sql = "SELECT user_id from laboratory_providers WHERE ";
  83. $where = "";
  84. if (ereg_replace("[:space:]", "", $lastName) != "")
  85. $where .= "provider_lname = '".add_escape_custom($lastName)."' " ;
  86. if (ereg_replace("[:space:]", "", $firstName) != "") {
  87. if ($where != "") $where .= "AND ";
  88. $where .= "provider_fname = '".add_escape_custom($firstName)."' " ;
  89. }
  90. if (ereg_replace("[:space:]", "", $id) != "") {
  91. if ($where != "") $where .= "AND ";
  92. $where .= "provider_id = '".add_escape_custom($id)."' " ;
  93. }
  94. if ($where == "") {
  95. return false;
  96. }
  97. else {
  98. $res = sqlQuery($sql . $where);
  99. if ($res['user_id']) {
  100. // echo "found id: " . $res['user_id'];
  101. return $res['user_id'];
  102. }
  103. else {
  104. // echo "found no id using " . $lastName .", " . $firstName .", " . $id;
  105. return false;
  106. }
  107. }
  108. }
  109. */
  110. /**
  111. * identify the lab ordering provider and return the userid.
  112. *
  113. * parameters are populated from the lab result
  114. *
  115. * @param <type> $id
  116. * @param <type> $lastName
  117. * @param <type> $firstName
  118. * @return <type> user.id if npi exists in users table; false if npi cannot be found
  119. */
  120. function lab_exchange_match_provider($npi)
  121. {
  122. $npi = trim($npi);
  123. if(!empty($npi))
  124. {
  125. $sql = "SELECT id from users WHERE npi = " . $npi;
  126. $res = sqlQuery($sql);
  127. }
  128. return isset($res['id']) ? $res['id'] : false;
  129. }
  130. /**
  131. * process the lab facility information
  132. *
  133. * @param <type> $facilities - potentially multiple facilities for performing lab info
  134. * @return <type> facilityID
  135. */
  136. function processFacility($facilities)
  137. {
  138. // Loop through the facility
  139. // There can be several facilities.
  140. // Also there is no good place to store a reference to users.id for facility info lookup,
  141. // so I'm concatenating the table id onto the lab id prior to the addition of a colon
  142. $facilityId = null;
  143. foreach ($facilities as $facility) {
  144. // Access facility fields
  145. $users_id = "";
  146. if(!$users_id = getLabFacility($facility))
  147. {
  148. $users_id = addNewLabFacility($facility);
  149. }
  150. $facilityId[] = $facility->FacilityID . "_" . $users_id; //=>procedure_result.facility
  151. }
  152. if (count($facilityId) > 0) {
  153. $str_facilityId = implode(":", $facilityId);
  154. }
  155. return $str_facilityId;
  156. }
  157. /**
  158. *
  159. * @param <type> $facility
  160. * @return <type> returns the user id for the lab facility record if it exists in the database, false otherwise.
  161. */
  162. function getLabFacility($facility)
  163. {
  164. $query = "select id from users where fname = '" . trim($facility->FacilityDirectorFirstName) . "' AND " .
  165. "lname = '" . trim($facility->FacilityDirectorLastName) . "' AND " .
  166. "street = '" . trim($facility->FacilityAddress) . "' AND " .
  167. "city = '" . trim($facility->FacilityCity) . "' AND " .
  168. "state = '" . trim($facility->FacilityState) . "' AND " .
  169. "zip = " . trim($facility->FacilityZip) . " AND " .
  170. "organization = '" . trim($facility->FacilityName) ."'";
  171. $res = sqlStatement($query);
  172. $result = sqlFetchArray($res);
  173. return isset($result['id']) ? $result['id'] : false;
  174. }
  175. /**
  176. *
  177. * @param <type> $facilityID
  178. * @return <type> the result set, false if the input is malformed
  179. */
  180. function getFacilityInfo($facilityID)
  181. {
  182. // 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.
  183. $facility = explode("_", $facilityID);
  184. if(count($facility) > 1)
  185. {
  186. $query = "select
  187. title,fname,lname,street,city,state,zip,organization,phone
  188. from users where id = " . $facility[1];
  189. $res = sqlStatement($query);
  190. return sqlFetchArray($res);
  191. }
  192. return false;
  193. }
  194. /**
  195. *
  196. * @param <type> $facility
  197. * @return <type> returns the id
  198. */
  199. function addNewLabFacility($facility)
  200. {
  201. $query = "INSERT INTO users ( " .
  202. "username, password, authorized, info, source, " .
  203. "title, fname, lname, mname, " .
  204. "federaltaxid, federaldrugid, upin, facility, see_auth, active, npi, taxonomy, " .
  205. "specialty, organization, valedictory, assistant, billname, email, url, " .
  206. "street, streetb, city, state, zip, " .
  207. "street2, streetb2, city2, state2, zip2," .
  208. "phone, phonew1, phonew2, phonecell, fax, notes, abook_type " .
  209. ") VALUES ( " .
  210. "'', " . // username
  211. "'', " . // password
  212. "0, " . // authorized
  213. "'', " . // info
  214. "NULL, " . // source
  215. "'" . trim($facility->FacilityDirectorTitle) . "', " .
  216. "'" . trim($facility->FacilityDirectorFirstName) . "', " .
  217. "'" . trim($facility->FacilityDirectorLastName) . "', " .
  218. "'', " .
  219. "'', " .
  220. "'', " . // federaldrugid
  221. "'', " .
  222. "'', " . // facility
  223. "0, " . // see_auth
  224. "1, " . // active
  225. "'', " .
  226. "'', " .
  227. "'', " .
  228. "'" . trim($facility->FacilityName) . "', " .
  229. "'', " .
  230. "'', " .
  231. "'', " . // billname
  232. "'', " .
  233. "'', " .
  234. "'" . trim($facility->FacilityAddress) . "', " .
  235. "'', " .
  236. "'" . trim($facility->FacilityCity) . "', " .
  237. "'" . trim($facility->FacilityState) . "', " .
  238. "'" . trim($facility->FacilityZip) . "', " .
  239. "'', " .
  240. "'', " .
  241. "'', " .
  242. "'', " .
  243. "'', " .
  244. "'" . trim($facility->FacilityPhone) . "', " .
  245. "'', " .
  246. "'', " .
  247. "'', " .
  248. "'', " .
  249. "'', " .
  250. "'ord_lab'" .
  251. ")";
  252. return sqlInsert($query);
  253. }
  254. function mapReportStatus($stat) {
  255. $return_status = $stat;
  256. // if($stat == "")
  257. // $return_status = "unknown";
  258. if($stat=="F" || $stat=="f")
  259. $return_status = "final";
  260. if($stat=="P" || $stat=="p")
  261. $return_status = "prelim";
  262. if($stat=="X" || $stat=="x")
  263. $return_status = "cancel";
  264. if($stat=="C" || $stat=="c")
  265. $return_status = "correct";
  266. return $return_status;
  267. }
  268. function mapResultStatus($stat) {
  269. $return_status = $stat;
  270. // if($stat == "")
  271. // $return_status = "unknown";
  272. if($stat=="F" || $stat=="f")
  273. $return_status = "final";
  274. if($stat=="P" || $stat=="p")
  275. $return_status = "prelim";
  276. if($stat=="X" || $stat=="x")
  277. $return_status = "cancel";
  278. if($stat=="C" || $stat=="c")
  279. $return_status = "correct";
  280. if($stat=="I" || $stat=="i")
  281. $return_status = "incomplete";
  282. return $return_status;
  283. }
  284. function mapAbnormalStatus($stat) {
  285. $return_status = $stat;
  286. // if($stat == "")
  287. // $return_status = "unknown";
  288. if($stat=="L" || $stat=="l")
  289. $return_status = "low";
  290. if($stat=="H" || $stat=="h")
  291. $return_status = "high";
  292. if($stat=="LL" || $stat=="ll")
  293. $return_status = "low";
  294. if($stat=="HH" || $stat=="hh")
  295. $return_status = "high";
  296. if($stat=="<")
  297. $return_status = "low";
  298. if($stat==">")
  299. $return_status = "high";
  300. if($stat=="A" || $stat=="a")
  301. $return_status = "yes";
  302. return $return_status;
  303. }
  304. function formatPhone($phone)
  305. {
  306. $phone = preg_replace("/[^0-9]/", "", $phone);
  307. if(strlen($phone) == 7)
  308. return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
  309. elseif(strlen($phone) == 10)
  310. return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
  311. else
  312. return $phone;
  313. }
  314. ?>