MU2 for items e2 and f1.
[openemr.git] / interface / modules / zend_modules / module / Immunization / src / Immunization / Model / ImmunizationTable.php
blobcb70b3226868595622108d4378b42e60c77ccccd
1 <?php
2 /* +-----------------------------------------------------------------------------+
3 * OpenEMR - Open Source Electronic Medical Record
4 * Copyright (C) 2014 Z&H Consultancy Services Private Limited <sam@zhservices.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * @author Bindia Nandakumar <bindia@zhservices.com>
19 * +------------------------------------------------------------------------------+
21 namespace Immunization\Model;
23 use Zend\Db\TableGateway\TableGateway;
24 use Zend\Db\TableGateway\AbstractTableGateway;
25 use Zend\Db\Adapter\Adapter;
26 use Zend\Db\ResultSet\ResultSet;
27 use Zend\Db\Sql\Select;
28 use Zend\InputFilter\Factory as InputFactory;
29 use Zend\InputFilter\InputFilter;
30 use Zend\InputFilter\InputFilterAwareInterface;
31 use Zend\InputFilter\InputFilterInterface;
33 use \Application\Model\ApplicationTable;
34 class ImmunizationTable extends AbstractTableGateway
36 public $tableGateway;
37 protected $applicationTable;
39 public function __construct(TableGateway $tableGateway)
41 $this->tableGateway = $tableGateway;
42 $adapter = \Zend\Db\TableGateway\Feature\GlobalAdapterFeature::getStaticAdapter();
43 $this->adapter = $adapter;
44 $this->resultSetPrototype = new ResultSet();
45 $this->applicationTable = new ApplicationTable;
48 /**
49 * function codeslist()
50 * Codes List
52 public function codeslist()
54 $sql = "SELECT id, CONCAT('CVX:',CODE) AS NAME FROM codes LEFT JOIN code_types ct ON codes.code_type = ct.ct_id WHERE ct.ct_key='CVX' ORDER BY NAME";
55 $result = $this->applicationTable->zQuery($sql);
56 return $result;
59 /**
60 * function immunized patient details
61 * @param type $form_data
62 * @return type
64 public function immunizedPatientDetails($form_data,$getCount=null)
66 $query_data = array();
67 $query_codes = $form_data['query_codes'];
68 $from_date = $form_data['form_from_date'];
69 $to_date = $form_data['form_to_date'];
70 $form_get_hl7 = $form_data['form_get_hl7'];
71 $query_pids = $form_data['query_pids'];
72 $fdate = '';
73 $todate = '';
74 $query =
75 "SELECT " .
76 "i.patient_id AS patientid, " .
77 "p.language, ".
78 "i.cvx_code , " ;
79 if ($form_get_hl7==='true') {
80 $query .=
81 "DATE_FORMAT(p.DOB,'%Y%m%d') AS DOB, ".
82 "p.pubpid, ".
83 "CONCAT(IF(p.street IS NULL,'',p.street), '^^', IF(p.city IS NULL,'',p.city), '^', IF(p.state IS NULL,'',p.state), '^', IF(p.postal_code IS NULL,'',p.postal_code) ,'^', IF(l.option_id IS NULL,'',l.notes)) AS address, ".
84 "p.country_code, ".
85 "p.phone_home, ".
86 "p.phone_biz, ".
87 "p.status, ".
88 "p.sex, ".
89 "p.ethnoracial, ".
90 "p.race, ".
91 "p.ethnicity, ".
92 "p.guardiansname, ".
93 "p.guardianrelationship, ".
94 "p.guardiansex, ".
95 "p.guardianphone, ".
96 "p.guardianworkphone, ".
97 "p.email, ".
98 "p.publicity_code, ".
99 "p.imm_reg_status, ".
100 "p.protect_indicator, ".
101 "DATE_FORMAT(p.prot_indi_effdate,'%Y%m%d') AS protection_effective_date, ".
102 "DATE_FORMAT(p.publ_code_eff_date,'%Y%m%d') AS publicity_code_effective_date, ".
103 "DATE_FORMAT(p.imm_reg_stat_effdate,'%Y%m%d') AS immunization_registry_status_effective_date, ".
104 "CONCAT(IF(p.guardianaddress IS NULL,'',p.guardianaddress), '^', '','^',IF(p.guardiancity IS NULL,'',p.guardiancity), '^', IF(p.guardianstate IS NULL,'',p.guardianstate), '^', IF(p.guardianpostalcode IS NULL,'',p.guardianpostalcode) ,'^', IF(l.option_id IS NULL,'',l.notes),'^','L', '^', '', '^', '', '^', '') AS guardian_address, ".
105 "p.ss, ".
106 "c.code_text, ".
107 "c.code, ".
108 "c.code_type, ".
109 "DATE_FORMAT(i.vis_date,'%Y%m%d') AS immunizationdate, ".
110 "DATE_FORMAT(i.administered_date,'%Y%m%d') AS administered_date, ".
111 "i.lot_number AS lot_number, ".
112 "i.manufacturer AS manufacturer, ".
113 "i.administration_site, ".
114 "CONCAT(IF(p.lname IS NULL,'',p.lname), '^', p.fname , '^', IF(p.mname IS NULL,'',p.mname)) AS patientname, ".
115 "f.id,".
116 "f.name AS fac_name, ".
117 "f.facility_code,".
118 "i.administered_by_id,".
119 "i.note,".
120 "CONCAT (IF(u.npi IS NULL,'',u.npi),'^',u.lname,'^',u.fname,'^',IF(u.mname IS NULL,'',u.mname)) as primary_care_provider_details, ".
121 "l.notes AS country_code, ".
122 "l1.notes AS route_code, ".
123 "i.route, ".
124 "CONCAT(u.lname,'^',u.fname,'^',IF(u.mname IS NULL,'',u.mname),'^','','^',IF(u.title IS NULL,'',u.title),'^','','^','','^','TX','^','L','^','','^','','^','','^','','^','') AS providername, ".
125 "CONCAT(f.id,'^',IF(f.street IS NULL,'',SUBSTRING(f.`street`,1,20)),'^',IF(f.`city` IS NULL,'',f.`city`),'^',IF(f.`state` IS NULL,'',f.`state`),'^',IF(f.`postal_code` IS NULL,'',f.`postal_code`),'^',IF(f.`country_code` IS NULL,'',f.`country_code`)) AS facility_address, ".
126 "u.id AS users_id, ".
127 "i.created_by, ".
128 "i.ordering_provider, ".
129 "CONCAT(u1.lname,'^',u1.fname,'^',IF(u1.mname IS NULL,'',u1.mname)) AS entered_by_name, ".
130 "CONCAT(u2.lname,'^',u2.fname,'^',IF(u2.mname IS NULL,'',u2.mname)) AS ordering_provider_name, ".
131 "i.administered_by_id,i.note,i.information_source,DATE_FORMAT(i.expiration_date,'%Y%m%d') AS expiration_date,i.refusal_reason,i.completion_status,";
132 } else {
133 $query .= "CONCAT(IF(p.fname IS NULL,'',p.fname),' ',IF(p.mname IS NULL,'',p.mname),' ',IF(p.lname IS NULL,'',p.lname)) AS patientname, ".
134 "i.vis_date AS immunizationdate, " ;
136 $query .=
137 "i.id AS immunizationid, c.code_text_short AS immunizationtitle, c.code_text,i.amount_administered AS administered_amount, i.amount_administered_unit AS administered_unit ".
138 "FROM (immunizations AS i, patient_data AS p, codes AS c) ".
139 "LEFT JOIN code_types ct ON c.code_type = ct.ct_id ".
140 "LEFT JOIN users AS u ON i.administered_by_id = u.id ".
141 "LEFT JOIN facility AS f ON f.id = u.facility_id ".
142 "LEFT JOIN list_options l ON l.option_id = p.country_code AND l.list_id='country' ".
143 "LEFT JOIN list_options l1 ON l1.option_id = i.route AND l1.list_id='drug_route' ".
144 "LEFT JOIN list_options l2 ON l2.option_id = p.guardiancountry AND l2.list_id='country' ".
145 "LEFT JOIN users AS u1 ON i.created_by = u1.id ".
146 "LEFT JOIN users AS u2 ON i.ordering_provider = u2.id ".
147 "WHERE ".
148 "ct.ct_key='CVX' and ";
149 if($from_date!=0) {
150 $query .= "i.vis_date >= ? " ;
151 $query_data[] = $from_date;
153 if($from_date!=0 and $to_date!=0) {
154 $query .= " and " ;
156 if($to_date!=0) {
157 $query .= "i.vis_date <= ? ";
158 $query_data[] = $to_date;
160 if($from_date!=0 or $to_date!=0) {
161 $query .= " and " ;
163 $query .= "i.patient_id=p.pid and ".
164 add_escape_custom($query_codes) .
165 $query_pids .
166 "i.cvx_code = c.code ORDER BY i.patient_id, i.id";
168 if($getCount){
169 $result = $this->applicationTable->zQuery($query,$query_data);
170 $resCount = $result->count();
171 return $resCount;
174 $query .= " LIMIT ".\Application\Plugin\CommonPlugin::escapeLimit($form_data['limit_start']).",".\Application\Plugin\CommonPlugin::escapeLimit($form_data['results']);
175 $result = $this->applicationTable->zQuery($query,$query_data);
176 return $result;
179 public function getNotes($option_id,$list_id)
181 if($option_id) {
182 $query = "SELECT
183 notes
184 FROM
185 list_options
186 WHERE list_id = ?
187 AND option_id = ?
188 AND activity = ?";
189 $result = $this->applicationTable->zQuery($query,array($list_id,$option_id,1));
190 $res_cur = $result->current();
192 return $res_cur['notes'];
196 * function getImmunizationObservationResultsData function to get immunization observation data
197 * @param type $pid
198 * @param type $id
199 * @return type Array $val
201 public function getImmunizationObservationResultsData($pid,$id)
203 $sql = " SELECT
205 FROM
206 immunization_observation
207 WHERE imo_pid = ?
208 AND imo_im_id = ?";
209 $result = $this->applicationTable->zQuery($sql, array($pid,$id));
210 foreach($result as $row) {
211 $val[] = $row;
213 return $val;
216 public function getCodes($option_id,$list_id)
218 if($option_id) {
219 $query = "SELECT
220 codes
221 FROM
222 list_options
223 WHERE list_id = ?
224 AND option_id = ?
225 AND activity = ?";
226 $result = $this->applicationTable->zQuery($query,array($list_id,$option_id,1));
227 $res_cur = $result->current();
229 $codes = explode(":",$res_cur['codes']);
230 return $codes[1];