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 AND lo.activity = 1
62 LEFT JOIN list_options AS lo2
63 ON lo2.list_id = 'drug_form' AND prescriptions.form = lo2.option_id AND lo2.activity = 1
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 AND lo.activity = 1
109 LEFT JOIN list_options AS lo2
110 ON lo2.list_id = 'drug_form' AND prescriptions.form = lo2.option_id AND lo2.activity = 1
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' AND immunizations.added_erroneously = 0
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 immunizations.added_erroneously = 0 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' AND list_options.activity = 1
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_order_code AS poc
221 ON poc.procedure_order_id = po.procedure_order_id
222 AND poc.procedure_order_seq = prp.procedure_order_seq
223 LEFT JOIN procedure_type AS pt
224 ON pt.lab_id = po.lab_id
225 AND pt.procedure_code = prs.result_code
226 AND pt.procedure_type = 'res'
227 LEFT JOIN procedure_type AS ptt
228 ON pt.parent = ptt.procedure_type_id
229 AND ptt.procedure_type = 'ord'
230 LEFT JOIN list_options AS lo
231 ON lo.list_id = 'proc_unit'
232 AND pt.units = lo.option_id AND lo.activity = 1
233 WHERE po.patient_id = ?
234 AND prs.date BETWEEN ? AND ?";
236 $result = sqlStatement($sql, array($pid,$start,$end,$pid,$start,$end) );
240 lists.title as proc_title,
241 lists.date as `date`,
242 list_options.title as outcome,
245 lists.type as `type`,
246 lists.diagnosis as `code`,
247 IF(SUBSTRING(lists.diagnosis,1,LOCATE(':',lists.diagnosis)-1) = 'ICD9','ICD9-CM',SUBSTRING(lists.diagnosis,1,LOCATE(':',lists.diagnosis)-1)) AS coding
250 LEFT JOIN issue_encounter
251 ON issue_encounter.list_id = lists.id
252 LEFT JOIN form_encounter
253 ON form_encounter.encounter = issue_encounter.encounter
255 ON form_encounter.facility_id = facility.id
257 ON form_encounter.provider_id = users.id
258 LEFT JOIN list_options
259 ON lists.outcome = list_options.option_id
260 AND list_options.list_id = 'outcome' AND list_options.activity = 1
261 WHERE lists.type = 'surgery'
265 pt.name as proc_title,
268 ptt.laterality as laterality,
269 ptt.body_site as body_site,
270 'Lab Order' as `type`,
271 ptt.standard_code as `code`,
272 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
274 procedure_result AS prs
275 LEFT JOIN procedure_report AS prp
276 ON prs.procedure_report_id = prp.procedure_report_id
277 LEFT JOIN procedure_order AS po
278 ON prp.procedure_order_id = po.procedure_order_id
279 LEFT JOIN procedure_order_code AS poc
280 ON poc.procedure_order_id = po.procedure_order_id
281 AND poc.procedure_order_seq = prp.procedure_order_seq
282 LEFT JOIN procedure_type AS pt
283 ON pt.lab_id = po.lab_id
284 AND pt.procedure_code = prs.result_code
285 AND pt.procedure_type = 'res'
286 LEFT JOIN procedure_type AS ptt
287 ON pt.parent = ptt.procedure_type_id
288 AND ptt.procedure_type = 'ord'
289 LEFT JOIN list_options AS lo
290 ON lo.list_id = 'proc_unit'
291 AND pt.units = lo.option_id AND lo.activity = 1
292 WHERE po.patient_id = ? ";
294 $result = sqlStatement($sql, array($pid,$pid) );
300 function getProblemData() {
302 # Note we are hard-coding (only allowing) problems that have been coded to ICD9. Would
303 # be easy to upgrade this to other codesets in future (ICD10,SNOMED) by using already
304 # existant flags in the code_types table.
305 # Additionally, only using problems that have one diagnosis code set in diagnosis field.
306 # Note OpenEMR allows multiple codes set per problem, but will limit to showing only
307 # problems with one diagnostic code set in order to maintain previous behavior
308 # (this will likely need to be dealt with at some point; ie. support multiple dx codes per problem).
310 global $pid,$set,$start,$end;
313 SELECT fe.encounter, fe.reason, fe.provider_id, u.title, u.fname, u.lname,
314 fe.facility_id, f.street, f.city, f.state, ie.list_id, l.pid, l.title AS prob_title, l.diagnosis,
315 l.outcome, l.groupname, l.begdate, l.enddate, l.type, l.comments , l.date
317 LEFT JOIN issue_encounter AS ie ON ie.list_id = l.id
318 LEFT JOIN form_encounter AS fe ON fe.encounter = ie.encounter
319 LEFT JOIN facility AS f ON fe.facility_id = f.id
320 LEFT JOIN users AS u ON fe.provider_id = u.id
321 WHERE l.type = 'medical_problem' AND l.pid=? AND l.diagnosis LIKE 'ICD9:%'
322 AND l.diagnosis NOT LIKE '%;%'
323 AND l.date BETWEEN ? AND ?";
324 $result = sqlStatement($sql, array($pid,$start,$end) );
327 SELECT fe.encounter, fe.reason, fe.provider_id, u.title, u.fname, u.lname,
328 fe.facility_id, f.street, f.city, f.state, ie.list_id, l.pid, l.title AS prob_title, l.diagnosis,
329 l.outcome, l.groupname, l.begdate, l.enddate, l.type, l.comments , l.date
331 LEFT JOIN issue_encounter AS ie ON ie.list_id = l.id
332 LEFT JOIN form_encounter AS fe ON fe.encounter = ie.encounter
333 LEFT JOIN facility AS f ON fe.facility_id = f.id
334 LEFT JOIN users AS u ON fe.provider_id = u.id
335 WHERE l.type = 'medical_problem' AND l.pid=? AND l.diagnosis LIKE 'ICD9:%'
336 AND l.diagnosis NOT LIKE '%;%'";
337 $result = sqlStatement($sql, array($pid) );
343 function getAlertData() {
345 global $pid,$set,$start,$end;
348 select fe.reason, fe.provider_id, fe.facility_id, fe.encounter,
349 ie.list_id, l.pid, l.title as alert_title, l.outcome,
350 l.groupname, l.begdate, l.enddate, l.type, l.diagnosis, l.date ,
351 l.reaction , l.comments ,
352 f.street, f.city, f.state, u.title, u.fname, u.lname, cd.code_text
354 left join issue_encounter as ie
356 left join form_encounter as fe
357 on fe.encounter = ie.encounter
358 left join facility as f
359 on fe.facility_id = f.id
361 on fe.provider_id = u.id
362 left join codes as cd
363 on cd.code = SUBSTRING(l.diagnosis, LOCATE(':',l.diagnosis)+1)
364 where l.type = 'allergy' and l.pid=?
365 AND l.date BETWEEN ? AND ?";
367 $result = sqlStatement($sql, array($pid,$start,$end) );
370 select fe.reason, fe.provider_id, fe.facility_id, fe.encounter,
371 ie.list_id, l.pid, l.title as alert_title, l.outcome,
372 l.groupname, l.begdate, l.enddate, l.type, l.diagnosis, l.date ,
373 l.reaction , l.comments ,
374 f.street, f.city, f.state, u.title, u.fname, u.lname, cd.code_text
376 left join issue_encounter as ie
378 left join form_encounter as fe
379 on fe.encounter = ie.encounter
380 left join facility as f
381 on fe.facility_id = f.id
383 on fe.provider_id = u.id
384 left join codes as cd
385 on cd.code = SUBSTRING(l.diagnosis, LOCATE(':',l.diagnosis)+1)
386 where l.type = 'allergy' and l.pid=?";
388 $result = sqlStatement($sql, array($pid) );
394 function getResultData() {
396 global $pid,$set,$start,$end;
400 prs.procedure_result_id as `pid`,
402 pt.procedure_type_id as `type`,
404 concat_ws(' ',prs.result,lo.title) as `result`,
405 prs.range as `range`,
406 prs.abnormal as `abnormal`,
407 prs.comments as `comments`,
410 procedure_result AS prs
411 LEFT JOIN procedure_report AS prp
412 ON prs.procedure_report_id = prp.procedure_report_id
413 LEFT JOIN procedure_order AS po
414 ON prp.procedure_order_id = po.procedure_order_id
415 LEFT JOIN procedure_order_code AS poc
416 ON poc.procedure_order_id = po.procedure_order_id
417 AND poc.procedure_order_seq = prp.procedure_order_seq
418 LEFT JOIN procedure_type AS pt
419 ON pt.lab_id = po.lab_id
420 AND pt.procedure_code = prs.result_code
421 AND pt.procedure_type = 'res'
422 LEFT JOIN procedure_type AS ptt
423 ON pt.parent = ptt.procedure_type_id
424 AND ptt.procedure_type = 'ord'
425 LEFT JOIN list_options AS lo
426 ON lo.list_id = 'proc_unit' AND pt.units = lo.option_id AND lo.activity = 1
427 WHERE po.patient_id=?
428 AND prs.date BETWEEN ? AND ?";
430 $result = sqlStatement($sql, array($pid,$start,$end) );
434 prs.procedure_result_id as `pid`,
436 pt.procedure_type_id as `type`,
438 concat_ws(' ',prs.result,lo.title) as `result`,
439 prs.range as `range`,
440 prs.abnormal as `abnormal`,
441 prs.comments as `comments`,
444 procedure_result AS prs
445 LEFT JOIN procedure_report AS prp
446 ON prs.procedure_report_id = prp.procedure_report_id
447 LEFT JOIN procedure_order AS po
448 ON prp.procedure_order_id = po.procedure_order_id
449 LEFT JOIN procedure_order_code AS poc
450 ON poc.procedure_order_id = po.procedure_order_id
451 AND poc.procedure_order_seq = prp.procedure_order_seq
452 LEFT JOIN procedure_type AS pt
453 ON pt.lab_id = po.lab_id
454 AND pt.procedure_code = prs.result_code
455 AND pt.procedure_type = 'res'
456 LEFT JOIN procedure_type AS ptt
457 ON pt.parent = ptt.procedure_type_id
458 AND ptt.procedure_type = 'ord'
459 LEFT JOIN list_options AS lo
460 ON lo.list_id = 'proc_unit' AND pt.units = lo.option_id AND lo.activity = 1
461 WHERE po.patient_id=?";
463 $result = sqlStatement($sql, array($pid) );
469 function getActorData() {
473 select fname, lname, DOB, sex, pid, street, city, state, postal_code, phone_contact
477 $result[0] = sqlStatement($sql, array($pid) );
480 SELECT * FROM users AS u LEFT JOIN facility AS f ON u.facility_id = f.id WHERE u.id=?";
482 $result[1] = sqlStatement($sql2, array($_SESSION['authUserID']) );
486 u.ppid AS id, u.name AS lname, '' AS fname, '' AS city, '' AS state, '' AS zip, '' AS phone
488 procedure_order AS po
490 ON f.form_id = po.procedure_order_id
491 AND f.formdir = 'procedure_order'
492 LEFT JOIN list_options AS lo
493 ON lo.title = f.form_name AND lo.activity = 1
494 LEFT JOIN procedure_providers AS u
495 ON po.lab_id = u.ppid
497 AND lo.list_id = 'proc_type'
498 AND lo.option_id = 'ord'
501 $result[2] = sqlStatement($sql3, array($pid) );
507 function getReportFilename() {
511 select fname, lname, pid
515 $result = sqlQuery($sql, array($pid) );
516 $result_filename = $result['lname']."-".$result['fname']."-".$result['pid']."-".date("mdY",time());
518 return $result_filename;