Revert "feat: Hook into the CDA Parsing functions (#6716)" (#6730)
[openemr.git] / src / Services / Cda / CdaTemplateParse.php
blobac040eba4002d32c6f91a9475f19510254fb878d
1 <?php
3 /**
4 * Qrda and Cda ParseService Class
6 * @package OpenEMR
7 * @link https://www.open-emr.org
8 * @author Jerry Padgett <sjpadgett@gmail.com>
9 * @copyright Copyright (c) 2021 Jerry Padgett <sjpadgett@gmail.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 namespace OpenEMR\Services\Cda;
15 use DOMDocument;
16 use OpenEMR\Services\CodeTypesService;
18 class CdaTemplateParse
20 private $templateData;
21 private $codeService;
22 private $currentOid;
23 protected $is_qrda_import;
25 public function __construct()
27 $this->templateData = [];
28 $this->is_qrda_import = false;
29 $this->codeService = new CodeTypesService();
32 public function parseCDAEntryComponents($components): array
35 $components_oids = array(
36 '2.16.840.1.113883.10.20.22.4.7' => 'allergy',
37 '2.16.840.1.113883.10.20.22.2.6.1' => 'allergy',
38 '2.16.840.1.113883.10.20.22.2.1' => 'medication',
39 '2.16.840.1.113883.10.20.22.2.1.1' => 'medication',
40 '2.16.840.1.113883.10.20.22.2.5.1' => 'medical_problem',
41 '2.16.840.1.113883.10.20.22.2.5' => 'medical_problem',
42 '2.16.840.1.113883.10.20.22.2.2' => 'immunization',
43 '2.16.840.1.113883.10.20.22.2.2.1' => 'immunization',
44 '2.16.840.1.113883.3.88.11.83.145' => 'procedure',
45 '2.16.840.1.113883.10.20.22.2.7.1' => 'procedure',
46 '2.16.840.1.113883.10.20.22.2.3.1' => 'labResult',
47 '2.16.840.1.113883.10.20.22.2.3' => 'labResult',
48 '2.16.840.1.113883.10.20.22.2.4.1' => 'VitalSign',
49 '2.16.840.1.113883.10.20.22.2.17' => 'socialHistory',
50 '2.16.840.1.113883.3.88.11.83.127' => 'encounter',
51 '2.16.840.1.113883.10.20.22.2.22.1' => 'encounter',
52 '2.16.840.1.113883.10.20.22.2.22' => 'encounter',
53 '2.16.840.1.113883.10.20.22.4.49' => 'encounter',
54 '2.16.840.1.113883.10.20.22.2.10' => 'carePlan',
55 '2.16.840.1.113883.10.20.22.2.60' => 'carePlan',
56 '2.16.840.1.113883.10.20.22.2.58' => 'carePlan',
57 '2.16.840.1.113883.10.20.22.2.14' => 'functionalCognitiveStatus',
58 '2.16.840.1.113883.10.20.22.2.56' => 'functionalCognitiveStatus',
59 '1.3.6.1.4.1.19376.1.5.3.1.3.1' => 'referral',
60 '2.16.840.1.113883.10.20.22.2.11.1' => 'dischargeMedications',
61 '2.16.840.1.113883.10.20.22.2.41' => 'dischargeSummary'
64 foreach ($components as $component) {
65 if (!empty($component['section']['templateId']['root'])) {
66 if (!empty($components_oids[$component['section']['templateId']['root']])) {
67 $this->currentOid = $component['section']['templateId']['root'];
68 $func_name = $components_oids[$component['section']['templateId']['root']];
69 $this->$func_name($component);
71 } elseif (empty($component['section']['templateId'])) {
72 // uncomment for debugging information.
73 error_log("section and template id empty for " . var_export($component, true));
74 } elseif (count($component['section']['templateId']) > 1) {
75 $this->currentOid = '';
76 foreach ($component['section']['templateId'] as $key_1 => $value_1) {
77 if (!empty($components_oids[$component['section']['templateId'][$key_1]['root']])) {
78 $this->currentOid = $component['section']['templateId'][$key_1]['root'];
79 $func_name = $components_oids[$component['section']['templateId'][$key_1]['root']];
80 $this->$func_name($component);
81 break;
86 return $this->templateData;
89 /**
90 * parsePatientDataSection
92 * @param $entryComponents //An array of QRDA entry templates
93 * @return array
95 public function parseQRDAPatientDataSection($entryComponents): array
97 $this->is_qrda_import = true;
98 $qrda_oids = array(
99 '2.16.840.1.113883.10.20.24.3.147' => 'fetchAllergyIntoleranceObservation',
100 '2.16.840.1.113883.10.20.24.3.41' => 'fetchMedicationData', // active medication @todo verify status all meds
101 '2.16.840.1.113883.10.20.24.3.42' => 'fetchMedicationData', // Medication Administered Act @todo honor end dates
102 '2.16.840.1.113883.10.20.24.3.139' => 'fetchMedicationData', // Medication Dispensed Act @todo set med type
103 '2.16.840.1.113883.10.20.24.3.105' => 'fetchMedicationData', // Medication Discharge Act
104 '2.16.840.1.113883.10.20.24.3.137' => 'fetchMedicalProblemData',// diagnosis
105 '2.16.840.1.113883.10.20.24.3.138' => 'fetchMedicalProblemData',// concern symtom
106 '2.16.840.1.113883.10.20.24.3.140' => 'fetchImmunizationData', // Immunization Administered (V3)
107 '2.16.840.1.113883.10.20.22.4.14' => 'fetchProcedureActivityData', // procedure activity-performed 2.16.840.1.113883.10.20.24.3.64
108 '2.16.840.1.113883.10.20.24.3.7' => 'fetchProcedureDeviceData', // procedure preformed Device Applied
109 '2.16.840.1.113883.10.20.24.3.32' => 'fetchProcedurePreformedActivity',// procedure activity-intervention
110 '2.16.840.1.113883.10.20.24.3.38' => 'fetchQrdaLabResultData', // lab test preformed
111 '2.16.840.1.113883.10.20.24.3.133' => 'fetchEncounterPerformed',
112 '2.16.840.1.113883.10.20.24.3.143' => 'fetchCarePlanData', // Immunization Order Substance Order @todo this is planned or goal MOVE
113 '2.16.840.1.113883.10.20.24.3.47' => 'fetchCarePlanData', // Plan of Care Medication Substance Observation Activity Ordered
114 '2.16.840.1.113883.10.20.24.3.130' => 'fetchCarePlanData', // Plan of Care Activity Supply CDA 2.16.840.1.113883.10.20.24.3.43
115 '2.16.840.1.113883.10.20.24.3.31' => 'fetchCarePlanData', // Plan of Care Activity (act) Intervention Order
116 '2.16.840.1.113883.10.20.24.3.37' => 'fetchCarePlanData', // Plan of Care Activity Observation Lab order
117 '2.16.840.1.113883.10.20.24.3.17' => 'fetchCarePlanData', // Plan of Care Activity Observation Diagnostic Study, Order
119 * CCDA 2.16.840.1.113883.10.20.22.4.13 - Procedure Activity Observation.
120 * QRDA 2.16.840.1.113883.10.20.24.3.18 Diagnostic Study, Performed,
121 * QRDA 2.16.840.1.113883.10.20.24.3.59 Physical Exam, Performed
122 * QRDA '2.16.840.1.113883.10.20.24.3.54' Deceased Observation (V3) is handled in patient data parse.
123 * */
124 '2.16.840.1.113883.10.20.24.3.59' => 'fetchPhysicalExamPerformedData', // Physical Exam, Performed observation Vitals
125 '2.16.840.1.113883.10.20.24.3.18' => 'fetchObservationPerformedData', //
126 '2.16.840.1.113883.10.20.24.3.144' => 'fetchObservationPerformedData', // Assessment Performed
127 '2.16.840.1.113883.10.20.24.3.54' => 'fetchDeceasedObservationData', // Deceased Observation (V3)
128 '2.16.840.1.113883.10.20.24.3.55' => 'fetchPaymentSourceData', // Patient Characteristic Payer
130 foreach ($entryComponents['section']['entry'] as $entry) {
131 $key = array_keys($entry)[0]; // need the entry template type i.e. observation, activity, substance etc.
132 if (!empty($entry[$key]['templateId']['root'])) {
133 if (!empty($qrda_oids[$entry[$key]['templateId']['root']])) {
134 $this->currentOid = $entry[$key]['templateId']['root'];
135 $func_name = $qrda_oids[$entry[$key]['templateId']['root']] ?? null;
136 if (!empty($func_name)) {
137 $this->$func_name($entry);
139 } else {
140 $text = $entry[$key]['templateId']['root'] . ' ' . ($entry[$key]['text'] ?: $entry[$key]['code']['displayName']);
141 error_log('Root Missing QDM: ' . $text);
143 } elseif (count($entry[$key]['templateId'] ?? []) > 1) {
144 $key_1 = 1;
145 if (!empty($qrda_oids[$entry[$key]['templateId'][$key_1]['root']])) {
146 $this->currentOid = $entry[$key]['templateId'][$key_1]['root'];
147 $func_name = $qrda_oids[$entry[$key]['templateId'][$key_1]['root']] ?? null;
148 if (!empty($func_name)) {
149 $this->$func_name($entry);
150 } else {
151 $text = $entry[$key]['templateId'][$key_1]['root'] . " Key: $key_1 " . ($entry[$key]['text'] ?: $entry[$key]['code']['displayName']);
152 error_log('Missing Function for: ' . $text);
154 } elseif (count($entry[$key]['templateId'] ?? []) > 1) {
155 $key_1 = 0;
156 if (!empty($qrda_oids[$entry[$key]['templateId'][$key_1]['root']])) {
157 $this->currentOid = $entry[$key]['templateId'][$key_1]['root'];
158 $func_name = $qrda_oids[$entry[$key]['templateId'][$key_1]['root']] ?? null;
159 if (!empty($func_name)) {
160 $this->$func_name($entry);
161 } else {
162 $text = $entry[$key]['templateId'][$key_1]['root'] . " Key: $key_1 " . ($entry[$key]['text'] ?: $entry[$key]['code']['displayName']);
163 error_log('Missing Function for: ' . $text);
165 } else {
166 $text = $entry[$key]['templateId'][$key_1]['root'] . " Key: $key_1 " . ($entry[$key]['text'] ?: $entry[$key]['code']['displayName']);
167 error_log('Missing QDM: ' . $text);
169 } else {
170 $text = $entry[$key]['templateId'][$key_1]['root'] . ' ' . ($entry[$key]['text'] ?: $entry[$key]['code']['displayName']);
171 error_log('Missing QDM: ' . $text);
173 } elseif (!empty($entry[$key]['root'])) {
174 if (!empty($qrda_oids[$entry[$key]['root']])) {
175 $this->currentOid = $entry[$key]['root'];
176 $func_name = $qrda_oids[$entry[$key]['root']] ?? null;
177 if (!empty($func_name)) {
178 $this->$func_name($entry);
180 } else {
181 $text = $entry[$key]['root'] . ' ' . ($entry[$key]['text'] ?: $entry[$key]['code']['displayName']);
182 error_log('Root Missing QDM: ' . $text);
186 if (empty($this->templateData)) {
187 error_log('Could not find any QDMs in document!');
189 return $this->templateData ?? [];
192 public function parseUnstructuredComponents($xml): array
194 $components = $xml['component'];
195 $uuid = $xml['recordTarget']['patientRole']['id']['extension'] ?? null;
196 if (!empty($components[0])) {
197 foreach ($components as $component) {
198 $item = $component['nonXMLBody']['text'];
199 $this->fetchFileForImport($item, $uuid);
201 } else {
202 $item = $components['nonXMLBody']['text'];
203 $this->fetchFileForImport($item, $uuid);
206 return $this->templateData ?? [];
209 public function fetchFileForImport($component, $uuid): void
211 $i = 1;
212 if (!empty($this->templateData['field_name_value_array']['import_file'])) {
213 $i += count($this->templateData['field_name_value_array']['import_file']);
215 $this->templateData['field_name_value_array']['import_file'][$i]['uuid'] = $uuid;
216 $this->templateData['field_name_value_array']['import_file'][$i]['hash'] = $component['hash'] ?? '';
217 $this->templateData['field_name_value_array']['import_file'][$i]['mediaType'] = $component['mediaType'] ?? '';
218 $this->templateData['field_name_value_array']['import_file'][$i]['category'] = $component['category'] ?? '';
219 $this->templateData['field_name_value_array']['import_file'][$i]['file_name'] = $component['name'] ?? '';
220 $this->templateData['field_name_value_array']['import_file'][$i]['compression'] = $component['compression'] ?? '';
221 $this->templateData['field_name_value_array']['import_file'][$i]['content'] = $component['_'] ?? '';
223 $this->templateData['entry_identification_array']['import_file'][$i] = $i;
226 public function fetchDeceasedObservationData($entry)
228 // handled in patient data parse.
229 // leave this function to prevent parse errors.
232 public function fetchPaymentSourceData($entry)
234 if (!empty($entry['observation']['effectiveTime']['low']['value'] ?? null)) {
235 $i = 1;
236 if (!empty($this->templateData['field_name_value_array']['payer'])) {
237 $i += count($this->templateData['field_name_value_array']['payer']);
239 $this->templateData['field_name_value_array']['payer'][$i]['status'] = $entry['observation']['statusCode']['code'] ?? null;
240 $this->templateData['field_name_value_array']['payer'][$i]['code'] = $entry['observation']['value']['code'] ?? null;
241 $this->templateData['field_name_value_array']['payer'][$i]['low_date'] = $entry['observation']['effectiveTime']['low']['value'] ?? null;
242 $this->templateData['field_name_value_array']['payer'][$i]['high_date'] = $entry['observation']['effectiveTime']['high']['value'] ?? null;
244 $this->templateData['entry_identification_array']['payer'][$i] = $i;
249 * @param $entry
251 public function fetchObservationPerformedData($entry): void
253 // was also called from fetchPhysicalExamPerformedData()
254 if (
255 !empty($entry['observation']['value']['code'] ?? null)
256 || !empty($entry['observation']['code']['code'] ?? null)
257 || (!empty($entry['observation']['value']['nullFlavor'] ?? null) && empty($entry['observation']['value']['code'] ?? null))
258 || (!empty($entry['observation']['entryRelationship']['observation']['value']['code'] ?? null) && ($entry['observation']['entryRelationship']['typeCode'] ?? null) === 'RSON')
260 $i = 1;
261 if (!empty($this->templateData['field_name_value_array']['observation_preformed'])) {
262 $i += count($this->templateData['field_name_value_array']['observation_preformed']);
264 $is_negated = !empty($entry['observation']['negationInd'] ?? false);
265 $ob_type = 'assessment'; // default and 2.16.840.1.113883.10.20.24.3.144
266 if ($this->currentOid === '2.16.840.1.113883.10.20.24.3.18') {
267 $ob_type = 'procedure_diagnostic';
268 } elseif ($this->currentOid === '2.16.840.1.113883.10.20.24.3.59') {
269 $ob_type = 'physical_exam_performed';
272 $ob_code = $entry['observation']['code']['code'];
273 $ob_system = $entry['observation']['code']['codeSystemName'] ?: ($entry['observation']['code']['codeSystem'] ?? '');
274 $ob_code_text = $entry['observation']['code']['displayName'] ?? '';
275 if (($entry['observation']['code']['codeSystemName'] ?? null) == 'CPT4' && !empty($entry['observation']['code']['translation']['code'])) {
276 $ob_code = $entry['observation']['code']['translation']['code'];
277 $ob_system = $entry['observation']['code']['translation']['codeSystemName'] ?: $entry['observation']['value']['codeSystem'] ?? '';
278 $ob_code_text = $entry['observation']['code']['translation']['displayName'] ?? '';
280 $code = $this->codeService->resolveCode($ob_code, $ob_system, $ob_code_text);
282 $result_code = [];
283 $result_status = $entry['observation']['statusCode']['code'] ?? '';
284 $reason_code = [];
285 $reason_status = '';
286 if (!empty($entry['observation']['value']['nullFlavor'])) {
287 $result_code['formatted_code'] = null;
288 $result_code['code_text'] = null;
289 $result_status = $entry['observation']['statusCode']['code'] ?? '';
290 } elseif (($entry['observation']['value']['type'] ?? '') === 'CD') {
291 $ob_code = $entry['observation']['value']['code'];
292 $ob_system = $entry['observation']['value']['codeSystemName'] ?: $entry['observation']['value']['codeSystem'] ?? '';
293 $ob_code_text = $entry['observation']['value']['displayName'] ?? '';
294 $result_code = $this->codeService->resolveCode($ob_code, $ob_system, $ob_code_text);
295 $result_status = $entry['observation']['statusCode']['code'] ?? '';
296 } elseif (($entry['observation']['value']['type'] ?? '') === 'PQ') {
297 $result_code['formatted_code'] = 'PQ';
298 $result_code['code_text'] = $entry['observation']['value']['value'] ?? '';
299 $result_unit = $entry['observation']['value']['unit'] ?? '';
300 $result_status = $entry['observation']['statusCode']['code'] ?? '';
302 if (!empty($entry['observation']['entryRelationship']['observation']['value']['code'] ?? null)) {
303 // @todo inter to this moodcode RSON in full template!
304 $ob_code = $entry['observation']['entryRelationship']['observation']['value']['code'];
305 $ob_system = $entry['observation']['entryRelationship']['observation']['value']['codeSystemName'] ?: $entry['observation']['entryRelationship']['observation']['value']['codeSystem'] ?? '';
306 $ob_code_text = $entry['observation']['entryRelationship']['observation']['value']['displayName'] ?? '';
307 $reason_code = $this->codeService->resolveCode($ob_code, $ob_system, $ob_code_text);
308 $reason_status = $entry['observation']['entryRelationship']['observation']['statusCode']['code'] ?? '';
311 $this->templateData['field_name_value_array']['observation_preformed'][$i]['observation_type'] = $ob_type;
312 $this->templateData['field_name_value_array']['observation_preformed'][$i]['extension'] = $entry['observation']['id']['extension'] ?? '';
313 $this->templateData['field_name_value_array']['observation_preformed'][$i]['root'] = $entry['observation']['id']['root'] ?? '';
314 $this->templateData['field_name_value_array']['observation_preformed'][$i]['date'] = $entry['observation']['effectiveTime']['value'] ?? '';
315 if (!empty($entry['observation']['effectiveTime']['low'])) {
316 $this->templateData['field_name_value_array']['observation_preformed'][$i]['date'] = $entry['observation']['effectiveTime']['low'];
317 $this->templateData['field_name_value_array']['observation_preformed'][$i]['date_end'] = $entry['observation']['effectiveTime']['high'] ?? null;
320 if (!empty($entry['observation']['code']["nullFlavor"]) && !empty($entry['observation']['code']["valueSet"])) {
321 $code['code'] = $entry['observation']['code']["valueSet"] ?? null;
322 $code['formatted_code'] = 'OID:' . $entry['observation']['code']["valueSet"] ?? null;
323 $code['code_text'] = $entry['observation']['text'] ?? '';
325 $this->templateData['field_name_value_array']['observation_preformed'][$i]['observation_status'] = $result_status ?? '';
326 $this->templateData['field_name_value_array']['observation_preformed'][$i]['observation'] = $entry['observation']['text'] ?: $code['code_text'] ?? null;
327 $this->templateData['field_name_value_array']['observation_preformed'][$i]['code'] = $code['formatted_code'] ?? null;
328 $this->templateData['field_name_value_array']['observation_preformed'][$i]['code_text'] = $code['code_text'] ?? '';
330 $this->templateData['field_name_value_array']['observation_preformed'][$i]['result_status'] = $result_status ?? '';
331 $this->templateData['field_name_value_array']['observation_preformed'][$i]['result_code'] = $result_code['formatted_code'] ?? '';
332 $this->templateData['field_name_value_array']['observation_preformed'][$i]['result_code_text'] = $result_code['code_text'] ?? '';
333 $this->templateData['field_name_value_array']['observation_preformed'][$i]['result_code_unit'] = $result_unit ?? '';
335 $this->templateData['field_name_value_array']['observation_preformed'][$i]['reason_status'] = $is_negated ? 'negated' : ($reason_status ?? '');
336 $this->templateData['field_name_value_array']['observation_preformed'][$i]['reason_code'] = $reason_code['formatted_code'] ?? '';
337 $this->templateData['field_name_value_array']['observation_preformed'][$i]['reason_code_text'] = $reason_code['code_text'] ?? '';
339 $this->templateData['entry_identification_array']['observation_preformed'][$i] = $i;
344 * @param $entry
346 public function fetchEncounterPerformed($entry): void
348 if ($this->is_qrda_import) {
349 $entry = $entry['act']['entryRelationship'];
351 if (!empty($entry['encounter']['effectiveTime']['value']) || !empty($entry['encounter']['effectiveTime']['low']['value'])) {
352 $i = 1;
353 if (!empty($this->templateData['field_name_value_array']['encounter'])) {
354 $i += count($this->templateData['field_name_value_array']['encounter']);
357 $this->templateData['field_name_value_array']['encounter'][$i]['extension'] = $entry['encounter']['id']['extension'];
358 $this->templateData['field_name_value_array']['encounter'][$i]['root'] = $entry['encounter']['id']['root'];
359 $this->templateData['field_name_value_array']['encounter'][$i]['date'] = ($entry['encounter']['effectiveTime']['value'] ?? null) ?: $entry['encounter']['effectiveTime']['low']['value'] ?? null;
360 $this->templateData['field_name_value_array']['encounter'][$i]['date_end'] = $entry['encounter']['effectiveTime']['high']['value'] ?? null;
362 $code_type = $entry['encounter']['code']['codeSystemName'] ?: $entry['encounter']['code']['codeSystem'] ?? '';
363 $code_text = $entry['encounter']['code']['displayName'] ?? '';
364 $code = $this->codeService->resolveCode($entry['encounter']['code']['code'], $code_type, $code_text);
365 $this->templateData['field_name_value_array']['encounter'][$i]['code'] = $code['formatted_code'];
366 $this->templateData['field_name_value_array']['encounter'][$i]['code_text'] = $code['code_text'];
368 $this->templateData['field_name_value_array']['encounter'][$i]['provider_npi'] = $entry['encounter']['performer']['assignedEntity']['id']['extension'] ?? null;
369 $this->templateData['field_name_value_array']['encounter'][$i]['provider_name'] = $entry['encounter']['performer']['assignedEntity']['assignedPerson']['name']['given'] ?? ''; // first
370 $this->templateData['field_name_value_array']['encounter'][$i]['provider_family'] = $entry['encounter']['performer']['assignedEntity']['assignedPerson']['name']['family'] ?? ''; // last
371 $this->templateData['field_name_value_array']['encounter'][$i]['provider_address'] = $entry['encounter']['performer']['assignedEntity']['addr']['streetAddressLine'] ?? '';
372 $this->templateData['field_name_value_array']['encounter'][$i]['provider_city'] = $entry['encounter']['performer']['assignedEntity']['addr']['city'] ?? '';
373 $this->templateData['field_name_value_array']['encounter'][$i]['provider_state'] = $entry['encounter']['performer']['assignedEntity']['addr']['state'] ?? '';
374 $this->templateData['field_name_value_array']['encounter'][$i]['provider_postalCode'] = $entry['encounter']['performer']['assignedEntity']['addr']['postalCode'] ?? '';
375 $this->templateData['field_name_value_array']['encounter'][$i]['provider_country'] = $entry['encounter']['performer']['assignedEntity']['addr']['country'] ?? '';
376 $this->templateData['field_name_value_array']['encounter'][$i]['represented_organization_name'] = $entry['encounter']['participant']['participantRole']['playingEntity']['name'] ?? '';
377 $this->templateData['field_name_value_array']['encounter'][$i]['represented_organization_address'] = $entry['encounter']['participant']['participantRole']['addr']['streetAddressLine'] ?? '';
378 $this->templateData['field_name_value_array']['encounter'][$i]['represented_organization_city'] = $entry['encounter']['participant']['participantRole']['addr']['city'] ?? '';
379 $this->templateData['field_name_value_array']['encounter'][$i]['represented_organization_state'] = $entry['encounter']['participant']['participantRole']['addr']['state'] ?? '';
380 $this->templateData['field_name_value_array']['encounter'][$i]['represented_organization_zip'] = $entry['encounter']['participant']['participantRole']['addr']['postalCode'] ?? '';
381 $this->templateData['field_name_value_array']['encounter'][$i]['represented_organization_country'] = $entry['encounter']['participant']['participantRole']['addr']['country'] ?? '';
382 $this->templateData['field_name_value_array']['encounter'][$i]['represented_organization_telecom'] = $entry['encounter']['participant']['participantRole']['telecom'] ?? '';
383 // encounter diagnosis to issues list
384 if (count($entry['encounter']['entryRelationship'] ?? []) > 1 && !empty($entry['encounter']['entryRelationship'][0]['act'])) {
385 foreach ($entry['encounter']['entryRelationship'] as $k => $act) {
386 $code = $this->codeService->resolveCode(
387 $act['act']['entryRelationship']['observation']['value']['code'] ?? '',
388 ($act['act']['entryRelationship']['observation']['value']['codeSystemName'] ?? '') ?: $act['act']['entryRelationship']['observation']['value']['codeSystem'] ?? '',
389 $act['act']['entryRelationship']['observation']['value']['displayName'] ?? ''
391 $this->templateData['field_name_value_array']['encounter'][$i]['encounter_diagnosis_code'][] = $code['formatted_code'];
392 $this->templateData['field_name_value_array']['encounter'][$i]['encounter_diagnosis_issue'][] = $code['code_text'];
393 $this->templateData['field_name_value_array']['encounter'][$i]['encounter_diagnosis_date'][] = $act['act']['entryRelationship']['observation']['effectiveTime']['low']['value'] ?? $this->templateData['field_name_value_array']['encounter'][$i]['date'] ?? null;
395 } else {
396 $code = $this->codeService->resolveCode(
397 $entry['encounter']['entryRelationship']['act']['entryRelationship']['observation']['value']['code'] ?? '',
398 ($entry['encounter']['entryRelationship']['act']['entryRelationship']['observation']['value']['codeSystemName'] ?? '') ?: $entry['encounter']['entryRelationship']['act']['entryRelationship']['observation']['value']['codeSystem'] ?? '',
399 $entry['encounter']['entryRelationship']['act']['entryRelationship']['observation']['value']['displayName'] ?? ''
401 $this->templateData['field_name_value_array']['encounter'][$i]['encounter_diagnosis_date'] = $entry['encounter']['entryRelationship']['act']['entryRelationship']['observation']['effectiveTime']['low']['value'] ?? $this->templateData['field_name_value_array']['encounter'][$i]['date'] ?? null;
402 if (empty($code['code'])) {
403 $code = $this->codeService->resolveCode(
404 $entry["encounter"]["entryRelationship"]["observation"]["value"]["code"] ?? '',
405 ($entry["encounter"]["entryRelationship"]["observation"]["value"]['codeSystemName'] ?? '') ?: $entry["encounter"]["entryRelationship"]["observation"]["value"]['codeSystem'] ?? '',
406 $entry["encounter"]["entryRelationship"]["observation"]["value"]['displayName'] ?? ''
408 $this->templateData['field_name_value_array']['encounter'][$i]['encounter_diagnosis_date'] = $this->templateData['field_name_value_array']['encounter'][$i]['date'] ?? null;
410 $this->templateData['field_name_value_array']['encounter'][$i]['encounter_diagnosis_code'] = $code['formatted_code'];
411 $this->templateData['field_name_value_array']['encounter'][$i]['encounter_diagnosis_issue'] = $code['code_text'];
414 $discharge = $entry['encounter']['sdtc:dischargeDispositionCode'] ?? null;
415 $code = '';
416 if (!empty($discharge)) {
417 $code = $this->codeService->getCodeWithType(($discharge['code'] ?? ''), ($discharge['codeSystemName'] ?? ''), true) ?? '';
418 $option = $this->codeService->dischargeOptionIdFromCode($code) ?? '';
419 if (empty($option)) {
420 $code = str_replace(" ", "-", $code); // Because "SNOMED CT" is "SNOMED-CT" in list options
421 $option = $this->codeService->dischargeOptionIdFromCode($code) ?? '';
424 $this->templateData['field_name_value_array']['encounter'][$i]['encounter_discharge_code'] = $option ?? '';
426 $this->templateData['entry_identification_array']['encounter'][$i] = $i;
431 * @param $entry
433 public function fetchMedicalProblemData($entry): void
435 if (!empty($entry['act']['entryRelationship']['observation']['value']['code'])) {
436 $i = 1;
437 if (!empty($this->templateData['field_name_value_array']['lists1'])) {
438 $i += count($this->templateData['field_name_value_array']['lists1']);
440 $classification = 'diagnosis';
441 if ($this->currentOid == '2.16.840.1.113883.10.20.24.3.138') {
442 $classification = 'concern';
444 $this->templateData['field_name_value_array']['lists1'][$i]['subtype'] = $classification;
445 $code = $this->codeService->resolveCode(
446 $entry['act']['entryRelationship']['observation']['value']['code'],
447 ($entry['act']['entryRelationship']['observation']['value']['codeSystemName'] ?? '') ?: $entry['act']['entryRelationship']['observation']['value']['codeSystem'] ?? '',
448 $entry['act']['entryRelationship']['observation']['value']['displayName']
450 $this->templateData['field_name_value_array']['lists1'][$i]['list_code'] = $code['formatted_code'] ?: $entry['act']['entryRelationship']['observation']['value']['code'] ?? '';
451 $this->templateData['field_name_value_array']['lists1'][$i]['list_code_text'] = $code['code_text'] ?: $entry['act']['entryRelationship']['observation']['value']['displayName'] ?? '';
453 $this->templateData['field_name_value_array']['lists1'][$i]['type'] = 'medical_problem';
454 $this->templateData['field_name_value_array']['lists1'][$i]['extension'] = $entry['act']['id']['extension'] ?? null;
455 $this->templateData['field_name_value_array']['lists1'][$i]['root'] = $entry['act']['id']['root'] ?? null;
456 $this->templateData['field_name_value_array']['lists1'][$i]['begdate'] = $entry['act']['effectiveTime']['low']['value'] ?? null;
457 $this->templateData['field_name_value_array']['lists1'][$i]['enddate'] = $entry['act']['effectiveTime']['high']['value'] ?? null;
458 $this->templateData['field_name_value_array']['lists1'][$i]['observation'] = $entry['act']['entryRelationship']['observation']['entryRelationship'][1]['observation']['value']['code'] ?? null;
459 $this->templateData['field_name_value_array']['lists1'][$i]['observation_text'] = $entry['act']['entryRelationship']['observation']['entryRelationship'][1]['observation']['value']['displayName'] ?? null;
460 $this->templateData['field_name_value_array']['lists1'][$i]['status'] = ($entry['act']['entryRelationship']['observation']['entryRelationship'][2]['observation']['value']['displayName'] ?? '') ?: $entry['act']['entryRelationship']['observation']['statusCode'] ?? null;
461 $this->templateData['field_name_value_array']['lists1'][$i]['modified_time'] = $entry['act']['entryRelationship']['observation']['performer']['assignedEntity']['time']['value'] ?? null;
462 $this->templateData['entry_identification_array']['lists1'][$i] = $i;
467 * @param $entry
469 public function fetchAllergyIntoleranceObservation($entry)
471 if (!empty($entry['act']['entryRelationship']['observation']['participant']['participantRole']['playingEntity']['code']['code'])) {
472 $i = 1;
473 // if there are already items here we want to add to them.
474 if (!empty($this->templateData['field_name_value_array']['lists2'])) {
475 $i += count($this->templateData['field_name_value_array']['lists2']);
478 $this->templateData['field_name_value_array']['lists2'][$i]['type'] = 'allergy';
479 $this->templateData['field_name_value_array']['lists2'][$i]['extension'] = $entry['act']['id']['extension'];
480 $this->templateData['field_name_value_array']['lists2'][$i]['begdate'] = $entry['act']['effectiveTime']['low']['value'] ?? null;
481 $this->templateData['field_name_value_array']['lists2'][$i]['enddate'] = $entry['act']['effectiveTime']['high']['value'] ?? null;
482 $this->templateData['field_name_value_array']['lists2'][$i]['list_code'] = $entry['act']['entryRelationship']['observation']['participant']['participantRole']['playingEntity']['code']['code'] ?? '';
483 $this->templateData['field_name_value_array']['lists2'][$i]['list_code_text'] = $entry['act']['entryRelationship']['observation']['participant']['participantRole']['playingEntity']['code']['displayName'];
484 $this->templateData['field_name_value_array']['lists2'][$i]['codeSystemName'] = $entry['act']['entryRelationship']['observation']['participant']['participantRole']['playingEntity']['code']['codeSystemName'];
485 $this->templateData['field_name_value_array']['lists2'][$i]['outcome'] = $entry['act']['entryRelationship']['observation']['entryRelationship'][1]['observation']['value']['code'] ?? '';
486 $this->templateData['field_name_value_array']['lists2'][$i]['severity_al_code'] = $entry['act']['entryRelationship']['observation']['entryRelationship'][2]['observation']['value']['code'] ?? '';
487 $this->templateData['field_name_value_array']['lists2'][$i]['severity_al'] = $entry['act']['entryRelationship']['observation']['entryRelationship'][2]['observation']['value']['code'] ?? '';
488 $this->templateData['field_name_value_array']['lists2'][$i]['status'] = $entry['act']['entryRelationship']['observation']['entryRelationship'][0]['observation']['value']['displayName'];
489 $this->templateData['field_name_value_array']['lists2'][$i]['reaction'] = $entry['act']['entryRelationship']['observation']['entryRelationship'][1]['observation']['value']['code'] ?? '';
490 $this->templateData['field_name_value_array']['lists2'][$i]['reaction_text'] = $entry['act']['entryRelationship']['observation']['entryRelationship'][1]['observation']['value']['displayName'];
491 $this->templateData['field_name_value_array']['lists2'][$i]['modified_time'] = $entry['act']['entryRelationship']['observation']['performer']['assignedEntity']['time']['value'] ?? null;
492 $this->templateData['entry_identification_array']['lists2'][$i] = $i;
493 } elseif (!empty($entry['observation']['participant']['participantRole']['playingEntity']['code']['code'])) {
494 $i = 1;
495 // if there are already items here we want to add to them.
496 if (!empty($this->templateData['field_name_value_array']['lists2'])) {
497 $i += count($this->templateData['field_name_value_array']['lists2']);
500 $this->templateData['field_name_value_array']['lists2'][$i]['type'] = 'allergy';
501 $this->templateData['field_name_value_array']['lists2'][$i]['extension'] = $entry['observation']['id']['extension'] ?? null;
502 $this->templateData['field_name_value_array']['lists2'][$i]['begdate'] = $entry['observation']['effectiveTime']['low']['value'] ?? null;
503 $this->templateData['field_name_value_array']['lists2'][$i]['enddate'] = $entry['observation']['effectiveTime']['high']['value'] ?? null;
504 $this->templateData['field_name_value_array']['lists2'][$i]['status'] = $entry['observation']['statusCode']['code'] ?? null;
506 $code = $this->codeService->resolveCode(
507 $entry['observation']['participant']['participantRole']['playingEntity']['code']['code'] ?? null,
508 $entry['observation']['participant']['participantRole']['playingEntity']['code']['codeSystemName'] ?? null,
509 $entry['observation']['participant']['participantRole']['playingEntity']['code']['displayName'] ?: $entry['observation']['participant']['participantRole']['playingEntity']['name'] ?? null
511 $this->templateData['field_name_value_array']['lists2'][$i]['list_code'] = $code['formatted_code'];
512 $this->templateData['field_name_value_array']['lists2'][$i]['list_code_text'] = $code['code_text'];
513 $this->templateData['field_name_value_array']['lists2'][$i]['codeSystemName'] = $code['formatted_code_type'];
515 $this->templateData['field_name_value_array']['lists2'][$i]['outcome'] = $entry['observation']['entryRelationship'][1]['observation']['value']['code'] ?? null;
516 $this->templateData['field_name_value_array']['lists2'][$i]['severity_al_code'] = $entry['observation']['entryRelationship'][2]['observation']['value']['code'] ?? null;
517 $this->templateData['field_name_value_array']['lists2'][$i]['severity_al'] = $entry['observation']['entryRelationship'][2]['observation']['value']['code'] ?? null;
518 $this->templateData['field_name_value_array']['lists2'][$i]['status'] = $entry['observation']['entryRelationship'][0]['observation']['value']['displayName'] ?? null;
519 $this->templateData['field_name_value_array']['lists2'][$i]['reaction'] = $entry['observation']['entryRelationship'][1]['observation']['value']['code'] ?? null;
520 $this->templateData['field_name_value_array']['lists2'][$i]['reaction_text'] = $entry['observation']['entryRelationship'][1]['observation']['value']['displayName'] ?? null;
521 $this->templateData['field_name_value_array']['lists2'][$i]['modified_time'] = $entry['observation']['performer']['assignedEntity']['time']['value'] ?? null;
522 $this->templateData['entry_identification_array']['lists2'][$i] = $i;
527 * @param $entry
529 public function fetchMedicationData($entry): void
531 // @todo provider not correct.
532 if (!empty($entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['code'])) {
533 $i = 1;
534 if (!empty($this->templateData['field_name_value_array']['lists3'])) {
535 $i += count($this->templateData['field_name_value_array']['lists3']);
538 $substanceAdministration_oids = array(
539 '2.16.840.1.113883.10.20.24.3.41' => 'active',
540 '2.16.840.1.113883.10.20.24.3.42' => 'administered',
541 '2.16.840.1.113883.10.20.24.3.139' => 'dispensed',
542 '2.16.840.1.113883.10.20.24.3.105' => 'discharge',
544 $request_type = '';
545 if ($this->is_qrda_import) {
546 if (!empty($entry['substanceAdministration']['templateId']['root'])) {
547 $request_type = $substanceAdministration_oids[$entry['substanceAdministration']['templateId']['root']];
548 } elseif (!empty($entry['substanceAdministration']['templateId'][1]['root'])) {
549 $request_type = $substanceAdministration_oids[$entry['substanceAdministration']['templateId'][1]['root']];
553 $code = $this->codeService->resolveCode(
554 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['code'],
555 'RXNORM',
556 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['displayName']
559 $this->templateData['field_name_value_array']['lists3'][$i]['type'] = 'medication';
560 $this->templateData['field_name_value_array']['lists3'][$i]['request_type'] = $request_type;
561 $this->templateData['field_name_value_array']['lists3'][$i]['extension'] = $entry['substanceAdministration']['id']['extension'] ?? null;
562 $this->templateData['field_name_value_array']['lists3'][$i]['root'] = $entry['substanceAdministration']['id']['root'] ?? null;
564 $this->templateData['field_name_value_array']['lists3'][$i]['begdate'] = date('Y-m-d');
565 if (!empty($entry['substanceAdministration']['effectiveTime'][0]['low']['value'])) {
566 $this->templateData['field_name_value_array']['lists3'][$i]['begdate'] = $entry['substanceAdministration']['effectiveTime'][0]['low']['value'];
567 $this->templateData['field_name_value_array']['lists3'][$i]['enddate'] = $entry['substanceAdministration']['effectiveTime'][0]['high']['value'] ?? null;
568 } elseif (!empty($entry['substanceAdministration']['effectiveTime']['low']['value'])) {
569 $this->templateData['field_name_value_array']['lists3'][$i]['begdate'] = $entry['substanceAdministration']['effectiveTime']['low']['value'];
570 $this->templateData['field_name_value_array']['lists3'][$i]['enddate'] = $entry['substanceAdministration']['effectiveTime']['high']['value'] ?? null;
573 $this->templateData['field_name_value_array']['lists3'][$i]['route'] = $entry['substanceAdministration']['routeCode']['code'] ?? null;
574 $this->templateData['field_name_value_array']['lists3'][$i]['route_display'] = $entry['substanceAdministration']['routeCode']['displayName'] ?? null;
575 $this->templateData['field_name_value_array']['lists3'][$i]['dose'] = $entry['substanceAdministration']['doseQuantity']['value'] ?? null;
576 $this->templateData['field_name_value_array']['lists3'][$i]['dose_unit'] = $entry['substanceAdministration']['doseQuantity']['unit'] ?? null;
577 $this->templateData['field_name_value_array']['lists3'][$i]['rate'] = $entry['substanceAdministration']['rateQuantity']['value'] ?? null;
578 $this->templateData['field_name_value_array']['lists3'][$i]['rate_unit'] = $entry['substanceAdministration']['rateQuantity']['unit'] ?? null;
579 $this->templateData['field_name_value_array']['lists3'][$i]['drug_code'] = $code['code'];
580 $this->templateData['field_name_value_array']['lists3'][$i]['drug_text'] = $code['code_text'];
581 $this->templateData['field_name_value_array']['lists3'][$i]['note'] = $entry['substanceAdministration']['text']['reference']['value'] ?? null;
582 $this->templateData['field_name_value_array']['lists3'][$i]['indication'] = $entry['substanceAdministration']['entryRelationship'][0]['observation']['value']['displayName'] ?? ($entry['substanceAdministration']['entryRelationship']['observation']['value']['displayName'] ?? null);
583 $this->templateData['field_name_value_array']['lists3'][$i]['prn'] = $entry['substanceAdministration']['precondition']['criterion']['value']['displayName'] ?? null;
584 $this->templateData['field_name_value_array']['lists3'][$i]['modified_time'] = $entry['substanceAdministration']['entryRelationship'][1]['supply']['author']['time']['value'] ?? null;
586 $this->templateData['field_name_value_array']['lists3'][$i]['provider_title'] = $entry['substanceAdministration']['performer']['assignedEntity']['assignedPerson']['name']['prefix'] ?? ($entry['substanceAdministration']['entryRelationship'][1]['supply']['author']['assignedAuthor']['assignedPerson']['name']['prefix'] ?? null);
587 $this->templateData['field_name_value_array']['lists3'][$i]['provider_fname'] = $entry['substanceAdministration']['performer']['assignedEntity']['assignedPerson']['name']['given'] ?? ($entry['substanceAdministration']['entryRelationship'][1]['supply']['author']['assignedAuthor']['assignedPerson']['name']['given'] ?? null);
588 $this->templateData['field_name_value_array']['lists3'][$i]['provider_lname'] = $entry['substanceAdministration']['performer']['assignedEntity']['assignedPerson']['name']['family'] ?? ($entry['substanceAdministration']['entryRelationship'][1]['supply']['author']['assignedAuthor']['assignedPerson']['name']['family'] ?? null);
589 $this->templateData['field_name_value_array']['lists3'][$i]['provider_root'] = $entry['substanceAdministration']['entryRelationship'][1]['supply']['author']['assignedAuthor']['id']['root'] ?? null;
590 $this->templateData['field_name_value_array']['lists3'][$i]['provider_address'] = $entry['substanceAdministration']['performer']['assignedEntity']['addr']['streetAddressLine'] ?? ($entry['substanceAdministration']['entryRelationship'][1]['supply']['performer']['assignedEntity']['addr']['streetAddressLine'] ?? null);
591 $this->templateData['field_name_value_array']['lists3'][$i]['provider_city'] = $entry['substanceAdministration']['performer']['assignedEntity']['addr']['city'] ?? ($entry['substanceAdministration']['entryRelationship'][1]['supply']['performer']['assignedEntity']['addr']['city'] ?? null);
592 $this->templateData['field_name_value_array']['lists3'][$i]['provider_state'] = $entry['substanceAdministration']['performer']['assignedEntity']['addr']['state'] ?? ($entry['substanceAdministration']['entryRelationship'][1]['supply']['performer']['assignedEntity']['addr']['state'] ?? null);
593 $this->templateData['field_name_value_array']['lists3'][$i]['provider_postalCode'] = $entry['substanceAdministration']['performer']['assignedEntity']['addr']['postalCode'] ?? ($entry['substanceAdministration']['entryRelationship'][1]['supply']['performer']['assignedEntity']['addr']['postalCode'] ?? null);
594 $this->templateData['field_name_value_array']['lists3'][$i]['provider_country'] = $entry['substanceAdministration']['performer']['assignedEntity']['addr']['country']['value'] ?? ($entry['substanceAdministration']['entryRelationship'][1]['supply']['performer']['assignedEntity']['addr']['country'] ?? null);
595 $this->templateData['entry_identification_array']['lists3'][$i] = $i;
600 * @param $entry
602 public function fetchImmunizationData($entry): void
604 if (
605 !empty($entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['code'])
606 || !empty($entry['substanceAdministration']['negationInd']) == "true"
608 $i = 1;
609 if (!empty($this->templateData['field_name_value_array']['immunization'])) {
610 $i += count($this->templateData['field_name_value_array']['immunization']);
612 $this->templateData['field_name_value_array']['immunization'][$i]['extension'] = $entry['substanceAdministration']['id']['extension'] ?? null;
613 $this->templateData['field_name_value_array']['immunization'][$i]['root'] = $entry['substanceAdministration']['id']['root'] ?? null;
614 $this->templateData['field_name_value_array']['immunization'][$i]['administered_date'] = $entry['substanceAdministration']['effectiveTime']['value'] ?: $entry['substanceAdministration']['effectiveTime']['low']['value'] ?? null;
615 $this->templateData['field_name_value_array']['immunization'][$i]['route_code'] = $entry['substanceAdministration']['routeCode']['code'] ?? null;
616 $this->templateData['field_name_value_array']['immunization'][$i]['route_code_text'] = $entry['substanceAdministration']['routeCode']['displayName'] ?? null;
618 $code = $this->codeService->resolveCode(
619 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['code'] ?? null,
620 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['codeSystemName'] ?? null,
621 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['displayName']
623 $code = $this->codeService->resolveCode(
624 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['code'],
625 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['codeSystemName'] ?? $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['codeSystem'],
626 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['displayName']
628 if (!empty($entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']["nullFlavor"])) {
629 $code['code'] = 'OID:' . $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']["valueSet"] ?? null;
630 $code['formatted_code'] = 'OID:' . $code['code'];
631 $code['code_text'] = $entry['substanceAdministration']['text'] ?? '';
633 $this->templateData['field_name_value_array']['immunization'][$i]['cvx_code'] = $code['code'];
634 $this->templateData['field_name_value_array']['immunization'][$i]['cvx_code_text'] = $code['code_text'];
635 $this->templateData['field_name_value_array']['immunization'][$i]['amount_administered'] = $entry['substanceAdministration']['doseQuantity']['value'] ?? null;
636 $this->templateData['field_name_value_array']['immunization'][$i]['amount_administered_unit'] = $entry['substanceAdministration']['doseQuantity']['unit'] ?? null;
637 $this->templateData['field_name_value_array']['immunization'][$i]['completion_status'] = $entry['substanceAdministration']['statusCode']['code'] ?? null;
638 $this->templateData['field_name_value_array']['immunization'][$i]['manufacturer'] = $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturerOrganization']['name'] ?? null;
639 $this->templateData['field_name_value_array']['immunization'][$i]['provider_npi'] = $entry['substanceAdministration']['performer']['assignedEntity']['id']['extension'] ?? null;
640 $this->templateData['field_name_value_array']['immunization'][$i]['provider_name'] = $entry['substanceAdministration']['performer']['assignedEntity']['assignedPerson']['name']['given'] ?? null;
641 $this->templateData['field_name_value_array']['immunization'][$i]['provider_family'] = $entry['substanceAdministration']['performer']['assignedEntity']['assignedPerson']['name']['family'] ?? null;
642 $this->templateData['field_name_value_array']['immunization'][$i]['provider_address'] = $entry['substanceAdministration']['performer']['assignedEntity']['addr']['streetAddressLine'] ?? null;
643 $this->templateData['field_name_value_array']['immunization'][$i]['provider_city'] = $entry['substanceAdministration']['performer']['assignedEntity']['addr']['city'] ?? null;
644 $this->templateData['field_name_value_array']['immunization'][$i]['provider_state'] = $entry['substanceAdministration']['performer']['assignedEntity']['addr']['state'] ?? null;
645 $this->templateData['field_name_value_array']['immunization'][$i]['provider_postalCode'] = $entry['substanceAdministration']['performer']['assignedEntity']['addr']['postalCode'] ?? null;
646 $this->templateData['field_name_value_array']['immunization'][$i]['provider_country'] = $entry['substanceAdministration']['performer']['assignedEntity']['addr']['country'] ?? null;
647 $this->templateData['field_name_value_array']['immunization'][$i]['provider_telecom'] = $entry['substanceAdministration']['performer']['assignedEntity']['telecom']['value'] ?? null;
648 $this->templateData['field_name_value_array']['immunization'][$i]['represented_organization'] = $entry['substanceAdministration']['performer']['assignedEntity']['representedOrganization']['name'] ?? null;
649 $this->templateData['field_name_value_array']['immunization'][$i]['represented_organization_tele'] = $entry['substanceAdministration']['performer']['assignedEntity']['representedOrganization']['telecom'] ?? null;
651 if ($entry['substanceAdministration']['entryRelationship']['observation']['value']['code']) {
652 $code = $this->codeService->resolveCode(
653 $entry['substanceAdministration']['entryRelationship']['observation']['value']['code'],
654 $entry['substanceAdministration']['entryRelationship']['observation']['value']['codeSystemName'] ?: $entry['substanceAdministration']['entryRelationship']['observation']['value']['codeSystem'] ?? '',
655 $entry['substanceAdministration']['entryRelationship']['observation']['value']['displayName']
657 $this->templateData['field_name_value_array']['immunization'][$i]['reason_code'] = $code['formatted_code'];
658 $this->templateData['field_name_value_array']['immunization'][$i]['reason_code_text'] = $code['code_text'];
659 $this->templateData['field_name_value_array']['immunization'][$i]['reason_description'] = $code['code_text'] ?? $entry['observation']['text'];
660 $date_low = $entry['substanceAdministration']['entryRelationship']['observation']['effectiveTime']['low']['value'] ?? null;
661 $date_high = $entry['substanceAdministration']['entryRelationship']['observation']['effectiveTime']['high']['value'] ?? null;
662 $this->templateData['field_name_value_array']['immunization'][$i]['reason_date_low'] = $date_low;
663 $this->templateData['field_name_value_array']['immunization'][$i]['reason_date_high'] = $date_high;
666 $this->templateData['field_name_value_array']['immunization'][$i]['reason_status'] = (($entry['substanceAdministration']['negationInd'] ?? 'false') == 'true') ? 'negated' : null;
667 $this->templateData['entry_identification_array']['immunization'][$i] = $i;
672 * @param $entry
674 public function fetchProcedureActivityData($entry): void
676 if (
677 (!empty($entry['procedure']['code']['code']) || !empty($entry['procedure']['code']["nullFlavor"]))
678 && $entry['procedure']['code']["nullFlavor"] != 'NI'
680 $i = 1;
681 if (!empty($this->templateData['field_name_value_array']['procedure'])) {
682 $i += count($this->templateData['field_name_value_array']['procedure']);
685 $code = $this->codeService->resolveCode(
686 $entry['procedure']['code']['code'] ?? '',
687 ($entry['procedure']['code']['codeSystemName'] ?? '') ?: $entry['procedure']['code']['codeSystem'] ?? null,
688 $entry['procedure']['code']['displayName'] ?? ''
690 if (!empty($entry['procedure']['code']["nullFlavor"]) && !empty($entry['procedure']['code']["valueSet"])) {
691 $code['code'] = $entry['procedure']['code']["valueSet"] ?? null;
692 $code['formatted_code'] = 'OID:' . $entry['procedure']['code']["valueSet"] ?? null;
693 $code['code_text'] = $entry['procedure']['text'] ?? '';
696 $procedure_type = 'order';
698 $this->templateData['field_name_value_array']['procedure'][$i]['extension'] = $entry['procedure']['id']['extension'] ?? null;
699 $this->templateData['field_name_value_array']['procedure'][$i]['root'] = $entry['procedure']['id']['root'] ?? null;
701 $this->templateData['field_name_value_array']['procedure'][$i]['code'] = $code['formatted_code'];
702 $this->templateData['field_name_value_array']['procedure'][$i]['code_text'] = $code['code_text'];
703 $this->templateData['field_name_value_array']['procedure'][$i]['codeSystemName'] = $code['formatted_code_type'];
705 $this->templateData['field_name_value_array']['procedure'][$i]['procedure_type'] = $procedure_type;
706 $this->templateData['field_name_value_array']['procedure'][$i]['status'] = $entry['procedure']['statusCode']['code'] ?? '';
708 if (!empty($entry['procedure']['effectiveTime']['low']['value'])) {
709 $this->templateData['field_name_value_array']['procedure'][$i]['date'] = $entry['procedure']['effectiveTime']['low']['value'] ?? null;
710 $this->templateData['field_name_value_array']['procedure'][$i]['date_end'] = $entry['procedure']['effectiveTime']['high']['value'] ?? null;
711 } else {
712 $this->templateData['field_name_value_array']['procedure'][$i]['date'] = $entry['procedure']['effectiveTime']['value'] ?? null;
715 // check for a reason code if observation.
716 if (is_array($entry['procedure']['entryRelationship'])) {
717 $entryRelationship = $entry['procedure']['entryRelationship'][1];
718 } else {
719 $entryRelationship = $entry['procedure']['entryRelationship'];
721 if ($entryRelationship['observation']['value']['code']) {
722 $code = $this->codeService->resolveCode(
723 $entryRelationship['observation']['value']['code'],
724 $entryRelationship['observation']['value']['codeSystemName'] ?: $entryRelationship['observation']['value']['codeSystem'] ?? '',
725 $entryRelationship['observation']['value']['displayName']
727 $this->templateData['field_name_value_array']['procedure'][$i]['reason_code'] = $code['formatted_code'];
728 $this->templateData['field_name_value_array']['procedure'][$i]['reason_code_text'] = $code['code_text'];
729 $this->templateData['field_name_value_array']['procedure'][$i]['reason_description'] = $code['code_text'] ?? $entry['observation']['text'];
730 $date_low = $entryRelationship['observation']['effectiveTime']['low']['value'] ?? null;
731 $date_high = $entryRelationship['observation']['effectiveTime']['high']['value'] ?? null;
732 $this->templateData['field_name_value_array']['procedure'][$i]['reason_date_low'] = $date_low;
733 $this->templateData['field_name_value_array']['procedure'][$i]['reason_date_high'] = $date_high;
736 $this->templateData['field_name_value_array']['procedure'][$i]['reason_status'] = (($entry['procedure']['negationInd'] ?? 'false') == 'true') ? 'negated' : null;
738 $this->templateData['field_name_value_array']['procedure'][$i]['represented_organization1'] = $entry['procedure']['performer']['assignedEntity']['representedOrganization']['name'] ?? null;
739 $this->templateData['field_name_value_array']['procedure'][$i]['represented_organization_address1'] = $entry['procedure']['performer']['assignedEntity']['addr']['streetAddressLine'] ?? null;
740 $this->templateData['field_name_value_array']['procedure'][$i]['represented_organization_city1'] = $entry['procedure']['performer']['assignedEntity']['addr']['city'] ?? null;
741 $this->templateData['field_name_value_array']['procedure'][$i]['represented_organization_state1'] = $entry['procedure']['performer']['assignedEntity']['addr']['state'] ?? null;
742 $this->templateData['field_name_value_array']['procedure'][$i]['represented_organization_postalcode1'] = $entry['procedure']['performer']['assignedEntity']['addr']['postalCode'] ?? null;
743 $this->templateData['field_name_value_array']['procedure'][$i]['represented_organization_country1'] = $entry['procedure']['performer']['assignedEntity']['addr']['country'] ?? null;
744 $this->templateData['field_name_value_array']['procedure'][$i]['represented_organization_telecom1'] = $entry['procedure']['performer']['assignedEntity']['telecom']['value'] ?? null;
746 $this->templateData['field_name_value_array']['procedure'][$i]['represented_organization2'] = $entry['procedure']['participant']['participantRole']['playingEntity']['name'] ?? null;
747 $this->templateData['field_name_value_array']['procedure'][$i]['represented_organization_address2'] = $entry['procedure']['participant']['participantRole']['addr']['streetAddressLine'] ?? null;
748 $this->templateData['field_name_value_array']['procedure'][$i]['represented_organization_city2'] = $entry['procedure']['participant']['participantRole']['addr']['city'] ?? null;
749 $this->templateData['field_name_value_array']['procedure'][$i]['represented_organization_state2'] = $entry['procedure']['participant']['participantRole']['addr']['state'] ?? null;
750 $this->templateData['field_name_value_array']['procedure'][$i]['represented_organization_postalcode2'] = $entry['procedure']['participant']['participantRole']['addr']['postalCode'] ?? null;
751 $this->templateData['field_name_value_array']['procedure'][$i]['represented_organization_country2'] = $entry['procedure']['participant']['participantRole']['addr']['country'] ?? null;
753 $this->templateData['entry_identification_array']['procedure'][$i] = $i;
758 * @param $entry
760 public function fetchProcedureDeviceData($entry): void
762 if (!empty($entry['procedure']['code']['code']) && (($entry['procedure']['negationInd'] ?? 'false') != 'true')) {
763 $i = 1;
764 if (!empty($this->templateData['field_name_value_array']['procedure'])) {
765 $i += count($this->templateData['field_name_value_array']['procedure']);
768 // future may need device status code
769 /*$code_proc_type = $this->codeService->resolveCode(
770 $entry['procedure']['code']['code'] ?? '',
771 $entry['procedure']['code']['codeSystemName'] ?: $entry['procedure']['code']['codeSystem'] ?? null,
772 $entry['procedure']['code']['displayName'] ?? ''
773 );*/
775 $code = $this->codeService->resolveCode(
776 $entry['procedure']['participant']['participantRole']['playingDevice']['code']['code'] ?? '',
777 $entry['procedure']['participant']['participantRole']['playingDevice']['code']['codeSystem'] ?? null,
778 $entry['procedure']['participant']['participantRole']['playingDevice']['code']['displayName'] ?? ''
781 $this->templateData['field_name_value_array']['procedure'][$i]['procedure_type'] = 'device';
783 $this->templateData['field_name_value_array']['procedure'][$i]['status'] = $entry['procedure']['statusCode']['code'] ?? '';
785 $this->templateData['field_name_value_array']['procedure'][$i]['extension'] = $entry['procedure']['id']['extension'] ?? null;
786 $this->templateData['field_name_value_array']['procedure'][$i]['root'] = $entry['procedure']['id']['root'] ?? null;
788 $this->templateData['field_name_value_array']['procedure'][$i]['code'] = $code['code'];
789 $this->templateData['field_name_value_array']['procedure'][$i]['code_text'] = $code['code_text'];
790 $this->templateData['field_name_value_array']['procedure'][$i]['codeSystemName'] = $code['formatted_code_type'];
792 if (!empty($entry['procedure']['effectiveTime']['low']['value'])) {
793 $this->templateData['field_name_value_array']['procedure'][$i]['date'] = $entry['procedure']['effectiveTime']['low']['value'] ?? null;
794 } else {
795 $this->templateData['field_name_value_array']['procedure'][$i]['date'] = $entry['procedure']['effectiveTime']['value'] ?? null;
798 $this->templateData['entry_identification_array']['procedure'][$i] = $i;
803 * @param $entry
805 public function fetchProcedurePreformedActivity($entry): void
807 if (!empty($entry['act']['code']['code']) || $entry['act']['negationInd'] ?? 'false' == 'true') {
808 $i = 1;
809 if (!empty($this->templateData['field_name_value_array']['procedure'])) {
810 $i += count($this->templateData['field_name_value_array']['procedure']);
813 $procedure_type = 'intervention';
814 if (!empty($entry['act']['templateId'][1]['root']) && $entry['act']['templateId'][1]['root'] == '2.16.840.1.113883.10.20.24.3.32') {
815 $procedure_type = 'intervention';
818 $code = $this->codeService->resolveCode(
819 $entry['act']['code']['code'] ?? '',
820 $entry['act']['code']['codeSystemName'] ?: $entry['act']['code']['codeSystem'] ?? null,
821 $entry['act']['code']['displayName'] ?? $entry['act']['text']
823 // negated oid
824 if (!empty($entry["act"]["code"]["nullFlavor"]) && !empty($entry['act']['code']["valueSet"])) {
825 $code['code'] = 'OID:' . $entry["act"]["code"]["valueSet"] ?? null;
826 $code['formatted_code'] = 'OID:' . $entry["act"]["code"]["valueSet"] ?? null;
827 $code['code_text'] = $entry['act']['text'] ?? '';
830 $this->templateData['field_name_value_array']['procedure'][$i]['procedure_type'] = $procedure_type;
832 $this->templateData['field_name_value_array']['procedure'][$i]['status'] = $entry['act']['statusCode']['code'] ?? '';
834 $this->templateData['field_name_value_array']['procedure'][$i]['extension'] = $entry['act']['id']['extension'] ?? null;
835 $this->templateData['field_name_value_array']['procedure'][$i]['root'] = $entry['act']['id']['root'] ?? null;
837 $this->templateData['field_name_value_array']['procedure'][$i]['code'] = $code['code'] ?? '';
838 $this->templateData['field_name_value_array']['procedure'][$i]['code_text'] = $entry['act']['text'] ?? $code['code_text'] ?? '';
839 $this->templateData['field_name_value_array']['procedure'][$i]['codeSystemName'] = $code['formatted_code_type'] ?? '';
841 if (!empty($entry['act']['effectiveTime']['low']['value'])) {
842 $this->templateData['field_name_value_array']['procedure'][$i]['date'] = $entry['act']['effectiveTime']['low']['value'] ?? null;
843 $this->templateData['field_name_value_array']['procedure'][$i]['date_end'] = $entry['act']['effectiveTime']['high']['value'] ?? null;
844 } else {
845 $this->templateData['field_name_value_array']['procedure'][$i]['date'] = $entry['act']['effectiveTime']['value'] ?? null;
848 $reason_code = $entry["act"]["entryRelationship"]["observation"]["value"]["code"] ?? null;
849 if ($reason_code) {
850 $reason_system = $entry["act"]["entryRelationship"]["observation"]["value"]["codeSystem"];
851 $reason_name = $entry["act"]["entryRelationship"]["observation"]["value"]["codeSystemName"];
852 $code = $this->codeService->resolveCode(
853 $reason_code,
854 $reason_name ?: $reason_system ?? '',
857 $this->templateData['field_name_value_array']['procedure'][$i]['reason_code'] = $code['formatted_code'];
858 $this->templateData['field_name_value_array']['procedure'][$i]['reason_code_text'] = $code['code_text'];
859 $this->templateData['field_name_value_array']['procedure'][$i]['reason_description'] = $code['code_text'] ?? $entry['act']['text'];
860 $date_low = $entry["act"]["entryRelationship"]["observation"]['effectiveTime']['low']['value'] ?? null;
861 $date_high = $entry["act"]["entryRelationship"]["observation"]['effectiveTime']['high']['value'] ?? null;
862 $this->templateData['field_name_value_array']['procedure'][$i]['reason_date_low'] = $date_low;
863 $this->templateData['field_name_value_array']['procedure'][$i]['reason_date_high'] = $date_high;
865 $this->templateData['field_name_value_array']['procedure'][$i]['reason_status'] = (($entry['act']['negationInd'] ?? 'false') == 'true') ? 'negated' : null;
866 $this->templateData['entry_identification_array']['procedure'][$i] = $i;
870 public function allergy($component)
872 if (!empty($component['section']['entry'][0])) {
873 foreach ($component['section']['entry'] as $key => $value) {
874 $this->fetchAllergyIntoleranceObservation($value);
876 } else {
877 $this->fetchAllergyIntoleranceObservation($component['section']['entry'] ?? null);
881 public function medication($component)
883 $component['section']['text'] = '';
884 if (!empty($component['section']['entry'][0])) {
885 foreach ($component['section']['entry'] as $key => $value) {
886 $this->fetchMedicationData($value);
888 } else {
889 $this->fetchMedicationData($component['section']['entry'] ?? null);
893 public function medical_problem($component)
895 $component['section']['text'] = '';
896 if (!empty($component['section']['entry'][0])) {
897 foreach ($component['section']['entry'] as $key => $value) {
898 $this->fetchMedicalProblemData($value);
900 } else {
901 $this->fetchMedicalProblemData($component['section']['entry'] ?? null);
905 public function immunization($component)
907 $component['section']['text'] = '';
908 if (!empty($component['section']['entry'][0])) {
909 foreach ($component['section']['entry'] as $key => $value) {
910 $this->fetchImmunizationData($value);
912 } else {
913 $this->fetchImmunizationData($component['section']['entry']);
917 public function procedure($component)
919 $component['section']['text'] = '';
920 if (!empty($component['section']['entry'][0])) {
921 foreach ($component['section']['entry'] as $key => $value) {
922 if ($key % 3 != 0) {
923 // TODO add two additional templates for type if needed in future.
924 //continue; //every third entry section has the procedure details
927 $this->fetchProcedureActivityData($value);
929 } else {
930 $this->fetchProcedureActivityData($component['section']['entry'] ?? null);
934 public function labResult($component): void
936 $component['section']['text'] = '';
938 if (!empty($component['section']['entry'][0])) {
939 foreach ($component['section']['entry'] as $key => $value) {
940 $this->fetchLabResultData($value);
942 } else {
943 $this->fetchLabResultData($component['section']['entry'] ?? null);
947 public function fetchLabResultData($lab_result_data): void
949 $i = 1;
950 if (!empty($this->templateData['field_name_value_array']['procedure_result'])) {
951 $i += count($this->templateData['field_name_value_array']['procedure_result']);
953 if (!empty($lab_result_data['organizer']['component'])) {
954 foreach ($lab_result_data['organizer']['component'] as $key => $value) {
955 if (empty($value['observation'])) {
956 $value = [$key => $value];
958 if (!empty($value['observation']['code']['code'])) {
959 $code = $this->codeService->resolveCode(
960 $lab_result_data['organizer']['code']['code'] ?? '',
961 ($lab_result_data['organizer']['code']['codeSystemName'] ?? '') ?: $lab_result_data['organizer']['code']['codeSystem'] ?? '',
962 $lab_result_data['organizer']['code']['displayName'] ?? ''
964 if (empty($lab_result_data['organizer']['id']['extension'])) {
965 $lab_result_data['organizer']['id']['extension'] = $lab_result_data['organizer']['id']['root'] ?? null;
967 $this->templateData['field_name_value_array']['procedure_result'][$i]['extension'] = $lab_result_data['organizer']['id']['extension'] ?? null;
968 $this->templateData['field_name_value_array']['procedure_result'][$i]['root'] = $lab_result_data['organizer']['id']['root'] ?? null;
969 $this->templateData['field_name_value_array']['procedure_result'][$i]['proc_code'] = $code['formatted_code'] ?? null;
970 $this->templateData['field_name_value_array']['procedure_result'][$i]['proc_text'] = $code['code_text'] ?? null;
971 if (!empty($value['observation']['effectiveTime']['low']['value'])) {
972 $this->templateData['field_name_value_array']['procedure_result'][$i]['date'] = $value['observation']['effectiveTime']['low']['value'];
973 } else {
974 $this->templateData['field_name_value_array']['procedure_result'][$i]['date'] = $value['observation']['effectiveTime']['value'] ?? null;
977 $this->templateData['field_name_value_array']['procedure_result'][$i]['status'] = $lab_result_data['organizer']['statusCode']['code'] ?? null;
979 if (empty($value['observation']['id']['extension'])) {
980 $value['observation']['id']['extension'] = $value['observation']['id']['root'] ?? null;
982 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_extension'] = $value['observation']['id']['extension'] ?? null;
983 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_root'] = $value['observation']['id']['root'] ?? null;
984 // @TODO code lookup here
985 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_code'] = $value['observation']['code']['code'] ?? null;
986 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_text'] = $value['observation']['code']['displayName'] ?? null;
987 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_date'] = $value['observation']['effectiveTime']['value'] ?? null;
988 if ($value['observation']['value']['type'] == 'ST') {
989 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_value'] = $value['observation']['value']['_'] ?? null;
990 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_unit'] = 'UNK'; // must be set to save
991 } elseif ($value['observation']['value']['type'] == 'CO') {
992 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_value'] = $value['observation']['value']['displayName'] ?? null;
993 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_unit'] = $value['observation']['value']['unit'] ?: 'UNK';
994 } else {
995 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_value'] = $value['observation']['value']['value'] ?? null;
996 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_unit'] = $value['observation']['value']['unit'] ?: 'UNK';
998 if (!empty($value['observation']['referenceRange']['observationRange']['text'])) {
999 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_range'] = $value['observation']['referenceRange']['observationRange']['text'];
1000 } else {
1001 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_range'] = ($value['observation']['referenceRange']['observationRange']['value']['low']['value'] ?? '') . ' ' . ($value['observation']['referenceRange']['observationRange']['value']['high']['value'] ?? '') . ' ' . ($value['observation']['referenceRange']['observationRange']['value']['low']['unit'] ?? '');
1004 $this->templateData['entry_identification_array']['procedure_result'][$i] = $i;
1005 $i++;
1011 public function fetchQrdaLabResultData($entry)
1013 $i = 1;
1014 if (!empty($this->templateData['field_name_value_array']['procedure_result'])) {
1015 $i += count($this->templateData['field_name_value_array']['procedure_result']);
1017 if (!empty($entry['observation']['code']['code'])) {
1018 $code = $this->codeService->resolveCode(
1019 $entry['observation']['code']['code'] ?? '',
1020 $entry['observation']['code']['codeSystemName'] ?: $entry['observation']['code']['codeSystem'] ?? '',
1021 $entry['observation']['code']['displayName'] ?? ''
1024 $this->templateData['field_name_value_array']['procedure_result'][$i]['extension'] = $entry['observation']['id']['extension'] ?? null;
1025 $this->templateData['field_name_value_array']['procedure_result'][$i]['root'] = $entry['observation']['id']['root'] ?? null;
1026 $this->templateData['field_name_value_array']['procedure_result'][$i]['proc_code'] = $code['formatted_code'];
1027 $this->templateData['field_name_value_array']['procedure_result'][$i]['proc_text'] = $code['code_text'];
1029 if (!empty($entry['observation']['effectiveTime']['low']['value'])) {
1030 $this->templateData['field_name_value_array']['procedure_result'][$i]['date'] = $entry['observation']['effectiveTime']['low']['value'];
1031 } else {
1032 $this->templateData['field_name_value_array']['procedure_result'][$i]['date'] = $entry['observation']['effectiveTime']['value'] ?? null;
1034 $this->templateData['field_name_value_array']['procedure_result'][$i]['status'] = $entry['observation']['statusCode']['code'] ?? null;
1036 $value = $entry['observation']['entryRelationship'] ?? null;
1037 if (!empty($value)) {
1038 // find the result template
1039 foreach ($entry['observation'] as $key => $find_value) {
1040 // check if a ccda result template
1041 $flag = false;
1042 foreach ($find_value as $key1 => $val) {
1043 if (is_array($val)) {
1044 if ($val['templateId'][0]['root'] == '2.16.840.1.113883.10.20.22.4.2') {
1045 $flag = true;
1046 break;
1048 if ($val['observation']['templateId'][0]['root'] == '2.16.840.1.113883.10.20.22.4.2') {
1049 $flag = true;
1050 $value = $val;
1051 break;
1055 if ($flag) {
1056 break;
1060 if (empty($value)) {
1061 $this->templateData['entry_identification_array']['procedure_result'][$i] = $i;
1062 return;
1064 $code = $this->codeService->resolveCode(
1065 $value['observation']['code']['code'] ?? null,
1066 $value['observation']['code']['codeSystemName'] ?? $value['observation']['code']['codeSystem'] ?? '',
1067 $value['observation']['code']['displayName'] ?? ''
1069 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_code'] = $code['formatted_code'];
1070 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_text'] = $code['code_text'];
1072 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_extension'] = $value['observation']['id']['extension'] ?? null;
1073 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_root'] = $value['observation']['id']['root'] ?? null;
1074 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_date'] = $value['observation']['effectiveTime']['value'] ?? null;
1075 if ($value['observation']['value']['type'] == 'ST') {
1076 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_value'] = $value['observation']['value']['_'] ?? null;
1077 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_unit'] = 'UNK'; // must be set to save
1078 } else {
1079 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_value'] = $value['observation']['value']['value'] ?? null;
1080 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_unit'] = $value['observation']['value']['unit'] ?: 'UNK';
1083 if (!empty($value['observation']['referenceRange']['observationRange']['text'])) {
1084 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_range'] = $value['observation']['referenceRange']['observationRange']['text'];
1085 } else {
1086 $this->templateData['field_name_value_array']['procedure_result'][$i]['results_range'] = ($value['observation']['referenceRange']['observationRange']['value']['low']['value'] ?? '') . '-' . ($value['observation']['referenceRange']['observationRange']['value']['high']['value'] ?? '') . ' ' . ($value['observation']['referenceRange']['observationRange']['value']['low']['unit'] ?? '');
1089 $this->templateData['entry_identification_array']['procedure_result'][$i] = $i;
1093 public function VitalSign($component)
1095 $component['section']['text'] = '';
1096 if (!empty($component['section']['entry'][0])) {
1097 foreach ($component['section']['entry'] as $key => $value) {
1098 $this->fetchVitalSignData($value);
1100 } else {
1101 $this->fetchVitalSignData($component['section']['entry'] ?? null);
1105 public function fetchVitalSignData($vital_sign_data)
1107 if (!empty($vital_sign_data['organizer']['component'][0]['observation']['effectiveTime']['value'])) {
1108 $i = 1;
1109 if (!empty($this->templateData['field_name_value_array']['vital_sign'])) {
1110 $i += count($this->templateData['field_name_value_array']['vital_sign']);
1112 $this->templateData['field_name_value_array']['vital_sign'][$i]['extension'] = $vital_sign_data['organizer']['id']['extension'] ?? null;
1113 $this->templateData['field_name_value_array']['vital_sign'][$i]['root'] = $vital_sign_data['organizer']['id']['root'] ?? null;
1114 $this->templateData['field_name_value_array']['vital_sign'][$i]['date'] = $vital_sign_data['organizer']['component'][0]['observation']['effectiveTime']['value'] ?? null;
1115 $vitals_array = array(
1116 '8310-5' => 'temperature',
1117 '8462-4' => 'bpd',
1118 '8480-6' => 'bps',
1119 '8287-5' => 'head_circ',
1120 '8867-4' => 'pulse',
1121 '8302-2' => 'height',
1122 '2710-2' => 'oxygen_saturation',
1123 '9279-1' => 'respiration',
1124 '3141-9' => 'weight',
1125 '39156-5' => 'BMI'
1128 for ($j = 0; $j < 9; $j++) {
1129 $code = $vital_sign_data['organizer']['component'][$j]['observation']['code']['code'] ?? null;
1130 if (!empty($vital_sign_data['organizer']['component'][$j]['observation']['entryRelationship'])) {
1131 $this->templateData['field_name_value_array']['vital_sign'][$i]['bps'] = $vital_sign_data['organizer']['component'][$j]['observation']['entryRelationship'][0]['observation']['value']['value'] ?? null;
1132 $this->templateData['field_name_value_array']['vital_sign'][$i]['bpd'] = $vital_sign_data['organizer']['component'][$j]['observation']['entryRelationship'][1]['observation']['value']['value'] ?? null;
1133 } else {
1134 if (array_key_exists($code, $vitals_array)) {
1135 $this->templateData['field_name_value_array']['vital_sign'][$i][$vitals_array[$code]] = $vital_sign_data['organizer']['component'][$j]['observation']['value']['value'] ?? null;
1140 $this->templateData['entry_identification_array']['vital_sign'][$i] = $i;
1144 public function fetchPhysicalExamPerformedData($entry)
1146 // create an observation for this exam.
1147 $this->fetchObservationPerformedData($entry);
1148 // and a vital in vital forms.
1149 if (
1150 (!empty($entry['observation']['effectiveTime']['value']) && !empty($entry['observation']['value']['value']))
1151 || (!empty($entry['observation']['entryRelationship']['observation']['value']['code'] ?? null) && ($entry['observation']['entryRelationship']['typeCode'] ?? null) === 'RSON')
1153 $i = 1;
1154 if (!empty($this->templateData['field_name_value_array']['vital_sign'])) {
1155 $cnt = count($this->templateData['field_name_value_array']['vital_sign'] ?? []);
1156 $v_date = $entry['observation']['effectiveTime']['value'] ?? null;
1157 for ($c = 1; $c <= $cnt; $c++) {
1158 if ($this->templateData['field_name_value_array']['vital_sign'][$c]['date'] == $v_date) {
1159 $i = 0;
1160 $cnt = $c;
1161 break;
1164 $i += $cnt;
1166 $this->templateData['field_name_value_array']['vital_sign'][$i]['extension'] = $entry['organizer']['id']['extension'] ?? null;
1167 $this->templateData['field_name_value_array']['vital_sign'][$i]['root'] = $entry['organizer']['id']['root'] ?? null;
1168 $this->templateData['field_name_value_array']['vital_sign'][$i]['date'] = $entry['observation']['effectiveTime']['value'] ?? null;
1169 $vitals_array = array(
1170 '8310-5' => 'temperature',
1171 '8462-4' => 'bpd',
1172 '8480-6' => 'bps',
1173 '8287-5' => 'head_circ',
1174 '8867-4' => 'pulse',
1175 '8302-2' => 'height',
1176 '2710-2' => 'oxygen_saturation', // deprecated code
1177 '59408-5' => 'oxygen_saturation',
1178 '9279-1' => 'respiration',
1179 '3141-9' => 'weight',
1180 '29463-7' => 'weight', // with clothes
1181 '39156-5' => 'BMI'
1183 $is_negated = !empty($entry['observation']['negationInd'] ?? false);
1184 $code = $entry['observation']['code']['code'] ?? null;
1185 if (array_key_exists($code, $vitals_array)) {
1186 $this->templateData['field_name_value_array']['vital_sign'][$i][$vitals_array[$code]] = $entry['observation']['value']['value'] ?? null;
1187 $this->templateData['field_name_value_array']['vital_sign'][$i]['vital_column'] = $vitals_array[$code] ?? '';
1188 } else {
1189 // log missed exam
1190 error_log('Missed Physical Exam code (likely vital): ' . $code);
1193 if (!empty($entry['observation']['entryRelationship']['observation']['value']['code'] ?? null)) {
1194 // @todo inter to this moodcode RSON in full template!
1195 $ob_code = $entry['observation']['entryRelationship']['observation']['value']['code'];
1196 $ob_system = $entry['observation']['entryRelationship']['observation']['value']['codeSystemName'] ?: $entry['observation']['entryRelationship']['observation']['value']['codeSystem'] ?? '';
1197 $ob_code_text = $entry['observation']['entryRelationship']['observation']['value']['displayName'] ?? '';
1198 $reason_code = $this->codeService->resolveCode($ob_code, $ob_system, $ob_code_text);
1199 $reason_status = $entry['observation']['entryRelationship']['observation']['statusCode']['code'] ?? '';
1202 $this->templateData['field_name_value_array']['vital_sign'][$i]['reason_status'] = $is_negated ? 'negated' : ($reason_status ?? '');
1203 $this->templateData['field_name_value_array']['vital_sign'][$i]['reason_code'] = $reason_code['formatted_code'] ?? '';
1204 $this->templateData['field_name_value_array']['vital_sign'][$i]['reason_code_text'] = $reason_code['code_text'] ?? '';
1206 $this->templateData['entry_identification_array']['vital_sign'][$i] = $i;
1210 public function socialHistory($component)
1212 $component['section']['text'] = '';
1213 if (!empty($component['section']['entry'][0])) {
1214 foreach ($component['section']['entry'] as $key => $value) {
1215 $this->fetchSocialHistoryData($value);
1217 } else {
1218 $this->fetchSocialHistoryData($component['section']['entry'] ?? null);
1222 public function fetchSocialHistoryData($social_history_data)
1224 if (!empty($social_history_data['observation']['value']['code'])) {
1225 $social_history_array = array(
1226 '2.16.840.1.113883.10.20.22.4.78' => 'smoking'
1228 $i = 0;
1229 $code = $social_history_data['observation']['templateId']['root'];
1230 if (!empty($this->templateData['field_name_value_array']['social_history'])) {
1231 foreach ($this->templateData['field_name_value_array']['social_history'] as $key => $value) {
1232 if (!array_key_exists($social_history_array[$code], $value)) {
1233 $i = $key;
1234 } else {
1235 $i = count($this->templateData['field_name_value_array']['social_history']) + 1;
1240 $this->templateData['field_name_value_array']['social_history'][$i][$social_history_array[$code]]['extension'] = $social_history_data['observation']['id']['extension'];
1241 $this->templateData['field_name_value_array']['social_history'][$i][$social_history_array[$code]]['root'] = $social_history_data['observation']['id']['root'];
1242 $this->templateData['field_name_value_array']['social_history'][$i][$social_history_array[$code]]['status'] = $social_history_data['observation']['value']['code'];
1243 $this->templateData['field_name_value_array']['social_history'][$i][$social_history_array[$code]]['begdate'] = $social_history_data['observation']['effectiveTime']['low']['value'];
1244 $this->templateData['field_name_value_array']['social_history'][$i][$social_history_array[$code]]['enddate'] = $social_history_data['observation']['effectiveTime']['high']['value'];
1245 $this->templateData['field_name_value_array']['social_history'][$i][$social_history_array[$code]]['value'] = $social_history_data['observation']['value']['displayName'];
1246 $this->templateData['entry_identification_array']['social_history'][$i] = $i;
1250 public function encounter($component)
1252 $component['section']['text'] = '';
1253 if ($component['section']['entry'][0]) {
1254 foreach ($component['section']['entry'] as $key => $value) {
1255 $this->fetchEncounterPerformed($value);
1257 } else {
1258 $this->fetchEncounterPerformed($component['section']['entry']);
1261 unset($component);
1262 return;
1265 public function carePlan($component)
1267 if ($this->currentOid != '2.16.840.1.113883.10.20.22.2.58') {
1268 $component['section']['text'] = null;
1270 if (!empty($component['section']['entry'][0])) {
1271 foreach ($component['section']['entry'] as $key => $value) {
1272 $this->fetchCarePlanData($value, $component['section']['text']);
1274 } else {
1275 $this->fetchCarePlanData($component['section']['entry'] ?? null, $component['section']['text']);
1279 public function fetchCarePlanData($entry, $section_text = '')
1281 $plan_type = 'plan_of_care';
1282 if ($this->currentOid == '2.16.840.1.113883.10.20.24.3.31') {
1283 $plan_type = 'intervention';
1284 } elseif ($this->currentOid == '2.16.840.1.113883.10.20.24.3.37') {
1285 $plan_type = 'test_or_order';
1286 } elseif ($this->currentOid == '2.16.840.1.113883.10.20.24.3.143' || $this->currentOid == '2.16.840.1.113883.10.20.24.3.47') {
1287 $plan_type = 'planned_medication_activity';
1288 } elseif ($this->currentOid == '2.16.840.1.113883.10.20.24.3.130') {
1289 $plan_type = 'supply_order';
1290 if (($entry["act"]["entryRelationship"]["supply"]["templateId"][1]["root"] ?? null) == '2.16.840.1.113883.10.20.24.3.9') {
1291 $plan_type = 'device_order';
1293 } elseif ($this->currentOid == '2.16.840.1.113883.10.20.22.2.60') {
1294 $plan_type = 'goal';
1295 } elseif ($this->currentOid == '2.16.840.1.113883.10.20.22.2.58') {
1296 $plan_type = 'health_concern';
1299 $i = 1;
1301 if (
1302 (!empty($entry['act']['code']['code']) && $plan_type == 'device_order')
1303 || (($entry['act']['negationInd'] ?? 'false') == 'true' && $plan_type == 'device_order')
1305 if (!empty($this->templateData['field_name_value_array']['care_plan'])) {
1306 $i += count($this->templateData['field_name_value_array']['care_plan']);
1309 $device_code = $entry["act"]["entryRelationship"]["supply"]["participant"]["participantRole"]["playingDevice"]["code"]["code"];
1310 $device_system = $entry["act"]["entryRelationship"]["supply"]["participant"]["participantRole"]["playingDevice"]["code"]["codeSystem"];
1311 $device_name = $entry["act"]["entryRelationship"]["supply"]["participant"]["participantRole"]["playingDevice"]["code"]["codeSystemName"];
1313 $code = $this->codeService->resolveCode(
1314 $device_code,
1315 $device_name ?: $device_system ?? null,
1319 $this->templateData['field_name_value_array']['care_plan'][$i]['plan_type'] = $plan_type;
1320 $this->templateData['field_name_value_array']['care_plan'][$i]['extension'] = $entry['act']['templateId']['extension'];
1321 $this->templateData['field_name_value_array']['care_plan'][$i]['root'] = $entry['act']['templateId']['root'];
1322 $this->templateData['field_name_value_array']['care_plan'][$i]['code'] = $code['formatted_code'];
1323 $this->templateData['field_name_value_array']['care_plan'][$i]['code_text'] = $code['code_text'];
1324 $this->templateData['field_name_value_array']['care_plan'][$i]['description'] = $entry["act"]["entryRelationship"]["supply"]['text'] ?? $code['code_text'];
1325 $this->templateData['field_name_value_array']['care_plan'][$i]['date'] = $entry['act']['effectiveTime']['center']['value'] ?? $entry["act"]["entryRelationship"]["supply"]['author']['time']['value'] ?? null;
1327 // reason
1328 $reason_code = $entry["act"]["entryRelationship"]["supply"]["entryRelationship"]["observation"]["value"]["code"] ?? null;
1329 if ($reason_code) {
1330 $reason_system = $entry["act"]["entryRelationship"]["supply"]["entryRelationship"]["observation"]["value"]["codeSystem"];
1331 $reason_name = $entry["act"]["entryRelationship"]["supply"]["entryRelationship"]["observation"]["value"]["codeSystemName"];
1332 $code = $this->codeService->resolveCode(
1333 $reason_code,
1334 $reason_name ?: $reason_system ?? '',
1337 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_code'] = $code['formatted_code'];
1338 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_code_text'] = $code['code_text'];
1339 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_description'] = $code['code_text'] ?? $entry['act']['text'];
1340 $date_low = $entry["act"]["entryRelationship"]["supply"]["entryRelationship"]["observation"]['effectiveTime']['low']['value'] ?? null;
1341 $date_high = $entry["act"]["entryRelationship"]["supply"]["entryRelationship"]["observation"]['effectiveTime']['high']['value'] ?? null;
1342 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_date_low'] = $date_low;
1343 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_date_high'] = $date_high;
1345 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_status'] = (($entry['act']['negationInd'] ?? 'false') == 'true') ? 'negated' : null;
1346 $this->templateData['entry_identification_array']['care_plan'][$i] = $i;
1347 } elseif (!empty($entry['act']['code']['code']) || ($entry['act']['negationInd'] ?? 'false') == 'true') {
1348 if (!empty($this->templateData['field_name_value_array']['care_plan'])) {
1349 $i += count($this->templateData['field_name_value_array']['care_plan']);
1352 $code = $this->codeService->resolveCode(
1353 $entry['act']['code']['code'] ?? '',
1354 ($entry['act']['code']['codeSystemName'] ?? null) ?: $entry['act']['code']['codeSystem'] ?? null,
1355 $entry['act']['code']['displayName'] ?? $entry['act']['text']
1357 if (!empty($entry["act"]["code"]["nullFlavor"]) && !empty($entry['act']['code']["valueSet"])) {
1358 $code['code'] = $entry["act"]["code"]["valueSet"] ?? null;
1359 $code['formatted_code'] = 'OID:' . $entry["act"]["code"]["valueSet"] ?? null;
1360 $code['code_text'] = $entry['act']['text'] ?? '';
1362 $this->templateData['field_name_value_array']['care_plan'][$i]['plan_type'] = $plan_type;
1363 $this->templateData['field_name_value_array']['care_plan'][$i]['extension'] = $entry['act']['templateId']['extension'] ?? '';
1364 $this->templateData['field_name_value_array']['care_plan'][$i]['root'] = $entry['act']['templateId']['root'] ?? '';
1365 $this->templateData['field_name_value_array']['care_plan'][$i]['code'] = $code['formatted_code'];
1366 $this->templateData['field_name_value_array']['care_plan'][$i]['code_text'] = $code['code_text'];
1367 $this->templateData['field_name_value_array']['care_plan'][$i]['description'] = $entry['act']['text'] ?? $code['code_text'];
1368 $this->templateData['field_name_value_array']['care_plan'][$i]['date'] = $entry['act']['effectiveTime']['center']['value'] ?? $entry['act']['author']['time']['value'] ?? null;
1370 // negate
1371 if ($entry['act']['entryRelationship']['observation']['value']['code']) {
1372 $code = $this->codeService->resolveCode(
1373 $entry['act']['entryRelationship']['observation']['value']['code'],
1374 $entry['act']['entryRelationship']['observation']['value']['codeSystemName'] ?: $entry['act']['entryRelationship']['observation']['value']['codeSystem'] ?? '',
1375 $entry['act']['entryRelationship']['observation']['value']['displayName'] ?? ''
1377 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_code'] = $code['formatted_code'];
1378 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_code_text'] = $code['code_text'];
1379 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_description'] = $code['code_text'] ?? $entry['act']['text'];
1380 $date_low = $entry['act']['entryRelationship']['observation']['effectiveTime']['low']['value'] ?? null;
1381 $date_high = $entry['act']['entryRelationship']['observation']['effectiveTime']['high']['value'] ?? null;
1382 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_date_low'] = $date_low;
1383 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_date_high'] = $date_high;
1385 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_status'] = (($entry['act']['negationInd'] ?? 'false') == 'true') ? 'negated' : null;
1386 $this->templateData['entry_identification_array']['care_plan'][$i] = $i;
1387 } elseif (!empty($entry['observation']['code']['code']) || ($entry['observation']['negationInd'] ?? 'false') == 'true') { // it's an observation template
1388 if (!empty($this->templateData['field_name_value_array']['care_plan'])) {
1389 $i += count($this->templateData['field_name_value_array']['care_plan']);
1392 $code = $this->codeService->resolveCode(
1393 $entry['observation']['code']['code'] ?? null,
1394 $entry['observation']['code']['codeSystemName'] ?? $value['observation']['code']['codeSystem'] ?? '',
1395 $entry['observation']['code']['displayName'] ?? ''
1397 if (!empty($entry['observation']['code']["nullFlavor"]) && !empty($entry['observation']['code']["valueSet"])) {
1398 $code['code'] = $entry['observation']['code']["valueSet"] ?? null;
1399 $code['formatted_code'] = 'OID:' . $entry['observation']['code']["valueSet"] ?? null;
1400 $code['code_text'] = $entry['observation']['text'] ?? '';
1402 $this->templateData['field_name_value_array']['care_plan'][$i]['plan_type'] = $plan_type;
1403 $this->templateData['field_name_value_array']['care_plan'][$i]['extension'] = $entry['observation']['id']['extension'] ?? null;
1404 $this->templateData['field_name_value_array']['care_plan'][$i]['root'] = $entry['observation']['id']['root'] ?? null;
1405 $this->templateData['field_name_value_array']['care_plan'][$i]['code'] = $code['formatted_code'];
1406 $this->templateData['field_name_value_array']['care_plan'][$i]['code_text'] = $code['code_text'];
1408 $text = $section_text ?: ($entry['observation']['text'] ?? null);
1409 if (empty($text)) {
1410 if ($entry['observation']['value']['type'] == 'ST') {
1411 $text = $entry['observation']['value']['_'];
1412 } elseif ($entry['observation']['value']['type'] == 'CO') {
1413 $text = $entry['observation']['value']['displayName'];
1416 $this->templateData['field_name_value_array']['care_plan'][$i]['description'] = $text ?: ($code['code_text'] ?? null);
1417 // TODO unsure why the author was used for plan date just be aware
1418 //$this->templateData['field_name_value_array']['care_plan'][$i]['date'] = $entry['observation']['author']['time']['value'] ?? null;
1419 $this->templateData['field_name_value_array']['care_plan'][$i]['date'] = $entry['observation']['effectiveTime']['value'] ?? $entry['observation']['author']['time']['value'] ?? null;
1421 // check for a reason code if observation.
1422 if ($entry['observation']['entryRelationship']['observation']['value']['code']) {
1423 $code = $this->codeService->resolveCode(
1424 $entry['observation']['entryRelationship']['observation']['value']['code'],
1425 $entry['observation']['entryRelationship']['observation']['value']['codeSystemName'] ?: $entry['observation']['entryRelationship']['observation']['value']['codeSystem'] ?? '',
1426 $entry['observation']['entryRelationship']['observation']['value']['displayName']
1428 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_code'] = $code['formatted_code'];
1429 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_code_text'] = $code['code_text'];
1430 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_description'] = $code['code_text'] ?? $entry['observation']['text'];
1431 $date_low = $entry['observation']['entryRelationship']['observation']['effectiveTime']['low']['value'] ?? null;
1432 $date_high = $entry['observation']['entryRelationship']['observation']['effectiveTime']['high']['value'] ?? null;
1433 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_date_low'] = $date_low;
1434 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_date_high'] = $date_high;
1437 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_status'] = (($entry['observation']['negationInd'] ?? 'false') == 'true') ? 'negated' : null;
1438 $this->templateData['entry_identification_array']['care_plan'][$i] = $i;
1439 } elseif (
1440 !empty($entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['code'])
1441 || ($entry['substanceAdministration']['negationInd'] ?? 'false') == 'true'
1443 if (!empty($this->templateData['field_name_value_array']['care_plan'])) {
1444 $i += count($this->templateData['field_name_value_array']['care_plan']);
1447 //$plan_type = 'substance_medication_order';
1448 $code = $this->codeService->resolveCode(
1449 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['code'] ?? '',
1450 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['codeSystemName'] ?? ($entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['codeSystem'] ?? ''),
1451 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['displayName'] ?? ''
1453 if (!empty($entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']["nullFlavor"])) {
1454 $code['code'] = $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']["valueSet"] ?? null;
1455 $code['formatted_code'] = 'OID:' . $code['code'];
1456 $code['code_text'] = $entry['substanceAdministration']['text'] ?? '';
1459 $this->templateData['field_name_value_array']['care_plan'][$i]['plan_type'] = $plan_type;
1460 $this->templateData['field_name_value_array']['care_plan'][$i]['extension'] = $entry['substanceAdministration']['id']['extension'] ?? null;
1462 $this->templateData['field_name_value_array']['care_plan'][$i]['root'] = $entry['substanceAdministration']['id']['root'] ?? null;
1463 $this->templateData['field_name_value_array']['care_plan'][$i]['code'] = $code['formatted_code'];
1464 $this->templateData['field_name_value_array']['care_plan'][$i]['code_text'] = $code['code_text'];
1465 $this->templateData['field_name_value_array']['care_plan'][$i]['description'] = $entry['substanceAdministration']['text'] ?? $code['code_text'];
1467 $this->templateData['field_name_value_array']['care_plan'][$i]['end_date'] = null;
1468 if (!empty($entry['substanceAdministration']['effectiveTime']['low']['value'])) {
1469 $this->templateData['field_name_value_array']['care_plan'][$i]['date'] = $entry['substanceAdministration']['effectiveTime']['low']['value'];
1470 $this->templateData['field_name_value_array']['care_plan'][$i]['end_date'] = $entry['substanceAdministration']['effectiveTime']['high']['value'] ?? null;
1471 } elseif (!empty($entry['substanceAdministration']['effectiveTime'][0]['low']['value'])) {
1472 $this->templateData['field_name_value_array']['care_plan'][$i]['date'] = $entry['substanceAdministration']['effectiveTime'][0]['low']['value'];
1473 $this->templateData['field_name_value_array']['care_plan'][$i]['end_date'] = $entry['substanceAdministration']['effectiveTime'][0]['high']['value'] ?? null;
1474 } elseif (!empty($entry['substanceAdministration']['effectiveTime']['value'])) {
1475 $this->templateData['field_name_value_array']['care_plan'][$i]['date'] = $entry['substanceAdministration']['effectiveTime']['value'];
1476 } else {
1477 $this->templateData['field_name_value_array']['care_plan'][$i]['date'] = date('Y-m-d');
1480 if ($entry['substanceAdministration']['entryRelationship']['observation']['value']['code']) {
1481 $code = $this->codeService->resolveCode(
1482 $entry['substanceAdministration']['entryRelationship']['observation']['value']['code'],
1483 $entry['substanceAdministration']['entryRelationship']['observation']['value']['codeSystemName'] ?: $entry['substanceAdministration']['entryRelationship']['observation']['value']['codeSystem'] ?? '',
1484 $entry['substanceAdministration']['entryRelationship']['observation']['value']['displayName'] ?? ''
1486 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_code'] = $code['formatted_code'];
1487 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_code_text'] = $code['code_text'];
1488 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_description'] = $code['code_text'] ?? $entry['observation']['text'];
1489 $date_low = $entry['substanceAdministration']['entryRelationship']['observation']['effectiveTime']['low']['value'] ?? null;
1490 $date_high = $entry['substanceAdministration']['entryRelationship']['observation']['effectiveTime']['high']['value'] ?? null;
1491 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_date_low'] = $date_low;
1492 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_date_high'] = $date_high;
1495 $this->templateData['field_name_value_array']['care_plan'][$i]['reason_status'] = (($entry['substanceAdministration']['negationInd'] ?? 'false') == 'true') ? 'negated' : null;
1496 $this->templateData['entry_identification_array']['care_plan'][$i] = $i;
1500 public function functionalCognitiveStatus($component)
1502 if ($this->currentOid != '2.16.840.1.113883.10.20.22.2.56') {
1503 $component['section']['text'] = '';
1505 $ccnt = 0;
1506 if ($component['section']['entry'][0]) {
1507 foreach ($component['section']['entry'] as $key => $value) {
1508 $this->fetchFunctionalCognitiveStatusData($value, $component['section']['text'][$ccnt]);
1509 $ccnt++;
1511 } else {
1512 $this->fetchFunctionalCognitiveStatusData($component['section']['entry'], $component['section']['text']);
1516 public function fetchFunctionalCognitiveStatusData($entry, $section_text = null)
1518 $cognitive = '0';
1520 if ($this->currentOid == '2.16.840.1.113883.10.20.22.2.56') {
1521 $cognitive = '1';
1522 return;
1524 if (!empty($entry['observation']['value']['code'])) {
1525 $i = 1;
1526 if (!empty($this->templateData['field_name_value_array']['functional_cognitive_status'])) {
1527 $i += count($this->templateData['field_name_value_array']['functional_cognitive_status']);
1530 $code = $this->codeService->resolveCode(
1531 $entry['observation']['value']['code'] ?? null,
1532 $value['observation']['value']['codeSystemName'] ?? $value['observation']['code']['codeSystem'] ?? '',
1533 $value['observation']['value']['displayName'] ?? ''
1535 $this->templateData['field_name_value_array']['functional_cognitive_status'][$i]['cognitive'] = $cognitive;
1536 $this->templateData['field_name_value_array']['functional_cognitive_status'][$i]['extension'] = $entry['observation']['id']['extension'];
1537 $this->templateData['field_name_value_array']['functional_cognitive_status'][$i]['root'] = $entry['observation']['id']['root'];
1538 $this->templateData['field_name_value_array']['functional_cognitive_status'][$i]['date'] = $entry['observation']['effectiveTime']['low']['value'];
1539 $this->templateData['field_name_value_array']['functional_cognitive_status'][$i]['code'] = $code['formatted_code'];
1540 $this->templateData['field_name_value_array']['functional_cognitive_status'][$i]['code_text'] = $section_text ?: $code['code_text'];
1541 $this->templateData['field_name_value_array']['functional_cognitive_status'][$i]['description'] = $section_text ?: $code['code_text'];
1542 $this->templateData['entry_identification_array']['functional_cognitive_status'][$i] = $i;
1543 } elseif (!empty($entry['organizer']['code']['code'])) {
1544 // a CCDA template
1545 $i = 1;
1546 if (!empty($this->templateData['field_name_value_array']['functional_cognitive_status'])) {
1547 $i += count($this->templateData['field_name_value_array']['functional_cognitive_status']);
1550 foreach ($entry['organizer']['component'] as $k => $fscomponent) {
1551 // patient self-status @todo why am I sending an independent status by default.
1552 if (($fscomponent["observation"]["templateId"]["root"] ?? null) == "2.16.840.1.113883.10.20.22.4.128") {
1553 continue;
1555 $code = $this->codeService->resolveCode(
1556 $fscomponent['observation']['value']['code'] ?? null,
1557 $fscomponent['observation']['value']['codeSystem'] ?? '',
1558 $fscomponent['observation']['value']['displayName'] ?? ''
1561 $this->templateData['field_name_value_array']['functional_cognitive_status'][$i]['cognitive'] = $cognitive;
1562 $this->templateData['field_name_value_array']['functional_cognitive_status'][$i]['extension'] = $fscomponent['observation']['id']['extension'];
1563 $this->templateData['field_name_value_array']['functional_cognitive_status'][$i]['root'] = $fscomponent['observation']['id']['root'];
1564 $this->templateData['field_name_value_array']['functional_cognitive_status'][$i]['date'] = $fscomponent['observation']['effectiveTime']['value'];
1565 $this->templateData['field_name_value_array']['functional_cognitive_status'][$i]['code'] = $fscomponent['observation']['value']['code'] ?? $code['formatted_code'] ?? null;
1566 $this->templateData['field_name_value_array']['functional_cognitive_status'][$i]['code_text'] = $fscomponent['observation']['value']['displayName'] ?? $code['code_text'] ?? null;
1567 $this->templateData['field_name_value_array']['functional_cognitive_status'][$i]['description'] = $fscomponent['observation']['value']['displayName'] ?? $code['code_text'] ?? null;
1569 $this->templateData['entry_identification_array']['functional_cognitive_status'][$i] = $i;
1574 public function referral($component)
1576 if ($component['section']['entry'][0]) {
1577 foreach ($component['section']['entry'] as $key => $value) {
1578 $this->fetchReferralData($value);
1580 } else {
1581 $this->fetchReferralData($component['section']);
1585 public function fetchReferralData($referral_data)
1587 if (!empty($referral_data['text']['paragraph']) && is_array($referral_data['text']['paragraph'])) {
1588 $i = 1;
1589 foreach ($referral_data['text']['paragraph'] as $key => $value) {
1590 if ($value) {
1591 $this->templateData['field_name_value_array']['referral'][$i]['body'] = preg_replace('/\s+/', ' ', $value);
1592 $this->templateData['entry_identification_array']['referral'][$i] = $i;
1593 $i++;
1596 } else {
1597 $i = 1;
1598 if (!empty($this->templateData['field_name_value_array']['referral'])) {
1599 $i += count($this->templateData['field_name_value_array']['referral']);
1601 $this->templateData['field_name_value_array']['referral'][$i]['root'] = $referral_data['templateId']['root'];
1602 $this->templateData['field_name_value_array']['referral'][$i]['body'] = (!empty($referral_data['text']['paragraph'])) ? preg_replace('/\s+/', ' ', $referral_data['text']['paragraph']) : '';
1604 $this->templateData['entry_identification_array']['referral'][$i] = $i;
1607 return;
1610 public function dischargeMedications($component)
1612 $component['section']['text'] = '';
1613 if ($component['section']['entry'][0]) {
1614 foreach ($component['section']['entry'] as $key => $value) {
1615 $this->fetchDischargeMedicationsData($value);
1617 } else {
1618 $this->fetchDischargeMedicationsData($component['section']['entry']);
1622 public function fetchDischargeMedicationsData($discharge_medications_data)
1624 $i = 1;
1625 if (!empty($this->templateData['field_name_value_array']['discharge_medication'])) {
1626 $i += count($this->templateData['field_name_value_array']['discharge_medication']);
1628 $this->templateData['field_name_value_array']['discharge_medication'][$i]['extension'] = $discharge_medications_data['act']['id']['extension'];
1629 $this->templateData['field_name_value_array']['discharge_medication'][$i]['root'] = $discharge_medications_data['act']['id']['root'];
1630 $this->templateData['field_name_value_array']['discharge_medication'][$i]['begdate'] = $discharge_medications_data['act']['effectiveTime']['low']['value'];
1631 $this->templateData['field_name_value_array']['discharge_medication'][$i]['enddate'] = $discharge_medications_data['act']['effectiveTime']['high']['value'];
1632 $this->templateData['field_name_value_array']['discharge_medication'][$i]['route'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['routeCode']['code'];
1633 $this->templateData['field_name_value_array']['discharge_medication'][$i]['route_display'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['routeCode']['displayName'];
1634 $this->templateData['field_name_value_array']['discharge_medication'][$i]['dose'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['doseQuantity']['value'];
1635 $this->templateData['field_name_value_array']['discharge_medication'][$i]['dose_unit'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['doseQuantity']['unit'];
1636 $this->templateData['field_name_value_array']['discharge_medication'][$i]['rate'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['rateQuantity']['value'];
1637 $this->templateData['field_name_value_array']['discharge_medication'][$i]['rate_unit'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['rateQuantity']['unit'];
1638 $this->templateData['field_name_value_array']['discharge_medication'][$i]['drug_code'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['code'];
1639 $this->templateData['field_name_value_array']['discharge_medication'][$i]['drug_text'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['displayName'];
1640 $this->templateData['field_name_value_array']['discharge_medication'][$i]['note'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['text']['reference']['value'];
1641 $this->templateData['field_name_value_array']['discharge_medication'][$i]['indication'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship'][0]['observation']['value']['displayName'] ?: $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship']['observation']['value']['displayName'];
1642 $this->templateData['field_name_value_array']['discharge_medication'][$i]['prn'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['precondition']['criterion']['value']['displayName'];
1644 $this->templateData['field_name_value_array']['discharge_medication'][$i]['provider_title'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship'][1]['supply']['author']['assignedAuthor']['assignedPerson']['name']['prefix'];
1645 $this->templateData['field_name_value_array']['discharge_medication'][$i]['provider_fname'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship'][1]['supply']['author']['assignedAuthor']['assignedPerson']['name']['given'];
1646 $this->templateData['field_name_value_array']['discharge_medication'][$i]['provider_lname'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship'][1]['supply']['author']['assignedAuthor']['assignedPerson']['name']['family'];
1647 $this->templateData['field_name_value_array']['discharge_medication'][$i]['provider_root'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship'][1]['supply']['author']['assignedAuthor']['id']['root'];
1648 $this->templateData['field_name_value_array']['discharge_medication'][$i]['provider_address'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship'][1]['supply']['performer']['assignedEntity']['addr']['streetAddressLine'];
1649 $this->templateData['field_name_value_array']['discharge_medication'][$i]['provider_city'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship'][1]['supply']['performer']['assignedEntity']['addr']['city'];
1650 $this->templateData['field_name_value_array']['discharge_medication'][$i]['provider_state'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship'][1]['supply']['performer']['assignedEntity']['addr']['state'];
1651 $this->templateData['field_name_value_array']['discharge_medication'][$i]['provider_postalCode'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship'][1]['supply']['performer']['assignedEntity']['addr']['postalCode'];
1652 $this->templateData['field_name_value_array']['discharge_medication'][$i]['provider_country'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship'][1]['supply']['performer']['assignedEntity']['addr']['country'];
1653 $this->templateData['entry_identification_array']['discharge_medication'][$i] = $i;
1654 unset($discharge_medications_data);
1655 return;
1658 public function dischargeSummary($component)
1660 if ($component['section']['entry'][0]) {
1661 foreach ($component['section']['entry'] as $key => $value) {
1662 $this->fetchDischargeSummaryData($value);
1664 } else {
1665 $this->fetchDischargeSummaryData($component['section']);
1668 unset($component);
1669 return;
1672 public function fetchDischargeSummaryData($discharge_summary_data)
1674 $i = 1;
1675 if (!empty($this->templateData['field_name_value_array']['discharge_summary'])) {
1676 $i += count($this->templateData['field_name_value_array']['discharge_summary']);
1678 $this->templateData['field_name_value_array']['discharge_summary'][$i]['root'] = $discharge_summary_data['templateId']['root'];
1679 $text = preg_replace('/\s+/', ' ', $discharge_summary_data['text']['content']);
1680 for ($j = 0, $jMax = count($discharge_summary_data['text']['list']['item']); $j < $jMax; $j++) {
1681 if (is_array($discharge_summary_data['text']['list']['item'][$j])) {
1682 for ($k = 0, $kMax = count($discharge_summary_data['text']['list']['item'][$j]['list']['item']); $k < $kMax; $k++) {
1683 $text .= '#$%' . preg_replace('/\s+/', ' ', $discharge_summary_data['text']['list']['item'][$j]['list']['item'][$k]);
1685 } else {
1686 $text .= '#$%' . preg_replace('/\s+/', ' ', $discharge_summary_data['text']['list']['item'][$j]);
1690 $this->templateData['field_name_value_array']['discharge_summary'][$i]['text'] = $text;
1692 $this->templateData['entry_identification_array']['discharge_summary'][$i] = $i;