fix: Uninitialised zip and missing file size error in Native Data Loads (#7081)
[openemr.git] / library / lab.inc.php
blob290c7dd4cd0219c077c99c0518d3fc0cd6e621a4
1 <?php
3 /**
4 * lab.inc
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Sherwin Gaddis <sherwingaddis@gmail.com>
9 * @copyright Copyright (c) 2016-2023 Sherwin Gaddis <sherwingaddis@gmail.com>
10 * @copyright Copyright (c) 2010 OpenEMR Support LLC
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 /**
16 * @param $pid
17 * @param $encounter
18 * @return mixed
20 function fetchProcedureId($pid, $encounter): mixed
22 $sql = "SELECT procedure_order_id FROM procedure_order WHERE patient_id = ? AND encounter_id = ?";
23 $res = sqlQuery($sql, array($pid,$encounter));
25 return $res['procedure_order_id'];
28 /**
29 * @param $oid
30 * @param $encounter
31 * @return array
33 function getProceduresInfo($oid, $encounter): array
36 $sql = "SELECT pc.procedure_order_id, pc.procedure_order_seq, pc.procedure_code, pc.procedure_name,
37 pc.diagnoses, po.provider_id, po.date_collected,po.lab_id, po.clinical_hx, po.date_ordered, po.patient_instructions, po.specimen_type,
38 po.specimen_location, po.specimen_volume
39 FROM procedure_order_code AS pc
40 JOIN procedure_order AS po ON pc.procedure_order_id
41 AND po.procedure_order_id
42 WHERE pc.procedure_order_id = ?
43 AND po.encounter_id = ?
44 AND po.procedure_order_id = ?";
46 $listOrders = sqlStatement($sql, array($oid,$encounter,$oid));
47 $orders = array();
48 while ($rows = sqlFetchArray($listOrders)) {
49 $orders[] = $rows['procedure_order_id'];
50 $orders[] = $rows['procedure_order_seq'];
51 $orders[] = $rows['procedure_code'];
52 $orders[] = $rows['procedure_name'];
53 $orders[] = $rows['diagnoses'];
54 $orders[] = $rows['provider_id'];
55 $orders[] = $rows['date_collected'];
56 $orders[] = $rows['lab_id']; //procedure_order.ppid
57 $orders[] = $rows['clinical_hx'];
58 $orders[] = $rows['date_ordered'];
59 $orders[] = $rows['patient_instructions'];
60 $orders[] = $rows['specimen_type'];
61 $orders[] = $rows['specimen_location'];
62 $orders[] = $rows['specimen_volume'];
65 return $orders;
68 /**
69 * @param $pid
70 * @return mixed
73 function getSelfPay($pid)
75 $sql = "SELECT `subscriber_relationship` FROM `insurance_data` WHERE pid = ?";
76 $res = sqlQuery($sql, array($pid));
78 return $res['subscriber_relationship'];
81 /**
82 * @param $prov_id
83 * @return array
85 function getNPI($prov_id)
87 $sql = "SELECT `npi`, `upin` FROM `users` WHERE `id` = ?";
88 $res = sqlQuery($sql, array($prov_id));
89 return array($res['npi'], $res['upin']);
92 /**
93 * @return array
95 function getProcedureProvider($prov_id): array|bool
97 $sql = "SELECT i.organization, i.street, i.city, i.state, i.zip, i.fax, i.phone, pi.lab_director " .
98 "FROM users AS i, procedure_providers AS pi WHERE pi.ppid = ? AND pi.lab_director = i.id ";
100 $res = sqlStatement($sql, array($prov_id));
101 return sqlFetchArray($res) ?? [];
105 * @param $prov_id
106 * @return array|null
108 function getLabProviders($prov_id): ?array
111 $sql = "select fname, lname from users where authorized = 1 and active = 1 and username != '' and id = ?";
112 $rez = sqlQuery($sql, array($prov_id));
115 return $rez;
119 * This is going to be adjusted when there is more than one provider.
121 function getLabconfig(): bool|array|null
123 $sql = "SELECT recv_app_id, recv_fac_id FROM procedure_providers ";
124 $res = sqlQuery($sql);
125 return $res;
128 function saveBarCode($bar, $pid, $order): void
130 $sql = "INSERT INTO `requisition` (`id`, `req_id`, `pid`, `lab_id`) VALUES (NULL, ?, ?, ?)";
131 $inarr = array($bar,$pid,$order);
132 sqlStatement($sql, $inarr);
135 function getBarId($lab_id, $pid): bool|array|string|null
137 //housekeeping needs to be done. -SG 2023-05-03
138 //lab orders can be deleted, and it does not reflect in the requisition table.
139 //the requisition needs to be removed if a lab order is deleted
140 $checkForLabOrder = "SELECT * FROM `procedure_order` WHERE `procedure_order_id` = ? AND `patient_id` = ?";
141 $sql = "SELECT `req_id` FROM `requisition` WHERE `lab_id` = ? AND `pid` = ?";
142 $bar = sqlQuery($sql, array($lab_id,$pid));
143 $isOrder = sqlQuery($checkForLabOrder, array($lab_id,$pid));
144 if (empty($isOrder['procedure_order_id'])) {
145 sqlStatement("DELETE FROM `requisition` WHERE `lab_id` = ? AND `pid` = ?", array($lab_id,$pid));
146 return '';
148 return $bar;
153 * @param <type> $facilityID
154 * @return <type> the result set, false if the input is malformed
156 function getFacilityInfo($facilityID): bool|array
158 // 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.
159 $facility = explode("_", $facilityID);
161 if (count($facility) > 1) {
162 $query = "SELECT `title`, `fname`, `lname`, `street`, `city`, `state`, `zip`, `organization`, `phone` FROM `users` WHERE `id` = ?";
164 $res = sqlStatement($query, array($facility[1]));
165 return sqlFetchArray($res);
168 return false;
171 function formatPhone($phone): array|string|null
173 $phone = preg_replace("/[^0-9]/", "", $phone);
174 if (strlen($phone) == 7) {
175 return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
176 } elseif (strlen($phone) == 10) {
177 return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
178 } else {
179 return $phone;