forked from LibreHealthIO/lh-ehr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql-ccr.inc
521 lines (480 loc) · 17.6 KB
/
sql-ccr.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
<?php
/**
* Functions for CCR.
*
* Copyright (C) 2010 Garden State Health Systems <http://www.gshsys.com/>
*
* LICENSE: This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
*
* @package LibreEHR
* @author Garden State Health Systems <http://www.gshsys.com/>
* @link http://librehealth.io
*/
if($_POST['ccrAction'] == 'generate'){
if(isset($_POST['show_date'])){
$set = "on";
$start = $_POST['Start'];
$start = $start." 00:00:00";
$end = $_POST['End'];
$end = $end." 23:59:59";
}
}
function getHeaderData() {
// Reserved for future use
}
function getMedicationData() {
global $pid,$set,$start,$end;
if($set == "on"){
$sql = "
SELECT prescriptions.date_added ,
prescriptions.patient_id,
prescriptions.start_date,
prescriptions.quantity,
prescriptions.interval,
prescriptions.note,
prescriptions.drug,
prescriptions.medication,
IF(prescriptions.active=1,'Active','Prior History No Longer Active') AS active,
prescriptions.provider_id,
prescriptions.size,
prescriptions.rxnorm_drugcode,
IFNULL(prescriptions.refills,0) AS refills,
lo2.title AS form,
lo.title
FROM prescriptions
LEFT JOIN list_options AS lo
ON lo.list_id = 'drug_units' AND prescriptions.unit = lo.option_id
LEFT JOIN list_options AS lo2
ON lo2.list_id = 'drug_form' AND prescriptions.form = lo2.option_id
WHERE prescriptions.patient_id = ?
AND prescriptions.date_added BETWEEN ? AND ?
UNION
SELECT
DATE(DATE) AS date_added,
pid AS patient_id,
begdate AS start_date,
'' AS quantity,
'' AS `interval`,
comments AS note,
title AS drug,
'' AS medication,
IF((isnull(enddate) OR enddate = '0000-00-00' OR enddate >= CURDATE()),'Active','Prior History No Longer Active') AS active,
'' AS provider_id,
'' AS size,
'' AS rxnorm_drugcode,
0 AS refills,
'' AS form,
'' AS title
FROM
lists
WHERE `type` = 'medication'
AND pid = ?
AND `date` BETWEEN ? AND ?";
$result = sqlStatement($sql, array($pid,$start,$end,$pid,$start,$end) );
}else{
$sql = "
SELECT prescriptions.date_added ,
prescriptions.patient_id,
prescriptions.start_date,
prescriptions.quantity,
prescriptions.interval,
prescriptions.note,
prescriptions.drug,
prescriptions.medication,
IF(prescriptions.active=1,'Active','Prior History No Longer Active') AS active,
prescriptions.provider_id,
prescriptions.size,
prescriptions.rxnorm_drugcode,
IFNULL(prescriptions.refills,0) AS refills,
lo2.title AS form,
lo.title
FROM prescriptions
LEFT JOIN list_options AS lo
ON lo.list_id = 'drug_units' AND prescriptions.unit = lo.option_id
LEFT JOIN list_options AS lo2
ON lo2.list_id = 'drug_form' AND prescriptions.form = lo2.option_id
WHERE prescriptions.patient_id = ?
UNION
SELECT
DATE(DATE) AS date_added,
pid AS patient_id,
begdate AS start_date,
'' AS quantity,
'' AS `interval`,
comments AS note,
title AS drug,
'' AS medication,
IF((isnull(enddate) OR enddate = '0000-00-00' OR enddate >= CURDATE()),'Active','Prior History No Longer Active') AS active,
'' AS provider_id,
'' AS size,
'' AS rxnorm_drugcode,
0 AS refills,
'' AS form,
'' AS title
FROM
lists
WHERE `type` = 'medication'
AND pid = ?";
$result = sqlStatement($sql, array($pid,$pid) );
}
return $result;
}
function getImmunizationData() {
global $pid,$set,$start,$end;
if($set == "on"){
$sql = "SELECT
immunizations.administered_date,
immunizations.patient_id,
immunizations.vis_date,
immunizations.note,
immunizations.immunization_id,
immunizations.manufacturer,
codes.code_text AS title
FROM immunizations
LEFT JOIN codes ON immunizations.cvx_code = codes.code
LEFT JOIN code_types ON codes.code_type = code_types.ct_id
WHERE immunizations.patient_id = ? AND code_types.ct_key = 'CVX' AND immunizations.added_erroneously = 0
AND create_date BETWEEN ? AND ?" ;
$result = sqlStatement($sql, array($pid,$start,$end) );
}else{
$sql = "SELECT
immunizations.administered_date,
immunizations.patient_id,
immunizations.vis_date,
immunizations.note,
immunizations.immunization_id,
immunizations.manufacturer,
codes.code_text AS title
FROM immunizations
LEFT JOIN codes ON immunizations.cvx_code = codes.code
LEFT JOIN code_types ON codes.code_type = code_types.ct_id
WHERE immunizations.patient_id = ? AND immunizations.added_erroneously = 0 AND code_types.ct_key = 'CVX'";
$result = sqlStatement($sql, array($pid) );
}
return $result;
}
function getProcedureData() {
global $pid,$set,$start,$end;
if($set == "on"){
$sql = "
SELECT
lists.title as proc_title,
lists.date as `date`,
list_options.title as outcome,
'' as laterality,
'' as body_site,
lists.type as `type`,
lists.diagnosis as `code`,
IF(SUBSTRING(lists.diagnosis,1,LOCATE(':',lists.diagnosis)-1) = 'ICD9','ICD9-CM',SUBSTRING(lists.diagnosis,1,LOCATE(':',lists.diagnosis)-1)) AS coding
FROM
lists
LEFT JOIN issue_encounter
ON issue_encounter.list_id = lists.id
LEFT JOIN form_encounter
ON form_encounter.encounter = issue_encounter.encounter
LEFT JOIN facility
ON form_encounter.facility_id = facility.id
LEFT JOIN users
ON form_encounter.provider_id = users.id
LEFT JOIN list_options
ON lists.outcome = list_options.option_id
AND list_options.list_id = 'outcome'
WHERE lists.type = 'surgery'
AND lists.pid = ?
AND lists.date BETWEEN ? AND ?
UNION
SELECT
pt.name as proc_title,
prs.date as `date`,
'' as outcome,
ptt.laterality as laterality,
ptt.body_site as body_site,
'Lab Order' as `type`,
ptt.standard_code as `code`,
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
FROM
procedure_result AS prs
LEFT JOIN procedure_report AS prp
ON prs.procedure_report_id = prp.procedure_report_id
LEFT JOIN procedure_order AS po
ON prp.procedure_order_id = po.procedure_order_id
LEFT JOIN procedure_order_code AS poc
ON poc.procedure_order_id = po.procedure_order_id
AND poc.procedure_order_seq = prp.procedure_order_seq
LEFT JOIN procedure_type AS pt
ON pt.lab_id = po.lab_id
AND pt.procedure_code = prs.result_code
AND pt.procedure_type = 'res'
LEFT JOIN procedure_type AS ptt
ON pt.parent = ptt.procedure_type_id
AND ptt.procedure_type = 'ord'
LEFT JOIN list_options AS lo
ON lo.list_id = 'proc_unit'
AND pt.units = lo.option_id
WHERE po.patient_id = ?
AND prs.date BETWEEN ? AND ?";
$result = sqlStatement($sql, array($pid,$start,$end,$pid,$start,$end) );
}else{
$sql = "
SELECT
lists.title as proc_title,
lists.date as `date`,
list_options.title as outcome,
'' as laterality,
'' as body_site,
lists.type as `type`,
lists.diagnosis as `code`,
IF(SUBSTRING(lists.diagnosis,1,LOCATE(':',lists.diagnosis)-1) = 'ICD9','ICD9-CM',SUBSTRING(lists.diagnosis,1,LOCATE(':',lists.diagnosis)-1)) AS coding
FROM
lists
LEFT JOIN issue_encounter
ON issue_encounter.list_id = lists.id
LEFT JOIN form_encounter
ON form_encounter.encounter = issue_encounter.encounter
LEFT JOIN facility
ON form_encounter.facility_id = facility.id
LEFT JOIN users
ON form_encounter.provider_id = users.id
LEFT JOIN list_options
ON lists.outcome = list_options.option_id
AND list_options.list_id = 'outcome'
WHERE lists.type = 'surgery'
AND lists.pid = ?
UNION
SELECT
pt.name as proc_title,
prs.date as `date`,
'' as outcome,
ptt.laterality as laterality,
ptt.body_site as body_site,
'Lab Order' as `type`,
ptt.standard_code as `code`,
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
FROM
procedure_result AS prs
LEFT JOIN procedure_report AS prp
ON prs.procedure_report_id = prp.procedure_report_id
LEFT JOIN procedure_order AS po
ON prp.procedure_order_id = po.procedure_order_id
LEFT JOIN procedure_order_code AS poc
ON poc.procedure_order_id = po.procedure_order_id
AND poc.procedure_order_seq = prp.procedure_order_seq
LEFT JOIN procedure_type AS pt
ON pt.lab_id = po.lab_id
AND pt.procedure_code = prs.result_code
AND pt.procedure_type = 'res'
LEFT JOIN procedure_type AS ptt
ON pt.parent = ptt.procedure_type_id
AND ptt.procedure_type = 'ord'
LEFT JOIN list_options AS lo
ON lo.list_id = 'proc_unit'
AND pt.units = lo.option_id
WHERE po.patient_id = ? ";
$result = sqlStatement($sql, array($pid,$pid) );
}
return $result;
}
function getProblemData() {
# Note we are hard-coding (only allowing) problems that have been coded to ICD9. Would
# be easy to upgrade this to other codesets in future (ICD10,SNOMED) by using already
# existant flags in the code_types table.
# Additionally, only using problems that have one diagnosis code set in diagnosis field.
# Note LibreEHR allows multiple codes set per problem, but will limit to showing only
# problems with one diagnostic code set in order to maintain previous behavior
# (this will likely need to be dealt with at some point; ie. support multiple dx codes per problem).
global $pid,$set,$start,$end;
if($set == "on"){
$sql = "
SELECT fe.encounter, fe.reason, fe.provider_id, u.title, u.fname, u.lname,
fe.facility_id, f.street, f.city, f.state, ie.list_id, l.pid, l.title AS prob_title, l.diagnosis,
l.outcome, l.groupname, l.begdate, l.enddate, l.type, l.comments , l.date
FROM lists AS l
LEFT JOIN issue_encounter AS ie ON ie.list_id = l.id
LEFT JOIN form_encounter AS fe ON fe.encounter = ie.encounter
LEFT JOIN facility AS f ON fe.facility_id = f.id
LEFT JOIN users AS u ON fe.provider_id = u.id
WHERE l.type = 'medical_problem' AND l.pid=? AND l.diagnosis LIKE 'ICD10:%'
AND l.diagnosis NOT LIKE '%;%'
AND l.date BETWEEN ? AND ?";
$result = sqlStatement($sql, array($pid,$start,$end) );
}else{
$sql = "
SELECT fe.encounter, fe.reason, fe.provider_id, u.title, u.fname, u.lname,
fe.facility_id, f.street, f.city, f.state, ie.list_id, l.pid, l.title AS prob_title, l.diagnosis,
l.outcome, l.groupname, l.begdate, l.enddate, l.type, l.comments , l.date
FROM lists AS l
LEFT JOIN issue_encounter AS ie ON ie.list_id = l.id
LEFT JOIN form_encounter AS fe ON fe.encounter = ie.encounter
LEFT JOIN facility AS f ON fe.facility_id = f.id
LEFT JOIN users AS u ON fe.provider_id = u.id
WHERE l.type = 'medical_problem' AND l.pid=? AND l.diagnosis LIKE 'ICD10:%'
AND l.diagnosis NOT LIKE '%;%'";
$result = sqlStatement($sql, array($pid) );
}
return $result;
}
function getAlertData() {
global $pid,$set,$start,$end;
if($set == "on"){
$sql = "
select fe.reason, fe.provider_id, fe.facility_id, fe.encounter,
ie.list_id, l.pid, l.title as alert_title, l.outcome,
l.groupname, l.begdate, l.enddate, l.type, l.diagnosis, l.date ,
l.reaction , l.comments ,
f.street, f.city, f.state, u.title, u.fname, u.lname, cd.code_text
from lists as l
left join issue_encounter as ie
on ie.list_id = l.id
left join form_encounter as fe
on fe.encounter = ie.encounter
left join facility as f
on fe.facility_id = f.id
left join users as u
on fe.provider_id = u.id
left join codes as cd
on cd.code = SUBSTRING(l.diagnosis, LOCATE(':',l.diagnosis)+1)
where l.type = 'allergy' and l.pid=?
AND l.date BETWEEN ? AND ?";
$result = sqlStatement($sql, array($pid,$start,$end) );
}else{
$sql = "
select fe.reason, fe.provider_id, fe.facility_id, fe.encounter,
ie.list_id, l.pid, l.title as alert_title, l.outcome,
l.groupname, l.begdate, l.enddate, l.type, l.diagnosis, l.date ,
l.reaction , l.comments ,
f.street, f.city, f.state, u.title, u.fname, u.lname, cd.code_text
from lists as l
left join issue_encounter as ie
on ie.list_id = l.id
left join form_encounter as fe
on fe.encounter = ie.encounter
left join facility as f
on fe.facility_id = f.id
left join users as u
on fe.provider_id = u.id
left join codes as cd
on cd.code = SUBSTRING(l.diagnosis, LOCATE(':',l.diagnosis)+1)
where l.type = 'allergy' and l.pid=?";
$result = sqlStatement($sql, array($pid) );
}
return $result;
}
function getResultData() {
global $pid,$set,$start,$end;
if($set == "on"){
$sql = "
SELECT
prs.procedure_result_id as `pid`,
pt.name as `name`,
pt.procedure_type_id as `type`,
prs.date as `date`,
concat_ws(' ',prs.result,lo.title) as `result`,
prs.range as `range`,
prs.abnormal as `abnormal`,
prs.comments as `comments`,
ptt.lab_id AS `lab`
FROM
procedure_result AS prs
LEFT JOIN procedure_report AS prp
ON prs.procedure_report_id = prp.procedure_report_id
LEFT JOIN procedure_order AS po
ON prp.procedure_order_id = po.procedure_order_id
LEFT JOIN procedure_order_code AS poc
ON poc.procedure_order_id = po.procedure_order_id
AND poc.procedure_order_seq = prp.procedure_order_seq
LEFT JOIN procedure_type AS pt
ON pt.lab_id = po.lab_id
AND pt.procedure_code = prs.result_code
AND pt.procedure_type = 'res'
LEFT JOIN procedure_type AS ptt
ON pt.parent = ptt.procedure_type_id
AND ptt.procedure_type = 'ord'
LEFT JOIN list_options AS lo
ON lo.list_id = 'proc_unit' AND pt.units = lo.option_id
WHERE po.patient_id=?
AND prs.date BETWEEN ? AND ?";
$result = sqlStatement($sql, array($pid,$start,$end) );
}else{
$sql = "
SELECT
prs.procedure_result_id as `pid`,
pt.name as `name`,
pt.procedure_type_id as `type`,
prs.date as `date`,
concat_ws(' ',prs.result,lo.title) as `result`,
prs.range as `range`,
prs.abnormal as `abnormal`,
prs.comments as `comments`,
ptt.lab_id AS `lab`
FROM
procedure_result AS prs
LEFT JOIN procedure_report AS prp
ON prs.procedure_report_id = prp.procedure_report_id
LEFT JOIN procedure_order AS po
ON prp.procedure_order_id = po.procedure_order_id
LEFT JOIN procedure_order_code AS poc
ON poc.procedure_order_id = po.procedure_order_id
AND poc.procedure_order_seq = prp.procedure_order_seq
LEFT JOIN procedure_type AS pt
ON pt.lab_id = po.lab_id
AND pt.procedure_code = prs.result_code
AND pt.procedure_type = 'res'
LEFT JOIN procedure_type AS ptt
ON pt.parent = ptt.procedure_type_id
AND ptt.procedure_type = 'ord'
LEFT JOIN list_options AS lo
ON lo.list_id = 'proc_unit' AND pt.units = lo.option_id
WHERE po.patient_id=?";
$result = sqlStatement($sql, array($pid) );
}
return $result;
}
function getActorData() {
global $pid;
$sql = "
select fname, lname, DOB, sex, pid, street, city, state, postal_code, phone_contact
from patient_data
where pid=?";
$result[0] = sqlStatement($sql, array($pid) );
$sql2 = "
SELECT * FROM users AS u LEFT JOIN facility AS f ON u.facility_id = f.id WHERE u.id=?";
$result[1] = sqlStatement($sql2, array($_SESSION['authUserID']) );
$sql3 = "
SELECT
u.ppid AS id, u.name AS lname, '' AS fname, '' AS city, '' AS state, '' AS zip, '' AS phone
FROM
procedure_order AS po
LEFT JOIN forms AS f
ON f.form_id = po.procedure_order_id
AND f.formdir = 'procedure_order'
LEFT JOIN list_options AS lo
ON lo.title = f.form_name
LEFT JOIN procedure_providers AS u
ON po.lab_id = u.ppid
WHERE f.pid = ?
AND lo.list_id = 'proc_type'
AND lo.option_id = 'ord'
GROUP BY u.ppid";
$result[2] = sqlStatement($sql3, array($pid) );
return $result;
}
function getReportFilename() {
global $pid;
$sql = "
select fname, lname, pid
from patient_data
where pid=?";
$result = sqlQuery($sql, array($pid) );
$result_filename = $result['lname']."-".$result['fname']."-".$result['pid']."-".date("mdY",time());
return $result_filename;
}
?>