5 * Copyright (C) 2010 Garden State Health Systems <http://www.gshsys.com/>
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the 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 General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
19 * @author Garden State Health Systems <http://www.gshsys.com/>
20 * @link http://www.open-emr.org
24 if($_POST['ccrAction'] == 'generate'){
25 if(isset($_POST['show_date'])){
27 $start = $_POST['Start'];
28 $start = $start." 00:00:00";
30 $end = $end." 23:59:59";
34 function getHeaderData() {
36 // Reserved for future use
40 function getMedicationData() {
41 global $pid,$set,$start,$end;
44 SELECT prescriptions.date_added ,
45 prescriptions.patient_id,
46 prescriptions.start_date,
47 prescriptions.quantity,
48 prescriptions.interval,
51 prescriptions.medication,
52 IF(prescriptions.active=1,'Active','Prior History No Longer Active') AS active,
53 prescriptions.provider_id,
55 prescriptions.rxnorm_drugcode,
56 IFNULL(prescriptions.refills,0) AS refills,
60 LEFT JOIN list_options AS lo
61 ON lo.list_id = 'drug_units' AND prescriptions.unit = lo.option_id
62 LEFT JOIN list_options AS lo2
63 ON lo2.list_id = 'drug_form' AND prescriptions.form = lo2.option_id
64 WHERE prescriptions.patient_id = ?
65 AND prescriptions.date_added BETWEEN ? AND ?
68 DATE(DATE) AS date_added,
70 begdate AS start_date,
76 IF((isnull(enddate) OR enddate = '0000-00-00' OR enddate >= CURDATE()),'Active','Prior History No Longer Active') AS active,
79 '' AS rxnorm_drugcode,
85 WHERE `type` = 'medication'
87 AND `date` BETWEEN ? AND ?";
88 $result = sqlStatement($sql, array($pid,$start,$end,$pid,$start,$end) );
91 SELECT prescriptions.date_added ,
92 prescriptions.patient_id,
93 prescriptions.start_date,
94 prescriptions.quantity,
95 prescriptions.interval,
98 prescriptions.medication,
99 IF(prescriptions.active=1,'Active','Prior History No Longer Active') AS active,
100 prescriptions.provider_id,
102 prescriptions.rxnorm_drugcode,
103 IFNULL(prescriptions.refills,0) AS refills,
107 LEFT JOIN list_options AS lo
108 ON lo.list_id = 'drug_units' AND prescriptions.unit = lo.option_id
109 LEFT JOIN list_options AS lo2
110 ON lo2.list_id = 'drug_form' AND prescriptions.form = lo2.option_id
111 WHERE prescriptions.patient_id = ?
114 DATE(DATE) AS date_added,
116 begdate AS start_date,
122 IF((isnull(enddate) OR enddate = '0000-00-00' OR enddate >= CURDATE()),'Active','Prior History No Longer Active') AS active,
125 '' AS rxnorm_drugcode,
131 WHERE `type` = 'medication'
133 $result = sqlStatement($sql, array($pid,$pid) );
138 function getImmunizationData() {
139 global $pid,$set,$start,$end;
142 immunizations.administered_date,
143 immunizations.patient_id,
144 immunizations.vis_date,
146 immunizations.immunization_id,
147 immunizations.manufacturer,
148 codes.code_text AS title
150 LEFT JOIN codes ON immunizations.cvx_code = codes.code
151 LEFT JOIN code_types ON codes.code_type = code_types.ct_id
152 WHERE immunizations.patient_id = ? AND code_types.ct_key = 'CVX'
153 AND create_date BETWEEN ? AND ?" ;
154 $result = sqlStatement($sql, array($pid,$start,$end) );
157 immunizations.administered_date,
158 immunizations.patient_id,
159 immunizations.vis_date,
161 immunizations.immunization_id,
162 immunizations.manufacturer,
163 codes.code_text AS title
165 LEFT JOIN codes ON immunizations.cvx_code = codes.code
166 LEFT JOIN code_types ON codes.code_type = code_types.ct_id
167 WHERE immunizations.patient_id = ? AND code_types.ct_key = 'CVX'";
168 $result = sqlStatement($sql, array($pid) );
174 function getProcedureData() {
176 global $pid,$set,$start,$end;
180 lists.title as proc_title,
181 lists.date as `date`,
182 list_options.title as outcome,
185 lists.type as `type`,
186 lists.diagnosis as `code`,
187 IF(SUBSTRING(lists.diagnosis,1,LOCATE(':',lists.diagnosis)-1) = 'ICD9','ICD9-CM',SUBSTRING(lists.diagnosis,1,LOCATE(':',lists.diagnosis)-1)) AS coding
190 LEFT JOIN issue_encounter
191 ON issue_encounter.list_id = lists.id
192 LEFT JOIN form_encounter
193 ON form_encounter.encounter = issue_encounter.encounter
195 ON form_encounter.facility_id = facility.id
197 ON form_encounter.provider_id = users.id
198 LEFT JOIN list_options
199 ON lists.outcome = list_options.option_id
200 AND list_options.list_id = 'outcome'
201 WHERE lists.type = 'surgery'
203 AND lists.date BETWEEN ? AND ?
206 pt.name as proc_title,
209 ptt.laterality as laterality,
210 ptt.body_site as body_site,
211 'Lab Order' as `type`,
212 ptt.standard_code as `code`,
213 IF(SUBSTRING(ptt.standard_code,1,LOCATE(':',ptt.standard_code)-1) = 'ICD9','ICD9-CM',SUBSTRING(ptt.standard_code,1,LOCATE(':',ptt.standard_code)-1)) AS coding
215 procedure_result AS prs
216 LEFT JOIN procedure_report AS prp
217 ON prs.procedure_report_id = prp.procedure_report_id
218 LEFT JOIN procedure_order AS po
219 ON prp.procedure_order_id = po.procedure_order_id
220 LEFT JOIN procedure_type AS pt
221 ON prs.procedure_type_id = pt.procedure_type_id
222 LEFT JOIN procedure_type AS ptt
223 ON pt.parent = ptt.procedure_type_id
224 AND ptt.procedure_type = 'ord'
225 LEFT JOIN list_options AS lo
226 ON lo.list_id = 'proc_unit'
227 AND pt.units = lo.option_id
228 WHERE po.patient_id = ?
229 AND prs.date BETWEEN ? AND ?";
231 $result = sqlStatement($sql, array($pid,$start,$end,$pid,$start,$end) );
235 lists.title as proc_title,
236 lists.date as `date`,
237 list_options.title as outcome,
240 lists.type as `type`,
241 lists.diagnosis as `code`,
242 IF(SUBSTRING(lists.diagnosis,1,LOCATE(':',lists.diagnosis)-1) = 'ICD9','ICD9-CM',SUBSTRING(lists.diagnosis,1,LOCATE(':',lists.diagnosis)-1)) AS coding
245 LEFT JOIN issue_encounter
246 ON issue_encounter.list_id = lists.id
247 LEFT JOIN form_encounter
248 ON form_encounter.encounter = issue_encounter.encounter
250 ON form_encounter.facility_id = facility.id
252 ON form_encounter.provider_id = users.id
253 LEFT JOIN list_options
254 ON lists.outcome = list_options.option_id
255 AND list_options.list_id = 'outcome'
256 WHERE lists.type = 'surgery'
260 pt.name as proc_title,
263 ptt.laterality as laterality,
264 ptt.body_site as body_site,
265 'Lab Order' as `type`,
266 ptt.standard_code as `code`,
267 IF(SUBSTRING(ptt.standard_code,1,LOCATE(':',ptt.standard_code)-1) = 'ICD9','ICD9-CM',SUBSTRING(ptt.standard_code,1,LOCATE(':',ptt.standard_code)-1)) AS coding
269 procedure_result AS prs
270 LEFT JOIN procedure_report AS prp
271 ON prs.procedure_report_id = prp.procedure_report_id
272 LEFT JOIN procedure_order AS po
273 ON prp.procedure_order_id = po.procedure_order_id
274 LEFT JOIN procedure_type AS pt
275 ON prs.procedure_type_id = pt.procedure_type_id
276 LEFT JOIN procedure_type AS ptt
277 ON pt.parent = ptt.procedure_type_id
278 AND ptt.procedure_type = 'ord'
279 LEFT JOIN list_options AS lo
280 ON lo.list_id = 'proc_unit'
281 AND pt.units = lo.option_id
282 WHERE po.patient_id = ? ";
284 $result = sqlStatement($sql, array($pid,$pid) );
290 function getProblemData() {
292 # Note we are hard-coding (only allowing) problems that have been coded to ICD9. Would
293 # be easy to upgrade this to other codesets in future (ICD10,SNOMED) by using already
294 # existant flags in the code_types table.
295 # Additionally, only using problems that have one diagnosis code set in diagnosis field.
296 # Note OpenEMR allows multiple codes set per problem, but will limit to showing only
297 # problems with one diagnostic code set in order to maintain previous behavior
298 # (this will likely need to be dealt with at some point; ie. support multiple dx codes per problem).
300 global $pid,$set,$start,$end;
303 SELECT fe.encounter, fe.reason, fe.provider_id, u.title, u.fname, u.lname,
304 fe.facility_id, f.street, f.city, f.state, ie.list_id, l.pid, l.title AS prob_title, l.diagnosis,
305 l.outcome, l.groupname, l.begdate, l.enddate, l.type, l.comments , l.date
307 LEFT JOIN issue_encounter AS ie ON ie.list_id = l.id
308 LEFT JOIN form_encounter AS fe ON fe.encounter = ie.encounter
309 LEFT JOIN facility AS f ON fe.facility_id = f.id
310 LEFT JOIN users AS u ON fe.provider_id = u.id
311 WHERE l.type = 'medical_problem' AND l.pid=? AND l.diagnosis LIKE 'ICD9:%'
312 AND l.diagnosis NOT LIKE '%;%'
313 AND l.date BETWEEN ? AND ?";
314 $result = sqlStatement($sql, array($pid,$start,$end) );
317 SELECT fe.encounter, fe.reason, fe.provider_id, u.title, u.fname, u.lname,
318 fe.facility_id, f.street, f.city, f.state, ie.list_id, l.pid, l.title AS prob_title, l.diagnosis,
319 l.outcome, l.groupname, l.begdate, l.enddate, l.type, l.comments , l.date
321 LEFT JOIN issue_encounter AS ie ON ie.list_id = l.id
322 LEFT JOIN form_encounter AS fe ON fe.encounter = ie.encounter
323 LEFT JOIN facility AS f ON fe.facility_id = f.id
324 LEFT JOIN users AS u ON fe.provider_id = u.id
325 WHERE l.type = 'medical_problem' AND l.pid=? AND l.diagnosis LIKE 'ICD9:%'
326 AND l.diagnosis NOT LIKE '%;%'";
327 $result = sqlStatement($sql, array($pid) );
333 function getAlertData() {
335 global $pid,$set,$start,$end;
338 select fe.reason, fe.provider_id, fe.facility_id, fe.encounter,
339 ie.list_id, l.pid, l.title as alert_title, l.outcome,
340 l.groupname, l.begdate, l.enddate, l.type, l.diagnosis, l.date ,
341 l.reaction , l.comments ,
342 f.street, f.city, f.state, u.title, u.fname, u.lname, cd.code_text
344 left join issue_encounter as ie
346 left join form_encounter as fe
347 on fe.encounter = ie.encounter
348 left join facility as f
349 on fe.facility_id = f.id
351 on fe.provider_id = u.id
352 left join codes as cd
353 on cd.code = SUBSTRING(l.diagnosis, LOCATE(':',l.diagnosis)+1)
354 where l.type = 'allergy' and l.pid=?
355 AND l.date BETWEEN ? AND ?";
357 $result = sqlStatement($sql, array($pid,$start,$end) );
360 select fe.reason, fe.provider_id, fe.facility_id, fe.encounter,
361 ie.list_id, l.pid, l.title as alert_title, l.outcome,
362 l.groupname, l.begdate, l.enddate, l.type, l.diagnosis, l.date ,
363 l.reaction , l.comments ,
364 f.street, f.city, f.state, u.title, u.fname, u.lname, cd.code_text
366 left join issue_encounter as ie
368 left join form_encounter as fe
369 on fe.encounter = ie.encounter
370 left join facility as f
371 on fe.facility_id = f.id
373 on fe.provider_id = u.id
374 left join codes as cd
375 on cd.code = SUBSTRING(l.diagnosis, LOCATE(':',l.diagnosis)+1)
376 where l.type = 'allergy' and l.pid=?";
378 $result = sqlStatement($sql, array($pid) );
384 function getResultData() {
386 global $pid,$set,$start,$end;
390 prs.procedure_result_id as `pid`,
392 pt.procedure_type_id as `type`,
394 concat_ws(' ',prs.result,lo.title) as `result`,
395 prs.range as `range`,
396 prs.abnormal as `abnormal`,
397 prs.comments as `comments`,
400 procedure_result AS prs
401 LEFT JOIN procedure_report AS prp
402 ON prs.procedure_report_id = prp.procedure_report_id
403 LEFT JOIN procedure_order AS po
404 ON prp.procedure_order_id = po.procedure_order_id
405 LEFT JOIN procedure_type AS pt
406 ON prs.procedure_type_id = pt.procedure_type_id
407 LEFT JOIN procedure_type AS ptt
408 ON pt.parent = ptt.procedure_type_id
409 AND ptt.procedure_type = 'ord'
410 LEFT JOIN list_options AS lo
411 ON lo.list_id = 'proc_unit' AND pt.units = lo.option_id
412 WHERE po.patient_id=?
413 AND prs.date BETWEEN ? AND ?";
415 $result = sqlStatement($sql, array($pid,$start,$end) );
419 prs.procedure_result_id as `pid`,
421 pt.procedure_type_id as `type`,
423 concat_ws(' ',prs.result,lo.title) as `result`,
424 prs.range as `range`,
425 prs.abnormal as `abnormal`,
426 prs.comments as `comments`,
429 procedure_result AS prs
430 LEFT JOIN procedure_report AS prp
431 ON prs.procedure_report_id = prp.procedure_report_id
432 LEFT JOIN procedure_order AS po
433 ON prp.procedure_order_id = po.procedure_order_id
434 LEFT JOIN procedure_type AS pt
435 ON prs.procedure_type_id = pt.procedure_type_id
436 LEFT JOIN procedure_type AS ptt
437 ON pt.parent = ptt.procedure_type_id
438 AND ptt.procedure_type = 'ord'
439 LEFT JOIN list_options AS lo
440 ON lo.list_id = 'proc_unit' AND pt.units = lo.option_id
441 WHERE po.patient_id=?";
443 $result = sqlStatement($sql, array($pid) );
449 function getActorData() {
453 select fname, lname, DOB, sex, pid, street, city, state, postal_code, phone_contact
457 $result[0] = sqlStatement($sql, array($pid) );
460 SELECT * FROM users AS u LEFT JOIN facility AS f ON u.facility_id = f.id WHERE u.id=?";
462 $result[1] = sqlStatement($sql2, array($_SESSION['authUserID']) );
469 LEFT JOIN procedure_order AS po
470 ON po.procedure_type_id = pt.procedure_type_id
472 ON f.form_id = po.procedure_order_id
473 LEFT JOIN list_options AS lo
474 ON lo.title = f.form_name
478 AND lo.list_id = 'proc_type'
479 AND lo.option_id = 'ord'
482 $result[2] = sqlStatement($sql3, array($pid) );
488 function getReportFilename() {
492 select fname, lname, pid
496 $result = sqlQuery($sql, array($pid) );
497 $result_filename = $result['lname']."-".$result['fname']."-".$result['pid']."-".date("mdY",time());
499 return $result_filename;