forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FhirAllergyIntoleranceService.php
223 lines (196 loc) · 8.3 KB
/
FhirAllergyIntoleranceService.php
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
<?php
namespace OpenEMR\Services\FHIR;
use OpenEMR\FHIR\R4\FHIRDomainResource\FHIRAllergyIntolerance;
use OpenEMR\FHIR\R4\FHIRElement\FHIRAllergyIntoleranceCategory;
use OpenEMR\FHIR\R4\FHIRElement\FHIRAllergyIntoleranceCriticality;
use OpenEMR\FHIR\R4\FHIRElement\FHIRAllergyIntoleranceType;
use OpenEMR\FHIR\R4\FHIRElement\FHIRAnnotation;
use OpenEMR\FHIR\R4\FHIRElement\FHIRCodeableConcept;
use OpenEMR\FHIR\R4\FHIRElement\FHIRCoding;
use OpenEMR\FHIR\R4\FHIRElement\FHIRDateTime;
use OpenEMR\FHIR\R4\FHIRElement\FHIRId;
use OpenEMR\FHIR\R4\FHIRElement\FHIRMeta;
use OpenEMR\FHIR\R4\FHIRElement\FHIRReference;
use OpenEMR\FHIR\R4\FHIRElement\FHIRUrl;
use OpenEMR\FHIR\R4\FHIRResource\FHIRAllergyIntolerance\FHIRAllergyIntoleranceReaction;
use OpenEMR\FHIR\R4\FHIRResource\FHIRBundle;
use OpenEMR\FHIR\R4\FHIRResource\FHIRBundle\FHIRBundleLink;
use OpenEMR\FHIR\R4\PHPFHIRResponseParser;
use OpenEMR\Services\ListService;
class FhirAllergyIntoleranceService
{
private $id;
public function __construct()
{
}
public function setId($id)
{
$this->id = $id;
}
public function getAll($search)
{
$SQL = "SELECT id,
date as recorded_date,
type,
subtype,
title,
begdate,
enddate,
returndate,
referredby,
extrainfo,
diagnosis,
pid,
outcome,
reaction,
severity_al
FROM lists
WHERE type = 'allergy'";
if (isset($search['patient'])) {
$SQL .= " AND pid = ?;";
}
$allergyIntolerenceresults = sqlStatement($SQL, $search['patient']);
$results = array();
while ($row = sqlFetchArray($allergyIntolerenceresults)) {
$codeSQL = "SELECT dx_code
FROM icd10_dx_order_code
WHERE short_desc = ?;";
$code = sqlQuery($codeSQL, array($row['reaction']));
$row['code'] = $code['dx_code'];
array_push($results, $row);
}
return $results;
}
public function getOne($id)
{
$SQL = "SELECT id,
date as recorded_date,
type,
subtype,
title,
begdate,
enddate,
returndate,
referredby,
extrainfo,
diagnosis,
pid,
outcome,
reaction,
severity_al
FROM lists
WHERE type = 'allergy'
AND id = ?;";
$sqlResult = sqlStatement($SQL, $id);
$result = sqlFetchArray($sqlResult);
$codeSQL = "SELECT dx_code
FROM icd10_dx_order_code
WHERE short_desc = ?;";
$code = sqlQuery($codeSQL, array($result['reaction']));
$result['code'] = $code['dx_code'];
return $result;
}
public function createAllergyIntoleranceResource($id = '', $data = '', $encode = true)
{
$typeCoding = new FHIRCoding();
$typeCoding->setSystem("http:https://terminology.hl7.org/CodeSystem/allergyintolerance-clinical");
$typeCoding->setCode($data['type']);
$typeCoding->setDisplay(ucwords($data['type']));
$type = new FHIRAllergyIntoleranceType();
$type->setValue($typeCoding);
$onSetDateTime = new FHIRDateTime();
$onSetDateTime->setValue($data['begdate']);
$recordedDate = new FHIRDateTime();
$recordedDate->setValue($data['recorded_date']);
$clinicalStatus = '';
if ($data['outcome'] == '1' && isset($data['enddate'])) {
$clinicalStatus = "resolved";
} elseif (!isset($data['enddate'])) {
$clinicalStatus = "active";
} else {
$clinicalStatus = "inactive";
}
$clinicalStatusCoding = new FHIRCoding();
$clinicalStatusCoding->setSystem("http:https://terminology.hl7.org/CodeSystem/allergyintolerance-clinical");
$clinicalStatusCoding->setCode($clinicalStatus);
$clinicalStatusCoding->setDisplay($clinicalStatus);
$categoryCoding = new FHIRCoding();
$categoryCoding->setSystem("http:https://hl7.org/fhir/allergy-intolerance-category");
$categoryCoding->setCode("medication");
$categoryCoding->setDisplay("Medication");
$category = new FHIRAllergyIntoleranceCategory();
$category->setValue($categoryCoding);
$criticalityCoding = new FHIRCoding();
$criticality = new FHIRAllergyIntoleranceCriticality();
if (isset($data['severity_al'])) {
$severityToCriticalityCode = array(
"mild" => "low",
"moderate" => "high",
"severe" => "unable-to-assess"
);
$severityToCriticalityDisplay = array(
"mild" => "Low Risk",
"moderate" => "High Risk",
"severe" => "Unable to Assess Risk"
);
$criticalityCoding->setSystem("http:https://hl7.org/fhir/allergy-intolerance-criticality");
$criticalityCoding->setCode($severityToCriticalityCode[strtolower($data['severity_al'])]);
$criticalityCoding->setDisplay($severityToCriticalityDisplay[strtolower($data['severity_al'])]);
$criticality->setValue($criticalityCoding);
} else {
$criticalityCoding->setSystem("http:https://terminology.hl7.org/CodeSystem/data-absent-reason");
$criticalityCoding->setCode("unknown");
$criticalityCoding->setDisplay("Unknown");
$criticality->setValue($criticalityCoding);
}
$patient = new FHIRReference();
$patient->setReference('Patient/' . $data['pid']);
$recorder = new FHIRReference();
$recorder->setReference('Practitioner/' . $data['referredby']);
$asserter = new FHIRReference();
$asserter->setReference('Patient/' . $data['pid']);
$note = new FHIRAnnotation();
$note->setText($data['extrainfo']);
$lastOccurrence = new FHIRDateTime();
$lastOccurrence->setValue($data['returndate']);
$manifestation = new FHIRCoding();
$manifestation->setSystem("http:https://hl7.org/fhir/sid/icd-10-cm");
$manifestation->setCode($data['code']);
$manifestation->setDisplay($data['reaction']);
$description = $data['comments'];
$reaction = new FHIRAllergyIntoleranceReaction();
$reaction->addManifestation($manifestation->jsonSerialize());
$reaction->setDescription($description);
$reaction->setOnset($data['begdate']);
$severityCoding = new FHIRCoding();
if (isset($data['severity_al'])) {
$severityCoding->setSystem("http:https://hl7.org/fhir/reaction-event-severity");
$severityCoding->setCode($data['severity_al']);
$severityCoding->setDisplay(ucwords($data['severity_al']));
$reaction->setSeverity($severityCoding);
} else {
$severityCoding->setSystem("http:https://terminology.hl7.org/CodeSystem/data-absent-reason");
$severityCoding->setCode("unknown");
$severityCoding->setDisplay("Unknown");
$reaction->setSeverity($severityCoding);
}
$resource = new FHIRAllergyIntolerance();
$resource->setClinicalStatus($clinicalStatusCoding);
$resource->setType($type);
$resource->addCategory($category);
$resource->setOnsetDateTime($onSetDateTime);
$resource->setRecordedDate($recordedDate);
$resource->setCriticality($criticality);
$resource->setPatient($patient);
$resource->setRecorder($recorder);
$resource->setAsserter($asserter);
$resource->setLastOccurrence($lastOccurrence);
$resource->addNote($note);
$resource->addReaction($reaction);
if ($encode) {
return json_encode($resource);
} else {
return $resource;
}
}
}