update functionality. (#1427)
[openemr.git] / phpfhir / classes / src / fetch_live.class.php
blob6188e7a8df7191e514d8d65d3be91194b9fb2bca
1 <?php
2 /**
3 * FetchLiveData class
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Jerry Padgett <sjpadgett@gmail.com>
8 * @copyright Copyright (c) 2018 Jerry Padgett <sjpadgett@gmail.com>
9 * @license https://www.gnu.org/licenses/agpl-3.0.en.html GNU Affero General Public License 3
12 namespace oeFHIR;
14 require_once(dirname(__file__) . "/../../../custom/code_types.inc.php");
16 class FetchLiveData
18 /**
19 * Function oeQuery
20 * All DB Transactions take place
22 * @param String $sql SQL Query Statement
23 * @param array $params SQL Parameters
24 * @param boolean $log Logging True / False
25 * @param boolean $error Error Display True / False
26 * @return records Array
28 public function oeQuery($sql, $params = [], $log = false, $error = true)
30 $return = false;
31 $result = false;
33 try {
34 $return = sqlStatement($sql, $params);
35 $result = true;
36 } catch (Exception $e) {
37 if ($error) {
41 return $return;
45 * Fetch the current demographics data of a patient from patient_data table
47 * @param pid Integer Patient ID
48 * @return records Array current patient data
50 public function getDemographicsCurrent($pid)
53 $query = "SELECT *
54 FROM patient_data
55 WHERE pid = ?";
56 $result = $this->oeQuery($query, array($pid));
57 $records = array();
58 foreach ($result as $row) {
59 $records[] = $row;
62 return $records[0];
66 * Fetch the current Problems of a patient from lists table
68 * @param pid Integer patient id
69 * @return records Array list of problems
71 public function getProblems($pid)
74 $query = "SELECT *
75 FROM lists
76 WHERE pid = ? AND TYPE = 'medical_problem'";
77 $result = $this->oeQuery($query, array($pid));
78 $records = array();
79 foreach ($result as $row) {
80 $records[] = $row;
83 return $records;
87 * Fetch the current Allergies of a patient from lists table
89 * @param pid Integer patient id
90 * @return records Array list of allergies
92 public function getAllergies($pid)
95 $query = "SELECT *
96 FROM lists
97 WHERE pid = ? AND TYPE = 'allergy'";
98 $result = $this->oeQuery($query, array($pid));
99 $records = array();
100 foreach ($result as $row) {
101 $records[] = $row;
104 return $records;
108 * Fetch the current Medications of a patient from prescriptions table
110 * @param pid Integer patient id
111 * @return records Array list of medications
113 public function getMedications($pid)
116 $query = "SELECT *
117 FROM prescriptions
118 WHERE patient_id = ?";
119 $result = $this->oeQuery($query, array($pid));
120 $records = array();
121 foreach ($result as $row) {
122 $records[] = $row;
125 return $records;
129 * Fetch the current Immunizations of a patient from immunizations table
131 * @param pid Integer patient id
132 * @return records Array list of immunizations
134 public function getImmunizations($pid)
137 $query = "SELECT im.*, cd.code_text, DATE(administered_date) AS administered_date,
138 DATE_FORMAT(administered_date,'%m/%d/%Y') AS administered_formatted, lo.title AS route_of_administration,
139 u.title, u.fname, u.mname, u.lname, u.npi, u.street, u.streetb, u.city, u.state, u.zip, u.phonew1,
140 f.name, f.phone, lo.notes AS route_code
141 FROM immunizations AS im
142 LEFT JOIN codes AS cd ON cd.code = im.cvx_code
143 JOIN code_types AS ctype ON ctype.ct_key = 'CVX' AND ctype.ct_id=cd.code_type
144 LEFT JOIN list_options AS lo ON lo.list_id = 'drug_route' AND lo.option_id = im.route
145 LEFT JOIN users AS u ON u.id = im.administered_by_id
146 LEFT JOIN facility AS f ON f.id = u.facility_id
147 WHERE im.patient_id=?";
148 $result = $this->oeQuery($query, array($pid));
149 $records = array();
150 foreach ($result as $row) {
151 $records[] = $row;
154 return $records;
158 * Fetch the currect Lab Results of a patient
160 * @param pid Integer patient id
161 * @return records Array list of lab results
163 public function getLabResults($pid)
166 $query = "SELECT CONCAT_WS('',po.procedure_order_id,poc.`procedure_order_seq`) AS tcode,
167 prs.result AS result_value,
168 prs.units, prs.range,
169 poc.procedure_name AS order_title,
170 prs.result_code AS result_code,
171 prs.result_text AS result_desc,
172 po.date_ordered,
173 prs.date AS result_time,
174 prs.abnormal AS abnormal_flag,
175 prs.procedure_result_id AS result_id
176 FROM procedure_order AS po
177 JOIN procedure_order_code AS poc ON poc.`procedure_order_id`=po.`procedure_order_id`
178 JOIN procedure_report AS pr ON pr.procedure_order_id = po.procedure_order_id
179 AND pr.`procedure_order_seq`=poc.`procedure_order_seq`
180 JOIN procedure_result AS prs ON prs.procedure_report_id = pr.procedure_report_id
181 WHERE po.patient_id = ? AND prs.result NOT IN ('DNR','TNP')";
182 $result = $this->oeQuery($query, array($pid));
183 $records = array();
184 foreach ($result as $row) {
185 $records[] = $row;
188 return $records;
191 public function getUser($id)
194 $query = "SELECT *
195 FROM users
196 WHERE id = ?";
197 $result = $this->oeQuery($query, array($id));
198 $records = array();
199 foreach ($result as $row) {
200 $records[] = $row;
203 return $records[0];
207 * Fetch the current Vitals of a patient from form_vitals table
209 * @param pid Integer patient id
210 * @return records Array list of vitals
212 public function getVitals($pid)
215 $query = "SELECT *
216 FROM form_vitals
217 WHERE pid = ? AND activity=?";
218 $result = $this->oeQuery($query, array($pid, 1));
219 $records = array();
220 foreach ($result as $row) {
221 $records[] = $row;
224 return $records;
228 * Fetch the social history of a patient from history_data table
230 * @param pid Integer patient id
231 * @return records Array history data
233 public function getSocialHistory($pid)
236 $query = "SELECT *
237 FROM history_data
238 WHERE pid=?
239 ORDER BY id DESC LIMIT 1";
240 $result = $this->oeQuery($query, array($pid));
241 $records = array();
242 foreach ($result as $row) {
243 $records[] = $row;
246 return $records;
250 * Fetch the list of encounter Id's of a patient from form_encounter table
252 * @param pid Integer patient id
253 * @return records Array encounter data
255 public function getEncounterIdList($pid)
258 $query = "SELECT form_encounter.encounter,form_encounter.reason
259 FROM form_encounter
260 WHERE pid = ?";
262 $result = $this->oeQuery($query, array($pid));
263 $records = array();
264 foreach ($result as $row) {
265 $records[] = $row;
268 return $records;
272 * Fetch the encounter data of a patient from form_encounter table
274 * @param pid Integer patient id
275 * @return records Array encounter data
277 public function getEncounterData($pid, $eid = '')
280 $query = "SELECT form_encounter.*,u.lname AS provider_name
281 FROM form_encounter
282 LEFT JOIN users AS u
283 ON form_encounter.provider_id=u.id
284 WHERE pid = ?";
285 $data = array($pid);
286 if ($eid) {
287 $query .= " && encounter=?";
288 $data = array($pid, $eid);
290 $result = $this->oeQuery($query, $data);
291 $records = array();
292 foreach ($result as $row) {
293 $records[] = $row;
295 if ($eid) {
296 $records = $records[0];
298 return $records;
302 * Fetch the billing data of a patient from billing table
304 * @param pid Integer patient id
305 * @return records Array billing data
307 public function getProcedure($pid)
310 $query = "SELECT *
311 FROM billing
312 WHERE pid=? AND code_type=?";
313 $result = $this->oeQuery($query, array($pid, 'CPT4'));
314 $records = array();
315 foreach ($result as $row) {
316 $records[] = $row;
319 return $records;
323 * Fetch the current Care Plan of a patient from form_care_plan table
325 * @param pid Integer patient id
326 * @return records Array list of Care Plans
328 public function getCarePlan($pid)
331 $query = "SELECT *
332 FROM form_care_plan
333 WHERE pid = ? AND activity=?";
334 $result = $this->oeQuery($query, array($pid, 1));
335 $records = array();
336 foreach ($result as $row) {
337 $records[] = $row;
340 return $records;
344 * Fetch the current Functional Cognitive Status of a patient from form_functional_cognitive_status table
346 * @param pid Integer patient id
347 * @return records Array list of Functional Cognitive Status
349 public function getFunctionalCognitiveStatus($pid)
352 $query = "SELECT *
353 FROM form_functional_cognitive_status
354 WHERE pid = ? AND activity=?";
355 $result = $this->oeQuery($query, array($pid, 1));
356 $records = array();
357 foreach ($result as $row) {
358 $records[] = $row;
361 return $records;