6 * Copyright (C) 2010 Garden State Health Systems <http://www.gshsys.com/>
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 3
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>;.
20 * @author Garden State Health Systems <http://www.gshsys.com/>
21 * @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()
37 // Reserved for future use
40 function getMedicationData()
42 global $pid,$set,$start,$end;
45 SELECT prescriptions.date_added ,
46 prescriptions.patient_id,
47 prescriptions.start_date,
48 prescriptions.quantity,
49 prescriptions.interval,
52 prescriptions.medication,
53 IF(prescriptions.active=1,'Active','Prior History No Longer Active') AS active,
54 prescriptions.provider_id,
56 prescriptions.rxnorm_drugcode,
57 IFNULL(prescriptions.refills,0) AS refills,
61 LEFT JOIN list_options AS lo
62 ON lo.list_id = 'drug_units' AND prescriptions.unit = lo.option_id AND lo.activity = 1
63 LEFT JOIN list_options AS lo2
64 ON lo2.list_id = 'drug_form' AND prescriptions.form = lo2.option_id AND lo2.activity = 1
65 WHERE prescriptions.patient_id = ?
66 AND prescriptions.date_added BETWEEN ? AND ?
69 DATE(DATE) AS date_added,
71 begdate AS start_date,
77 IF((isnull(enddate) OR enddate = '0000-00-00' OR enddate >= CURDATE()),'Active','Prior History No Longer Active') AS active,
80 '' AS rxnorm_drugcode,
86 WHERE `type` = 'medication'
88 AND `date` BETWEEN ? AND ?";
89 $result = sqlStatement($sql, array($pid,$start,$end,$pid,$start,$end));
92 SELECT prescriptions.date_added ,
93 prescriptions.patient_id,
94 prescriptions.start_date,
95 prescriptions.quantity,
96 prescriptions.interval,
99 prescriptions.medication,
100 IF(prescriptions.active=1,'Active','Prior History No Longer Active') AS active,
101 prescriptions.provider_id,
103 prescriptions.rxnorm_drugcode,
104 IFNULL(prescriptions.refills,0) AS refills,
108 LEFT JOIN list_options AS lo
109 ON lo.list_id = 'drug_units' AND prescriptions.unit = lo.option_id AND lo.activity = 1
110 LEFT JOIN list_options AS lo2
111 ON lo2.list_id = 'drug_form' AND prescriptions.form = lo2.option_id AND lo2.activity = 1
112 WHERE prescriptions.patient_id = ?
115 DATE(DATE) AS date_added,
117 begdate AS start_date,
123 IF((isnull(enddate) OR enddate = '0000-00-00' OR enddate >= CURDATE()),'Active','Prior History No Longer Active') AS active,
126 '' AS rxnorm_drugcode,
132 WHERE `type` = 'medication'
134 $result = sqlStatement($sql, array($pid,$pid));
140 function getImmunizationData()
142 global $pid,$set,$start,$end;
145 immunizations.administered_date,
146 immunizations.patient_id,
147 immunizations.vis_date,
149 immunizations.immunization_id,
150 immunizations.manufacturer,
151 codes.code_text AS title
153 LEFT JOIN codes ON immunizations.cvx_code = codes.code
154 LEFT JOIN code_types ON codes.code_type = code_types.ct_id
155 WHERE immunizations.patient_id = ? AND code_types.ct_key = 'CVX' AND immunizations.added_erroneously = 0
156 AND create_date BETWEEN ? AND ?" ;
157 $result = sqlStatement($sql, array($pid,$start,$end));
160 immunizations.administered_date,
161 immunizations.patient_id,
162 immunizations.vis_date,
164 immunizations.immunization_id,
165 immunizations.manufacturer,
166 codes.code_text AS title
168 LEFT JOIN codes ON immunizations.cvx_code = codes.code
169 LEFT JOIN code_types ON codes.code_type = code_types.ct_id
170 WHERE immunizations.patient_id = ? AND immunizations.added_erroneously = 0 AND code_types.ct_key = 'CVX'";
171 $result = sqlStatement($sql, array($pid));
178 function getProcedureData()
181 global $pid,$set,$start,$end;
185 lists.title as proc_title,
186 lists.date as `date`,
187 list_options.title as outcome,
190 lists.type as `type`,
191 lists.diagnosis as `code`,
192 IF(SUBSTRING(lists.diagnosis,1,LOCATE(':',lists.diagnosis)-1) = 'ICD9','ICD9-CM',SUBSTRING(lists.diagnosis,1,LOCATE(':',lists.diagnosis)-1)) AS coding
195 LEFT JOIN issue_encounter
196 ON issue_encounter.list_id = lists.id
197 LEFT JOIN form_encounter
198 ON form_encounter.encounter = issue_encounter.encounter
200 ON form_encounter.facility_id = facility.id
202 ON form_encounter.provider_id = users.id
203 LEFT JOIN list_options
204 ON lists.outcome = list_options.option_id
205 AND list_options.list_id = 'outcome' AND list_options.activity = 1
206 WHERE lists.type = 'surgery'
208 AND lists.date BETWEEN ? AND ?
211 pt.name as proc_title,
214 ptt.laterality as laterality,
215 ptt.body_site as body_site,
216 'Lab Order' as `type`,
217 ptt.standard_code as `code`,
218 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
220 procedure_result AS prs
221 LEFT JOIN procedure_report AS prp
222 ON prs.procedure_report_id = prp.procedure_report_id
223 LEFT JOIN procedure_order AS po
224 ON prp.procedure_order_id = po.procedure_order_id
225 LEFT JOIN procedure_order_code AS poc
226 ON poc.procedure_order_id = po.procedure_order_id
227 AND poc.procedure_order_seq = prp.procedure_order_seq
228 LEFT JOIN procedure_type AS pt
229 ON pt.lab_id = po.lab_id
230 AND pt.procedure_code = prs.result_code
231 AND pt.procedure_type = 'res'
232 LEFT JOIN procedure_type AS ptt
233 ON pt.parent = ptt.procedure_type_id
234 AND ptt.procedure_type = 'ord'
235 LEFT JOIN list_options AS lo
236 ON lo.list_id = 'proc_unit'
237 AND pt.units = lo.option_id AND lo.activity = 1
238 WHERE po.patient_id = ?
239 AND prs.date BETWEEN ? AND ?";
241 $result = sqlStatement($sql, array($pid,$start,$end,$pid,$start,$end));
245 lists.title as proc_title,
246 lists.date as `date`,
247 list_options.title as outcome,
250 lists.type as `type`,
251 lists.diagnosis as `code`,
252 IF(SUBSTRING(lists.diagnosis,1,LOCATE(':',lists.diagnosis)-1) = 'ICD9','ICD9-CM',SUBSTRING(lists.diagnosis,1,LOCATE(':',lists.diagnosis)-1)) AS coding
255 LEFT JOIN issue_encounter
256 ON issue_encounter.list_id = lists.id
257 LEFT JOIN form_encounter
258 ON form_encounter.encounter = issue_encounter.encounter
260 ON form_encounter.facility_id = facility.id
262 ON form_encounter.provider_id = users.id
263 LEFT JOIN list_options
264 ON lists.outcome = list_options.option_id
265 AND list_options.list_id = 'outcome' AND list_options.activity = 1
266 WHERE lists.type = 'surgery'
270 pt.name as proc_title,
273 ptt.laterality as laterality,
274 ptt.body_site as body_site,
275 'Lab Order' as `type`,
276 ptt.standard_code as `code`,
277 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
279 procedure_result AS prs
280 LEFT JOIN procedure_report AS prp
281 ON prs.procedure_report_id = prp.procedure_report_id
282 LEFT JOIN procedure_order AS po
283 ON prp.procedure_order_id = po.procedure_order_id
284 LEFT JOIN procedure_order_code AS poc
285 ON poc.procedure_order_id = po.procedure_order_id
286 AND poc.procedure_order_seq = prp.procedure_order_seq
287 LEFT JOIN procedure_type AS pt
288 ON pt.lab_id = po.lab_id
289 AND pt.procedure_code = prs.result_code
290 AND pt.procedure_type = 'res'
291 LEFT JOIN procedure_type AS ptt
292 ON pt.parent = ptt.procedure_type_id
293 AND ptt.procedure_type = 'ord'
294 LEFT JOIN list_options AS lo
295 ON lo.list_id = 'proc_unit'
296 AND pt.units = lo.option_id AND lo.activity = 1
297 WHERE po.patient_id = ? ";
299 $result = sqlStatement($sql, array($pid,$pid));
306 function getProblemData()
309 # Note we are hard-coding (only allowing) problems that have been coded to ICD9. Would
310 # be easy to upgrade this to other codesets in future (ICD10,SNOMED) by using already
311 # existant flags in the code_types table.
312 # Additionally, only using problems that have one diagnosis code set in diagnosis field.
313 # Note OpenEMR allows multiple codes set per problem, but will limit to showing only
314 # problems with one diagnostic code set in order to maintain previous behavior
315 # (this will likely need to be dealt with at some point; ie. support multiple dx codes per problem).
317 global $pid,$set,$start,$end;
320 SELECT fe.encounter, fe.reason, fe.provider_id, u.title, u.fname, u.lname,
321 fe.facility_id, f.street, f.city, f.state, ie.list_id, l.pid, l.title AS prob_title, l.diagnosis,
322 l.outcome, l.groupname, l.begdate, l.enddate, l.type, l.comments , l.date
324 LEFT JOIN issue_encounter AS ie ON ie.list_id = l.id
325 LEFT JOIN form_encounter AS fe ON fe.encounter = ie.encounter
326 LEFT JOIN facility AS f ON fe.facility_id = f.id
327 LEFT JOIN users AS u ON fe.provider_id = u.id
328 WHERE l.type = 'medical_problem' AND l.pid=? AND l.diagnosis LIKE 'ICD9:%'
329 AND l.diagnosis NOT LIKE '%;%'
330 AND l.date BETWEEN ? AND ?";
331 $result = sqlStatement($sql, array($pid,$start,$end));
334 SELECT fe.encounter, fe.reason, fe.provider_id, u.title, u.fname, u.lname,
335 fe.facility_id, f.street, f.city, f.state, ie.list_id, l.pid, l.title AS prob_title, l.diagnosis,
336 l.outcome, l.groupname, l.begdate, l.enddate, l.type, l.comments , l.date
338 LEFT JOIN issue_encounter AS ie ON ie.list_id = l.id
339 LEFT JOIN form_encounter AS fe ON fe.encounter = ie.encounter
340 LEFT JOIN facility AS f ON fe.facility_id = f.id
341 LEFT JOIN users AS u ON fe.provider_id = u.id
342 WHERE l.type = 'medical_problem' AND l.pid=? AND l.diagnosis LIKE 'ICD9:%'
343 AND l.diagnosis NOT LIKE '%;%'";
344 $result = sqlStatement($sql, array($pid));
351 function getAlertData()
354 global $pid,$set,$start,$end;
357 select fe.reason, fe.provider_id, fe.facility_id, fe.encounter,
358 ie.list_id, l.pid, l.title as alert_title, l.outcome,
359 l.groupname, l.begdate, l.enddate, l.type, l.diagnosis, l.date ,
360 l.reaction , l.comments ,
361 f.street, f.city, f.state, u.title, u.fname, u.lname, cd.code_text
363 left join issue_encounter as ie
365 left join form_encounter as fe
366 on fe.encounter = ie.encounter
367 left join facility as f
368 on fe.facility_id = f.id
370 on fe.provider_id = u.id
371 left join codes as cd
372 on cd.code = SUBSTRING(l.diagnosis, LOCATE(':',l.diagnosis)+1)
373 where l.type = 'allergy' and l.pid=?
374 AND l.date BETWEEN ? AND ?";
376 $result = sqlStatement($sql, array($pid,$start,$end));
379 select fe.reason, fe.provider_id, fe.facility_id, fe.encounter,
380 ie.list_id, l.pid, l.title as alert_title, l.outcome,
381 l.groupname, l.begdate, l.enddate, l.type, l.diagnosis, l.date ,
382 l.reaction , l.comments ,
383 f.street, f.city, f.state, u.title, u.fname, u.lname, cd.code_text
385 left join issue_encounter as ie
387 left join form_encounter as fe
388 on fe.encounter = ie.encounter
389 left join facility as f
390 on fe.facility_id = f.id
392 on fe.provider_id = u.id
393 left join codes as cd
394 on cd.code = SUBSTRING(l.diagnosis, LOCATE(':',l.diagnosis)+1)
395 where l.type = 'allergy' and l.pid=?";
397 $result = sqlStatement($sql, array($pid));
404 function getResultData()
407 global $pid,$set,$start,$end;
411 prs.procedure_result_id as `pid`,
413 pt.procedure_type_id as `type`,
415 concat_ws(' ',prs.result,lo.title) as `result`,
416 prs.range as `range`,
417 prs.abnormal as `abnormal`,
418 prs.comments as `comments`,
421 procedure_result AS prs
422 LEFT JOIN procedure_report AS prp
423 ON prs.procedure_report_id = prp.procedure_report_id
424 LEFT JOIN procedure_order AS po
425 ON prp.procedure_order_id = po.procedure_order_id
426 LEFT JOIN procedure_order_code AS poc
427 ON poc.procedure_order_id = po.procedure_order_id
428 AND poc.procedure_order_seq = prp.procedure_order_seq
429 LEFT JOIN procedure_type AS pt
430 ON pt.lab_id = po.lab_id
431 AND pt.procedure_code = prs.result_code
432 AND pt.procedure_type = 'res'
433 LEFT JOIN procedure_type AS ptt
434 ON pt.parent = ptt.procedure_type_id
435 AND ptt.procedure_type = 'ord'
436 LEFT JOIN list_options AS lo
437 ON lo.list_id = 'proc_unit' AND pt.units = lo.option_id AND lo.activity = 1
438 WHERE po.patient_id=?
439 AND prs.date BETWEEN ? AND ?";
441 $result = sqlStatement($sql, array($pid,$start,$end));
445 prs.procedure_result_id as `pid`,
447 pt.procedure_type_id as `type`,
449 concat_ws(' ',prs.result,lo.title) as `result`,
450 prs.range as `range`,
451 prs.abnormal as `abnormal`,
452 prs.comments as `comments`,
455 procedure_result AS prs
456 LEFT JOIN procedure_report AS prp
457 ON prs.procedure_report_id = prp.procedure_report_id
458 LEFT JOIN procedure_order AS po
459 ON prp.procedure_order_id = po.procedure_order_id
460 LEFT JOIN procedure_order_code AS poc
461 ON poc.procedure_order_id = po.procedure_order_id
462 AND poc.procedure_order_seq = prp.procedure_order_seq
463 LEFT JOIN procedure_type AS pt
464 ON pt.lab_id = po.lab_id
465 AND pt.procedure_code = prs.result_code
466 AND pt.procedure_type = 'res'
467 LEFT JOIN procedure_type AS ptt
468 ON pt.parent = ptt.procedure_type_id
469 AND ptt.procedure_type = 'ord'
470 LEFT JOIN list_options AS lo
471 ON lo.list_id = 'proc_unit' AND pt.units = lo.option_id AND lo.activity = 1
472 WHERE po.patient_id=?";
474 $result = sqlStatement($sql, array($pid));
481 function getActorData()
486 select fname, lname, DOB, sex, pid, street, city, state, postal_code, phone_contact
490 $result[0] = sqlStatement($sql, array($pid));
493 SELECT * FROM users AS u LEFT JOIN facility AS f ON u.facility_id = f.id WHERE u.id=?";
495 $result[1] = sqlStatement($sql2, array($_SESSION['authUserID']));
499 u.ppid AS id, u.name AS lname, '' AS fname, '' AS city, '' AS state, '' AS zip, '' AS phone
501 procedure_order AS po
503 ON f.form_id = po.procedure_order_id
504 AND f.formdir = 'procedure_order'
505 LEFT JOIN list_options AS lo
506 ON lo.title = f.form_name AND lo.activity = 1
507 LEFT JOIN procedure_providers AS u
508 ON po.lab_id = u.ppid
510 AND lo.list_id = 'proc_type'
511 AND lo.option_id = 'ord'
514 $result[2] = sqlStatement($sql3, array($pid));
520 function getReportFilename()
525 select fname, lname, pid
529 $result = sqlQuery($sql, array($pid));
530 $result_filename = $result['lname'] . "-" . $result['fname'] . "-" . $result['pid'] . "-" . date("mdY", time());
532 return $result_filename;