Fix for exporting a large number of lists.
[openemr.git] / library / lab.inc
blobf2e48f4e53df51467cc0a538d102b1ab4b2fae79
1 <?php
3 /**
4  * lab.inc
5  *
6  * @package OpenEMR
7  * @link    http://www.open-emr.org
8  * @author  Sherwin Gaddis <sherwingaddis@gmail.com>
9  * @copyright Copyright (c) 2016-2017 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
12  */
15 /**
16  * @param $pid
17  * @param $encounter
18  * @return mixed
19  */
20 function fetchProcedureId($pid, $encounter)
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
32  */
33 function getProceduresInfo($oid, $encounter)
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'];
63     }
65     return $orders;
68 /**
69  * @param $pid
70  * @return mixed
71  */
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
84  */
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
94  */
95 function getProcedureProvider($prov_id)
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     $labs = sqlFetchArray($res);
103     return $labs;
107  * @param $prov_id
108  * @return array|null
109  */
110 function getLabProviders($prov_id)
113     $sql = "select fname, lname from users where authorized = 1 and active = 1 and username != '' and id = ?";
114     $rez = sqlQuery($sql, array($prov_id));
117     return $rez;
121 * This is going to be adjusted when there is more than one provider.
123 function getLabconfig()
125     $sql = "SELECT recv_app_id, recv_fac_id FROM procedure_providers ";
126     $res = sqlQuery($sql);
127     return $res;
130 function saveBarCode($bar, $pid, $order)
132     $sql = "INSERT INTO `requisition` (`id`, `req_id`, `pid`, `lab_id`) VALUES (NULL, ?, ?, ?)";
133     $inarr = array($bar,$pid,$order);
134     sqlStatement($sql, $inarr);
137 function getBarId($lab_id, $pid)
139     $sql = "SELECT req_id FROM requisition WHERE lab_id = ? AND pid = ?";
140     $bar = sqlQuery($sql, array($lab_id,$pid));
142     return $bar;
147  * @param <type> $facilityID
148  * @return <type> the result set, false if the input is malformed
149  */
150 function getFacilityInfo($facilityID)
152     // 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.
153     $facility = explode("_", $facilityID);
155     if (count($facility) > 1) {
156         $query = "SELECT title, fname, lname, street, city, state, zip, organization, phone FROM users WHERE id = ?";
158         $res = sqlStatement($query, array($facility[1]));
159         return sqlFetchArray($res);
160     }
162     return false;
165 function formatPhone($phone)
167     $phone = preg_replace("/[^0-9]/", "", $phone);
168     if (strlen($phone) == 7) {
169         return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
170     } elseif (strlen($phone) == 10) {
171         return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
172     } else {
173         return $phone;
174     }