4 * Qrda and Cda ParseService Class
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
;
16 use OpenEMR\Events\CDA\CDAPreParseEvent
;
17 use OpenEMR\Events\CDA\CDAPostParseEvent
;
18 use OpenEMR\Services\CodeTypesService
;
20 class CdaTemplateParse
22 private $templateData;
25 protected $is_qrda_import;
28 * @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
32 public function __construct()
34 $this->templateData
= [];
35 $this->is_qrda_import
= false;
36 $this->codeService
= new CodeTypesService();
37 $this->ed
= $GLOBALS['kernel']->getEventDispatcher();
40 public function parseCDAEntryComponents($components): array
43 $components_oids = array(
44 '2.16.840.1.113883.10.20.22.4.7' => 'allergy',
45 '2.16.840.1.113883.10.20.22.2.6.1' => 'allergy',
46 '2.16.840.1.113883.10.20.22.2.1' => 'medication',
47 '2.16.840.1.113883.10.20.22.2.1.1' => 'medication',
48 '2.16.840.1.113883.10.20.22.2.5.1' => 'medical_problem',
49 '2.16.840.1.113883.10.20.22.2.5' => 'medical_problem',
50 '2.16.840.1.113883.10.20.22.2.2' => 'immunization',
51 '2.16.840.1.113883.10.20.22.2.2.1' => 'immunization',
52 '2.16.840.1.113883.3.88.11.83.145' => 'procedure',
53 '2.16.840.1.113883.10.20.22.2.7.1' => 'procedure',
54 '2.16.840.1.113883.10.20.22.2.3.1' => 'labResult',
55 '2.16.840.1.113883.10.20.22.2.3' => 'labResult',
56 '2.16.840.1.113883.10.20.22.2.4.1' => 'VitalSign',
57 '2.16.840.1.113883.10.20.22.2.17' => 'socialHistory',
58 '2.16.840.1.113883.3.88.11.83.127' => 'encounter',
59 '2.16.840.1.113883.10.20.22.2.22.1' => 'encounter',
60 '2.16.840.1.113883.10.20.22.2.22' => 'encounter',
61 '2.16.840.1.113883.10.20.22.4.49' => 'encounter',
62 '2.16.840.1.113883.10.20.22.2.10' => 'carePlan',
63 '2.16.840.1.113883.10.20.22.2.60' => 'carePlan',
64 '2.16.840.1.113883.10.20.22.2.58' => 'carePlan',
65 '2.16.840.1.113883.10.20.22.2.14' => 'functionalCognitiveStatus',
66 '2.16.840.1.113883.10.20.22.2.56' => 'functionalCognitiveStatus',
67 '1.3.6.1.4.1.19376.1.5.3.1.3.1' => 'referral',
68 '2.16.840.1.113883.10.20.22.2.11.1' => 'dischargeMedications',
69 '2.16.840.1.113883.10.20.22.2.41' => 'dischargeSummary'
72 $preParseEvent = new CDAPreParseEvent($components);
73 $this->ed
->dispatch($preParseEvent, CDAPreParseEvent
::EVENT_HANDLE
);
75 foreach ($preParseEvent->getComponents() as $component) {
76 if (!empty($component['section']['templateId']['root'])) {
77 if (!empty($components_oids[$component['section']['templateId']['root']])) {
78 $this->currentOid
= $component['section']['templateId']['root'];
79 $func_name = $components_oids[$component['section']['templateId']['root']];
80 $this->$func_name($component);
81 $postParseEvent = new CDAPostParseEvent($func_name, $this->currentOid
, $component, $this->templateData
);
82 $this->ed
->dispatch($postParseEvent, CDAPostParseEvent
::EVENT_HANDLE
);
84 } elseif (empty($component['section']['templateId'])) {
85 // uncomment for debugging information.
86 error_log("section and template id empty for " . var_export($component, true));
87 } elseif (count($component['section']['templateId']) > 1) {
88 $this->currentOid
= '';
89 foreach ($component['section']['templateId'] as $key_1 => $value_1) {
90 if (!empty($components_oids[$component['section']['templateId'][$key_1]['root']])) {
91 $this->currentOid
= $component['section']['templateId'][$key_1]['root'];
92 $func_name = $components_oids[$component['section']['templateId'][$key_1]['root']];
93 $this->$func_name($component);
94 $postParseEvent = new CDAPostParseEvent($func_name, $this->currentOid
, $component, $this->templateData
);
95 $this->ed
->dispatch($postParseEvent, CDAPostParseEvent
::EVENT_HANDLE
);
101 if (!empty($postParseEvent) && $postParseEvent instanceof CDAPostParseEvent
) {
102 $this->templateData
= $postParseEvent->getTemplateData();
105 return $this->templateData
;
109 * parsePatientDataSection
111 * @param $entryComponents //An array of QRDA entry templates
114 public function parseQRDAPatientDataSection($entryComponents): array
116 $this->is_qrda_import
= true;
118 '2.16.840.1.113883.10.20.24.3.147' => 'fetchAllergyIntoleranceObservation',
119 '2.16.840.1.113883.10.20.24.3.41' => 'fetchMedicationData', // active medication @todo verify status all meds
120 '2.16.840.1.113883.10.20.24.3.42' => 'fetchMedicationData', // Medication Administered Act @todo honor end dates
121 '2.16.840.1.113883.10.20.24.3.139' => 'fetchMedicationData', // Medication Dispensed Act @todo set med type
122 '2.16.840.1.113883.10.20.24.3.105' => 'fetchMedicationData', // Medication Discharge Act
123 '2.16.840.1.113883.10.20.24.3.137' => 'fetchMedicalProblemData',// diagnosis
124 '2.16.840.1.113883.10.20.24.3.138' => 'fetchMedicalProblemData',// concern symtom
125 '2.16.840.1.113883.10.20.24.3.140' => 'fetchImmunizationData', // Immunization Administered (V3)
126 '2.16.840.1.113883.10.20.22.4.14' => 'fetchProcedureActivityData', // procedure activity-performed 2.16.840.1.113883.10.20.24.3.64
127 '2.16.840.1.113883.10.20.24.3.7' => 'fetchProcedureDeviceData', // procedure preformed Device Applied
128 '2.16.840.1.113883.10.20.24.3.32' => 'fetchProcedurePreformedActivity',// procedure activity-intervention
129 '2.16.840.1.113883.10.20.24.3.38' => 'fetchQrdaLabResultData', // lab test preformed
130 '2.16.840.1.113883.10.20.24.3.133' => 'fetchEncounterPerformed',
131 '2.16.840.1.113883.10.20.24.3.143' => 'fetchCarePlanData', // Immunization Order Substance Order @todo this is planned or goal MOVE
132 '2.16.840.1.113883.10.20.24.3.47' => 'fetchCarePlanData', // Plan of Care Medication Substance Observation Activity Ordered
133 '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
134 '2.16.840.1.113883.10.20.24.3.31' => 'fetchCarePlanData', // Plan of Care Activity (act) Intervention Order
135 '2.16.840.1.113883.10.20.24.3.37' => 'fetchCarePlanData', // Plan of Care Activity Observation Lab order
136 '2.16.840.1.113883.10.20.24.3.17' => 'fetchCarePlanData', // Plan of Care Activity Observation Diagnostic Study, Order
138 * CCDA 2.16.840.1.113883.10.20.22.4.13 - Procedure Activity Observation.
139 * QRDA 2.16.840.1.113883.10.20.24.3.18 Diagnostic Study, Performed,
140 * QRDA 2.16.840.1.113883.10.20.24.3.59 Physical Exam, Performed
141 * QRDA '2.16.840.1.113883.10.20.24.3.54' Deceased Observation (V3) is handled in patient data parse.
143 '2.16.840.1.113883.10.20.24.3.59' => 'fetchPhysicalExamPerformedData', // Physical Exam, Performed observation Vitals
144 '2.16.840.1.113883.10.20.24.3.18' => 'fetchObservationPerformedData', //
145 '2.16.840.1.113883.10.20.24.3.144' => 'fetchObservationPerformedData', // Assessment Performed
146 '2.16.840.1.113883.10.20.24.3.54' => 'fetchDeceasedObservationData', // Deceased Observation (V3)
147 '2.16.840.1.113883.10.20.24.3.55' => 'fetchPaymentSourceData', // Patient Characteristic Payer
149 foreach ($entryComponents['section']['entry'] as $entry) {
150 $key = array_keys($entry)[0]; // need the entry template type i.e. observation, activity, substance etc.
151 if (!empty($entry[$key]['templateId']['root'])) {
152 if (!empty($qrda_oids[$entry[$key]['templateId']['root']])) {
153 $this->currentOid
= $entry[$key]['templateId']['root'];
154 $func_name = $qrda_oids[$entry[$key]['templateId']['root']] ??
null;
155 if (!empty($func_name)) {
156 $this->$func_name($entry);
159 $text = $entry[$key]['templateId']['root'] . ' ' . ($entry[$key]['text'] ?
: $entry[$key]['code']['displayName']);
160 error_log('Root Missing QDM: ' . $text);
162 } elseif (count($entry[$key]['templateId'] ??
[]) > 1) {
164 if (!empty($qrda_oids[$entry[$key]['templateId'][$key_1]['root']])) {
165 $this->currentOid
= $entry[$key]['templateId'][$key_1]['root'];
166 $func_name = $qrda_oids[$entry[$key]['templateId'][$key_1]['root']] ??
null;
167 if (!empty($func_name)) {
168 $this->$func_name($entry);
170 $text = $entry[$key]['templateId'][$key_1]['root'] . " Key: $key_1 " . ($entry[$key]['text'] ?
: $entry[$key]['code']['displayName']);
171 error_log('Missing Function for: ' . $text);
173 } elseif (count($entry[$key]['templateId'] ??
[]) > 1) {
175 if (!empty($qrda_oids[$entry[$key]['templateId'][$key_1]['root']])) {
176 $this->currentOid
= $entry[$key]['templateId'][$key_1]['root'];
177 $func_name = $qrda_oids[$entry[$key]['templateId'][$key_1]['root']] ??
null;
178 if (!empty($func_name)) {
179 $this->$func_name($entry);
181 $text = $entry[$key]['templateId'][$key_1]['root'] . " Key: $key_1 " . ($entry[$key]['text'] ?
: $entry[$key]['code']['displayName']);
182 error_log('Missing Function for: ' . $text);
185 $text = $entry[$key]['templateId'][$key_1]['root'] . " Key: $key_1 " . ($entry[$key]['text'] ?
: $entry[$key]['code']['displayName']);
186 error_log('Missing QDM: ' . $text);
189 $text = $entry[$key]['templateId'][$key_1]['root'] . ' ' . ($entry[$key]['text'] ?
: $entry[$key]['code']['displayName']);
190 error_log('Missing QDM: ' . $text);
192 } elseif (!empty($entry[$key]['root'])) {
193 if (!empty($qrda_oids[$entry[$key]['root']])) {
194 $this->currentOid
= $entry[$key]['root'];
195 $func_name = $qrda_oids[$entry[$key]['root']] ??
null;
196 if (!empty($func_name)) {
197 $this->$func_name($entry);
200 $text = $entry[$key]['root'] . ' ' . ($entry[$key]['text'] ?
: $entry[$key]['code']['displayName']);
201 error_log('Root Missing QDM: ' . $text);
205 if (empty($this->templateData
)) {
206 error_log('Could not find any QDMs in document!');
208 return $this->templateData ??
[];
211 public function parseUnstructuredComponents($xml): array
213 $components = $xml['component'];
214 $uuid = $xml['recordTarget']['patientRole']['id']['extension'] ??
null;
215 if (!empty($components[0])) {
216 foreach ($components as $component) {
217 $item = $component['nonXMLBody']['text'];
218 $this->fetchFileForImport($item, $uuid);
221 $item = $components['nonXMLBody']['text'];
222 $this->fetchFileForImport($item, $uuid);
225 return $this->templateData ??
[];
228 public function fetchFileForImport($component, $uuid): void
231 if (!empty($this->templateData
['field_name_value_array']['import_file'])) {
232 $i +
= count($this->templateData
['field_name_value_array']['import_file']);
234 $this->templateData
['field_name_value_array']['import_file'][$i]['uuid'] = $uuid;
235 $this->templateData
['field_name_value_array']['import_file'][$i]['hash'] = $component['hash'] ??
'';
236 $this->templateData
['field_name_value_array']['import_file'][$i]['mediaType'] = $component['mediaType'] ??
'';
237 $this->templateData
['field_name_value_array']['import_file'][$i]['category'] = $component['category'] ??
'';
238 $this->templateData
['field_name_value_array']['import_file'][$i]['file_name'] = $component['name'] ??
'';
239 $this->templateData
['field_name_value_array']['import_file'][$i]['compression'] = $component['compression'] ??
'';
240 $this->templateData
['field_name_value_array']['import_file'][$i]['content'] = $component['_'] ??
'';
242 $this->templateData
['entry_identification_array']['import_file'][$i] = $i;
245 public function fetchDeceasedObservationData($entry)
247 // handled in patient data parse.
248 // leave this function to prevent parse errors.
251 public function fetchPaymentSourceData($entry)
253 if (!empty($entry['observation']['effectiveTime']['low']['value'] ??
null)) {
255 if (!empty($this->templateData
['field_name_value_array']['payer'])) {
256 $i +
= count($this->templateData
['field_name_value_array']['payer']);
258 $this->templateData
['field_name_value_array']['payer'][$i]['status'] = $entry['observation']['statusCode']['code'] ??
null;
259 $this->templateData
['field_name_value_array']['payer'][$i]['code'] = $entry['observation']['value']['code'] ??
null;
260 $this->templateData
['field_name_value_array']['payer'][$i]['low_date'] = $entry['observation']['effectiveTime']['low']['value'] ??
null;
261 $this->templateData
['field_name_value_array']['payer'][$i]['high_date'] = $entry['observation']['effectiveTime']['high']['value'] ??
null;
263 $this->templateData
['entry_identification_array']['payer'][$i] = $i;
270 public function fetchObservationPerformedData($entry): void
272 // was also called from fetchPhysicalExamPerformedData()
274 !empty($entry['observation']['value']['code'] ??
null)
275 ||
!empty($entry['observation']['code']['code'] ??
null)
276 ||
(!empty($entry['observation']['value']['nullFlavor'] ??
null) && empty($entry['observation']['value']['code'] ??
null))
277 ||
(!empty($entry['observation']['entryRelationship']['observation']['value']['code'] ??
null) && ($entry['observation']['entryRelationship']['typeCode'] ??
null) === 'RSON')
280 if (!empty($this->templateData
['field_name_value_array']['observation_preformed'])) {
281 $i +
= count($this->templateData
['field_name_value_array']['observation_preformed']);
283 $is_negated = !empty($entry['observation']['negationInd'] ??
false);
284 $ob_type = 'assessment'; // default and 2.16.840.1.113883.10.20.24.3.144
285 if ($this->currentOid
=== '2.16.840.1.113883.10.20.24.3.18') {
286 $ob_type = 'procedure_diagnostic';
287 } elseif ($this->currentOid
=== '2.16.840.1.113883.10.20.24.3.59') {
288 $ob_type = 'physical_exam_performed';
291 $ob_code = $entry['observation']['code']['code'];
292 $ob_system = $entry['observation']['code']['codeSystemName'] ?
: ($entry['observation']['code']['codeSystem'] ??
'');
293 $ob_code_text = $entry['observation']['code']['displayName'] ??
'';
294 if (($entry['observation']['code']['codeSystemName'] ??
null) == 'CPT4' && !empty($entry['observation']['code']['translation']['code'])) {
295 $ob_code = $entry['observation']['code']['translation']['code'];
296 $ob_system = $entry['observation']['code']['translation']['codeSystemName'] ?
: $entry['observation']['value']['codeSystem'] ??
'';
297 $ob_code_text = $entry['observation']['code']['translation']['displayName'] ??
'';
299 $code = $this->codeService
->resolveCode($ob_code, $ob_system, $ob_code_text);
302 $result_status = $entry['observation']['statusCode']['code'] ??
'';
305 if (!empty($entry['observation']['value']['nullFlavor'])) {
306 $result_code['formatted_code'] = null;
307 $result_code['code_text'] = null;
308 $result_status = $entry['observation']['statusCode']['code'] ??
'';
309 } elseif (($entry['observation']['value']['type'] ??
'') === 'CD') {
310 $ob_code = $entry['observation']['value']['code'];
311 $ob_system = $entry['observation']['value']['codeSystemName'] ?
: $entry['observation']['value']['codeSystem'] ??
'';
312 $ob_code_text = $entry['observation']['value']['displayName'] ??
'';
313 $result_code = $this->codeService
->resolveCode($ob_code, $ob_system, $ob_code_text);
314 $result_status = $entry['observation']['statusCode']['code'] ??
'';
315 } elseif (($entry['observation']['value']['type'] ??
'') === 'PQ') {
316 $result_code['formatted_code'] = 'PQ';
317 $result_code['code_text'] = $entry['observation']['value']['value'] ??
'';
318 $result_unit = $entry['observation']['value']['unit'] ??
'';
319 $result_status = $entry['observation']['statusCode']['code'] ??
'';
321 if (!empty($entry['observation']['entryRelationship']['observation']['value']['code'] ??
null)) {
322 // @todo inter to this moodcode RSON in full template!
323 $ob_code = $entry['observation']['entryRelationship']['observation']['value']['code'];
324 $ob_system = $entry['observation']['entryRelationship']['observation']['value']['codeSystemName'] ?
: $entry['observation']['entryRelationship']['observation']['value']['codeSystem'] ??
'';
325 $ob_code_text = $entry['observation']['entryRelationship']['observation']['value']['displayName'] ??
'';
326 $reason_code = $this->codeService
->resolveCode($ob_code, $ob_system, $ob_code_text);
327 $reason_status = $entry['observation']['entryRelationship']['observation']['statusCode']['code'] ??
'';
330 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['observation_type'] = $ob_type;
331 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['extension'] = $entry['observation']['id']['extension'] ??
'';
332 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['root'] = $entry['observation']['id']['root'] ??
'';
333 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['date'] = $entry['observation']['effectiveTime']['value'] ??
'';
334 if (!empty($entry['observation']['effectiveTime']['low'])) {
335 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['date'] = $entry['observation']['effectiveTime']['low'];
336 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['date_end'] = $entry['observation']['effectiveTime']['high'] ??
null;
339 if (!empty($entry['observation']['code']["nullFlavor"]) && !empty($entry['observation']['code']["valueSet"])) {
340 $code['code'] = $entry['observation']['code']["valueSet"] ??
null;
341 $code['formatted_code'] = 'OID:' . $entry['observation']['code']["valueSet"] ??
null;
342 $code['code_text'] = $entry['observation']['text'] ??
'';
344 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['observation_status'] = $result_status ??
'';
345 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['observation'] = $entry['observation']['text'] ?
: $code['code_text'] ??
null;
346 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['code'] = $code['formatted_code'] ??
null;
347 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['code_text'] = $code['code_text'] ??
'';
349 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['result_status'] = $result_status ??
'';
350 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['result_code'] = $result_code['formatted_code'] ??
'';
351 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['result_code_text'] = $result_code['code_text'] ??
'';
352 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['result_code_unit'] = $result_unit ??
'';
354 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['reason_status'] = $is_negated ?
'negated' : ($reason_status ??
'');
355 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['reason_code'] = $reason_code['formatted_code'] ??
'';
356 $this->templateData
['field_name_value_array']['observation_preformed'][$i]['reason_code_text'] = $reason_code['code_text'] ??
'';
358 $this->templateData
['entry_identification_array']['observation_preformed'][$i] = $i;
365 public function fetchEncounterPerformed($entry): void
367 if ($this->is_qrda_import
) {
368 $entry = $entry['act']['entryRelationship'];
370 if (!empty($entry['encounter']['effectiveTime']['value']) ||
!empty($entry['encounter']['effectiveTime']['low']['value'])) {
372 if (!empty($this->templateData
['field_name_value_array']['encounter'])) {
373 $i +
= count($this->templateData
['field_name_value_array']['encounter']);
376 $this->templateData
['field_name_value_array']['encounter'][$i]['extension'] = $entry['encounter']['id']['extension'] ??
'';
377 $this->templateData
['field_name_value_array']['encounter'][$i]['root'] = $entry['encounter']['id']['root'] ??
'';
378 $this->templateData
['field_name_value_array']['encounter'][$i]['date'] = ($entry['encounter']['effectiveTime']['value'] ??
null) ?
: $entry['encounter']['effectiveTime']['low']['value'] ??
null;
379 $this->templateData
['field_name_value_array']['encounter'][$i]['date_end'] = $entry['encounter']['effectiveTime']['high']['value'] ??
null;
381 $code_type = $entry['encounter']['code']['codeSystemName'] ??
'' ?
: $entry['encounter']['code']['codeSystem'] ??
'';
382 $code_text = $entry['encounter']['code']['displayName'] ??
'';
383 $code = $this->codeService
->resolveCode($entry['encounter']['code']['code'], $code_type, $code_text);
384 $this->templateData
['field_name_value_array']['encounter'][$i]['code'] = $code['formatted_code'];
385 $this->templateData
['field_name_value_array']['encounter'][$i]['code_text'] = $code['code_text'];
387 $this->templateData
['field_name_value_array']['encounter'][$i]['provider_npi'] = $entry['encounter']['performer']['assignedEntity']['id']['extension'] ??
null;
388 $this->templateData
['field_name_value_array']['encounter'][$i]['provider_name'] = $entry['encounter']['performer']['assignedEntity']['assignedPerson']['name']['given'] ??
''; // first
389 $this->templateData
['field_name_value_array']['encounter'][$i]['provider_family'] = $entry['encounter']['performer']['assignedEntity']['assignedPerson']['name']['family'] ??
''; // last
390 $this->templateData
['field_name_value_array']['encounter'][$i]['provider_address'] = $entry['encounter']['performer']['assignedEntity']['addr']['streetAddressLine'] ??
'';
391 $this->templateData
['field_name_value_array']['encounter'][$i]['provider_city'] = $entry['encounter']['performer']['assignedEntity']['addr']['city'] ??
'';
392 $this->templateData
['field_name_value_array']['encounter'][$i]['provider_state'] = $entry['encounter']['performer']['assignedEntity']['addr']['state'] ??
'';
393 $this->templateData
['field_name_value_array']['encounter'][$i]['provider_postalCode'] = $entry['encounter']['performer']['assignedEntity']['addr']['postalCode'] ??
'';
394 $this->templateData
['field_name_value_array']['encounter'][$i]['provider_country'] = $entry['encounter']['performer']['assignedEntity']['addr']['country'] ??
'';
395 $this->templateData
['field_name_value_array']['encounter'][$i]['represented_organization_name'] = $entry['encounter']['participant']['participantRole']['playingEntity']['name'] ??
'';
396 $this->templateData
['field_name_value_array']['encounter'][$i]['represented_organization_address'] = $entry['encounter']['participant']['participantRole']['addr']['streetAddressLine'] ??
'';
397 $this->templateData
['field_name_value_array']['encounter'][$i]['represented_organization_city'] = $entry['encounter']['participant']['participantRole']['addr']['city'] ??
'';
398 $this->templateData
['field_name_value_array']['encounter'][$i]['represented_organization_state'] = $entry['encounter']['participant']['participantRole']['addr']['state'] ??
'';
399 $this->templateData
['field_name_value_array']['encounter'][$i]['represented_organization_zip'] = $entry['encounter']['participant']['participantRole']['addr']['postalCode'] ??
'';
400 $this->templateData
['field_name_value_array']['encounter'][$i]['represented_organization_country'] = $entry['encounter']['participant']['participantRole']['addr']['country'] ??
'';
401 $this->templateData
['field_name_value_array']['encounter'][$i]['represented_organization_telecom'] = $entry['encounter']['participant']['participantRole']['telecom'] ??
'';
402 // encounter diagnosis to issues list
403 if (count($entry['encounter']['entryRelationship'] ??
[]) > 1 && !empty($entry['encounter']['entryRelationship'][0]['act'])) {
404 foreach ($entry['encounter']['entryRelationship'] as $k => $act) {
405 $code = $this->codeService
->resolveCode(
406 $act['act']['entryRelationship']['observation']['value']['code'] ??
'',
407 ($act['act']['entryRelationship']['observation']['value']['codeSystemName'] ??
'') ?
: $act['act']['entryRelationship']['observation']['value']['codeSystem'] ??
'',
408 $act['act']['entryRelationship']['observation']['value']['displayName'] ??
''
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'];
412 $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;
415 $code = $this->codeService
->resolveCode(
416 $entry['encounter']['entryRelationship']['act']['entryRelationship']['observation']['value']['code'] ??
'',
417 ($entry['encounter']['entryRelationship']['act']['entryRelationship']['observation']['value']['codeSystemName'] ??
'') ?
: $entry['encounter']['entryRelationship']['act']['entryRelationship']['observation']['value']['codeSystem'] ??
'',
418 $entry['encounter']['entryRelationship']['act']['entryRelationship']['observation']['value']['displayName'] ??
''
420 $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;
421 if (empty($code['code'])) {
422 $code = $this->codeService
->resolveCode(
423 $entry["encounter"]["entryRelationship"]["observation"]["value"]["code"] ??
'',
424 ($entry["encounter"]["entryRelationship"]["observation"]["value"]['codeSystemName'] ??
'') ?
: $entry["encounter"]["entryRelationship"]["observation"]["value"]['codeSystem'] ??
'',
425 $entry["encounter"]["entryRelationship"]["observation"]["value"]['displayName'] ??
''
427 $this->templateData
['field_name_value_array']['encounter'][$i]['encounter_diagnosis_date'] = $this->templateData
['field_name_value_array']['encounter'][$i]['date'] ??
null;
429 $this->templateData
['field_name_value_array']['encounter'][$i]['encounter_diagnosis_code'] = $code['formatted_code'];
430 $this->templateData
['field_name_value_array']['encounter'][$i]['encounter_diagnosis_issue'] = $code['code_text'];
433 $discharge = $entry['encounter']['sdtc:dischargeDispositionCode'] ??
null;
435 if (!empty($discharge)) {
436 $code = $this->codeService
->getCodeWithType(($discharge['code'] ??
''), ($discharge['codeSystemName'] ??
''), true) ??
'';
437 $option = $this->codeService
->dischargeOptionIdFromCode($code) ??
'';
438 if (empty($option)) {
439 $code = str_replace(" ", "-", $code); // Because "SNOMED CT" is "SNOMED-CT" in list options
440 $option = $this->codeService
->dischargeOptionIdFromCode($code) ??
'';
443 $this->templateData
['field_name_value_array']['encounter'][$i]['encounter_discharge_code'] = $option ??
'';
445 $this->templateData
['entry_identification_array']['encounter'][$i] = $i;
452 public function fetchMedicalProblemData($entry): void
454 if (!empty($entry['act']['entryRelationship']['observation']['value']['code'])) {
456 if (!empty($this->templateData
['field_name_value_array']['lists1'])) {
457 $i +
= count($this->templateData
['field_name_value_array']['lists1']);
459 $classification = 'diagnosis';
460 if ($this->currentOid
== '2.16.840.1.113883.10.20.24.3.138') {
461 $classification = 'concern';
463 $this->templateData
['field_name_value_array']['lists1'][$i]['subtype'] = $classification;
464 $code = $this->codeService
->resolveCode(
465 $entry['act']['entryRelationship']['observation']['value']['code'],
466 ($entry['act']['entryRelationship']['observation']['value']['codeSystemName'] ??
'') ?
: $entry['act']['entryRelationship']['observation']['value']['codeSystem'] ??
'',
467 $entry['act']['entryRelationship']['observation']['value']['displayName']
469 $this->templateData
['field_name_value_array']['lists1'][$i]['list_code'] = $code['formatted_code'] ?
: $entry['act']['entryRelationship']['observation']['value']['code'] ??
'';
470 $this->templateData
['field_name_value_array']['lists1'][$i]['list_code_text'] = $code['code_text'] ?
: $entry['act']['entryRelationship']['observation']['value']['displayName'] ??
'';
472 $this->templateData
['field_name_value_array']['lists1'][$i]['type'] = 'medical_problem';
473 $this->templateData
['field_name_value_array']['lists1'][$i]['extension'] = $entry['act']['id']['extension'] ??
null;
474 $this->templateData
['field_name_value_array']['lists1'][$i]['root'] = $entry['act']['id']['root'] ??
null;
475 $this->templateData
['field_name_value_array']['lists1'][$i]['begdate'] = $entry['act']['effectiveTime']['low']['value'] ??
null;
476 $this->templateData
['field_name_value_array']['lists1'][$i]['enddate'] = $entry['act']['effectiveTime']['high']['value'] ??
null;
477 $this->templateData
['field_name_value_array']['lists1'][$i]['observation'] = $entry['act']['entryRelationship']['observation']['entryRelationship'][1]['observation']['value']['code'] ??
null;
478 $this->templateData
['field_name_value_array']['lists1'][$i]['observation_text'] = $entry['act']['entryRelationship']['observation']['entryRelationship'][1]['observation']['value']['displayName'] ??
null;
479 $this->templateData
['field_name_value_array']['lists1'][$i]['status'] = ($entry['act']['entryRelationship']['observation']['entryRelationship'][2]['observation']['value']['displayName'] ??
'') ?
: $entry['act']['entryRelationship']['observation']['statusCode'] ??
null;
480 $this->templateData
['field_name_value_array']['lists1'][$i]['modified_time'] = $entry['act']['entryRelationship']['observation']['performer']['assignedEntity']['time']['value'] ??
null;
481 $this->templateData
['entry_identification_array']['lists1'][$i] = $i;
488 public function fetchAllergyIntoleranceObservation($entry)
490 if (!empty($entry['act']['entryRelationship']['observation']['participant']['participantRole']['playingEntity']['code']['code'])) {
492 // if there are already items here we want to add to them.
493 if (!empty($this->templateData
['field_name_value_array']['lists2'])) {
494 $i +
= count($this->templateData
['field_name_value_array']['lists2']);
497 $this->templateData
['field_name_value_array']['lists2'][$i]['type'] = 'allergy';
498 $this->templateData
['field_name_value_array']['lists2'][$i]['extension'] = $entry['act']['id']['extension'] ??
'';
499 $this->templateData
['field_name_value_array']['lists2'][$i]['begdate'] = $entry['act']['effectiveTime']['low']['value'] ??
null;
500 $this->templateData
['field_name_value_array']['lists2'][$i]['enddate'] = $entry['act']['effectiveTime']['high']['value'] ??
null;
501 $this->templateData
['field_name_value_array']['lists2'][$i]['list_code'] = $entry['act']['entryRelationship']['observation']['participant']['participantRole']['playingEntity']['code']['code'] ??
'';
502 $this->templateData
['field_name_value_array']['lists2'][$i]['list_code_text'] = $entry['act']['entryRelationship']['observation']['participant']['participantRole']['playingEntity']['code']['displayName'];
503 $this->templateData
['field_name_value_array']['lists2'][$i]['codeSystemName'] = $entry['act']['entryRelationship']['observation']['participant']['participantRole']['playingEntity']['code']['codeSystemName'] ??
'';
504 $this->templateData
['field_name_value_array']['lists2'][$i]['outcome'] = $entry['act']['entryRelationship']['observation']['entryRelationship'][1]['observation']['value']['code'] ??
'';
505 $this->templateData
['field_name_value_array']['lists2'][$i]['severity_al_code'] = $entry['act']['entryRelationship']['observation']['entryRelationship'][2]['observation']['value']['code'] ??
'';
506 $this->templateData
['field_name_value_array']['lists2'][$i]['severity_al'] = $entry['act']['entryRelationship']['observation']['entryRelationship'][2]['observation']['value']['code'] ??
'';
507 $this->templateData
['field_name_value_array']['lists2'][$i]['status'] = $entry['act']['entryRelationship']['observation']['entryRelationship'][0]['observation']['value']['displayName'] ??
'';
508 $this->templateData
['field_name_value_array']['lists2'][$i]['reaction'] = $entry['act']['entryRelationship']['observation']['entryRelationship'][1]['observation']['value']['code'] ??
'';
509 $this->templateData
['field_name_value_array']['lists2'][$i]['reaction_text'] = $entry['act']['entryRelationship']['observation']['entryRelationship'][1]['observation']['value']['displayName'] ??
'';
510 $this->templateData
['field_name_value_array']['lists2'][$i]['modified_time'] = $entry['act']['entryRelationship']['observation']['performer']['assignedEntity']['time']['value'] ??
null;
511 $this->templateData
['entry_identification_array']['lists2'][$i] = $i;
512 } elseif (!empty($entry['observation']['participant']['participantRole']['playingEntity']['code']['code'])) {
514 // if there are already items here we want to add to them.
515 if (!empty($this->templateData
['field_name_value_array']['lists2'])) {
516 $i +
= count($this->templateData
['field_name_value_array']['lists2']);
519 $this->templateData
['field_name_value_array']['lists2'][$i]['type'] = 'allergy';
520 $this->templateData
['field_name_value_array']['lists2'][$i]['extension'] = $entry['observation']['id']['extension'] ??
null;
521 $this->templateData
['field_name_value_array']['lists2'][$i]['begdate'] = $entry['observation']['effectiveTime']['low']['value'] ??
null;
522 $this->templateData
['field_name_value_array']['lists2'][$i]['enddate'] = $entry['observation']['effectiveTime']['high']['value'] ??
null;
523 $this->templateData
['field_name_value_array']['lists2'][$i]['status'] = $entry['observation']['statusCode']['code'] ??
null;
525 $code = $this->codeService
->resolveCode(
526 $entry['observation']['participant']['participantRole']['playingEntity']['code']['code'] ??
null,
527 $entry['observation']['participant']['participantRole']['playingEntity']['code']['codeSystemName'] ??
null,
528 $entry['observation']['participant']['participantRole']['playingEntity']['code']['displayName'] ?
: $entry['observation']['participant']['participantRole']['playingEntity']['name'] ??
null
530 $this->templateData
['field_name_value_array']['lists2'][$i]['list_code'] = $code['formatted_code'];
531 $this->templateData
['field_name_value_array']['lists2'][$i]['list_code_text'] = $code['code_text'];
532 $this->templateData
['field_name_value_array']['lists2'][$i]['codeSystemName'] = $code['formatted_code_type'];
534 $this->templateData
['field_name_value_array']['lists2'][$i]['outcome'] = $entry['observation']['entryRelationship'][1]['observation']['value']['code'] ??
null;
535 $this->templateData
['field_name_value_array']['lists2'][$i]['severity_al_code'] = $entry['observation']['entryRelationship'][2]['observation']['value']['code'] ??
null;
536 $this->templateData
['field_name_value_array']['lists2'][$i]['severity_al'] = $entry['observation']['entryRelationship'][2]['observation']['value']['code'] ??
null;
537 $this->templateData
['field_name_value_array']['lists2'][$i]['status'] = $entry['observation']['entryRelationship'][0]['observation']['value']['displayName'] ??
null;
538 $this->templateData
['field_name_value_array']['lists2'][$i]['reaction'] = $entry['observation']['entryRelationship'][1]['observation']['value']['code'] ??
null;
539 $this->templateData
['field_name_value_array']['lists2'][$i]['reaction_text'] = $entry['observation']['entryRelationship'][1]['observation']['value']['displayName'] ??
null;
540 $this->templateData
['field_name_value_array']['lists2'][$i]['modified_time'] = $entry['observation']['performer']['assignedEntity']['time']['value'] ??
null;
541 $this->templateData
['entry_identification_array']['lists2'][$i] = $i;
548 public function fetchMedicationData($entry): void
550 // @todo provider not correct.
551 if (!empty($entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['code'])) {
553 if (!empty($this->templateData
['field_name_value_array']['lists3'])) {
554 $i +
= count($this->templateData
['field_name_value_array']['lists3']);
557 $substanceAdministration_oids = array(
558 '2.16.840.1.113883.10.20.24.3.41' => 'active',
559 '2.16.840.1.113883.10.20.24.3.42' => 'administered',
560 '2.16.840.1.113883.10.20.24.3.139' => 'dispensed',
561 '2.16.840.1.113883.10.20.24.3.105' => 'discharge',
564 if ($this->is_qrda_import
) {
565 if (!empty($entry['substanceAdministration']['templateId']['root'])) {
566 $request_type = $substanceAdministration_oids[$entry['substanceAdministration']['templateId']['root']];
567 } elseif (!empty($entry['substanceAdministration']['templateId'][1]['root'])) {
568 $request_type = $substanceAdministration_oids[$entry['substanceAdministration']['templateId'][1]['root']];
572 $code = $this->codeService
->resolveCode(
573 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['code'],
575 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['displayName']
578 $this->templateData
['field_name_value_array']['lists3'][$i]['type'] = 'medication';
579 $this->templateData
['field_name_value_array']['lists3'][$i]['request_type'] = $request_type;
580 $this->templateData
['field_name_value_array']['lists3'][$i]['extension'] = $entry['substanceAdministration']['id']['extension'] ??
null;
581 $this->templateData
['field_name_value_array']['lists3'][$i]['root'] = $entry['substanceAdministration']['id']['root'] ??
null;
583 $this->templateData
['field_name_value_array']['lists3'][$i]['begdate'] = date('Y-m-d');
584 if (!empty($entry['substanceAdministration']['effectiveTime'][0]['low']['value'])) {
585 $this->templateData
['field_name_value_array']['lists3'][$i]['begdate'] = $entry['substanceAdministration']['effectiveTime'][0]['low']['value'];
586 $this->templateData
['field_name_value_array']['lists3'][$i]['enddate'] = $entry['substanceAdministration']['effectiveTime'][0]['high']['value'] ??
null;
587 } elseif (!empty($entry['substanceAdministration']['effectiveTime']['low']['value'])) {
588 $this->templateData
['field_name_value_array']['lists3'][$i]['begdate'] = $entry['substanceAdministration']['effectiveTime']['low']['value'];
589 $this->templateData
['field_name_value_array']['lists3'][$i]['enddate'] = $entry['substanceAdministration']['effectiveTime']['high']['value'] ??
null;
592 $this->templateData
['field_name_value_array']['lists3'][$i]['route'] = $entry['substanceAdministration']['routeCode']['code'] ??
null;
593 $this->templateData
['field_name_value_array']['lists3'][$i]['route_display'] = $entry['substanceAdministration']['routeCode']['displayName'] ??
null;
594 $this->templateData
['field_name_value_array']['lists3'][$i]['dose'] = $entry['substanceAdministration']['doseQuantity']['value'] ??
null;
595 $this->templateData
['field_name_value_array']['lists3'][$i]['dose_unit'] = $entry['substanceAdministration']['doseQuantity']['unit'] ??
null;
596 $this->templateData
['field_name_value_array']['lists3'][$i]['rate'] = $entry['substanceAdministration']['rateQuantity']['value'] ??
null;
597 $this->templateData
['field_name_value_array']['lists3'][$i]['rate_unit'] = $entry['substanceAdministration']['rateQuantity']['unit'] ??
null;
598 $this->templateData
['field_name_value_array']['lists3'][$i]['drug_code'] = $code['code'];
599 $this->templateData
['field_name_value_array']['lists3'][$i]['drug_text'] = $code['code_text'];
600 $this->templateData
['field_name_value_array']['lists3'][$i]['note'] = $entry['substanceAdministration']['text']['reference']['value'] ??
null;
601 $this->templateData
['field_name_value_array']['lists3'][$i]['indication'] = $entry['substanceAdministration']['entryRelationship'][0]['observation']['value']['displayName'] ??
($entry['substanceAdministration']['entryRelationship']['observation']['value']['displayName'] ??
null);
602 $this->templateData
['field_name_value_array']['lists3'][$i]['prn'] = $entry['substanceAdministration']['precondition']['criterion']['value']['displayName'] ??
null;
603 $this->templateData
['field_name_value_array']['lists3'][$i]['modified_time'] = $entry['substanceAdministration']['entryRelationship'][1]['supply']['author']['time']['value'] ??
null;
605 $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);
606 $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);
607 $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);
608 $this->templateData
['field_name_value_array']['lists3'][$i]['provider_root'] = $entry['substanceAdministration']['entryRelationship'][1]['supply']['author']['assignedAuthor']['id']['root'] ??
null;
609 $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);
610 $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);
611 $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);
612 $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);
613 $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);
614 $this->templateData
['entry_identification_array']['lists3'][$i] = $i;
621 public function fetchImmunizationData($entry): void
624 !empty($entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['code'])
625 ||
!empty($entry['substanceAdministration']['negationInd']) == "true"
628 if (!empty($this->templateData
['field_name_value_array']['immunization'])) {
629 $i +
= count($this->templateData
['field_name_value_array']['immunization']);
631 $this->templateData
['field_name_value_array']['immunization'][$i]['extension'] = $entry['substanceAdministration']['id']['extension'] ??
null;
632 $this->templateData
['field_name_value_array']['immunization'][$i]['root'] = $entry['substanceAdministration']['id']['root'] ??
null;
633 $this->templateData
['field_name_value_array']['immunization'][$i]['administered_date'] = $entry['substanceAdministration']['effectiveTime']['value'] ?
: $entry['substanceAdministration']['effectiveTime']['low']['value'] ??
null;
634 $this->templateData
['field_name_value_array']['immunization'][$i]['route_code'] = $entry['substanceAdministration']['routeCode']['code'] ??
null;
635 $this->templateData
['field_name_value_array']['immunization'][$i]['route_code_text'] = $entry['substanceAdministration']['routeCode']['displayName'] ??
null;
637 $code = $this->codeService
->resolveCode(
638 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['code'] ??
null,
639 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['codeSystemName'] ??
null,
640 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['displayName']
642 $code = $this->codeService
->resolveCode(
643 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['code'],
644 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['codeSystemName'] ??
$entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['codeSystem'],
645 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['displayName']
647 if (!empty($entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']["nullFlavor"])) {
648 $code['code'] = 'OID:' . $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']["valueSet"] ??
null;
649 $code['formatted_code'] = 'OID:' . $code['code'];
650 $code['code_text'] = $entry['substanceAdministration']['text'] ??
'';
652 $this->templateData
['field_name_value_array']['immunization'][$i]['cvx_code'] = $code['code'];
653 $this->templateData
['field_name_value_array']['immunization'][$i]['cvx_code_text'] = $code['code_text'];
654 $this->templateData
['field_name_value_array']['immunization'][$i]['amount_administered'] = $entry['substanceAdministration']['doseQuantity']['value'] ??
null;
655 $this->templateData
['field_name_value_array']['immunization'][$i]['amount_administered_unit'] = $entry['substanceAdministration']['doseQuantity']['unit'] ??
null;
656 $this->templateData
['field_name_value_array']['immunization'][$i]['completion_status'] = $entry['substanceAdministration']['statusCode']['code'] ??
null;
657 $this->templateData
['field_name_value_array']['immunization'][$i]['manufacturer'] = $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturerOrganization']['name'] ??
null;
658 $this->templateData
['field_name_value_array']['immunization'][$i]['provider_npi'] = $entry['substanceAdministration']['performer']['assignedEntity']['id']['extension'] ??
null;
659 $this->templateData
['field_name_value_array']['immunization'][$i]['provider_name'] = $entry['substanceAdministration']['performer']['assignedEntity']['assignedPerson']['name']['given'] ??
null;
660 $this->templateData
['field_name_value_array']['immunization'][$i]['provider_family'] = $entry['substanceAdministration']['performer']['assignedEntity']['assignedPerson']['name']['family'] ??
null;
661 $this->templateData
['field_name_value_array']['immunization'][$i]['provider_address'] = $entry['substanceAdministration']['performer']['assignedEntity']['addr']['streetAddressLine'] ??
null;
662 $this->templateData
['field_name_value_array']['immunization'][$i]['provider_city'] = $entry['substanceAdministration']['performer']['assignedEntity']['addr']['city'] ??
null;
663 $this->templateData
['field_name_value_array']['immunization'][$i]['provider_state'] = $entry['substanceAdministration']['performer']['assignedEntity']['addr']['state'] ??
null;
664 $this->templateData
['field_name_value_array']['immunization'][$i]['provider_postalCode'] = $entry['substanceAdministration']['performer']['assignedEntity']['addr']['postalCode'] ??
null;
665 $this->templateData
['field_name_value_array']['immunization'][$i]['provider_country'] = $entry['substanceAdministration']['performer']['assignedEntity']['addr']['country'] ??
null;
666 $this->templateData
['field_name_value_array']['immunization'][$i]['provider_telecom'] = $entry['substanceAdministration']['performer']['assignedEntity']['telecom']['value'] ??
null;
667 $this->templateData
['field_name_value_array']['immunization'][$i]['represented_organization'] = $entry['substanceAdministration']['performer']['assignedEntity']['representedOrganization']['name'] ??
null;
668 $this->templateData
['field_name_value_array']['immunization'][$i]['represented_organization_tele'] = $entry['substanceAdministration']['performer']['assignedEntity']['representedOrganization']['telecom'] ??
null;
670 if (($entry['substanceAdministration']['entryRelationship'] ??
'') && $entry['substanceAdministration']['entryRelationship']['observation']['value']['code']) {
671 $code = $this->codeService
->resolveCode(
672 $entry['substanceAdministration']['entryRelationship']['observation']['value']['code'],
673 $entry['substanceAdministration']['entryRelationship']['observation']['value']['codeSystemName'] ?
: $entry['substanceAdministration']['entryRelationship']['observation']['value']['codeSystem'] ??
'',
674 $entry['substanceAdministration']['entryRelationship']['observation']['value']['displayName']
676 $this->templateData
['field_name_value_array']['immunization'][$i]['reason_code'] = $code['formatted_code'];
677 $this->templateData
['field_name_value_array']['immunization'][$i]['reason_code_text'] = $code['code_text'];
678 $this->templateData
['field_name_value_array']['immunization'][$i]['reason_description'] = $code['code_text'] ??
$entry['observation']['text'];
679 $date_low = $entry['substanceAdministration']['entryRelationship']['observation']['effectiveTime']['low']['value'] ??
null;
680 $date_high = $entry['substanceAdministration']['entryRelationship']['observation']['effectiveTime']['high']['value'] ??
null;
681 $this->templateData
['field_name_value_array']['immunization'][$i]['reason_date_low'] = $date_low;
682 $this->templateData
['field_name_value_array']['immunization'][$i]['reason_date_high'] = $date_high;
685 $this->templateData
['field_name_value_array']['immunization'][$i]['reason_status'] = (($entry['substanceAdministration']['negationInd'] ??
'false') == 'true') ?
'negated' : null;
686 $this->templateData
['entry_identification_array']['immunization'][$i] = $i;
693 public function fetchProcedureActivityData($entry): void
696 (!empty($entry['procedure']['code']['code']) ||
!empty($entry['procedure']['code']["nullFlavor"] ??
''))
697 && ($entry['procedure']['code']["nullFlavor"] ??
'') != 'NI'
700 if (!empty($this->templateData
['field_name_value_array']['procedure'])) {
701 $i +
= count($this->templateData
['field_name_value_array']['procedure']);
704 $code = $this->codeService
->resolveCode(
705 $entry['procedure']['code']['code'] ??
'',
706 ($entry['procedure']['code']['codeSystemName'] ??
'') ?
: $entry['procedure']['code']['codeSystem'] ??
null,
707 $entry['procedure']['code']['displayName'] ??
''
709 if (!empty($entry['procedure']['code']["nullFlavor"]) && !empty($entry['procedure']['code']["valueSet"])) {
710 $code['code'] = $entry['procedure']['code']["valueSet"] ??
null;
711 $code['formatted_code'] = 'OID:' . $entry['procedure']['code']["valueSet"] ??
null;
712 $code['code_text'] = $entry['procedure']['text'] ??
'';
715 $procedure_type = 'order';
717 $this->templateData
['field_name_value_array']['procedure'][$i]['extension'] = $entry['procedure']['id']['extension'] ??
null;
718 $this->templateData
['field_name_value_array']['procedure'][$i]['root'] = $entry['procedure']['id']['root'] ??
null;
720 $this->templateData
['field_name_value_array']['procedure'][$i]['code'] = $code['formatted_code'];
721 $this->templateData
['field_name_value_array']['procedure'][$i]['code_text'] = $code['code_text'];
722 $this->templateData
['field_name_value_array']['procedure'][$i]['codeSystemName'] = $code['formatted_code_type'];
724 $this->templateData
['field_name_value_array']['procedure'][$i]['procedure_type'] = $procedure_type;
725 $this->templateData
['field_name_value_array']['procedure'][$i]['status'] = $entry['procedure']['statusCode']['code'] ??
'';
727 if (!empty($entry['procedure']['effectiveTime']['low']['value'])) {
728 $this->templateData
['field_name_value_array']['procedure'][$i]['date'] = $entry['procedure']['effectiveTime']['low']['value'] ??
null;
729 $this->templateData
['field_name_value_array']['procedure'][$i]['date_end'] = $entry['procedure']['effectiveTime']['high']['value'] ??
null;
731 $this->templateData
['field_name_value_array']['procedure'][$i]['date'] = $entry['procedure']['effectiveTime']['value'] ??
null;
734 // check for a reason code if observation.
735 if (is_array($entry['procedure']['entryRelationship'] ??
'')) {
736 $entryRelationship = $entry['procedure']['entryRelationship'][1];
738 $entryRelationship = $entry['procedure']['entryRelationship'] ??
'';
740 if ($entryRelationship['observation']['value']['code'] ??
'') {
741 $code = $this->codeService
->resolveCode(
742 $entryRelationship['observation']['value']['code'],
743 $entryRelationship['observation']['value']['codeSystemName'] ?
: $entryRelationship['observation']['value']['codeSystem'] ??
'',
744 $entryRelationship['observation']['value']['displayName']
746 $this->templateData
['field_name_value_array']['procedure'][$i]['reason_code'] = $code['formatted_code'];
747 $this->templateData
['field_name_value_array']['procedure'][$i]['reason_code_text'] = $code['code_text'];
748 $this->templateData
['field_name_value_array']['procedure'][$i]['reason_description'] = $code['code_text'] ??
$entry['observation']['text'];
749 $date_low = $entryRelationship['observation']['effectiveTime']['low']['value'] ??
null;
750 $date_high = $entryRelationship['observation']['effectiveTime']['high']['value'] ??
null;
751 $this->templateData
['field_name_value_array']['procedure'][$i]['reason_date_low'] = $date_low;
752 $this->templateData
['field_name_value_array']['procedure'][$i]['reason_date_high'] = $date_high;
755 $this->templateData
['field_name_value_array']['procedure'][$i]['reason_status'] = (($entry['procedure']['negationInd'] ??
'false') == 'true') ?
'negated' : null;
757 $this->templateData
['field_name_value_array']['procedure'][$i]['represented_organization1'] = $entry['procedure']['performer']['assignedEntity']['representedOrganization']['name'] ??
null;
758 $this->templateData
['field_name_value_array']['procedure'][$i]['represented_organization_address1'] = $entry['procedure']['performer']['assignedEntity']['addr']['streetAddressLine'] ??
null;
759 $this->templateData
['field_name_value_array']['procedure'][$i]['represented_organization_city1'] = $entry['procedure']['performer']['assignedEntity']['addr']['city'] ??
null;
760 $this->templateData
['field_name_value_array']['procedure'][$i]['represented_organization_state1'] = $entry['procedure']['performer']['assignedEntity']['addr']['state'] ??
null;
761 $this->templateData
['field_name_value_array']['procedure'][$i]['represented_organization_postalcode1'] = $entry['procedure']['performer']['assignedEntity']['addr']['postalCode'] ??
null;
762 $this->templateData
['field_name_value_array']['procedure'][$i]['represented_organization_country1'] = $entry['procedure']['performer']['assignedEntity']['addr']['country'] ??
null;
763 $this->templateData
['field_name_value_array']['procedure'][$i]['represented_organization_telecom1'] = $entry['procedure']['performer']['assignedEntity']['telecom']['value'] ??
null;
765 $this->templateData
['field_name_value_array']['procedure'][$i]['represented_organization2'] = $entry['procedure']['participant']['participantRole']['playingEntity']['name'] ??
null;
766 $this->templateData
['field_name_value_array']['procedure'][$i]['represented_organization_address2'] = $entry['procedure']['participant']['participantRole']['addr']['streetAddressLine'] ??
null;
767 $this->templateData
['field_name_value_array']['procedure'][$i]['represented_organization_city2'] = $entry['procedure']['participant']['participantRole']['addr']['city'] ??
null;
768 $this->templateData
['field_name_value_array']['procedure'][$i]['represented_organization_state2'] = $entry['procedure']['participant']['participantRole']['addr']['state'] ??
null;
769 $this->templateData
['field_name_value_array']['procedure'][$i]['represented_organization_postalcode2'] = $entry['procedure']['participant']['participantRole']['addr']['postalCode'] ??
null;
770 $this->templateData
['field_name_value_array']['procedure'][$i]['represented_organization_country2'] = $entry['procedure']['participant']['participantRole']['addr']['country'] ??
null;
772 $this->templateData
['entry_identification_array']['procedure'][$i] = $i;
779 public function fetchProcedureDeviceData($entry): void
781 if (!empty($entry['procedure']['code']['code']) && (($entry['procedure']['negationInd'] ??
'false') != 'true')) {
783 if (!empty($this->templateData
['field_name_value_array']['procedure'])) {
784 $i +
= count($this->templateData
['field_name_value_array']['procedure']);
787 // future may need device status code
788 /*$code_proc_type = $this->codeService->resolveCode(
789 $entry['procedure']['code']['code'] ?? '',
790 $entry['procedure']['code']['codeSystemName'] ?: $entry['procedure']['code']['codeSystem'] ?? null,
791 $entry['procedure']['code']['displayName'] ?? ''
794 $code = $this->codeService
->resolveCode(
795 $entry['procedure']['participant']['participantRole']['playingDevice']['code']['code'] ??
'',
796 $entry['procedure']['participant']['participantRole']['playingDevice']['code']['codeSystem'] ??
null,
797 $entry['procedure']['participant']['participantRole']['playingDevice']['code']['displayName'] ??
''
800 $this->templateData
['field_name_value_array']['procedure'][$i]['procedure_type'] = 'device';
802 $this->templateData
['field_name_value_array']['procedure'][$i]['status'] = $entry['procedure']['statusCode']['code'] ??
'';
804 $this->templateData
['field_name_value_array']['procedure'][$i]['extension'] = $entry['procedure']['id']['extension'] ??
null;
805 $this->templateData
['field_name_value_array']['procedure'][$i]['root'] = $entry['procedure']['id']['root'] ??
null;
807 $this->templateData
['field_name_value_array']['procedure'][$i]['code'] = $code['code'];
808 $this->templateData
['field_name_value_array']['procedure'][$i]['code_text'] = $code['code_text'];
809 $this->templateData
['field_name_value_array']['procedure'][$i]['codeSystemName'] = $code['formatted_code_type'];
811 if (!empty($entry['procedure']['effectiveTime']['low']['value'])) {
812 $this->templateData
['field_name_value_array']['procedure'][$i]['date'] = $entry['procedure']['effectiveTime']['low']['value'] ??
null;
814 $this->templateData
['field_name_value_array']['procedure'][$i]['date'] = $entry['procedure']['effectiveTime']['value'] ??
null;
817 $this->templateData
['entry_identification_array']['procedure'][$i] = $i;
824 public function fetchProcedurePreformedActivity($entry): void
826 if (!empty($entry['act']['code']['code']) ||
$entry['act']['negationInd'] ??
'false' == 'true') {
828 if (!empty($this->templateData
['field_name_value_array']['procedure'])) {
829 $i +
= count($this->templateData
['field_name_value_array']['procedure']);
832 $procedure_type = 'intervention';
833 if (!empty($entry['act']['templateId'][1]['root']) && $entry['act']['templateId'][1]['root'] == '2.16.840.1.113883.10.20.24.3.32') {
834 $procedure_type = 'intervention';
837 $code = $this->codeService
->resolveCode(
838 $entry['act']['code']['code'] ??
'',
839 $entry['act']['code']['codeSystemName'] ?
: $entry['act']['code']['codeSystem'] ??
null,
840 $entry['act']['code']['displayName'] ??
$entry['act']['text']
843 if (!empty($entry["act"]["code"]["nullFlavor"]) && !empty($entry['act']['code']["valueSet"])) {
844 $code['code'] = 'OID:' . $entry["act"]["code"]["valueSet"] ??
null;
845 $code['formatted_code'] = 'OID:' . $entry["act"]["code"]["valueSet"] ??
null;
846 $code['code_text'] = $entry['act']['text'] ??
'';
849 $this->templateData
['field_name_value_array']['procedure'][$i]['procedure_type'] = $procedure_type;
851 $this->templateData
['field_name_value_array']['procedure'][$i]['status'] = $entry['act']['statusCode']['code'] ??
'';
853 $this->templateData
['field_name_value_array']['procedure'][$i]['extension'] = $entry['act']['id']['extension'] ??
null;
854 $this->templateData
['field_name_value_array']['procedure'][$i]['root'] = $entry['act']['id']['root'] ??
null;
856 $this->templateData
['field_name_value_array']['procedure'][$i]['code'] = $code['code'] ??
'';
857 $this->templateData
['field_name_value_array']['procedure'][$i]['code_text'] = $entry['act']['text'] ??
$code['code_text'] ??
'';
858 $this->templateData
['field_name_value_array']['procedure'][$i]['codeSystemName'] = $code['formatted_code_type'] ??
'';
860 if (!empty($entry['act']['effectiveTime']['low']['value'])) {
861 $this->templateData
['field_name_value_array']['procedure'][$i]['date'] = $entry['act']['effectiveTime']['low']['value'] ??
null;
862 $this->templateData
['field_name_value_array']['procedure'][$i]['date_end'] = $entry['act']['effectiveTime']['high']['value'] ??
null;
864 $this->templateData
['field_name_value_array']['procedure'][$i]['date'] = $entry['act']['effectiveTime']['value'] ??
null;
867 $reason_code = $entry["act"]["entryRelationship"]["observation"]["value"]["code"] ??
null;
869 $reason_system = $entry["act"]["entryRelationship"]["observation"]["value"]["codeSystem"];
870 $reason_name = $entry["act"]["entryRelationship"]["observation"]["value"]["codeSystemName"];
871 $code = $this->codeService
->resolveCode(
873 $reason_name ?
: $reason_system ??
'',
876 $this->templateData
['field_name_value_array']['procedure'][$i]['reason_code'] = $code['formatted_code'];
877 $this->templateData
['field_name_value_array']['procedure'][$i]['reason_code_text'] = $code['code_text'];
878 $this->templateData
['field_name_value_array']['procedure'][$i]['reason_description'] = $code['code_text'] ??
$entry['act']['text'];
879 $date_low = $entry["act"]["entryRelationship"]["observation"]['effectiveTime']['low']['value'] ??
null;
880 $date_high = $entry["act"]["entryRelationship"]["observation"]['effectiveTime']['high']['value'] ??
null;
881 $this->templateData
['field_name_value_array']['procedure'][$i]['reason_date_low'] = $date_low;
882 $this->templateData
['field_name_value_array']['procedure'][$i]['reason_date_high'] = $date_high;
884 $this->templateData
['field_name_value_array']['procedure'][$i]['reason_status'] = (($entry['act']['negationInd'] ??
'false') == 'true') ?
'negated' : null;
885 $this->templateData
['entry_identification_array']['procedure'][$i] = $i;
889 public function allergy($component)
891 if (!empty($component['section']['entry'][0])) {
892 foreach ($component['section']['entry'] as $key => $value) {
893 $this->fetchAllergyIntoleranceObservation($value);
896 $this->fetchAllergyIntoleranceObservation($component['section']['entry'] ??
null);
900 public function medication($component)
902 $component['section']['text'] = '';
903 if (!empty($component['section']['entry'][0])) {
904 foreach ($component['section']['entry'] as $key => $value) {
905 $this->fetchMedicationData($value);
908 $this->fetchMedicationData($component['section']['entry'] ??
null);
912 public function medical_problem($component)
914 $component['section']['text'] = '';
915 if (!empty($component['section']['entry'][0])) {
916 foreach ($component['section']['entry'] as $key => $value) {
917 $this->fetchMedicalProblemData($value);
920 $this->fetchMedicalProblemData($component['section']['entry'] ??
null);
924 public function immunization($component)
926 $component['section']['text'] = '';
927 if (!empty($component['section']['entry'][0])) {
928 foreach ($component['section']['entry'] as $key => $value) {
929 $this->fetchImmunizationData($value);
932 $this->fetchImmunizationData($component['section']['entry'] ??
'');
936 public function procedure($component)
938 $component['section']['text'] = '';
939 if (!empty($component['section']['entry'][0])) {
940 foreach ($component['section']['entry'] as $key => $value) {
942 // TODO add two additional templates for type if needed in future.
943 //continue; //every third entry section has the procedure details
946 $this->fetchProcedureActivityData($value);
949 $this->fetchProcedureActivityData($component['section']['entry'] ??
null);
953 public function labResult($component): void
955 $component['section']['text'] = '';
957 if (!empty($component['section']['entry'][0])) {
958 foreach ($component['section']['entry'] as $key => $value) {
959 $this->fetchLabResultData($value);
962 $this->fetchLabResultData($component['section']['entry'] ??
null);
966 public function fetchLabResultData($lab_result_data): void
969 if (!empty($this->templateData
['field_name_value_array']['procedure_result'])) {
970 $i +
= count($this->templateData
['field_name_value_array']['procedure_result']);
972 if (!empty($lab_result_data['organizer']['component'])) {
973 foreach ($lab_result_data['organizer']['component'] as $key => $value) {
974 if (empty($value['observation'])) {
975 $value = [$key => $value];
977 if (!empty($value['observation']['code']['code'])) {
978 $code = $this->codeService
->resolveCode(
979 $lab_result_data['organizer']['code']['code'] ??
'',
980 ($lab_result_data['organizer']['code']['codeSystemName'] ??
'') ?
: $lab_result_data['organizer']['code']['codeSystem'] ??
'',
981 $lab_result_data['organizer']['code']['displayName'] ??
''
983 if (empty($lab_result_data['organizer']['id']['extension'])) {
984 $lab_result_data['organizer']['id']['extension'] = $lab_result_data['organizer']['id']['root'] ??
null;
986 $this->templateData
['field_name_value_array']['procedure_result'][$i]['extension'] = $lab_result_data['organizer']['id']['extension'] ??
null;
987 $this->templateData
['field_name_value_array']['procedure_result'][$i]['root'] = $lab_result_data['organizer']['id']['root'] ??
null;
988 $this->templateData
['field_name_value_array']['procedure_result'][$i]['proc_code'] = $code['formatted_code'] ??
null;
989 $this->templateData
['field_name_value_array']['procedure_result'][$i]['proc_text'] = $code['code_text'] ??
null;
990 if (!empty($value['observation']['effectiveTime']['low']['value'])) {
991 $this->templateData
['field_name_value_array']['procedure_result'][$i]['date'] = $value['observation']['effectiveTime']['low']['value'];
993 $this->templateData
['field_name_value_array']['procedure_result'][$i]['date'] = $value['observation']['effectiveTime']['value'] ??
null;
996 $this->templateData
['field_name_value_array']['procedure_result'][$i]['status'] = $lab_result_data['organizer']['statusCode']['code'] ??
null;
998 if (empty($value['observation']['id']['extension'])) {
999 $value['observation']['id']['extension'] = $value['observation']['id']['root'] ??
null;
1001 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_extension'] = $value['observation']['id']['extension'] ??
null;
1002 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_root'] = $value['observation']['id']['root'] ??
null;
1003 // @TODO code lookup here
1004 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_code'] = $value['observation']['code']['code'] ??
null;
1005 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_text'] = $value['observation']['code']['displayName'] ??
null;
1006 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_date'] = $value['observation']['effectiveTime']['value'] ??
null;
1007 if ($value['observation']['value']['type'] == 'ST') {
1008 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_value'] = $value['observation']['value']['_'] ??
null;
1009 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_unit'] = 'UNK'; // must be set to save
1010 } elseif ($value['observation']['value']['type'] == 'CO') {
1011 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_value'] = $value['observation']['value']['displayName'] ??
null;
1012 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_unit'] = $value['observation']['value']['unit'] ?
: 'UNK';
1014 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_value'] = $value['observation']['value']['value'] ??
null;
1015 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_unit'] = $value['observation']['value']['unit'] ?
: 'UNK';
1017 if (!empty($value['observation']['referenceRange']['observationRange']['text'])) {
1018 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_range'] = $value['observation']['referenceRange']['observationRange']['text'];
1020 $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'] ??
'');
1023 $this->templateData
['entry_identification_array']['procedure_result'][$i] = $i;
1030 public function fetchQrdaLabResultData($entry)
1033 if (!empty($this->templateData
['field_name_value_array']['procedure_result'])) {
1034 $i +
= count($this->templateData
['field_name_value_array']['procedure_result']);
1036 if (!empty($entry['observation']['code']['code'])) {
1037 $code = $this->codeService
->resolveCode(
1038 $entry['observation']['code']['code'] ??
'',
1039 $entry['observation']['code']['codeSystemName'] ?
: $entry['observation']['code']['codeSystem'] ??
'',
1040 $entry['observation']['code']['displayName'] ??
''
1043 $this->templateData
['field_name_value_array']['procedure_result'][$i]['extension'] = $entry['observation']['id']['extension'] ??
null;
1044 $this->templateData
['field_name_value_array']['procedure_result'][$i]['root'] = $entry['observation']['id']['root'] ??
null;
1045 $this->templateData
['field_name_value_array']['procedure_result'][$i]['proc_code'] = $code['formatted_code'];
1046 $this->templateData
['field_name_value_array']['procedure_result'][$i]['proc_text'] = $code['code_text'];
1048 if (!empty($entry['observation']['effectiveTime']['low']['value'])) {
1049 $this->templateData
['field_name_value_array']['procedure_result'][$i]['date'] = $entry['observation']['effectiveTime']['low']['value'];
1051 $this->templateData
['field_name_value_array']['procedure_result'][$i]['date'] = $entry['observation']['effectiveTime']['value'] ??
null;
1053 $this->templateData
['field_name_value_array']['procedure_result'][$i]['status'] = $entry['observation']['statusCode']['code'] ??
null;
1055 $value = $entry['observation']['entryRelationship'] ??
null;
1056 if (!empty($value)) {
1057 // find the result template
1058 foreach ($entry['observation'] as $key => $find_value) {
1059 // check if a ccda result template
1061 foreach ($find_value as $key1 => $val) {
1062 if (is_array($val)) {
1063 if ($val['templateId'][0]['root'] == '2.16.840.1.113883.10.20.22.4.2') {
1067 if ($val['observation']['templateId'][0]['root'] == '2.16.840.1.113883.10.20.22.4.2') {
1079 if (empty($value)) {
1080 $this->templateData
['entry_identification_array']['procedure_result'][$i] = $i;
1083 $code = $this->codeService
->resolveCode(
1084 $value['observation']['code']['code'] ??
null,
1085 $value['observation']['code']['codeSystemName'] ??
$value['observation']['code']['codeSystem'] ??
'',
1086 $value['observation']['code']['displayName'] ??
''
1088 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_code'] = $code['formatted_code'];
1089 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_text'] = $code['code_text'];
1091 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_extension'] = $value['observation']['id']['extension'] ??
null;
1092 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_root'] = $value['observation']['id']['root'] ??
null;
1093 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_date'] = $value['observation']['effectiveTime']['value'] ??
null;
1094 if ($value['observation']['value']['type'] == 'ST') {
1095 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_value'] = $value['observation']['value']['_'] ??
null;
1096 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_unit'] = 'UNK'; // must be set to save
1098 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_value'] = $value['observation']['value']['value'] ??
null;
1099 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_unit'] = $value['observation']['value']['unit'] ?
: 'UNK';
1102 if (!empty($value['observation']['referenceRange']['observationRange']['text'])) {
1103 $this->templateData
['field_name_value_array']['procedure_result'][$i]['results_range'] = $value['observation']['referenceRange']['observationRange']['text'];
1105 $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'] ??
'');
1108 $this->templateData
['entry_identification_array']['procedure_result'][$i] = $i;
1112 public function VitalSign($component)
1114 $component['section']['text'] = '';
1115 if (!empty($component['section']['entry'][0])) {
1116 foreach ($component['section']['entry'] as $key => $value) {
1117 $this->fetchVitalSignData($value);
1120 $this->fetchVitalSignData($component['section']['entry'] ??
null);
1124 public function fetchVitalSignData($vital_sign_data)
1126 if (!empty($vital_sign_data['organizer']['component'][0]['observation']['effectiveTime']['value'])) {
1128 if (!empty($this->templateData
['field_name_value_array']['vital_sign'])) {
1129 $i +
= count($this->templateData
['field_name_value_array']['vital_sign']);
1131 $this->templateData
['field_name_value_array']['vital_sign'][$i]['extension'] = $vital_sign_data['organizer']['id']['extension'] ??
null;
1132 $this->templateData
['field_name_value_array']['vital_sign'][$i]['root'] = $vital_sign_data['organizer']['id']['root'] ??
null;
1133 $this->templateData
['field_name_value_array']['vital_sign'][$i]['date'] = $vital_sign_data['organizer']['component'][0]['observation']['effectiveTime']['value'] ??
null;
1134 $vitals_array = array(
1135 '8310-5' => 'temperature',
1138 '8287-5' => 'head_circ',
1139 '8867-4' => 'pulse',
1140 '8302-2' => 'height',
1141 '2710-2' => 'oxygen_saturation',
1142 '9279-1' => 'respiration',
1143 '3141-9' => 'weight',
1147 for ($j = 0; $j < 9; $j++
) {
1148 $code = $vital_sign_data['organizer']['component'][$j]['observation']['code']['code'] ??
null;
1149 if (!empty($vital_sign_data['organizer']['component'][$j]['observation']['entryRelationship'])) {
1150 $this->templateData
['field_name_value_array']['vital_sign'][$i]['bps'] = $vital_sign_data['organizer']['component'][$j]['observation']['entryRelationship'][0]['observation']['value']['value'] ??
null;
1151 $this->templateData
['field_name_value_array']['vital_sign'][$i]['bpd'] = $vital_sign_data['organizer']['component'][$j]['observation']['entryRelationship'][1]['observation']['value']['value'] ??
null;
1153 if (array_key_exists($code, $vitals_array)) {
1154 $this->templateData
['field_name_value_array']['vital_sign'][$i][$vitals_array[$code]] = $vital_sign_data['organizer']['component'][$j]['observation']['value']['value'] ??
null;
1159 $this->templateData
['entry_identification_array']['vital_sign'][$i] = $i;
1163 public function fetchPhysicalExamPerformedData($entry)
1165 // create an observation for this exam.
1166 $this->fetchObservationPerformedData($entry);
1167 // and a vital in vital forms.
1169 (!empty($entry['observation']['effectiveTime']['value']) && !empty($entry['observation']['value']['value']))
1170 ||
(!empty($entry['observation']['entryRelationship']['observation']['value']['code'] ??
null) && ($entry['observation']['entryRelationship']['typeCode'] ??
null) === 'RSON')
1173 if (!empty($this->templateData
['field_name_value_array']['vital_sign'])) {
1174 $cnt = count($this->templateData
['field_name_value_array']['vital_sign'] ??
[]);
1175 $v_date = $entry['observation']['effectiveTime']['value'] ??
null;
1176 for ($c = 1; $c <= $cnt; $c++
) {
1177 if ($this->templateData
['field_name_value_array']['vital_sign'][$c]['date'] == $v_date) {
1185 $this->templateData
['field_name_value_array']['vital_sign'][$i]['extension'] = $entry['organizer']['id']['extension'] ??
null;
1186 $this->templateData
['field_name_value_array']['vital_sign'][$i]['root'] = $entry['organizer']['id']['root'] ??
null;
1187 $this->templateData
['field_name_value_array']['vital_sign'][$i]['date'] = $entry['observation']['effectiveTime']['value'] ??
null;
1188 $vitals_array = array(
1189 '8310-5' => 'temperature',
1192 '8287-5' => 'head_circ',
1193 '8867-4' => 'pulse',
1194 '8302-2' => 'height',
1195 '2710-2' => 'oxygen_saturation', // deprecated code
1196 '59408-5' => 'oxygen_saturation',
1197 '9279-1' => 'respiration',
1198 '3141-9' => 'weight',
1199 '29463-7' => 'weight', // with clothes
1202 $is_negated = !empty($entry['observation']['negationInd'] ??
false);
1203 $code = $entry['observation']['code']['code'] ??
null;
1204 if (array_key_exists($code, $vitals_array)) {
1205 $this->templateData
['field_name_value_array']['vital_sign'][$i][$vitals_array[$code]] = $entry['observation']['value']['value'] ??
null;
1206 $this->templateData
['field_name_value_array']['vital_sign'][$i]['vital_column'] = $vitals_array[$code] ??
'';
1209 error_log('Missed Physical Exam code (likely vital): ' . $code);
1212 if (!empty($entry['observation']['entryRelationship']['observation']['value']['code'] ??
null)) {
1213 // @todo inter to this moodcode RSON in full template!
1214 $ob_code = $entry['observation']['entryRelationship']['observation']['value']['code'];
1215 $ob_system = $entry['observation']['entryRelationship']['observation']['value']['codeSystemName'] ?
: $entry['observation']['entryRelationship']['observation']['value']['codeSystem'] ??
'';
1216 $ob_code_text = $entry['observation']['entryRelationship']['observation']['value']['displayName'] ??
'';
1217 $reason_code = $this->codeService
->resolveCode($ob_code, $ob_system, $ob_code_text);
1218 $reason_status = $entry['observation']['entryRelationship']['observation']['statusCode']['code'] ??
'';
1221 $this->templateData
['field_name_value_array']['vital_sign'][$i]['reason_status'] = $is_negated ?
'negated' : ($reason_status ??
'');
1222 $this->templateData
['field_name_value_array']['vital_sign'][$i]['reason_code'] = $reason_code['formatted_code'] ??
'';
1223 $this->templateData
['field_name_value_array']['vital_sign'][$i]['reason_code_text'] = $reason_code['code_text'] ??
'';
1225 $this->templateData
['entry_identification_array']['vital_sign'][$i] = $i;
1229 public function socialHistory($component)
1231 $component['section']['text'] = '';
1232 if (!empty($component['section']['entry'][0])) {
1233 foreach ($component['section']['entry'] as $key => $value) {
1234 $this->fetchSocialHistoryData($value);
1237 $this->fetchSocialHistoryData($component['section']['entry'] ??
null);
1241 public function fetchSocialHistoryData($social_history_data)
1243 if (!empty($social_history_data['observation']['value']['code'])) {
1244 $social_history_array = array(
1245 '2.16.840.1.113883.10.20.22.4.78' => 'smoking'
1248 $code = $social_history_data['observation']['templateId']['root'] ??
'';
1249 if (!empty($this->templateData
['field_name_value_array']['social_history'])) {
1250 foreach ($this->templateData
['field_name_value_array']['social_history'] as $key => $value) {
1251 if (!array_key_exists($social_history_array[$code], $value)) {
1254 $i = count($this->templateData
['field_name_value_array']['social_history']) +
1;
1259 if (!empty($this->templateData
['field_name_value_array']['social_history'])) {
1260 $this->templateData
['field_name_value_array']['social_history'][$i][$social_history_array[$code]]['extension'] = $social_history_data['observation']['id']['extension'];
1261 $this->templateData
['field_name_value_array']['social_history'][$i][$social_history_array[$code]]['root'] = $social_history_data['observation']['id']['root'];
1262 $this->templateData
['field_name_value_array']['social_history'][$i][$social_history_array[$code]]['status'] = $social_history_data['observation']['value']['code'];
1263 $this->templateData
['field_name_value_array']['social_history'][$i][$social_history_array[$code]]['begdate'] = $social_history_data['observation']['effectiveTime']['low']['value'];
1264 $this->templateData
['field_name_value_array']['social_history'][$i][$social_history_array[$code]]['enddate'] = $social_history_data['observation']['effectiveTime']['high']['value'];
1265 $this->templateData
['field_name_value_array']['social_history'][$i][$social_history_array[$code]]['value'] = $social_history_data['observation']['value']['displayName'];
1268 $this->templateData
['entry_identification_array']['social_history'][$i] = $i;
1272 public function encounter($component)
1274 $component['section']['text'] = '';
1275 if ($component['section']['entry'][0]) {
1276 foreach ($component['section']['entry'] as $key => $value) {
1277 $this->fetchEncounterPerformed($value);
1280 $this->fetchEncounterPerformed($component['section']['entry']);
1287 public function carePlan($component)
1289 if ($this->currentOid
!= '2.16.840.1.113883.10.20.22.2.58') {
1290 $component['section']['text'] = null;
1292 if (!empty($component['section']['entry'][0])) {
1293 foreach ($component['section']['entry'] as $key => $value) {
1294 $this->fetchCarePlanData($value, $component['section']['text']);
1297 $this->fetchCarePlanData($component['section']['entry'] ??
null, $component['section']['text']);
1301 public function fetchCarePlanData($entry, $section_text = '')
1303 $plan_type = 'plan_of_care';
1304 if ($this->currentOid
== '2.16.840.1.113883.10.20.24.3.31') {
1305 $plan_type = 'intervention';
1306 } elseif ($this->currentOid
== '2.16.840.1.113883.10.20.24.3.37') {
1307 $plan_type = 'test_or_order';
1308 } 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') {
1309 $plan_type = 'planned_medication_activity';
1310 } elseif ($this->currentOid
== '2.16.840.1.113883.10.20.24.3.130') {
1311 $plan_type = 'supply_order';
1312 if (($entry["act"]["entryRelationship"]["supply"]["templateId"][1]["root"] ??
null) == '2.16.840.1.113883.10.20.24.3.9') {
1313 $plan_type = 'device_order';
1315 } elseif ($this->currentOid
== '2.16.840.1.113883.10.20.22.2.60') {
1316 $plan_type = 'goal';
1317 } elseif ($this->currentOid
== '2.16.840.1.113883.10.20.22.2.58') {
1318 $plan_type = 'health_concern';
1324 (!empty($entry['act']['code']['code']) && $plan_type == 'device_order')
1325 ||
(($entry['act']['negationInd'] ??
'false') == 'true' && $plan_type == 'device_order')
1327 if (!empty($this->templateData
['field_name_value_array']['care_plan'])) {
1328 $i +
= count($this->templateData
['field_name_value_array']['care_plan']);
1331 $device_code = $entry["act"]["entryRelationship"]["supply"]["participant"]["participantRole"]["playingDevice"]["code"]["code"];
1332 $device_system = $entry["act"]["entryRelationship"]["supply"]["participant"]["participantRole"]["playingDevice"]["code"]["codeSystem"];
1333 $device_name = $entry["act"]["entryRelationship"]["supply"]["participant"]["participantRole"]["playingDevice"]["code"]["codeSystemName"];
1335 $code = $this->codeService
->resolveCode(
1337 $device_name ?
: $device_system ??
null,
1341 $this->templateData
['field_name_value_array']['care_plan'][$i]['plan_type'] = $plan_type;
1342 $this->templateData
['field_name_value_array']['care_plan'][$i]['extension'] = $entry['act']['templateId']['extension'];
1343 $this->templateData
['field_name_value_array']['care_plan'][$i]['root'] = $entry['act']['templateId']['root'];
1344 $this->templateData
['field_name_value_array']['care_plan'][$i]['code'] = $code['formatted_code'];
1345 $this->templateData
['field_name_value_array']['care_plan'][$i]['code_text'] = $code['code_text'];
1346 $this->templateData
['field_name_value_array']['care_plan'][$i]['description'] = $entry["act"]["entryRelationship"]["supply"]['text'] ??
$code['code_text'];
1347 $this->templateData
['field_name_value_array']['care_plan'][$i]['date'] = $entry['act']['effectiveTime']['center']['value'] ??
$entry["act"]["entryRelationship"]["supply"]['author']['time']['value'] ??
null;
1350 $reason_code = $entry["act"]["entryRelationship"]["supply"]["entryRelationship"]["observation"]["value"]["code"] ??
null;
1352 $reason_system = $entry["act"]["entryRelationship"]["supply"]["entryRelationship"]["observation"]["value"]["codeSystem"];
1353 $reason_name = $entry["act"]["entryRelationship"]["supply"]["entryRelationship"]["observation"]["value"]["codeSystemName"];
1354 $code = $this->codeService
->resolveCode(
1356 $reason_name ?
: $reason_system ??
'',
1359 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_code'] = $code['formatted_code'];
1360 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_code_text'] = $code['code_text'];
1361 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_description'] = $code['code_text'] ??
$entry['act']['text'];
1362 $date_low = $entry["act"]["entryRelationship"]["supply"]["entryRelationship"]["observation"]['effectiveTime']['low']['value'] ??
null;
1363 $date_high = $entry["act"]["entryRelationship"]["supply"]["entryRelationship"]["observation"]['effectiveTime']['high']['value'] ??
null;
1364 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_date_low'] = $date_low;
1365 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_date_high'] = $date_high;
1367 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_status'] = (($entry['act']['negationInd'] ??
'false') == 'true') ?
'negated' : null;
1368 $this->templateData
['entry_identification_array']['care_plan'][$i] = $i;
1369 } elseif (!empty($entry['act']['code']['code']) ||
($entry['act']['negationInd'] ??
'false') == 'true') {
1370 if (!empty($this->templateData
['field_name_value_array']['care_plan'])) {
1371 $i +
= count($this->templateData
['field_name_value_array']['care_plan']);
1374 $code = $this->codeService
->resolveCode(
1375 $entry['act']['code']['code'] ??
'',
1376 ($entry['act']['code']['codeSystemName'] ??
null) ?
: $entry['act']['code']['codeSystem'] ??
null,
1377 $entry['act']['code']['displayName'] ??
$entry['act']['text']
1379 if (!empty($entry["act"]["code"]["nullFlavor"]) && !empty($entry['act']['code']["valueSet"])) {
1380 $code['code'] = $entry["act"]["code"]["valueSet"] ??
null;
1381 $code['formatted_code'] = 'OID:' . $entry["act"]["code"]["valueSet"] ??
null;
1382 $code['code_text'] = $entry['act']['text'] ??
'';
1384 $this->templateData
['field_name_value_array']['care_plan'][$i]['plan_type'] = $plan_type;
1385 $this->templateData
['field_name_value_array']['care_plan'][$i]['extension'] = $entry['act']['templateId']['extension'] ??
'';
1386 $this->templateData
['field_name_value_array']['care_plan'][$i]['root'] = $entry['act']['templateId']['root'] ??
'';
1387 $this->templateData
['field_name_value_array']['care_plan'][$i]['code'] = $code['formatted_code'];
1388 $this->templateData
['field_name_value_array']['care_plan'][$i]['code_text'] = $code['code_text'];
1389 $this->templateData
['field_name_value_array']['care_plan'][$i]['description'] = $entry['act']['text'] ??
$code['code_text'];
1390 $this->templateData
['field_name_value_array']['care_plan'][$i]['date'] = $entry['act']['effectiveTime']['center']['value'] ??
$entry['act']['author']['time']['value'] ??
null;
1393 if ($entry['act']['entryRelationship']['observation']['value']['code']) {
1394 $code = $this->codeService
->resolveCode(
1395 $entry['act']['entryRelationship']['observation']['value']['code'],
1396 $entry['act']['entryRelationship']['observation']['value']['codeSystemName'] ?
: $entry['act']['entryRelationship']['observation']['value']['codeSystem'] ??
'',
1397 $entry['act']['entryRelationship']['observation']['value']['displayName'] ??
''
1399 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_code'] = $code['formatted_code'];
1400 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_code_text'] = $code['code_text'];
1401 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_description'] = $code['code_text'] ??
$entry['act']['text'];
1402 $date_low = $entry['act']['entryRelationship']['observation']['effectiveTime']['low']['value'] ??
null;
1403 $date_high = $entry['act']['entryRelationship']['observation']['effectiveTime']['high']['value'] ??
null;
1404 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_date_low'] = $date_low;
1405 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_date_high'] = $date_high;
1407 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_status'] = (($entry['act']['negationInd'] ??
'false') == 'true') ?
'negated' : null;
1408 $this->templateData
['entry_identification_array']['care_plan'][$i] = $i;
1409 } elseif (!empty($entry['observation']['code']['code']) ||
($entry['observation']['negationInd'] ??
'false') == 'true') { // it's an observation template
1410 if (!empty($this->templateData
['field_name_value_array']['care_plan'])) {
1411 $i +
= count($this->templateData
['field_name_value_array']['care_plan']);
1414 $code = $this->codeService
->resolveCode(
1415 $entry['observation']['code']['code'] ??
null,
1416 $entry['observation']['code']['codeSystemName'] ??
$value['observation']['code']['codeSystem'] ??
'',
1417 $entry['observation']['code']['displayName'] ??
''
1419 if (!empty($entry['observation']['code']["nullFlavor"]) && !empty($entry['observation']['code']["valueSet"])) {
1420 $code['code'] = $entry['observation']['code']["valueSet"] ??
null;
1421 $code['formatted_code'] = 'OID:' . $entry['observation']['code']["valueSet"] ??
null;
1422 $code['code_text'] = $entry['observation']['text'] ??
'';
1424 $this->templateData
['field_name_value_array']['care_plan'][$i]['plan_type'] = $plan_type;
1425 $this->templateData
['field_name_value_array']['care_plan'][$i]['extension'] = $entry['observation']['id']['extension'] ??
null;
1426 $this->templateData
['field_name_value_array']['care_plan'][$i]['root'] = $entry['observation']['id']['root'] ??
null;
1427 $this->templateData
['field_name_value_array']['care_plan'][$i]['code'] = $code['formatted_code'];
1428 $this->templateData
['field_name_value_array']['care_plan'][$i]['code_text'] = $code['code_text'];
1430 $text = $section_text ?
: ($entry['observation']['text'] ??
null);
1432 if ($entry['observation']['value']['type'] == 'ST') {
1433 $text = $entry['observation']['value']['_'];
1434 } elseif ($entry['observation']['value']['type'] == 'CO') {
1435 $text = $entry['observation']['value']['displayName'];
1438 $this->templateData
['field_name_value_array']['care_plan'][$i]['description'] = $text ?
: ($code['code_text'] ??
null);
1439 // TODO unsure why the author was used for plan date just be aware
1440 //$this->templateData['field_name_value_array']['care_plan'][$i]['date'] = $entry['observation']['author']['time']['value'] ?? null;
1441 $this->templateData
['field_name_value_array']['care_plan'][$i]['date'] = $entry['observation']['effectiveTime']['value'] ??
$entry['observation']['author']['time']['value'] ??
null;
1443 // check for a reason code if observation.
1444 if ($entry['observation']['entryRelationship']['observation']['value']['code']) {
1445 $code = $this->codeService
->resolveCode(
1446 $entry['observation']['entryRelationship']['observation']['value']['code'],
1447 $entry['observation']['entryRelationship']['observation']['value']['codeSystemName'] ?
: $entry['observation']['entryRelationship']['observation']['value']['codeSystem'] ??
'',
1448 $entry['observation']['entryRelationship']['observation']['value']['displayName']
1450 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_code'] = $code['formatted_code'];
1451 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_code_text'] = $code['code_text'];
1452 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_description'] = $code['code_text'] ??
$entry['observation']['text'];
1453 $date_low = $entry['observation']['entryRelationship']['observation']['effectiveTime']['low']['value'] ??
null;
1454 $date_high = $entry['observation']['entryRelationship']['observation']['effectiveTime']['high']['value'] ??
null;
1455 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_date_low'] = $date_low;
1456 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_date_high'] = $date_high;
1459 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_status'] = (($entry['observation']['negationInd'] ??
'false') == 'true') ?
'negated' : null;
1460 $this->templateData
['entry_identification_array']['care_plan'][$i] = $i;
1462 !empty($entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['code'])
1463 ||
($entry['substanceAdministration']['negationInd'] ??
'false') == 'true'
1465 if (!empty($this->templateData
['field_name_value_array']['care_plan'])) {
1466 $i +
= count($this->templateData
['field_name_value_array']['care_plan']);
1469 //$plan_type = 'substance_medication_order';
1470 $code = $this->codeService
->resolveCode(
1471 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['code'] ??
'',
1472 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['codeSystemName'] ??
($entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['codeSystem'] ??
''),
1473 $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['displayName'] ??
''
1475 if (!empty($entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']["nullFlavor"])) {
1476 $code['code'] = $entry['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']["valueSet"] ??
null;
1477 $code['formatted_code'] = 'OID:' . $code['code'];
1478 $code['code_text'] = $entry['substanceAdministration']['text'] ??
'';
1481 $this->templateData
['field_name_value_array']['care_plan'][$i]['plan_type'] = $plan_type;
1482 $this->templateData
['field_name_value_array']['care_plan'][$i]['extension'] = $entry['substanceAdministration']['id']['extension'] ??
null;
1484 $this->templateData
['field_name_value_array']['care_plan'][$i]['root'] = $entry['substanceAdministration']['id']['root'] ??
null;
1485 $this->templateData
['field_name_value_array']['care_plan'][$i]['code'] = $code['formatted_code'];
1486 $this->templateData
['field_name_value_array']['care_plan'][$i]['code_text'] = $code['code_text'];
1487 $this->templateData
['field_name_value_array']['care_plan'][$i]['description'] = $entry['substanceAdministration']['text'] ??
$code['code_text'];
1489 $this->templateData
['field_name_value_array']['care_plan'][$i]['end_date'] = null;
1490 if (!empty($entry['substanceAdministration']['effectiveTime']['low']['value'])) {
1491 $this->templateData
['field_name_value_array']['care_plan'][$i]['date'] = $entry['substanceAdministration']['effectiveTime']['low']['value'];
1492 $this->templateData
['field_name_value_array']['care_plan'][$i]['end_date'] = $entry['substanceAdministration']['effectiveTime']['high']['value'] ??
null;
1493 } elseif (!empty($entry['substanceAdministration']['effectiveTime'][0]['low']['value'])) {
1494 $this->templateData
['field_name_value_array']['care_plan'][$i]['date'] = $entry['substanceAdministration']['effectiveTime'][0]['low']['value'];
1495 $this->templateData
['field_name_value_array']['care_plan'][$i]['end_date'] = $entry['substanceAdministration']['effectiveTime'][0]['high']['value'] ??
null;
1496 } elseif (!empty($entry['substanceAdministration']['effectiveTime']['value'])) {
1497 $this->templateData
['field_name_value_array']['care_plan'][$i]['date'] = $entry['substanceAdministration']['effectiveTime']['value'];
1499 $this->templateData
['field_name_value_array']['care_plan'][$i]['date'] = date('Y-m-d');
1502 if ($entry['substanceAdministration']['entryRelationship']['observation']['value']['code']) {
1503 $code = $this->codeService
->resolveCode(
1504 $entry['substanceAdministration']['entryRelationship']['observation']['value']['code'],
1505 $entry['substanceAdministration']['entryRelationship']['observation']['value']['codeSystemName'] ?
: $entry['substanceAdministration']['entryRelationship']['observation']['value']['codeSystem'] ??
'',
1506 $entry['substanceAdministration']['entryRelationship']['observation']['value']['displayName'] ??
''
1508 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_code'] = $code['formatted_code'];
1509 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_code_text'] = $code['code_text'];
1510 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_description'] = $code['code_text'] ??
$entry['observation']['text'];
1511 $date_low = $entry['substanceAdministration']['entryRelationship']['observation']['effectiveTime']['low']['value'] ??
null;
1512 $date_high = $entry['substanceAdministration']['entryRelationship']['observation']['effectiveTime']['high']['value'] ??
null;
1513 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_date_low'] = $date_low;
1514 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_date_high'] = $date_high;
1517 $this->templateData
['field_name_value_array']['care_plan'][$i]['reason_status'] = (($entry['substanceAdministration']['negationInd'] ??
'false') == 'true') ?
'negated' : null;
1518 $this->templateData
['entry_identification_array']['care_plan'][$i] = $i;
1522 public function functionalCognitiveStatus($component)
1524 if ($this->currentOid
!= '2.16.840.1.113883.10.20.22.2.56') {
1525 $component['section']['text'] = '';
1528 if ($component['section']['entry'][0] ??
'') {
1529 foreach ($component['section']['entry'] as $key => $value) {
1530 if (!empty($component['section']['text'][$ccnt])) {
1531 $this->fetchFunctionalCognitiveStatusData($value, $component['section']['text'][$ccnt]);
1536 $this->fetchFunctionalCognitiveStatusData($component['section']['entry'] ??
'', $component['section']['text']);
1540 public function fetchFunctionalCognitiveStatusData($entry, $section_text = null)
1544 if ($this->currentOid
== '2.16.840.1.113883.10.20.22.2.56') {
1548 if (!empty($entry['observation']['value']['code'])) {
1550 if (!empty($this->templateData
['field_name_value_array']['functional_cognitive_status'])) {
1551 $i +
= count($this->templateData
['field_name_value_array']['functional_cognitive_status']);
1554 $code = $this->codeService
->resolveCode(
1555 $entry['observation']['value']['code'] ??
null,
1556 $value['observation']['value']['codeSystemName'] ??
$value['observation']['code']['codeSystem'] ??
'',
1557 $value['observation']['value']['displayName'] ??
''
1559 $this->templateData
['field_name_value_array']['functional_cognitive_status'][$i]['cognitive'] = $cognitive;
1560 $this->templateData
['field_name_value_array']['functional_cognitive_status'][$i]['extension'] = $entry['observation']['id']['extension'];
1561 $this->templateData
['field_name_value_array']['functional_cognitive_status'][$i]['root'] = $entry['observation']['id']['root'];
1562 $this->templateData
['field_name_value_array']['functional_cognitive_status'][$i]['date'] = $entry['observation']['effectiveTime']['low']['value'];
1563 $this->templateData
['field_name_value_array']['functional_cognitive_status'][$i]['code'] = $code['formatted_code'];
1564 $this->templateData
['field_name_value_array']['functional_cognitive_status'][$i]['code_text'] = $section_text ?
: $code['code_text'];
1565 $this->templateData
['field_name_value_array']['functional_cognitive_status'][$i]['description'] = $section_text ?
: $code['code_text'];
1566 $this->templateData
['entry_identification_array']['functional_cognitive_status'][$i] = $i;
1567 } elseif (!empty($entry['organizer']['code']['code'])) {
1570 if (!empty($this->templateData
['field_name_value_array']['functional_cognitive_status'])) {
1571 $i +
= count($this->templateData
['field_name_value_array']['functional_cognitive_status']);
1574 foreach ($entry['organizer']['component'] as $k => $fscomponent) {
1575 // patient self-status @todo why am I sending an independent status by default.
1576 if (($fscomponent["observation"]["templateId"]["root"] ??
null) == "2.16.840.1.113883.10.20.22.4.128") {
1579 $code = $this->codeService
->resolveCode(
1580 $fscomponent['observation']['value']['code'] ??
null,
1581 $fscomponent['observation']['value']['codeSystem'] ??
'',
1582 $fscomponent['observation']['value']['displayName'] ??
''
1585 $this->templateData
['field_name_value_array']['functional_cognitive_status'][$i]['cognitive'] = $cognitive;
1586 $this->templateData
['field_name_value_array']['functional_cognitive_status'][$i]['extension'] = $fscomponent['observation']['id']['extension'];
1587 $this->templateData
['field_name_value_array']['functional_cognitive_status'][$i]['root'] = $fscomponent['observation']['id']['root'];
1588 $this->templateData
['field_name_value_array']['functional_cognitive_status'][$i]['date'] = $fscomponent['observation']['effectiveTime']['value'];
1589 $this->templateData
['field_name_value_array']['functional_cognitive_status'][$i]['code'] = $fscomponent['observation']['value']['code'] ??
$code['formatted_code'] ??
null;
1590 $this->templateData
['field_name_value_array']['functional_cognitive_status'][$i]['code_text'] = $fscomponent['observation']['value']['displayName'] ??
$code['code_text'] ??
null;
1591 $this->templateData
['field_name_value_array']['functional_cognitive_status'][$i]['description'] = $fscomponent['observation']['value']['displayName'] ??
$code['code_text'] ??
null;
1593 $this->templateData
['entry_identification_array']['functional_cognitive_status'][$i] = $i;
1598 public function referral($component)
1600 if ($component['section']['entry'][0] ??
'') {
1601 foreach ($component['section']['entry'] as $key => $value) {
1602 $this->fetchReferralData($value);
1605 $this->fetchReferralData($component['section']);
1609 public function fetchReferralData($referral_data)
1611 if (!empty($referral_data['text']['paragraph']) && is_array($referral_data['text']['paragraph'])) {
1613 foreach ($referral_data['text']['paragraph'] as $key => $value) {
1615 $this->templateData
['field_name_value_array']['referral'][$i]['body'] = preg_replace('/\s+/', ' ', $value);
1616 $this->templateData
['entry_identification_array']['referral'][$i] = $i;
1622 if (!empty($this->templateData
['field_name_value_array']['referral'])) {
1623 $i +
= count($this->templateData
['field_name_value_array']['referral']);
1625 $this->templateData
['field_name_value_array']['referral'][$i]['root'] = $referral_data['templateId']['root'] ??
'';
1626 $this->templateData
['field_name_value_array']['referral'][$i]['body'] = (!empty($referral_data['text']['paragraph'])) ?
preg_replace('/\s+/', ' ', $referral_data['text']['paragraph']) : '';
1628 $this->templateData
['entry_identification_array']['referral'][$i] = $i;
1634 public function dischargeMedications($component)
1636 $component['section']['text'] = '';
1637 if ($component['section']['entry'][0]) {
1638 foreach ($component['section']['entry'] as $key => $value) {
1639 $this->fetchDischargeMedicationsData($value);
1642 $this->fetchDischargeMedicationsData($component['section']['entry']);
1646 public function fetchDischargeMedicationsData($discharge_medications_data)
1649 if (!empty($this->templateData
['field_name_value_array']['discharge_medication'])) {
1650 $i +
= count($this->templateData
['field_name_value_array']['discharge_medication']);
1652 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['extension'] = $discharge_medications_data['act']['id']['extension'];
1653 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['root'] = $discharge_medications_data['act']['id']['root'];
1654 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['begdate'] = $discharge_medications_data['act']['effectiveTime']['low']['value'];
1655 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['enddate'] = $discharge_medications_data['act']['effectiveTime']['high']['value'];
1656 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['route'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['routeCode']['code'];
1657 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['route_display'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['routeCode']['displayName'];
1658 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['dose'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['doseQuantity']['value'];
1659 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['dose_unit'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['doseQuantity']['unit'];
1660 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['rate'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['rateQuantity']['value'];
1661 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['rate_unit'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['rateQuantity']['unit'];
1662 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['drug_code'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['code'];
1663 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['drug_text'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['consumable']['manufacturedProduct']['manufacturedMaterial']['code']['displayName'];
1664 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['note'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['text']['reference']['value'];
1665 $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'];
1666 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['prn'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['precondition']['criterion']['value']['displayName'];
1668 $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'];
1669 $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'];
1670 $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'];
1671 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['provider_root'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship'][1]['supply']['author']['assignedAuthor']['id']['root'];
1672 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['provider_address'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship'][1]['supply']['performer']['assignedEntity']['addr']['streetAddressLine'];
1673 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['provider_city'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship'][1]['supply']['performer']['assignedEntity']['addr']['city'];
1674 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['provider_state'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship'][1]['supply']['performer']['assignedEntity']['addr']['state'];
1675 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['provider_postalCode'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship'][1]['supply']['performer']['assignedEntity']['addr']['postalCode'];
1676 $this->templateData
['field_name_value_array']['discharge_medication'][$i]['provider_country'] = $discharge_medications_data['act']['entryRelationship']['substanceAdministration']['entryRelationship'][1]['supply']['performer']['assignedEntity']['addr']['country'];
1677 $this->templateData
['entry_identification_array']['discharge_medication'][$i] = $i;
1678 unset($discharge_medications_data);
1682 public function dischargeSummary($component)
1684 if ($component['section']['entry'][0]) {
1685 foreach ($component['section']['entry'] as $key => $value) {
1686 $this->fetchDischargeSummaryData($value);
1689 $this->fetchDischargeSummaryData($component['section']);
1696 public function fetchDischargeSummaryData($discharge_summary_data)
1699 if (!empty($this->templateData
['field_name_value_array']['discharge_summary'])) {
1700 $i +
= count($this->templateData
['field_name_value_array']['discharge_summary']);
1702 $this->templateData
['field_name_value_array']['discharge_summary'][$i]['root'] = $discharge_summary_data['templateId']['root'];
1703 $text = preg_replace('/\s+/', ' ', $discharge_summary_data['text']['content']);
1704 for ($j = 0, $jMax = count($discharge_summary_data['text']['list']['item']); $j < $jMax; $j++
) {
1705 if (is_array($discharge_summary_data['text']['list']['item'][$j])) {
1706 for ($k = 0, $kMax = count($discharge_summary_data['text']['list']['item'][$j]['list']['item']); $k < $kMax; $k++
) {
1707 $text .= '#$%' . preg_replace('/\s+/', ' ', $discharge_summary_data['text']['list']['item'][$j]['list']['item'][$k]);
1710 $text .= '#$%' . preg_replace('/\s+/', ' ', $discharge_summary_data['text']['list']['item'][$j]);
1714 $this->templateData
['field_name_value_array']['discharge_summary'][$i]['text'] = $text;
1716 $this->templateData
['entry_identification_array']['discharge_summary'][$i] = $i;