fix for bug in doctrine when have redis extension on, but not using localhost (#1726)
[openemr.git] / custom / qrda_functions.php
blob53d6903fe48796b3979fdc834a6da03462d92918
1 <?php
2 /**
4 * QRDA Functions
6 * Copyright (C) 2015 Ensoftek, Inc
8 * LICENSE: This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
19 * @package OpenEMR
20 * @author Ensoftek
21 * @link http://www.open-emr.org
24 use OpenEMR\Services\FacilityService;
26 $facilityService = new FacilityService();
28 // Functions for QRDA Category I (or) III 2014 XML format.
30 //function for Stratification data getting for NQF# 0024 Rule
31 function getQRDAStratumInfo($patArr, $begin_date)
33 $startumArr = array();
34 if (count($patArr) > 0) {
35 //Age Between 3 and 11
36 $stratumOneQry = "SELECT FLOOR( DATEDIFF( '".add_escape_custom($begin_date)."' , DOB ) /365 ) as pt_age FROM patient_data WHERE pid IN (".add_escape_custom(implode(",", $patArr)).") HAVING (pt_age BETWEEN 1 AND 10) ";
37 $stratumOneRes = sqlStatement($stratumOneQry);
38 $stratumOneRows = sqlNumRows($stratumOneRes);
40 //Age Between 12 and 17
41 $stratumTwoQry = "SELECT FLOOR( DATEDIFF( '".add_escape_custom($begin_date)."' , DOB ) /365 ) as pt_age FROM patient_data WHERE pid IN (".add_escape_custom(implode(",", $patArr)).") HAVING (pt_age BETWEEN 11 AND 16) ";
42 $stratumTwoRes = sqlStatement($stratumTwoQry);
43 $stratumTwoRows = sqlNumRows($stratumTwoRes);
44 $startumArr[1] = $stratumOneRows;
45 $startumArr[2] = $stratumTwoRows;
46 } else {
47 $startumArr[1] = 0;
48 $startumArr[2] = 0;
51 return $startumArr;
54 //function for getting Payer(Insurance Type) Information for Export QRDA
55 function getQRDAPayerInfo($patArr)
57 $payerCheckArr = array();
58 $payerCheckArr['Medicare'] = 0;
59 $payerCheckArr['Medicaid'] = 0;
60 $payerCheckArr['Private Health Insurance'] = 0;
61 $payerCheckArr['Other'] = 0;
62 if (count($patArr) > 0) {
63 $insQry = "SELECT insd.*, ic.ins_type_code FROM (SELECT pid, provider FROM insurance_data WHERE type = 'primary' ORDER BY id DESC) insd ".
64 "INNER JOIN insurance_companies ic ON insd.provider = ic.id ".
65 "WHERE insd.pid IN (".add_escape_custom(implode(",", $patArr)).")";
66 $insRes = sqlStatement($insQry);
67 while ($insRow = sqlFetchArray($insRes)) {
68 if ($insRow['ins_type_code'] == 8) {//Self Pay (Private Insurance)
69 $payerCheckArr['Private Health Insurance']++;
70 } else if ($insRow['ins_type_code'] == 2) {//Medicare
71 $payerCheckArr['Medicare']++;
72 } else if ($insRow['ins_type_code'] == 3) {//Self Pay (Private Insurance)
73 $payerCheckArr['Medicaid']++;
74 } else {//Other
75 $payerCheckArr['Other']++;
80 return $payerCheckArr;
83 //function for getting Race, Ethnicity and Gender Information for Export QRDA
84 function getQRDAPatientNeedInfo($patArr)
86 //Defining Array elements
87 //Gender
88 $genderArr = array();
89 $genderArr['Male'] = 0;
90 $genderArr['Female'] = 0;
91 $genderArr['Unknown'] = 0;
92 //Race
93 $raceArr = array();
94 $raceArr['American Indian or Alaska Native'] = 0;
95 $raceArr['Asian'] = 0;
96 $raceArr['Black or African American'] = 0;
97 $raceArr['Native Hawaiian or Other Pacific Islander'] = 0;
98 $raceArr['White'] = 0;
99 $raceArr['Other'] = 0;
100 //Ethnicity
101 $ethincityArr = array();
102 $ethincityArr['Not Hispanic or Latino'] = 0;
103 $ethincityArr['Hispanic or Latino'] = 0;
105 $mainArr = array();
106 if (count($patArr) > 0) {
107 $patRes = sqlStatement("SELECT pid, sex, race, ethnicity FROM patient_data WHERE pid IN (".add_escape_custom(implode(",", $patArr)).")");
108 while ($patRow = sqlFetchArray($patRes)) {
109 //Gender Collection
110 if ($patRow['sex'] == "Male") {
111 $genderArr['Male']++;
112 } else if ($patRow['sex'] == "Female") {
113 $genderArr['Female']++;
114 } else {
115 $genderArr['Unknown']++;
118 //Race Section
119 if ($patRow['race'] == "amer_ind_or_alaska_native") {
120 $raceArr['American Indian or Alaska Native']++;
121 } else if ($patRow['race'] == "Asian") {
122 $raceArr['Asian']++;
123 } else if ($patRow['race'] == "black_or_afri_amer") {
124 $raceArr['Black or African American']++;
125 } else if ($patRow['race'] == "native_hawai_or_pac_island") {
126 $raceArr['Native Hawaiian or Other Pacific Islander']++;
127 } else if ($patRow['race'] == "white") {
128 $raceArr['White']++;
129 } else if ($patRow['race'] == "Asian_Pacific_Island") {
130 $raceArr['Other']++;
131 } else if ($patRow['race'] == "Black_not_of_Hispan") {
132 $raceArr['Other']++;
133 } else if ($patRow['race'] == "Hispanic") {
134 $raceArr['Other']++;
135 } else if ($patRow['race'] == "White_not_of_Hispan") {
136 $raceArr['Other']++;
137 } else {
138 $raceArr['Other']++;
141 if ($patRow['ethnicity'] == "hisp_or_latin") {
142 $ethincityArr['Hispanic or Latino']++;
143 } else if ($patRow['ethnicity'] == "not_hisp_or_latin") {
144 $ethincityArr['Not Hispanic or Latino']++;
149 $mainArr['gender'] = $genderArr;
150 $mainArr['race'] = $raceArr;
151 $mainArr['ethnicity'] = $ethincityArr;
153 return $mainArr;
156 function payerPatient($patient_id)
158 $payer = 'Other';
159 $insQry = "SELECT insd.*, ic.ins_type_code FROM (SELECT pid, provider FROM insurance_data WHERE type = 'primary' ORDER BY id DESC) insd ".
160 "INNER JOIN insurance_companies ic ON insd.provider = ic.id ".
161 "WHERE insd.pid = ?";
162 $insRes = sqlStatement($insQry, array($patient_id));
163 while ($insRow = sqlFetchArray($insRes)) {
164 if ($insRow['ins_type_code'] == 8) {//Self Pay (Private Insurance)
165 $payer = 'Private Health Insurance';
166 } else if ($insRow['ins_type_code'] == 2) {//Medicare
167 $payer = 'Medicare';
168 } else if ($insRow['ins_type_code'] == 3) {//Self Pay (Private Insurance)
169 $payer = 'Medicaid';
170 } else {//Other
171 $payer = 'Other';
175 return $payer;
178 function allEncPat($patient_id, $from_date, $to_date)
180 $encArr = array();
181 $patQry = "SELECT fe.encounter, fe.date,fe.pc_catid,opc.pc_catname FROM form_encounter fe inner join openemr_postcalendar_categories opc on opc.pc_catid = fe.pc_catid WHERE fe.pid = ? AND (DATE(fe.date) BETWEEN ? AND ?)";
182 $patRes = sqlStatement($patQry, array($patient_id, $from_date, $to_date));
183 while ($patRow = sqlFetchArray($patRes)) {
184 $encArr[] = $patRow;
187 return $encArr;
190 function allListsPat($type, $patient_id, $from_date, $to_date)
192 $diagArr = array();
193 $diagQry = "SELECT * FROM lists WHERE TYPE = ? AND pid = ? AND (DATE(date) BETWEEN ? AND ?)";
194 $diagRes = sqlStatement($diagQry, array($type, $patient_id, $from_date, $to_date));
195 while ($diagRow = sqlFetchArray($diagRes)) {
196 $diagArr[] = $diagRow;
199 return $diagArr;
202 function allOrderMedsPat($patient_id, $from_date, $to_date)
204 $medArr = array();
205 $medQry = "SELECT * FROM prescriptions where patient_id = ? AND active = 0 AND (DATE(date_added) BETWEEN ? AND ?)";
206 $medRes = sqlStatement($medQry, array($patient_id, $from_date, $to_date));
207 while ($medRow = sqlFetchArray($medRes)) {
208 $medArr[] = $medRow;
211 return $medArr;
214 function allActiveMedsPat($patient_id, $from_date, $to_date)
216 $medArr = array();
217 $medQry = "SELECT * FROM prescriptions where patient_id = ? AND active = 1 AND (DATE(date_added) BETWEEN ? AND ?)";
218 $medRes = sqlStatement($medQry, array($patient_id, $from_date, $to_date));
219 while ($medRow = sqlFetchArray($medRes)) {
220 $medArr[] = $medRow;
223 return $medArr;
226 function allProcPat($proc_type = "Procedure", $patient_id, $from_date, $to_date)
228 $procArr = array();
229 $procQry = "SELECT poc.procedure_code, poc.procedure_name, po.date_ordered, fe.encounter,fe.date FROM form_encounter fe ".
230 "INNER JOIN forms f ON f.encounter = fe.encounter AND f.deleted != 1 AND f.formdir = 'procedure_order' ".
231 "INNER JOIN procedure_order po ON po.encounter_id = f.encounter ".
232 "INNER JOIN procedure_order_code poc ON poc.procedure_order_id = po.procedure_order_id ".
233 "WHERE poc.procedure_order_title = ? AND po.patient_id = ? ".
234 "AND (po.date_ordered BETWEEN ? AND ?)";
235 $procRes = sqlStatement($procQry, array($proc_type, $patient_id, $from_date, $to_date));
236 while ($procRow = sqlFetchArray($procRes)) {
237 $procArr[] = $procRow;
240 return $procArr;
243 function allVitalsPat($patient_id, $from_date, $to_date)
245 $vitArr = array();
246 $vitQry = "SELECT fe.encounter, v.bps, v.date,v.bpd,v.BMI as bmi FROM form_encounter fe ".
247 "INNER JOIN forms f ON f.encounter = fe.encounter AND f.deleted != 1 AND f.formdir = 'vitals' ".
248 "INNER JOIN form_vitals v ON v.id = f.form_id ".
249 "WHERE v.pid = ? ".
250 "AND (v.date BETWEEN ? AND ?)";
251 $vitRes = sqlStatement($vitQry, array($patient_id, $from_date, $to_date));
252 while ($vitRow = sqlFetchArray($vitRes)) {
253 $vitArr[] = $vitRow;
256 return $vitArr;
259 function allImmuPat($patient_id, $from_date, $to_date)
261 $immArr = array();
262 $immQry = "SELECT * FROM immunizations ".
263 "WHERE patient_id = ? ".
264 "AND (administered_date BETWEEN ? AND ?)";
265 $immRes = sqlStatement($immQry, array($patient_id, $from_date, $to_date));
266 while ($immRow = sqlFetchArray($immRes)) {
267 $immArr[] = $immRow;
270 return $immArr;
272 function getPatData($patient_id)
274 $patientRow = sqlQuery("SELECT * FROM patient_data WHERE pid= ?", array($patient_id));
275 return $patientRow;
278 function getUsrDataCheck($provider_id)
280 $userRow = array();
281 if ($provider_id != "") {
282 $userRow = sqlQuery("SELECT facility, facility_id, federaltaxid, npi, phone,fname, lname FROM users WHERE id= ?", array($provider_id));
285 return $userRow;
288 function getFacilDataChk($facility_id)
290 global $facilityService;
291 return $facilityService->getById($facility_id);
294 function patientQRDAHistory($patient_id)
296 $patientHistRow = sqlQuery("SELECT tobacco, date FROM history_data WHERE pid= ? ORDER BY id DESC LIMIT 1", array($patient_id));
297 return $patientHistRow;