feat: Fixes #6634 CORS content-encoding (#6636)
[openemr.git] / library / sql-ccr.inc.php
blobc99c3ec456b961c72e8135c0b2b6b1ad676c4586
1 <?php
3 /**
4 * Functions for CCR.
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>;.
19 * @package OpenEMR
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'])) {
26 $set = "on";
27 $start = $_POST['Start'];
28 $start = $start . " 00:00:00";
29 $end = $_POST['End'];
30 $end = $end . " 23:59:59";
34 function getHeaderData()
37 // Reserved for future use
40 function getMedicationData()
42 global $pid,$set,$start,$end;
43 if ($set == "on") {
44 $sql = "
45 SELECT prescriptions.date_added ,
46 prescriptions.patient_id,
47 prescriptions.start_date,
48 prescriptions.quantity,
49 prescriptions.interval,
50 prescriptions.note,
51 prescriptions.drug,
52 prescriptions.medication,
53 IF(prescriptions.active=1,'Active','Prior History No Longer Active') AS active,
54 prescriptions.provider_id,
55 prescriptions.size,
56 prescriptions.rxnorm_drugcode,
57 IFNULL(prescriptions.refills,0) AS refills,
58 lo2.title AS form,
59 lo.title
60 FROM prescriptions
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 ?
67 UNION
68 SELECT
69 DATE(DATE) AS date_added,
70 pid AS patient_id,
71 begdate AS start_date,
72 '' AS quantity,
73 '' AS `interval`,
74 comments AS note,
75 title AS drug,
76 '' AS medication,
77 IF((isnull(enddate) OR enddate = '0000-00-00' OR enddate >= CURDATE()),'Active','Prior History No Longer Active') AS active,
78 '' AS provider_id,
79 '' AS size,
80 '' AS rxnorm_drugcode,
81 0 AS refills,
82 '' AS form,
83 '' AS title
84 FROM
85 lists
86 WHERE `type` = 'medication'
87 AND pid = ?
88 AND `date` BETWEEN ? AND ?";
89 $result = sqlStatement($sql, array($pid,$start,$end,$pid,$start,$end));
90 } else {
91 $sql = "
92 SELECT prescriptions.date_added ,
93 prescriptions.patient_id,
94 prescriptions.start_date,
95 prescriptions.quantity,
96 prescriptions.interval,
97 prescriptions.note,
98 prescriptions.drug,
99 prescriptions.medication,
100 IF(prescriptions.active=1,'Active','Prior History No Longer Active') AS active,
101 prescriptions.provider_id,
102 prescriptions.size,
103 prescriptions.rxnorm_drugcode,
104 IFNULL(prescriptions.refills,0) AS refills,
105 lo2.title AS form,
106 lo.title
107 FROM prescriptions
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 = ?
113 UNION
114 SELECT
115 DATE(DATE) AS date_added,
116 pid AS patient_id,
117 begdate AS start_date,
118 '' AS quantity,
119 '' AS `interval`,
120 comments AS note,
121 title AS drug,
122 '' AS medication,
123 IF((isnull(enddate) OR enddate = '0000-00-00' OR enddate >= CURDATE()),'Active','Prior History No Longer Active') AS active,
124 '' AS provider_id,
125 '' AS size,
126 '' AS rxnorm_drugcode,
127 0 AS refills,
128 '' AS form,
129 '' AS title
130 FROM
131 lists
132 WHERE `type` = 'medication'
133 AND pid = ?";
134 $result = sqlStatement($sql, array($pid,$pid));
137 return $result;
140 function getImmunizationData()
142 global $pid,$set,$start,$end;
143 if ($set == "on") {
144 $sql = "SELECT
145 immunizations.administered_date,
146 immunizations.patient_id,
147 immunizations.vis_date,
148 immunizations.note,
149 immunizations.immunization_id,
150 immunizations.manufacturer,
151 codes.code_text AS title
152 FROM immunizations
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));
158 } else {
159 $sql = "SELECT
160 immunizations.administered_date,
161 immunizations.patient_id,
162 immunizations.vis_date,
163 immunizations.note,
164 immunizations.immunization_id,
165 immunizations.manufacturer,
166 codes.code_text AS title
167 FROM immunizations
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));
174 return $result;
178 function getProcedureData()
181 global $pid,$set,$start,$end;
182 if ($set == "on") {
183 $sql = "
184 SELECT
185 lists.title as proc_title,
186 lists.date as `date`,
187 list_options.title as outcome,
188 '' as laterality,
189 '' as body_site,
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
193 FROM
194 lists
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
199 LEFT JOIN facility
200 ON form_encounter.facility_id = facility.id
201 LEFT JOIN users
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'
207 AND lists.pid = ?
208 AND lists.date BETWEEN ? AND ?
209 UNION
210 SELECT
211 pt.name as proc_title,
212 prs.date as `date`,
213 '' as outcome,
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
219 FROM
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));
242 } else {
243 $sql = "
244 SELECT
245 lists.title as proc_title,
246 lists.date as `date`,
247 list_options.title as outcome,
248 '' as laterality,
249 '' as body_site,
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
253 FROM
254 lists
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
259 LEFT JOIN facility
260 ON form_encounter.facility_id = facility.id
261 LEFT JOIN users
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'
267 AND lists.pid = ?
268 UNION
269 SELECT
270 pt.name as proc_title,
271 prs.date as `date`,
272 '' as outcome,
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
278 FROM
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));
302 return $result;
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;
318 if ($set == "on") {
319 $sql = "
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
323 FROM lists AS l
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));
332 } else {
333 $sql = "
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
337 FROM lists AS l
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));
347 return $result;
351 function getAlertData()
354 global $pid,$set,$start,$end;
355 if ($set == "on") {
356 $sql = "
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
362 from lists as l
363 left join issue_encounter as ie
364 on ie.list_id = l.id
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
369 left join users as u
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));
377 } else {
378 $sql = "
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
384 from lists as l
385 left join issue_encounter as ie
386 on ie.list_id = l.id
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
391 left join users as u
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));
400 return $result;
404 function getResultData()
407 global $pid,$set,$start,$end;
408 if ($set == "on") {
409 $sql = "
410 SELECT
411 prs.procedure_result_id as `pid`,
412 pt.name as `name`,
413 pt.procedure_type_id as `type`,
414 prs.date as `date`,
415 concat_ws(' ',prs.result,lo.title) as `result`,
416 prs.range as `range`,
417 prs.abnormal as `abnormal`,
418 prs.comments as `comments`,
419 ptt.lab_id AS `lab`
420 FROM
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));
442 } else {
443 $sql = "
444 SELECT
445 prs.procedure_result_id as `pid`,
446 pt.name as `name`,
447 pt.procedure_type_id as `type`,
448 prs.date as `date`,
449 concat_ws(' ',prs.result,lo.title) as `result`,
450 prs.range as `range`,
451 prs.abnormal as `abnormal`,
452 prs.comments as `comments`,
453 ptt.lab_id AS `lab`
454 FROM
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));
477 return $result;
481 function getActorData()
483 global $pid;
485 $sql = "
486 select fname, lname, DOB, sex, pid, street, city, state, postal_code, phone_contact
487 from patient_data
488 where pid=?";
490 $result[0] = sqlStatement($sql, array($pid));
492 $sql2 = "
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']));
497 $sql3 = "
498 SELECT
499 u.ppid AS id, u.name AS lname, '' AS fname, '' AS city, '' AS state, '' AS zip, '' AS phone
500 FROM
501 procedure_order AS po
502 LEFT JOIN forms AS f
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
509 WHERE f.pid = ?
510 AND lo.list_id = 'proc_type'
511 AND lo.option_id = 'ord'
512 GROUP BY u.ppid";
514 $result[2] = sqlStatement($sql3, array($pid));
516 return $result;
520 function getReportFilename()
522 global $pid;
524 $sql = "
525 select fname, lname, pid
526 from patient_data
527 where pid=?";
529 $result = sqlQuery($sql, array($pid));
530 $result_filename = $result['lname'] . "-" . $result['fname'] . "-" . $result['pid'] . "-" . date("mdY", time());
532 return $result_filename;