-
Notifications
You must be signed in to change notification settings - Fork 172
/
ADT_A01.liquid
834 lines (754 loc) · 60.1 KB
/
ADT_A01.liquid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
{% assign firstSegments = hl7v2Data | get_first_segments: 'PID|PD1|PV1|PV2|MSH|UAC|EVN|ACC|PDA' -%}
{% assign sftSegmentLists = hl7v2Data | get_segment_lists: 'SFT' -%}
{% assign arvSegmentLists = hl7v2Data | get_segment_lists: 'ARV' -%}
{% assign nk1SegmentLists = hl7v2Data | get_segment_lists: 'NK1' -%}
{% assign db1SegmentLists = hl7v2Data | get_segment_lists: 'DB1' -%}
{% assign obxSegmentLists = hl7v2Data | get_segment_lists: 'OBX' -%}
{% assign al1SegmentLists = hl7v2Data | get_segment_lists: 'AL1' -%}
{% assign dg1SegmentLists = hl7v2Data | get_segment_lists: 'DG1' -%}
{% assign drgSegmentLists = hl7v2Data | get_segment_lists: 'DRG' -%}
{% assign pr1SegmentLists = hl7v2Data | get_segment_lists: 'PR1' -%}
{% assign gt1SegmentLists = hl7v2Data | get_segment_lists: 'GT1' -%}
{% assign in1SegmentLists = hl7v2Data | get_segment_lists: 'IN1' -%}
{% assign in2SegmentLists = hl7v2Data | get_segment_lists: 'IN2' -%}
{% assign in3SegmentLists = hl7v2Data | get_segment_lists: 'IN3' -%}
{% assign autSegmentLists = hl7v2Data | get_segment_lists: 'AUT' -%}
{% assign rf1SegmentLists = hl7v2Data | get_segment_lists: 'RF1' -%}
{% assign ub1SegmentLists = hl7v2Data | get_segment_lists: 'UB1' -%}
{% assign ub2SegmentLists = hl7v2Data | get_segment_lists: 'UB2' -%}
{% evaluate bundleID using 'ID/Bundle' Data: firstSegments.MSH.10 -%}
{
"resourceType": "Bundle",
"type": "batch",
{% if firstSegments.MSH.7 -%}
"timestamp":"{{ firstSegments.MSH.7.Value | format_as_date_time }}",
{% endif -%}
"identifier":
{
"value":"{{ firstSegments.MSH.10.Value }}",
},
"id":"{{ bundleID }}",
"entry": [
{% evaluate patientId using 'ID/Patient' PID: firstSegments.PID, type: 'First' -%}
{% assign fullPatientId = patientId | prepend: 'Patient/' -%}
{% evaluate messageHeaderId using 'ID/MessageHeader' MSH: firstSegments.MSH -%}
{% if firstSegments.MSH -%}
{% include 'Resource/MessageHeader' MSH: firstSegments.MSH, ID: messageHeaderId -%}
{% evaluate provenanceId using 'ID/Provenance' MSH: firstSegments.MSH, baseId: patientId -%}
{% include 'Resource/Provenance' Root_Template: 'ADT_A01', MSH: firstSegments.MSH, REF_BUNDLE: bundleID, ID: provenanceId -%}
{% if firstSegments.MSH.4 -%}
{% if firstSegments.MSH.4.1 != "" and firstSegments.MSH.4.1 != null or firstSegments.MSH.4.2 != "" and firstSegments.MSH.4.2 != null or firstSegments.MSH.4.3 != "" and firstSegments.MSH.4.3 != null -%}
{% evaluate organization_ID_MSH_4 using 'ID/Organization' HD: firstSegments.MSH.4 -%}
{% include 'Resource/Organization' MSHHD1: firstSegments.MSH.4, MSH: firstSegments.MSH, ID: organization_ID_MSH_4 -%}
{% endif -%}
{% endif -%}
{% if firstSegments.MSH.6 %}
{% if firstSegments.MSH.6.1 != "" and firstSegments.MSH.6.1 != null or firstSegments.MSH.6.2 != "" and firstSegments.MSH.6.2 != null or firstSegments.MSH.6.3 != "" and firstSegments.MSH.6.3 != null -%}
{% evaluate organization_Id_MSH_6 using 'ID/Organization' HD: firstSegments.MSH.6 -%}
{% include 'Resource/Organization' MSHHD2: firstSegments.MSH.6, MSH: firstSegments.MSH, ID: organization_Id_MSH_6 -%}
{% endif -%}
{% endif -%}
{% if firstSegments.MSH.22 %}
{% if firstSegments.MSH.22.1 != "" and firstSegments.MSH.22.1 != null -%}
{% evaluate organization_Id_MSH_22 using 'ID/Organization' XON: firstSegments.MSH.22 -%}
{% include 'Resource/Organization' MSHXON1: firstSegments.MSH.22, ID: organization_Id_MSH_22 -%}
{% endif -%}
{% endif -%}
{% if firstSegments.MSH.23 %}
{% if firstSegments.MSH.23.1 != "" and firstSegments.MSH.23.1 != null -%}
{% evaluate organization_Id_MSH_23 using 'ID/Organization' XON: firstSegments.MSH.23 -%}
{% include 'Resource/Organization' MSHXON2: firstSegments.MSH.23, ID: organization_Id_MSH_23 -%}
{% endif -%}
{% endif -%}
{% if firstSegments.MSH.3 and firstSegments.MSH.24 %}
{% evaluate device_Id_MSH_3 using 'ID/Device' HD: firstSegments.MSH.3 -%}
{% include 'Resource/Device' MSH: firstSegments.MSH, ID: device_Id_MSH_3 -%}
{% endif -%}
{% endif -%}
{% for sftSegment in sftSegmentLists.SFT -%}
{% evaluate deviceId_SFT using 'ID/Device' SFT: sftSegment -%}
{% include 'Resource/Device' SFT:sftSegment, ID: deviceId_SFT -%}
{% include 'Resource/MessageHeader' MSH: firstSegments.MSH, SFT:sftSegment, ID: messageHeaderID -%}
{% endfor -%}
{% assign checkPDASeg = hl7v2Data | has_segments: 'PDA' -%}
{% include 'Resource/Patient' PID: firstSegments.PID, PD1: firstSegments.PD1, ID: patientId -%}
{% include 'Extensions/Patient/PatientExtension' ID: PatientId, PID: firstSegments.PID, PD1: firstSegments.PD1, PV1: firstSegments.PV1 -%}
{% if firstSegments.PID.18 -%}
{% evaluate accountId using 'ID/Account' CX: firstSegments.PID.18, baseId: patientId -%}
{% include 'Resource/Account' PID: firstSegments.PID, ID: accountId -%}
{% include 'Reference/Account/Subject' ID: accountId, REF: fullPatientId -%}
{% if firstSegments.PID.18.4.1 != "" and firstSegments.PID.18.4.1 != null and firstSegments.PID.18.4.2 != "" and firstSegments.PID.18.4.2 != null and firstSegments.PID.18.4.3 != "" and firstSegments.PID.18.4.3 != null -%}
{% evaluate Organization_ID_pid_CX_18 using 'ID/Organization' HDORG: firstSegments.PID.18.4 -%}
{% include 'Resource/Organization', PID: firstSegments.PID.18, ID: Organization_ID_pid_CX_18 -%}
{% endif -%}
{% endif -%}
{% unless firstSegments.PID.18 %}
{% evaluate accountId using 'ID/Account' GT1_Account: gt1SegmentLists.GT1[0], baseId: patientId -%}
{% endunless -%}
{% for gt1SegmentLists in gt1SegmentLists.GT1 -%}
{% unless gt1SegmentLists.21 %}
{% if gt1SegmentLists.11.1.Value == "SEL" -%}
{% include 'Resource/Patient' PID: firstSegments.PID, GT1: gt1SegmentLists, ID: patientId, NK1_PER: nk1SegmentLists.NK1[0], pda_seg: checkPDASeg -%}
{% if gt1SegmentLists.2.4.1 != "" and gt1SegmentLists.2.4.1 != null and gt1SegmentLists.2.4.2 != "" and gt1SegmentLists.2.4.2 != null and gt1SegmentLists.2.4.3 != "" and gt1SegmentLists.2.4.3 != null -%}
{% evaluate Organization_ID_gt1_CX_2 using 'ID/Organization' HDORG: gt1SegmentLists.2.4 -%}
{% include 'Resource/Organization', PID: gt1SegmentLists.2, ID: Organization_ID_gt1_CX_2 -%}
{% endif -%}
{% if gt1SegmentLists.19.4.1 != "" and gt1SegmentLists.19.4.1 != null and gt1SegmentLists.19.4.2 != "" and gt1SegmentLists.19.4.2 != null and gt1SegmentLists.19.4.3 != "" and gt1SegmentLists.19.4.3 != null -%}
{% evaluate Organization_ID_gt1_CX_19 using 'ID/Organization' HDORG: gt1SegmentLists.19.4 -%}
{% include 'Resource/Organization', PID: gt1SegmentLists.19, ID: Organization_ID_gt1_CX_19 -%}
{% endif -%}
{% if gt1SegmentLists.29.4.1 != "" and gt1SegmentLists.29.4.1 != null and gt1SegmentLists.29.4.2 != "" and gt1SegmentLists.29.4.2 != null and gt1SegmentLists.29.4.3 != "" and gt1SegmentLists.29.4.3 != null -%}
{% evaluate Organization_ID_gt1_CX_29 using 'ID/Organization' HDORG: gt1SegmentLists.29.4 -%}
{% include 'Resource/Organization', PID: gt1SegmentLists.29, ID: Organization_ID_gt1_CX_29 -%}
{% endif -%}
{% if gt1SegmentLists.51 -%}
{% evaluate Organization_ID_gt1_XON_51 using 'ID/Organization' XON: gt1SegmentLists.51 -%}
{% include 'Resource/Organization', GT1_51: gt1SegmentLists, ID: Organization_ID_gt1_XON_51 -%}
{% assign Organization_GT1_51 = Organization_ID_gt1_XON_51 | prepend: 'Organization/' -%}
{% endif -%}
{% else -%}
{% evaluate gt1relatedPersonId using 'ID/RelatedPerson' GT1: gt1SegmentLists, baseId: patientId -%}
{% include 'Resource/RelatedPerson' RelatedPerson_Patient_ID: fullPatientId, ID: gt1relatedPersonId, GT1: gt1SegmentLists -%}
{% if gt1SegmentLists.2.4.1 != "" and gt1SegmentLists.2.4.1 != null and gt1SegmentLists.2.4.2 != "" and gt1SegmentLists.2.4.2 != null and gt1SegmentLists.2.4.3 != "" and gt1SegmentLists.2.4.3 != null -%}
{% evaluate Organization_ID_gt1_CX_2 using 'ID/Organization' HDORG: gt1SegmentLists.2.4 -%}
{% include 'Resource/Organization', PID: gt1SegmentLists.2, ID: Organization_ID_gt1_CX_2 -%}
{% endif -%}
{% if gt1SegmentLists.19.4.1 != "" and gt1SegmentLists.19.4.1 != null and gt1SegmentLists.19.4.2 != "" and gt1SegmentLists.19.4.2 != null and gt1SegmentLists.19.4.3 != "" and gt1SegmentLists.19.4.3 != null -%}
{% evaluate Organization_ID_gt1_CX_19 using 'ID/Organization' HDORG: gt1SegmentLists.19.4 -%}
{% include 'Resource/Organization', PID: gt1SegmentLists.19, ID: Organization_ID_gt1_CX_19 -%}
{% endif -%}
{% endif -%}
{% endunless -%}
{% if gt1SegmentLists.21 -%}
{% evaluate Organization_ID_gt1_XON_21 using 'ID/Organization' XON: gt1SegmentLists.21 -%}
{% include 'Resource/Organization', GT1: gt1SegmentLists, ID: Organization_ID_gt1_XON_21 -%}
{% if gt1SegmentLists.2.4.1 != "" and gt1SegmentLists.2.4.1 != null and gt1SegmentLists.2.4.2 != "" and gt1SegmentLists.2.4.2 != null and gt1SegmentLists.2.4.3 != "" and gt1SegmentLists.2.4.3 != null -%}
{% evaluate Organization_ID_gt1_CX_2 using 'ID/Organization' HDORG: gt1SegmentLists.2.4 -%}
{% include 'Resource/Organization', PID: gt1SegmentLists.2, ID: Organization_ID_gt1_CX_2 -%}
{% endif -%}
{% endif -%}
{% include 'Resource/Account' GT1: gt1SegmentLists, ID: accountId, REF_PATIENT: fullPatientId, REF_RELATED_PERSON: gt1relatedPersonId, REF_ORG: Organization_ID_gt1_XON_21 -%}
{% include 'Reference/Account/Subject' ID: accountId, REF: fullPatientId -%}
{% endfor -%}
{% if firstSegments.PID.2 -%}
{% if firstSegments.PID.2.4.1 != "" and firstSegments.PID.2.4.1 != null and firstSegments.PID.2.4.2 != "" and firstSegments.PID.2.4.2 != null and firstSegments.PID.2.4.3 != "" and firstSegments.PID.2.4.3 != null -%}
{% evaluate Organization_ID_pid_CX_2 using 'ID/Organization' HDORG: firstSegments.PID.2.4 -%}
{% include 'Resource/Organization', PID: firstSegments.PID.2, ID: Organization_ID_pid_CX_2 -%}
{% endif -%}
{% endif -%}
{% for pid3 in firstSegments.PID.3.Repeats -%}
{% if pid3.4.1 != "" and pid3.4.1 != null and pid3.4.2 != "" and pid3.4.2 != null and pid3.4.3 != "" and pid3.4.3 != null -%}
{% evaluate Organization_ID_pid_CX using 'ID/Organization' HDORG: pid3.4 -%}
{% include 'Resource/Organization', PID: pid3, ID: Organization_ID_pid_CX -%}
{% endif -%}
{% endfor -%}
{% if firstSegments.PID.4 -%}
{% if firstSegments.PID.4.4.1 != "" and firstSegments.PID.4.4.1 != null and firstSegments.PID.4.4.2 != "" and firstSegments.PID.4.4.2 != null and firstSegments.PID.4.4.3 != "" and firstSegments.PID.4.4.3 != null -%}
{% evaluate Organization_ID_pid_CX_4 using 'ID/Organization' HDORG: firstSegments.PID.4.4 -%}
{% include 'Resource/Organization', PID: firstSegments.PID.4, ID: Organization_ID_pid_CX_4 -%}
{% endif -%}
{% endif -%}
{% unless nk1SegmentLists.NK1 -%}
{% if firstSegments.PID.21 -%}
{% if firstSegments.PID.21.4.1 != "" and firstSegments.PID.21.4.1 != null and firstSegments.PID.21.4.2 != "" and firstSegments.PID.21.4.2 != null and firstSegments.PID.21.4.3 != "" and firstSegments.PID.21.4.3 != null -%}
{% evaluate Organization_ID_pid_CX_21 using 'ID/Organization' HDORG: firstSegments.PID.21.4 -%}
{% include 'Resource/Organization', PID: firstSegments.PID.21, ID: Organization_ID_pid_CX_21 -%}
{% endif -%}
{% evaluate pidrelatedPersonId using 'ID/RelatedPerson' PID: firstSegments.PID, baseId: patientId -%}
{% include 'Resource/RelatedPerson' RelatedPerson_Patient_ID: fullPatientId, ID: pidrelatedPersonId, PID: firstSegments.PID -%}
{% endif -%}
{% endunless -%}
{% if firstSegments.PD1.3 -%}
{% evaluate Organization_ID_PD1_3 using 'ID/Organization' XON: firstSegments.PD1.3 -%}
{% include 'Resource/Organization' PD1: firstSegments.PD1, ID: Organization_ID_PD1_3 -%}
{% endif -%}
{% if firstSegments.PD1.4 -%}
{% if firstSegments.PD1.4.9.1 != "" and firstSegments.PD1.4.9.1 != null and firstSegments.PD1.4.9.2 != "" and firstSegments.PD1.4.9.2 != null and firstSegments.PD1.4.9.3 != "" and firstSegments.PD1.4.9.3 != null -%}
{% evaluate Organization_ID_PD1_4_9 using 'ID/Organization' HDORG: firstSegments.PD1.4.9 -%}
{% include 'Resource/Organization', PD1_4: firstSegments.PD1.4.9, ID: Organization_ID_PD1_4_9 -%}
{% endif -%}
{% evaluate Practitioner_ID_PD1_4 using 'ID/Practitioner' XCN: firstSegments.PD1.4 -%}
{% include 'Resource/Practitioner' PD1: firstSegments.PD1.4, ID: Practitioner_ID_PD1_4 -%}
{% endif -%}
{% if firstSegments.PV1 -%}
{% if firstSegments.PV1.5 -%}
{% if firstSegments.PV1.5.4.1 != "" and firstSegments.PV1.5.4.1 != null and firstSegments.PV1.5.4.2 != "" and firstSegments.PV1.5.4.2 != null and firstSegments.PV1.5.4.3 != "" and firstSegments.PV1.5.4.3 != null -%}
{% evaluate Organization_ID_PV1_5_4 using 'ID/Organization' HDORG: firstSegments.PV1.5.4 -%}
{% include 'Resource/Organization', PV1: firstSegments.PV1.5.4, ID: Organization_ID_PV1_5_4 -%}
{% endif -%}
{% endif -%}
{% if firstSegments.PV1.19 -%}
{% if firstSegments.PV1.19.4.1 != "" and firstSegments.PV1.19.4.1 != null and firstSegments.PV1.19.4.2 != "" and firstSegments.PV1.19.4.2 != null and firstSegments.PV1.19.4.3 != "" and firstSegments.PV1.19.4.3 != null -%}
{% evaluate Organization_ID_PV1_19_4 using 'ID/Organization' HDORG: firstSegments.PV1.19.4 -%}
{% include 'Resource/Organization', PV1: firstSegments.PV1.19.4, ID: Organization_ID_PV1_19_4 -%}
{% endif -%}
{% endif -%}
{% if firstSegments.PV1.7 -%}
{% if firstSegments.PV1.7.9.1 != "" and firstSegments.PV1.7.9.1 != null and firstSegments.PV1.7.9.2 != "" and firstSegments.PV1.7.9.2 != null and firstSegments.PV1.7.9.3 != "" and firstSegments.PV1.7.9.3 != null -%}
{% evaluate Organization_ID_PV1_7_9 using 'ID/Organization' HDORG: firstSegments.PV1.7.9 -%}
{% include 'Resource/Organization', PV1: firstSegments.PV1.7.9, ID: Organization_ID_PV1_7_9 -%}
{% endif -%}
{% evaluate practitionerId_PV1_7 using 'ID/Practitioner' XCN: firstSegments.PV1.7 -%}
{% include 'Resource/Practitioner' PV1: firstSegments.PV1.7, ID: practitionerId_PV1_7 -%}
{% endif -%}
{% if firstSegments.PV1.8 -%}
{% if firstSegments.PV1.8.9.1 != "" and firstSegments.PV1.8.9.1 != null and firstSegments.PV1.8.9.2 != "" and firstSegments.PV1.8.9.2 != null and firstSegments.PV1.8.9.3 != "" and firstSegments.PV1.8.9.3 != null -%}
{% evaluate Organization_ID_PV1_8_9 using 'ID/Organization' HDORG: firstSegments.PV1.8.9 -%}
{% include 'Resource/Organization', PV1: firstSegments.PV1.8.9, ID: Organization_ID_PV1_8_9 -%}
{% endif -%}
{% evaluate practitionerId_PV1_8 using 'ID/Practitioner' XCN: firstSegments.PV1.8 -%}
{% include 'Resource/Practitioner' PV1: firstSegments.PV1.8, ID: practitionerId_PV1_8 -%}
{% endif -%}
{% if firstSegments.PV1.9 -%}
{% if firstSegments.PV1.9.9.1 != "" and firstSegments.PV1.9.9.1 != null and firstSegments.PV1.9.9.2 != "" and firstSegments.PV1.9.9.2 != null and firstSegments.PV1.9.9.3 != "" and firstSegments.PV1.9.9.3 != null -%}
{% evaluate Organization_ID_PV1_9_9 using 'ID/Organization' HDORG: firstSegments.PV1.9.9 -%}
{% include 'Resource/Organization', PV1: firstSegments.PV1.9.9, ID: Organization_ID_PV1_9_9 -%}
{% endif -%}
{% evaluate practitionerId_PV1_9 using 'ID/Practitioner' XCN: firstSegments.PV1.9 -%}
{% include 'Resource/Practitioner' PV1: firstSegments.PV1.9, ID: practitionerId_PV1_9 -%}
{% endif -%}
{% if firstSegments.PV1.17 -%}
{% if firstSegments.PV1.17.9.1 != "" and firstSegments.PV1.17.9.1 != null and firstSegments.PV1.17.9.2 != "" and firstSegments.PV1.17.9.2 != null and firstSegments.PV1.17.9.3 != "" and firstSegments.PV1.17.9.3 != null -%}
{% evaluate Organization_ID_PV1_17_9 using 'ID/Organization' HDORG: firstSegments.PV1.17.9 -%}
{% include 'Resource/Organization', PV1: firstSegments.PV1.17.9, ID: Organization_ID_PV1_17_9 -%}
{% endif -%}
{% evaluate practitionerId_PV1_17 using 'ID/Practitioner' XCN: firstSegments.PV1.17 -%}
{% include 'Resource/Practitioner' PV1: firstSegments.PV1.17, ID: practitionerId_PV1_17 -%}
{% endif -%}
{% if firstSegments.PV1.50 -%}
{% for p in firstSegments.PV1.50.Repeats -%}
{% if p.4.1 != "" and p.4.1 != null and p.4.2 != "" and p.4.2 != null and p.4.3 != "" and p.4.3 != null -%}
{% evaluate Organization_ID_PV1_50_4 using 'ID/Organization' HDORG: p.4 -%}
{% include 'Resource/Organization', PV1: p.4, ID: Organization_ID_PV1_50_4 -%}
{% endif -%}
{% endfor -%}
{% endif -%}
{% if firstSegments.PV1.52 -%}
{% if firstSegments.PV1.52.9.1 != "" and firstSegments.PV1.52.9.1 != null and firstSegments.PV1.52.9.2 != "" and firstSegments.PV1.52.9.2 != null and firstSegments.PV1.52.9.3 != "" and firstSegments.PV1.52.9.3 != null -%}
{% evaluate Organization_ID_PV1_52_9 using 'ID/Organization' HDORG: firstSegments.PV1.52.9 -%}
{% include 'Resource/Organization', PV1: firstSegments.PV1.52.9, ID: Organization_ID_PV1_52_9 -%}
{% endif -%}
{% evaluate practitionerId_PV1_52 using 'ID/Practitioner' XCN: firstSegments.PV1.52 -%}
{% include 'Resource/Practitioner' PV1: firstSegments.PV1.52, ID: practitionerId_PV1_52 -%}
{% endif -%}
{% if firstSegments.PV2.13 -%}
{% if firstSegments.PV2.13.9.1 != "" and firstSegments.PV2.13.9.1 != null and firstSegments.PV2.13.9.2 != "" and firstSegments.PV2.13.9.2 != null and firstSegments.PV2.13.9.3 != "" and firstSegments.PV2.13.9.3 != null -%}
{% evaluate Organization_ID_PV2_13_9 using 'ID/Organization' HDORG: firstSegments.PV2.13.9 -%}
{% include 'Resource/Organization', PV2: firstSegments.PV2.13.9, ID: Organization_ID_PV2_13_9 -%}
{% endif -%}
{% evaluate practitionerId_PV2_13 using 'ID/Practitioner' XCN: firstSegments.PV2.13 -%}
{% include 'Resource/Practitioner' PV1: firstSegments.PV2.13, ID: practitionerId_PV2_13 -%}
{% endif -%}
{% if firstSegments.PV1.3 -%}
{% include 'Resource/PLLocation' PL: firstSegments.PV1.3-%}
{% endif -%}
{% if firstSegments.PV1.6 -%}
{% include 'Resource/PLLocation' PL: firstSegments.PV1.6 -%}
{% endif -%}
{% if firstSegments.PV1.11 -%}
{% include 'Resource/PLLocation' PL: firstSegments.PV1.11-%}
{% endif -%}
{% if firstSegments.PV1.37 -%}
{% evaluate location_ID_PV1_37 using 'ID/Location' DLD: firstSegments.PV1.37 -%}
{% include 'Resource/Location' PV1: firstSegments.PV1.37, ID: location_ID_PV1_37-%}
{% endif -%}
{% if firstSegments.PV1.42 -%}
{% include 'Resource/PLLocation' PL: firstSegments.PV1.42-%}
{% endif -%}
{% if firstSegments.PV1.43 -%}
{% include 'Resource/PLLocation' PL: firstSegments.PV1.43-%}
{% endif -%}
{% if firstSegments.PV2.1 -%}
{% include 'Resource/PLLocation' PL: firstSegments.PV2.1-%}
{% endif -%}
{% if firstSegments.PV1.54 -%}
{% if firstSegments.PV1.54.4.1 != "" and firstSegments.PV1.54.4.1 != null and firstSegments.PV1.54.4.2 != "" and firstSegments.PV1.54.4.2 != null and firstSegments.PV1.54.4.3 != "" and firstSegments.PV1.54.4.3 != null -%}
{% evaluate Organization_ID_PV1_54_4 using 'ID/Organization' HDORG: firstSegments.PV1.54.4 -%}
{% include 'Resource/Organization', PV1: firstSegments.PV1.54.4, ID: Organization_ID_PV1_54_4 -%}
{% endif -%}
{% evaluate EpisodeOfCare_ID using 'ID/EpisodeOfCare' PV1: firstSegments.PV1.54 -%}
{% include 'Resource/EpisodeOfCare' PV1: firstSegments.PV1, PV2: firstSegments.PV2, ID: EpisodeOfCare_ID, patientRefrenceID: fullPatientId, Root_Template: 'ADT_A01' -%}
{% endif -%}
{% evaluate encounterId using 'ID/Encounter' PV1: firstSegments.PV1, baseId: patientId -%}
{% include 'Resource/Encounter' Root_Template: 'ADT_A01', PV1: firstSegments.PV1, PV2: firstSegments.PV2, PDA: firstSegments.PDA, ID: encounterId, AccountId:accountId -%}
{% include 'Reference/Encounter/Subject' ID: encounterId, REF: fullPatientId -%}
{% include 'Extensions/Encounter/EncounterExtension' ID: encounterId, PV1: firstSegments.PV1, PV2: firstSegments.PV2, -%}
{% endif -%}
{% assign arvSegmentLists1 = hl7v2Data | get_related_segment_list: firstSegments.PID, 'ARV' -%}
{% for arvSegment1 in arvSegmentLists1.ARV %}
{% if arvSegment1 %}
{% include 'Resource/Patient' ARV: arvSegment1, ID: patientId %}
{% endif %}
{% endfor %}
{% assign arvSegmentLists2 = hl7v2Data | get_related_segment_list: firstSegments.PV1, 'ARV' -%}
{% for arvSegment2 in arvSegmentLists2.ARV %}
{% if arvSegment2 %}
{% include 'Resource/Encounter' ARV: arvSegment2, ID: encounterId %}
{% endif %}
{% endfor %}
{% if firstSegments.EVN -%}
{% evaluate provenanceId using 'ID/Provenance' EVN: firstSegments.EVN, baseId: patientId -%}
{% include 'Resource/Provenance' EVN: firstSegments.EVN, EVNTEXT: firstSegments.MSH.9, REF_BUNDLE: bundleID, ID: provenanceId -%}
{% if firstSegments.EVN.5 -%}
{% if firstSegments.EVN.5.9.1 != "" and firstSegments.EVN.5.9.1 != null and firstSegments.EVN.5.9.2 != "" and firstSegments.EVN.5.9.2 != null and firstSegments.EVN.5.9.3 != "" and firstSegments.EVN.5.9.3 != null -%}
{% evaluate Organization_ID_EVN_5_9 using 'ID/Organization' HDORG: firstSegments.EVN.5.9 -%}
{% include 'Resource/Organization', EVN: firstSegments.EVN.5.9, ID: Organization_ID_EVN_5_9 -%}
{% endif -%}
{% evaluate practitionerId_EVN_5 using 'ID/Practitioner' XCN: firstSegments.EVN.5 -%}
{% include 'Resource/Practitioner' EVN: firstSegments.EVN.5, ID: practitionerId_EVN_5 -%}
{% if firstSegments.EVN.7 -%}
{% evaluate locationId_EVN_7 using 'ID/Location' HD: firstSegments.EVN.7 -%}
{% include 'Resource/Location' EVN: firstSegments.EVN.7, ID: locationId_EVN_7 -%}
{% endif -%}
{% endif -%}
{% endif -%}
{% assign rolSegmentPositionIndex_PID = 0 %}
{% assign rolSegmentLists_PID = hl7v2Data | get_related_segment_list: firstSegments.PID, 'ROL' -%}
{% for rolSegment_PID in rolSegmentLists_PID.ROL %}
{% unless rolSegment_PID.2.Value == "DE" or rolSegment_PID.2.Value == "UN" %}
{% evaluate practitionerRoleId using 'ID/PractitionerRole' ROL: rolSegment_PID.3, XCN: rolSegment_PID.4.Repeats[0] -%}
{% include 'Resource/PractitionerRole' ROL: rolSegment_PID , ID: practitionerRoleId -%}
{%- assign fullPractitionerRoleId = practitionerRoleId | prepend: 'PractitionerRole/' -%}
{% include 'Reference/Patient/GeneralPractitioner' REF: fullPractitionerRoleId, ID: patientId -%}
{% evaluate practitionerId using 'ID/Practitioner' XCN: rolSegment_PID.4.Repeats[0] -%}
{% assign full_practitionerId = practitionerId | prepend: 'Practitioner/' %}
{% include 'Reference/PractitionerRole/Practitioner' REF: full_practitionerId, ID: practitionerRoleId -%}
{% for rol_4 in rolSegment_PID.4.Repeats %}
{% if rol_4.9.1 != "" and rol_4.9.1 != null and rol_4.9.2 != "" and rol_4.9.2 != null and rol_4.9.3 != "" and rol_4.9.3 != null -%}
{% evaluate Organization_ID using 'ID/Organization' HDORG: rol_4.9 -%}
{% include 'Resource/Organization' OBX: rol_4.9, ID: Organization_ID -%}
{% endif -%}
{% include 'Resource/Practitioner' ROL: rol_4, ID: practitionerId-%}
{% endfor %}
{% if rolSegment_PID.13 -%}
{% include 'Resource/PLLocation' PL: rolSegment_PID.13-%}
{% endif -%}
{% if rolSegment_PID.14 %}
{% evaluate organization_ID_ROL_14 using 'ID/Organization' XON: rolSegment_PID.14.Repeats[0] -%}
{% assign full_organizationId = organization_ID_ROL_14 | prepend: 'Organization/' %}
{% include 'Resource/Organization' ROL: rolSegment_PID , ID: organization_ID_ROL_14 -%}
{% include 'Reference/PractitionerRole/Organization' REF: full_organizationId, ID: practitionerRoleId %}
{% endif %}
{% endunless -%}
{% assign rolSegmentPositionIndex_PID = rolSegmentPositionIndex_PID | plus: 1 %}
{% endfor %}
{% assign rolSegmentPositionIndex_PV1 = 0 %}
{% assign rolSegmentLists_PV1 = hl7v2Data | get_related_segment_list: firstSegments.PV1, 'ROL' -%}
{% for rolSegment_PV1 in rolSegmentLists_PV1.ROL %}
{% unless rolSegment_PV1.2.Value == "DE" or rolSegment_PV1.2.Value == "UN" %}
{% evaluate practitionerRoleId using 'ID/PractitionerRole' ROL: rolSegment_PV1.3, XCN: rolSegment_PV1.4.Repeats[0] -%}
{% include 'Resource/PractitionerRole' ROL: rolSegment_PV1 , ID: practitionerRoleId -%}
{%- assign fullPractitionerRoleId = practitionerRoleId | prepend: 'PractitionerRole/' -%}
{% include 'Reference/Encounter/Participant_Individual' REF: fullPractitionerRoleId, ID: encounterId -%}
{% evaluate practitionerId using 'ID/Practitioner' XCN: rolSegment_PV1.4.Repeats[0] -%}
{% assign full_practitionerId = practitionerId | prepend: 'Practitioner/' %}
{% include 'Reference/PractitionerRole/Practitioner' REF: full_practitionerId, ID: practitionerRoleId -%}
{% for rol_4 in rolSegment_PV1.4.Repeats %}
{% if rol_4.9.1 != "" and rol_4.9.1 != null and rol_4.9.2 != "" and rol_4.9.2 != null and rol_4.9.3 != "" and rol_4.9.3 != null -%}
{% evaluate Organization_ID using 'ID/Organization' HDORG: rol_4.9 -%}
{% include 'Resource/Organization' OBX: rol_4.9, ID: Organization_ID -%}
{% endif -%}
{% include 'Resource/Practitioner' ROL: rol_4, ID: practitionerId-%}
{% endfor %}
{% if rolSegment_PV1.13 -%}
{% include 'Resource/PLLocation' PL: rolSegment_PV1.13-%}
{% endif -%}
{% if rolSegment_PV1.14 %}
{% evaluate organization_ID_ROL_14 using 'ID/Organization' XON: rolSegment_PV1.14.Repeats[0] -%}
{% assign full_organizationId = organization_ID_ROL_14 | prepend: 'Organization/' %}
{% include 'Resource/Organization' ROL: rolSegment_PV1 , ID: organization_ID_ROL_14 -%}
{% include 'Reference/PractitionerRole/Organization' REF: full_organizationId, ID: practitionerRoleId %}
{% endif %}
{% endunless -%}
{% assign rolSegmentPositionIndex_PV1 = rolSegmentPositionIndex_PV1 | plus: 1 %}
{% endfor %}
{% for pr1Segment in pr1SegmentLists.PR1 -%}
{% if pr1Segment.8 -%}
{% if pr1Segment.8.9.1 != "" and pr1Segment.8.9.1 != null and pr1Segment.8.9.2 != "" and pr1Segment.8.9.2 != null and pr1Segment.8.9.3 != "" and pr1Segment.8.9.3 != null -%}
{% evaluate Organization_ID_PR1_8 using 'ID/Organization' HDORG: pr1Segment.8.9 -%}
{% include 'Resource/Organization', PR1: pr1Segment.8.9, ID: Organization_ID_PR1_8 -%}
{% endif -%}
{% evaluate practitionerId_PR1_8 using 'ID/Practitioner' XCN: pr1Segment.8 -%}
{% include 'Resource/Practitioner' PR1: pr1Segment.8, ID: practitionerId_PR1_8 -%}
{% endif -%}
{% if pr1Segment.11 -%}
{% if pr1Segment.11.9.1 != "" and pr1Segment.11.9.1 != null and pr1Segment.11.9.2 != "" and pr1Segment.11.9.2 != null and pr1Segment.11.9.3 != "" and pr1Segment.11.9.3 != null -%}
{% evaluate Organization_ID_PR1_11 using 'ID/Organization' HDORG: pr1Segment.11.9 -%}
{% include 'Resource/Organization', PR1: pr1Segment.11.9, ID: Organization_ID_PR1_11 -%}
{% endif -%}
{% evaluate practitionerId_PR1_11 using 'ID/Practitioner' XCN: pr1Segment.11 -%}
{% include 'Resource/Practitioner' PR1: pr1Segment.11, ID: practitionerId_PR1_11 -%}
{% endif -%}
{% if pr1Segment.12 -%}
{% if pr1Segment.12.9.1 != "" and pr1Segment.12.9.1 != null and pr1Segment.12.9.2 != "" and pr1Segment.12.9.2 != null and pr1Segment.12.9.3 != "" and pr1Segment.12.9.3 != null -%}
{% evaluate Organization_ID_PR1_12 using 'ID/Organization' HDORG: pr1Segment.12.9 -%}
{% include 'Resource/Organization', PR1: pr1Segment.12.9, ID: Organization_ID_PR1_12 -%}
{% endif -%}
{% evaluate practitionerId_PR1_12 using 'ID/Practitioner' XCN: pr1Segment.12 -%}
{% include 'Resource/Practitioner' PR1: pr1Segment.12, ID: practitionerId_PR1_12 -%}
{% endif -%}
{% if pr1Segment.23 -%}
{% include 'Resource/PLLocation' PL: pr1Segment.23-%}
{% endif %}
{% if pr1Segment.25 -%}
{% evaluate Procedure_ID_PR1_25 using 'ID/Procedure' PR1: pr1Segment.25, baseId: fullPatientId -%}
{% include 'Resource/Procedure' PR1_25: pr1Segment.25, PV1: firstSegments.PV1, Procedure_Subject_ID: fullPatientId, ID: Procedure_ID_PR1_25 -%}
{% endif -%}
{% evaluate procedureId using 'ID/Procedure' PR1: pr1Segment, baseId: patientId -%}
{% include 'Resource/Procedure' PR1: pr1Segment, PV1: firstSegments.PV1, Procedure_Subject_ID: fullPatientId, ID: procedureId -%}
{% assign rolSegmentPositionIndex_PR1 = 0 %}
{% assign rolSegmentLists_PR1 = hl7v2Data | get_related_segment_list: pr1Segment, 'ROL' -%}
{% for rolSegment_PR1 in rolSegmentLists_PR1.ROL %}
{% unless rolSegment_PR1.2.Value == "DE" or rolSegment_PR1.2.Value == "UN" %}
{% evaluate practitionerRoleId using 'ID/PractitionerRole' ROL: rolSegment_PR1.3, XCN: rolSegment_PR1.4.Repeats[0] -%}
{% include 'Resource/PractitionerRole' ROL: rolSegment_PR1 , ID: practitionerRoleId -%}
{%- assign fullPractitionerRoleId = practitionerRoleId | prepend: 'PractitionerRole/' -%}
{% include 'Reference/Procedure/Performer_Actor' REF: fullPractitionerRoleId, ID: procedureId -%}
{% evaluate practitionerId using 'ID/Practitioner' XCN: rolSegment_PR1.4.Repeats[0] -%}
{% assign full_practitionerId = practitionerId | prepend: 'Practitioner/' %}
{% include 'Reference/PractitionerRole/Practitioner' REF: full_practitionerId, ID: practitionerRoleId -%}
{% for rol_4 in rolSegment_PR1.4.Repeats %}
{% if rol_4.9.1 != "" and rol_4.9.1 != null and rol_4.9.2 != "" and rol_4.9.2 != null and rol_4.9.3 != "" and rol_4.9.3 != null -%}
{% evaluate Organization_ID using 'ID/Organization' HDORG: rol_4.9 -%}
{% include 'Resource/Organization' OBX: rol_4.9, ID: Organization_ID -%}
{% endif -%}
{% include 'Resource/Practitioner' ROL: rol_4, ID: practitionerId-%}
{% endfor %}
{% if rolSegment_PR1.13 -%}
{% include 'Resource/PLLocation' PL: rolSegment_PR1.13-%}
{% endif -%}
{% if rolSegment_PR1.14 %}
{% evaluate organization_ID_ROL_14 using 'ID/Organization' XON: rolSegment_PR1.14.Repeats[0] -%}
{% assign full_organizationId = organization_ID_ROL_14 | prepend: 'Organization/' %}
{% include 'Resource/Organization' ROL: rolSegment_PR1 , ID: organization_ID_ROL_14 -%}
{% include 'Reference/PractitionerRole/Organization' REF: full_organizationId, ID: practitionerRoleId %}
{% endif %}
{% endunless -%}
{% assign rolSegmentPositionIndex_PR1 = rolSegmentPositionIndex_PR1 | plus: 1 %}
{% endfor %}
{% endfor -%}
{% for nk1Segment in nk1SegmentLists.NK1 -%}
{% evaluate relatedPersonId using 'ID/RelatedPerson' NK1: nk1Segment, baseId: patientId -%}
{% if nk1Segment.13 -%}
{% evaluate organizationId_NK1_13 using 'ID/Organization' XON: nk1Segment.13 -%}
{% include 'Resource/Organization' NK1: nk1Segment, ID: organizationId_NK1_13 -%}
{% assign Organization_NK1_13 = organizationId_NK1_13 | prepend: 'Organization/' -%}
{% include 'Reference/Patient/Contact_Organization' REF:Organization_NK1_13 , ID: patientId -%}
{% endif -%}
{% unless nk1Segment.3.1.Value == "EMR" or nk1Segment.3.1.Value == "CGV" or nk1Segment.7.1.Value == "E" or nk1Segment.7.1.Value =="F" or nk1Segment.7.1.Value == "I" or nk1Segment.7.1.Value =="S" -%}
{% include 'Resource/Patient' NK1: nk1Segment, ID: patientId -%}
{% include 'Resource/RelatedPerson' NK1: nk1Segment, RelatedPerson_Patient_ID: fullPatientId, ID: relatedPersonId, PID: firstSegments.PID -%}
{% include 'Extensions/RelatedPerson/RelatedPerson' ID: relatedPersonId, NK1: nk1Segment -%}
{% if nk1Segment.12.4.1 != "" and nk1Segment.12.4.1 != null and nk1Segment.12.4.2 != "" and nk1Segment.12.4.2 != null and nk1Segment.12.4.3 != "" and nk1Segment.12.4.3 != null -%}
{% evaluate Organization_ID_nk_CX_12 using 'ID/Organization' HDORG: nk1Segment.12.4 -%}
{% include 'Resource/Organization', NK1_12: nk1Segment.12, ID: Organization_ID_nk_CX_12 -%}
{% endif -%}
{% endunless -%}
{% if firstSegments.PID.21 -%}
{% include 'Resource/RelatedPerson' ID: relatedPersonId, PID: firstSegments.PID, RelatedPerson_Patient_ID: fullPatientId -%}
{% if firstSegments.PID.21.4.1 != "" and firstSegments.PID.21.4.1 != null and firstSegments.PID.21.4.2 != "" and firstSegments.PID.21.4.2 != null and firstSegments.PID.21.4.3 != "" and firstSegments.PID.21.4.3 != null -%}
{% evaluate Organization_ID_pid_CX_21 using 'ID/Organization' HDORG: firstSegments.PID.21.4 -%}
{% include 'Resource/Organization', PID: firstSegments.PID.21, ID: Organization_ID_pid_CX_21 -%}
{% endif -%}
{% endif -%}
{% for nk33 in nk1Segment.33.Repeats -%}
{% if nk33.4.1 != "" and nk33.4.1 != null and nk33.4.2 != "" and nk33.4.2 != null and nk33.4.3 != "" and nk33.4.3 != null -%}
{% evaluate Organization_ID_nk_CX_33 using 'ID/Organization' HDORG: nk33.4 -%}
{% include 'Resource/Organization', NK1_33: nk33, ID: Organization_ID_nk_CX_33 -%}
{% endif -%}
{% endfor -%}
{% endfor -%}
{% for db1Segment in db1SegmentLists.DB1 -%}
{% if db1Segment.2.1.Value == "PT" %}
{% for db in db1Segment.3.Repeats -%}
{% if db.4.1 != "" and db.4.1 != null and db.4.2 != "" and db.4.2 != null and db.4.3 != "" and db.4.3 != null -%}
{% evaluate Organization_ID_DB1_3 using 'ID/Organization' HDORG: db.4 -%}
{% include 'Resource/Organization', DB1: db.4, ID: Organization_ID_DB1_3 -%}
{% endif -%}
{% endfor %}
{% include 'Extensions/Patient/PatientExtension' ID: PatientId, DB1: db1Segment -%}
{% endif -%}
{% include 'Resource/Patient' DB1: db1Segment, ID: patientId -%}
{% endfor %}
{% for obxSegment in obxSegmentLists.OBX -%}
{% for obx_16 in obxSegment.16.Repeats -%}
{% if obx_16.9.1 != "" and obx_16.9.1 != null and obx_16.9.2 != "" and obx_16.9.2 != null and obx_16.9.3 != "" and obx_16.9.3 != null -%}
{% evaluate Organization_ID_OBX_16 using 'ID/Organization' HDORG: obx_16.9 -%}
{% include 'Resource/Organization' OBX: obx_16.9, ID: Organization_ID_OBX_16 -%}
{% endif -%}
{% evaluate practitionerId_OBX_16 using 'ID/Practitioner' XCN: obx_16 -%}
{% include 'Resource/Practitioner' OBX: obxSegment, OBXXCN1: obx_16, ID: practitionerId_OBX_16 -%}
{% evaluate practitionerRoleId_OBX_16 using 'ID/PractitionerRole' XCN: obx_16 -%}
{% assign full_practitionerId_OBX_16 = practitionerId_OBX_16 | prepend: 'Practitioner/' %}
{% include 'Resource/PractitionerRole' OBXXCN1: obx_16, ID: practitionerRoleId_OBX_16 -%}
{% include 'Reference/PractitionerRole/Practitioner' REF: full_practitionerId_OBX_16, ID: practitionerRoleId_OBX_16 -%}
{% endfor %}
{% if obxSegment.18 -%}
{% evaluate deviceId_OBX_18 using 'ID/Device' EI: obxSegment.18.Repeats[0] -%}
{% include 'Resource/Device' EI_OBX18: obxSegment.18, ID: deviceId_OBX_18 -%}
{% endif -%}
{% if obxSegment.23 or obxSegment.25 %}
{% evaluate practitionerRoleId_OBX_25 using 'ID/PractitionerRole' XCN: obxSegment.25 -%}
{% endif %}
{% if obxSegment.25 -%}
{% evaluate practitionerId_OBX_25 using 'ID/Practitioner' XCN: obxSegment.25 -%}
{% include 'Resource/Practitioner' OBX: obxSegment, OBXXCN2: obxSegment.25, ID: practitionerId_OBX_25 -%}
{% assign full_practitionerId_OBX_25 = practitionerId_OBX_25 | prepend: 'Practitioner/' %}
{% include 'Resource/PractitionerRole' OBXXCN2: obxSegment.25, ID: practitionerRoleId_OBX_25 -%}
{% include 'Reference/PractitionerRole/Practitioner' REF: full_practitionerId_OBX_25, ID: practitionerRoleId_OBX_25 -%}
{% endif -%}
{% if obxSegment.23 -%}
{% evaluate organizationId_OBX_23 using 'ID/Organization' XON: obxSegment.23 -%}
{% include 'Resource/Organization' OBX_23: obxSegment.23, OBX_24: obxSegment.24, ID: organizationId_OBX_23 -%}
{% if obxSegment.25 -%}
{% assign full_organizationId_OBX_23 = organizationId_OBX_23 | prepend: 'Organization/' %}
{% include 'Reference/PractitionerRole/Organization' REF: full_organizationId_OBX_23, ID: practitionerRoleId_OBX_25 -%}
{% endif -%}
{% endif -%}
{% evaluate observationId using 'ID/Observation' OBX: obxSegment, baseId: patientId -%}
{% include 'Resource/Observation' OBX: obxSegment, Observation_Subject_ID: fullPatientId, ID: observationId -%}
{% include 'Extensions/Observation/ObservationExtension' OBX: obxSegment, ID: observationId %}
{% endfor -%}
{% for al1Segment in al1SegmentLists.AL1 -%}
{% evaluate allergyIntoleranceId using 'ID/AllergyIntolerance' AL1: al1Segment, baseId: patientId -%}
{% include 'Resource/AllergyIntolerance' AL1: al1Segment, ID: allergyIntoleranceId, AllergyIntolerance_Patient_ID: fullPatientId -%}
{% endfor -%}
{% for dg1Segment in dg1SegmentLists.DG1 -%}
{% if dg1Segment.16.Repeats[0] %}
{% if dg1Segment.16.Repeats[0].9.1 != "" and dg1Segment.16.Repeats[0].9.1 != null and dg1Segment.16.Repeats[0].9.2 != "" and dg1Segment.16.Repeats[0].9.2 != null and dg1Segment.16.Repeats[0].9.3 != "" and dg1Segment.16.Repeats[0].9.3 != null -%}
{% evaluate Organization_ID_DG1_16 using 'ID/Organization' HDORG: dg1Segment.16.Repeats[0].9 -%}
{% include 'Resource/Organization', DG1: dg1Segment.16.Repeats[0].9, ID: Organization_ID_DG1_16 -%}
{% endif -%}
{% evaluate practitionerId_DG1_16 using 'ID/Practitioner' XCN: dg1Segment.16.Repeats[0] -%}
{% include 'Resource/Practitioner' DG1: dg1Segment, ID: practitionerId_DG1_16 -%}
{% endif -%}
{% evaluate conditionId using 'ID/Condition' DG1: dg1Segment, baseId: patientId -%}
{% include 'Resource/Condition' DG1: dg1Segment, Condition_Subject_ID: fullPatientId, baseId: patientId, ID: conditionId -%}
{% if encounterId -%}
{% include 'Resource/Encounter' DG1: dg1Segment, conditionId: conditionId, ID: encounterId -%}
{% endif %}
{% if EpisodeOfCare_ID %}
{% include 'Resource/EpisodeOfCare' DG1: dg1Segment, conditionId: conditionId, ID: EpisodeOfCare_ID -%}
{% endif %}
{% if dg1Segment.22 -%}
{% evaluate conditionId_DG1_22 using 'ID/Condition' DG1: dg1Segment.22, baseId: patientId -%}
{% include 'Resource/Condition' DG1_22: dg1Segment.22, Condition_Subject_ID_DG1_22: fullPatientId, ID: conditionId_DG1_22 -%}
{% endif -%}
{% include 'Extensions/Condition/ConditionExtension' DG1: dg1Segment, ID_DG1_22:conditionId_DG1_22, ID: conditionId %}
{% endfor -%}
{% for in1Segment in in1SegmentLists.IN1 -%}
{% evaluate coverageId using 'ID/Coverage' CX: in1Segment -%}
{% include 'Resource/Coverage' IN1: in1Segment, ID: coverageId -%}
{% include 'Reference/Coverage/Beneficiary' ID: coverageId, REF: fullPatientId -%}
{% for in1_49 in in1Segment.49.Repeats -%}
{% if in1_49.4.1 != "" and in1_49.4.1 != null and in1_49.4.2 != "" and in1_49.4.2 != null and in1_49.4.3 != "" and in1_49.4.3 != null -%}
{% evaluate Organization_ID_in1_CX_49 using 'ID/Organization' HDORG: in1_49.4 -%}
{% include 'Resource/Organization', IN1_49: in1_49, ID: Organization_ID_in1_CX_49 -%}
{% endif -%}
{% endfor -%}
{% for in1_3 in in1Segment.3.Repeats -%}
{% if in1_3.4.1 != "" and in1_3.4.1 != null and in1_3.4.2 != "" and in1_3.4.2 != null and in1_3.4.3 != "" and in1_3.4.3 != null -%}
{% evaluate Organization_ID_in1_CX_3 using 'ID/Organization' HDORG: in1_3.4 -%}
{% include 'Resource/Organization', IN1_3: in1_3, ID: Organization_ID_in1_CX_3 -%}
{% endif -%}
{% endfor -%}
{% for in1_10 in in1Segment.10.Repeats -%}
{% if in1_10.4.1 != "" and in1_10.4.1 != null and in1_10.4.2 != "" and in1_10.4.2 != null and in1_10.4.3 != "" and in1_10.4.3 != null -%}
{% evaluate Organization_ID_in1_CX_10 using 'ID/Organization' HDORG: in1_10.4 -%}
{% include 'Resource/Organization', IN1_10: in1_10, ID: Organization_ID_in1_CX_10 -%}
{% endif -%}
{% endfor -%}
{% if in1Segment.4 -%}
{% evaluate Organization_ID_in1_4 using 'ID/Organization' XON: in1Segment.4 -%}
{% else -%}
{% evaluate Organization_ID_in1_4 using 'ID/Organization' CX: in1Segment -%}
{% endif -%}
{% if Organization_ID_in1_4 -%}
{% include 'Resource/Organization', IN1: in1Segment, ID: Organization_ID_in1_4 -%}
{% assign org_in1_4 = Organization_ID_in1_4 | prepend: 'Organization/' -%}
{% include 'Reference/Coverage/Payor' ID: coverageId, REF: org_in1_4 -%}
{% endif -%}
{% if in1Segment.11 -%}
{% evaluate Organization_ID_in1_11 using 'ID/Organization' XON: in1Segment.11 -%}
{% else -%}
{% evaluate Organization_ID_in1_11 using 'ID/Organization' CX_IN1: in1Segment -%}
{% endif -%}
{% if Organization_ID_in1_11 -%}
{% include 'Resource/Organization', IN1_11: in1Segment, ID: Organization_ID_in1_11 -%}
{% assign org_in1_11 = Organization_ID_in1_11 | prepend: 'Organization/' -%}
{% include 'Reference/Coverage/PolicyHolder' ID: coverageId, REF: org_in1_11 -%}
{% endif -%}
{% if in1Segment.17.1.Value == "SEL" -%}
{% include 'Resource/Patient' PID_IN1: firstSegments.PID, IN1: in1Segment, ID: patientId -%}
{% include 'Reference/Coverage/Subscriber' ID: coverageId, REF: fullPatientId -%}
{% endif -%}
{% unless in1Segment.17.1.Value == "SEL" or in1Segment.17.Value == "" -%}
{% evaluate in1relatedPersonId using 'ID/RelatedPerson' IN1: in1Segment, baseId: patientId -%}
{% include 'Resource/RelatedPerson' RelatedPerson_Patient_ID: fullPatientId, ID: in1relatedPersonId, IN1: in1Segment -%}
{% assign rel_in1_17 = in1relatedPersonId | prepend: 'RelatedPerson/' -%}
{% include 'Reference/Coverage/Subscriber' ID: coverageId, REF: rel_in1_17 -%}
{% endunless -%}
{% for in2Segment in in2SegmentLists.IN2 -%}
{% if in1Segment.17.1.Value == "SEL" -%}
{% include 'Resource/Patient' IN2: in2Segment, ID: patientId -%}
{% endif -%}
{% unless in1Segment.17.1.Value == "SEL" or in1Segment.17.Value == "" -%}
{% include 'Resource/RelatedPerson' RelatedPerson_Patient_ID: fullPatientId, ID: in1relatedPersonId, IN2: in2Segment -%}
{% endunless -%}
{% unless in1Segment.10 and in1Segment.11 -%}
{% if in2Segment.3 -%}
{% evaluate in2relatedPersonId using 'ID/RelatedPerson' IN2: in2Segment, baseId: patientId -%}
{% if in2Segment.3.9.1 != "" and in2Segment.3.9.1 != null and in2Segment.3.9.2 != "" and in2Segment.3.9.2 != null and in2Segment.3.9.3 != "" and in2Segment.3.9.3 != null -%}
{% evaluate Organization_ID_IN2_3_9 using 'ID/Organization' HDORG: in2Segment.3.9 -%}
{% include 'Resource/Organization', IN2_3: in2Segment.3.9, ID: Organization_ID_IN2_3_9 -%}
{% endif -%}
{% include 'Resource/RelatedPerson' RelatedPerson_Patient_ID: fullPatientId, ID: in2relatedPersonId, IN2_3: in2Segment -%}
{% assign relatedperson_3 = in2relatedPersonId | prepend: 'RelatedPerson/' -%}
{% include 'Reference/Coverage/PolicyHolder' ID: coverageId, REF: relatedperson_3 -%}
{% endif -%}
{% endunless -%}
{% if in1Segment.10 == null and in1Segment.11 == null and in2Segment.3 == null -%}
{% if in2Segment.70 -%}
{% evaluate organization_Id_IN2_70 using 'ID/Organization' XON: in2Segment.70 -%}
{% include 'Resource/Organization' IN2_70: in2Segment, ID: organization_Id_IN2_70 -%}
{% assign org_in2_70 = organization_Id_IN2_70 | prepend: 'Organization/' -%}
{% include 'Reference/Coverage/PolicyHolder' ID: coverageId, REF: org_in2_70 -%}
{% endif -%}
{% endif -%}
{% if Organization_ID_in1_11 -%}
{% include 'Resource/Organization', IN2_49: in2Segment, ID: Organization_ID_in1_11 -%}
{% endif -%}
{% include 'Resource/Coverage' IN2: in2Segment, ID: coverageId -%}
{% include 'Resource/Patient' IN2_6: in2Segment, ID: patientId -%}
{% for in2_1 in in2Segment.1.Repeats -%}
{% if in2_1.4.1 != "" and in2_1.4.1 != null and in2_1.4.2 != "" and in2_1.4.2 != null and in2_1.4.3 != "" and in2_1.4.3 != null -%}
{% evaluate Organization_ID_in2_CX_1 using 'ID/Organization' HDORG: in2_1.4 -%}
{% include 'Resource/Organization', IN2_1: in2_1, ID: Organization_ID_in2_CX_1 -%}
{% endif -%}
{% endfor -%}
{% if in2Segment.61 -%}
{% if in2Segment.61.4.1 != "" and in2Segment.61.4.1 != null and in2Segment.61.4.2 != "" and in2Segment.61.4.2 != null and in2Segment.61.4.3 != "" and in2Segment.61.4.3 != null -%}
{% evaluate Organization_ID_in2_CX_61 using 'ID/Organization' HDORG: in2Segment.61.4 -%}
{% include 'Resource/Organization', IN2_61: in2Segment.61, ID: Organization_ID_in2_CX_61 -%}
{% endif -%}
{% endif -%}
{% endfor -%}
{% for autSegment in autSegmentLists.AUT %}
{% if autSegment.6 %}
{% if autSegment.29.Value != 'D' %}
{% assign status_error = false %}
{% endif %}
{% if firstSegments.PV1.2.Value != 'I' and firstSegments.PV1.2.Value != 'O' %}
{% unless autSegment.21 %}
{% assign default_type = true %}
{% endunless %}
{% endif %}
{% if autSegment.2 or autSegment.3 %}
{% if autSegment.3 %}
{% assign organization_name = autSegment.3.Value %}
{% elsif autSegment.2 %}
{% assign organization_name = autSegment.2.2.Value %}
{% endif %}
{% include 'Resource/Organization' aut_org_name: organization_name, ID: Organization_ID_in1_4 %}
{% endif %}
{% evaluate claimResponse_ID_AUT_6 using 'ID/ClaimResponse' AUT_6: autSegment.6 -%}
{% include 'Resource/ClaimResponse' AUT: autSegment, status_flag: status_error, type1: default_type, ID: claimResponse_ID_AUT_6 %}
{% include 'Reference/ClaimResponse/Patient' ID: claimResponse_ID_AUT_6, REF: fullPatientId -%}
{% assign full_Organization_ID_in1_4 = Organization_ID_in1_4 | prepend: 'Organization/' %}
{% assign check_PV1 = hl7v2Data | has_segments: 'PV1' -%}
{% if check_PV1 %}
{% if firstSegments.PV1.2 %}
{% include 'Resource/ClaimResponse' AUT_PV1_2: firstSegments.PV1.2, ID: claimResponse_ID_AUT_6 %}
{% endif %}
{% endif %}
{% include 'Reference/ClaimResponse/Insurer' ID: claimResponse_ID_AUT_6, REF: full_Organization_ID_in1_4 -%}
{% endif %}
{% endfor %}
{% for rf1Segment in rf1SegmentLists.RF1 -%}
{% if rf1Segment.19 -%}
{% evaluate Organization_ID_RF1_19 using 'ID/Organization' XON: rf1Segment.19 -%}
{% include 'Resource/Organization' RF1: rf1Segment.19, ID: Organization_ID_RF1_19 -%}
{% endif -%}
{% if rf1Segment.20 -%}
{% if rf1Segment.20.9.1 != "" and rf1Segment.20.9.1 != null and rf1Segment.20.9.2 != "" and rf1Segment.20.9.2 != null and rf1Segment.20.9.3 != "" and rf1Segment.20.9.3 != null -%}
{% evaluate Organization_ID_RF1_20_9 using 'ID/Organization' HDORG: rf1Segment.20.9 -%}
{% include 'Resource/Organization', RF1_20: rf1Segment.20.9, ID: Organization_ID_RF1_20_9 -%}
{% endif -%}
{% evaluate Practitioner_ID_RF1_20 using 'ID/Practitioner' XCN: rf1Segment.20 -%}
{% include 'Resource/Practitioner' RF1: rf1Segment.20, ID: Practitioner_ID_RF1_20 -%}
{% endif -%}
{% evaluate serviceRequestId using 'ID/ServiceRequest' RF1: rf1Segment, baseId: patientId -%}
{% assign full_encounterID = encounterId | prepend: 'Encounter/' %}
{% include 'Resource/ServiceRequest' RF1: rf1Segment, ID: serviceRequestId, ServiceRequest_Subject_ID: fullPatientId, msg_trigger: "ADT", ServiceRequest_Encounter_ID: full_encounterID -%}
{% endfor -%}
{% endfor -%}
{% if firstSegments.PDA %}
{% if firstSegments.PDA.2 -%}
{% include 'Resource/PLLocation' PL: firstSegments.PDA.2-%}
{% endif -%}
{% if firstSegments.PDA.5 -%}
{% if firstSegments.PDA.5.9.1 != "" and firstSegments.PDA.5.9.1 != null and firstSegments.PDA.5.9.2 != "" and firstSegments.PDA.5.9.2 != null and firstSegments.PDA.5.9.3 != "" and firstSegments.PDA.5.9.3 != null -%}
{% evaluate Organization_ID_pda_5 using 'ID/Organization' HDORG: firstSegments.PDA.5.9 -%}
{% include 'Resource/Organization' PDA_5: firstSegments.PDA.5, ID: Organization_ID_pda_5 -%}
{% endif -%}
{% evaluate practitionerId_PDA_5 using 'ID/Practitioner' XCN: firstSegments.PDA.5 -%}
{% include 'Resource/Practitioner' PDA_5: firstSegments.PDA.5, ID: practitionerId_PDA_5 -%}
{% endif -%}
{% if firstSegments.PDA.8 -%}
{% if firstSegments.PDA.8.9.1 != "" and firstSegments.PDA.8.9.1 != null and firstSegments.PDA.8.9.2 != "" and firstSegments.PDA.8.9.2 != null and firstSegments.PDA.8.9.3 != "" and firstSegments.PDA.8.9.3 != null -%}
{% evaluate Organization_ID_pda_8 using 'ID/Organization' HDORG: firstSegments.PDA.8.9 -%}
{% include 'Resource/Organization' PDA_8: firstSegments.PDA.8, ID: Organization_ID_pda_8 -%}
{% endif -%}
{% evaluate practitionerId_PDA_8 using 'ID/Practitioner' XCN: firstSegments.PDA.8 -%}
{% include 'Resource/Practitioner' PDA_8: firstSegments.PDA.8, ID: practitionerId_PDA_8 -%}
{% endif -%}
{% if firstSegments.PDA.1 %}
{% for PDA_1 in firstSegments.PDA.1.Repeats %}
{% evaluate condition_PDA_1_ID using 'ID/Condition' PDA: PDA_1, baseId: patientId -%}
{% include 'Resource/Condition' PDA_1: PDA_1, Condition_Subject_ID: fullPatientId, ID: condition_PDA_1_ID -%}
{% endfor %}
{% endif %}
{% if firstSegments.PDA.4 and firstSegments.PDA.5.1 %}
{% evaluate procedure_1_PDA_ID using 'ID/Procedure' PID_3: firstSegments.PID.3.Repeats[0], PDA_death: firstSegments.PDA, baseId: patientId -%}
{% include 'Resource/Procedure' PDA_proc_1: firstSegments.PDA, Procedure_Subject_ID: fullPatientId, ID: procedure_1_PDA_ID -%}
{% endif %}
{% if firstSegments.PDA.7 and firstSegments.PDA.8.1 %}
{% evaluate procedure_2_PDA_ID using 'ID/Procedure' PID_3: firstSegments.PID.3.Repeats[0], PDA_autopsy: firstSegments.PDA, baseId: patientId -%}
{% include 'Resource/Procedure' PDA_proc_2: firstSegments.PDA, Procedure_Subject_ID: fullPatientId, ID: procedure_2_PDA_ID -%}
{% endif %}
{% if firstSegments.PDA.3 and firstSegments.PDA.4 and firstSegments.PDA.5.1 -%}
{% evaluate observation_PDA_3_ID using 'ID/Observation' PDA_death: firstSegments.PDA -%}
{% include 'Resource/Observation' PDA_3: firstSegments.PDA.3, Observation_Subject_ID: fullPatientId, ID: observation_PDA_3_ID -%}
{% endif %}
{% if firstSegments.PDA.6 and firstSegments.PDA.7 and firstSegments.PDA.8.1 -%}
{% evaluate observation_PDA_6_ID using 'ID/Observation' PDA_autopsy: firstSegments.PDA -%}
{% include 'Resource/Observation' PDA_6: firstSegments.PDA.6, Observation_Subject_ID: fullPatientId, ID: observation_PDA_6_ID -%}
{% endif %}
{% if firstSegments.PDA.9 -%}
{% evaluate observation_PDA_9_ID using 'ID/Observation' PDA_coroner: firstSegments.PDA -%}
{% include 'Resource/Observation' PDA_9: firstSegments.PDA.9, Observation_Subject_ID: fullPatientId, ID: observation_PDA_9_ID -%}
{% endif %}
{% endif %}
{% if firstSegments.ACC -%}
{% if firstSegments.ACC.7 -%}
{% if firstSegments.ACC.7.9.1 != "" and firstSegments.ACC.7.9.1 != null and firstSegments.ACC.7.9.2 != "" and firstSegments.ACC.7.9.2 != null and firstSegments.ACC.7.9.3 != "" and firstSegments.ACC.7.9.3 != null -%}
{% evaluate Organization_ID_ACC_7_9 using 'ID/Organization' HDORG: firstSegments.ACC.7.9 -%}
{% include 'Resource/Organization', ACC: firstSegments.ACC.7.9, ID: Organization_ID_ACC_7_9 -%}
{% endif -%}
{% evaluate Practitioner_ID_ACC_7 using 'ID/Practitioner' XCN: firstSegments.ACC.7 -%}
{% include 'Resource/Practitioner' ACC: firstSegments.ACC.7, ID: Practitioner_ID_ACC_7 -%}
{% endif -%}
{% if firstSegments.ACC.3 or firstSegments.ACC.11 %}
{% evaluate locationId_ACC using 'ID/Location' ACC: firstSegments.ACC -%}
{% include 'Resource/Location' ACC: firstSegments.ACC, ID: locationId_ACC -%}
{% endif %}
{% evaluate observationId using 'ID/Observation' ACC: firstSegments.ACC, baseId: patientId -%}
{% include 'Resource/Observation' ACC: firstSegments.ACC, ID: observationId, Observation_Subject_ID: fullPatientId -%}
{% assign fullPractitioner_ID_ACC_7 = Practitioner_ID_ACC_7 | prepend: 'Practitioner/' -%}
{% include 'Reference/Observation/Performer' ID: observationId, REF: fullPractitioner_ID_ACC_7 -%}
{% if locationId_ACC %}
{% include 'Extensions/Observation/ObservationExtension' ID: observationId, locationId: locationId_ACC -%}
{% endif -%}
{% endif -%}
]
}