PK.PAT.ACT,
"HDR",@1,
"PATS",@2,
"TRLR",@6,
END;

[1]::HDR
"<?xml version="_D(34)_1.0_D(34)_" ?>"^!,
"<ReplyMessage>"^!,
"<TransactionId>"_7801_"</TransactionId>"^!,
"<Status>Success</Status>"^!,
"<StatusDescription>Accounts read: 1</StatusDescription>"^!,
"<Payload>"^!,
"<Accounts>"^!;

[2]::PATS
1^nr,
DO{nr</ARGC 1,
   /ARGV[nr]^ACT,
   ?(A)AAAI[ACT]^aa,
   "PAT",@3,
   nr+1^nr};

[3]::PAT
"<Account>"^!,
"<PatientNumber>"_$(A)AA[aa]|3_"</PatientNumber>"^!,
"<VisitNumber>"_$(A)AA[aa]|0_"</VisitNumber>"^!,
"<PatientName>"_$(A)AA[aa]|1_"</PatientName>"^!,
$(A)AA[aa]B|0^Q2,
<$(A)AA[aa]EV[""]^/tmp,
Q2_(/tmp#0S)_(/tmp#1S)^Q2,
""^ggg,
DO{+$(A)AA[aa]CDR[ggg]^ggg Q2_ggg^Q2},
"<Key>"_Q2_"</Key>"^!,
"<ArriveDate>"^!,
($(A)AA[aa]|13!$(A)AA[aa]|15!$(A)AA[aa]O3|1!$(A)AA[aa]T|0)^!,
($(A)AA[aa]|14!$(A)AA[aa]|16!$(A)AA[aa]O3|2!$(A)AA[aa]T|1)^!,
"</ArriveDate>"^!,
"<RegistrationDate>"_$(A)AA[aa]|13_$(A)AA[aa]|14_"</RegistrationDate>"^!,
"<DischargeDate>"_$(A)AA[aa]|17_$(A)AA[aa]|18_"</DischargeDate>"^!,
"<ReasonForVisit><![CDATA["_$(A)AA[aa]CO|1_"]]></ReasonForVisit>"^!,
"<FinancialNumber>"_$(A)AA[aa]|0_"</FinancialNumber>"^!,
"<NurseUnitName>"_&(G)GGN[$(A)AA[aa]|12]|1_"</NurseUnitName>"^!,
"<NurseUnitMnemonic>LOC."_$(A)AA[aa]|12_"</NurseUnitMnemonic>"^!,
"<MedicalService>"_&(G)GGS[$(A)AA[aa]|9]|1_"</MedicalService>"^!,
"<FinancialClass>"_&(G)GGK[$(A)AA[aa]A|3]|1_"</FinancialClass>"^!,
"<Location>"_($(A)AA[aa]|12)_" "_($(A)AA[aa]|10)_"-"_($(A)AA[aa]|11)_"</Location>"^!,
"<AdmittingPrsnlUserNumber>"_$(A)AA[aa]DR|0_"</AdmittingPrsnlUserNumber>"^!,
"<AttendingPrsnlUserNumber>"_$(A)AA[aa]DR|1_"</AttendingPrsnlUserNumber>"^!,
"<PrimaryCarePrsnlUserNumber>"_$(A)AA[aa]DR|5_"</PrimaryCarePrsnlUserNumber>"^!,
"<DynamicProperty>"^!,
"<DyPropName>MEDITECH ADT</DyPropName>"^!,
"<DyPropMeaning>MEDITECH ADT</DyPropMeaning>"^!,
"</DynamicProperty>"^!,
IF{$(A)AA[aa]O3|10 "<DynamicProperty>"^!,
"<DyPropName>OBSV</DyPropName>"^!,
"<DyPropMeaning>PTTY.OBSV</DyPropMeaning>"^!,
"<DyPropValue><![CDATA["_$(A)AA[aa]O3|10_"]]></DyPropValue>"^!,
"</DynamicProperty>"^!},
"<DynamicProperty>"^!,
"<DyPropName>"_$(A)AA[aa]|2_"</DyPropName>"^!,
"<DyPropMeaning>PTST."_$(A)AA[aa]|2_"</DyPropMeaning>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
$(A)AA[aa]|2^Q,
Q%(L(Q," "))^Q1,
"<DyPropName>"_Q1_"</DyPropName>"^!,
"<DyPropMeaning>PTTY."_Q1_"</DyPropMeaning>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
IF{Q1="IN" "Inpatient"^TYP;"Outpatient"^TYP},
"<DyPropName>"_TYP_"</DyPropName>"^!,
"<DyPropMeaning>VisitType."_TYP_"</DyPropMeaning>"^!,
"<DyPropValue><![CDATA[VisitType."_TYP_"]]></DyPropValue>"^!,
"</DynamicProperty>"^!,
"<Insurance><![CDATA["_$(A)AA[aa]IO[1]|0_"]]></Insurance>"^!,
0^insidx,
DO{insidx<3 insidx+1^insidx,
IF{insidx=1 "Primary"^adj,0^offset;
insidx=2 "Secondary"^adj,20^offset;
insidx=3 "Tertiary"^adj,40^offset},
$(A)AA[aa]IO[insidx]|0^ggm,
"<DynamicProperty>"^!,
"<DyPropName>"_adj_"Insurance.Company</DyPropName>"^!,
"<DyPropValue><![CDATA["_ggm_"]]></DyPropValue>"^!,
"<DyPropId>"_(50+offset)_"</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>"_adj_"Insurance.Street1</DyPropName>"^!,
"<DyPropValue><![CDATA["_IF{$(A)AA[aa]I[ggm]|0;&(G)GGM[ggm]|2}_"]]></DyPropValue>"^!,
"<DyPropId>"_(51+offset)_"</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>"_adj_"Insurance.Street2</DyPropName>"^!,
"<DyPropValue><![CDATA["_IF{$(A)AA[aa]I[ggm]|6;&(G)GGM[ggm]B|5}_"]]></DyPropValue>"^!,
"<DyPropId>"_(52+offset)_"</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>"_adj_"Insurance.City</DyPropName>"^!,
"<DyPropValue><![CDATA["_IF{$(A)AA[aa]I[ggm]|1;&(G)GGM[ggm]|3}_"]]></DyPropValue>"^!,
"<DyPropId>"_(53+offset)_"</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>"_adj_"Insurance.State</DyPropName>"^!,
"<DyPropValue><![CDATA["_IF{$(A)AA[aa]I[ggm]|2;&(G)GGM[ggm]|4}_"]]></DyPropValue>"^!,
"<DyPropId>"_(54+offset)_"</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
 "<DyPropName>"_adj_"Insurance.ZipCode</DyPropName>"^!,
"<DyPropValue><![CDATA["_IF{$(A)AA[aa]I[ggm]|3;&(G)GGM[ggm]|5}_"]]></DyPropValue>"^!,
"<DyPropId>"_(55+offset)_"</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>"_adj_"Insurance.Phone</DyPropName>"^!,
"<DyPropValue><![CDATA["_IF{$(A)AA[aa]I[ggm]|4;&(G)GGM[ggm]|6}_"]]></DyPropValue>"^!,
"<DyPropId>"_(56+offset)_"</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>"_adj_"Insurance.Type</DyPropName>"^!,
"<DyPropValue><![CDATA["_&(G)GGM[ggm]|9_"]]></DyPropValue>"^!,
"<DyPropId>"_(57+offset)_"</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>"_adj_"Insurance.Policy</DyPropName>"^!,
"<DyPropValue><![CDATA["_$(A)AA[aa]I[ggm]N|2_"]]></DyPropValue>"^!,
"<DyPropId>"_(58+offset)_"</DyPropId>"^!,
"</DynamicProperty>"^!,
$(A)AA[aa]I[ggm]N|10^eaSB,
*(E)ESB[eaSB]^subs,
"<DynamicProperty>"^!,
"<DyPropName>"_adj_"Insurance.GuarantorName</DyPropName>"^!,
"<DyPropValue><![CDATA["_subs|1_"]]></DyPropValue>"^!,
"<DyPropId>"_(59+offset)_"</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>"_adj_"Insurance.GroupAuthorization</DyPropName>"^!,
"<DyPropValue><![CDATA["_$(A)AA[aa]I[ggm]N|8_"]]></DyPropValue>"^!,
"<DyPropId>"_(60+offset)_"</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>"_adj_"Insurance.GuarantorRelationship</DyPropName>"^!,
"<DyPropValue><![CDATA["_$(A)AA[aa]I[ggm]N|1_"]]></DyPropValue>"^!,
"<DyPropId>"_(61+offset)_"</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>"_adj_"Insurance.GuarantorSex</DyPropName>"^!,
"<DyPropValue><![CDATA["_subs|9_"]]></DyPropValue>"^!,
"<DyPropId>"_(62+offset)_"</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>"_adj_"Insurance.GuarantorDOB</DyPropName>"^!,
"<DyPropValue><![CDATA["_subs|8_"]]></DyPropValue>"^!,
"<DyPropId>"_(63+offset)_"</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>"_adj_"Insurance.GuarantorEffectiveDate</DyPropName>"^!,
"<DyPropValue><![CDATA["_$(A)AA[aa]I[ggm]N|5_"]]></DyPropValue>"^!,
"<DyPropId>"_(64+offset)_"</DyPropId>"^!,
"</DynamicProperty>"^!},
"<DynamicProperty>"^!,
"<DyPropName>"_&(G)GGK[$(A)AA[aa]A|3]|1_"</DyPropName>"^!,
"<DyPropMeaning>FC."_$(A)AA[aa]A|3_"</DyPropMeaning>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>"_&(G)GGN[$(A)AA[aa]|12]|1_"</DyPropName>"^!,
"<DyPropMeaning>LOC."_$(A)AA[aa]|12_"</DyPropMeaning>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>"_&(G)GGS[$(A)AA[aa]|9]|1_"</DyPropName>"^!,
"<DyPropMeaning>SVC."_$(A)AA[aa]|9_"</DyPropMeaning>"^!,
"</DynamicProperty>"^!,
"<AcctPrsnlReltns>"^!,
IF{$(A)AA[aa]DR|0 "<AcctPrsnlReltn>"^!,
"<PersonnelName>"_&(G)GGG[$(A)AA[aa]DR|0]|1_"</PersonnelName>"^!,
"<PersonnelUserNumber><![CDATA["_$(A)AA[aa]DR|0_"]]></PersonnelUserNumber>"^!,
"<Display>Admitting Physician</Display>"^!,
"<AbbrevName>ADMITDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
IF{$(A)AA[aa]DR|1 "<AcctPrsnlReltn>"^!,
"<PersonnelName>"_&(G)GGG[$(A)AA[aa]DR|1]|1_"</PersonnelName>"^!,
"<PersonnelUserNumber><![CDATA["_$(A)AA[aa]DR|1_"]]></PersonnelUserNumber>"^!,
"<Display>Attending Physician</Display>"^!,
"<AbbrevName>ATTENDDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
IF{$(A)AA[aa]DR|2 "<AcctPrsnlReltn>"^!,
"<PersonnelName>"_&(G)GGG[$(A)AA[aa]DR|2]|1_"</PersonnelName>"^!,
"<PersonnelUserNumber><![CDATA["_$(A)AA[aa]DR|2_"]]></PersonnelUserNumber>"^!,
"<Display>Other Physician</Display>"^!,
"<AbbrevName>OTHERDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
IF{$(A)AA[aa]DR|3 "<AcctPrsnlReltn>"^!,
"<PersonnelName>"_&(G)GGG[$(A)AA[aa]DR|3]|1_"</PersonnelName>"^!,
"<PersonnelUserNumber><![CDATA["_$(A)AA[aa]DR|3_"]]></PersonnelUserNumber>"^!,
"<Display>Family Physician</Display>"^!,
"<AbbrevName>FAMILYDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
IF{$(A)AA[aa]DR|4 "<AcctPrsnlReltn>"^!,
"<PersonnelName>"_&(G)GGG[$(A)AA[aa]DR|4]|1_"</PersonnelName>"^!,
"<PersonnelUserNumber><![CDATA["_$(A)AA[aa]DR|4_"]]></PersonnelUserNumber>"^!,
"<Display>ER Physician</Display>"^!,
"<AbbrevName>ERDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
IF{$(A)AA[aa]DR|5 "<AcctPrsnlReltn>"^!,
"<PersonnelName>"_&(G)GGG[$(A)AA[aa]DR|5]|1_"</PersonnelName>"^!,
"<PersonnelUserNumber><![CDATA["_$(A)AA[aa]DR|5_"]]></PersonnelUserNumber>"^!,
"<Display>Primary Care Physician</Display>"^!,
"<AbbrevName>PRIMCAREDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
"DO.OTH.DOCS",@4,
"DO.OE.CONS",5,
"</AcctPrsnlReltns>"^!,
"</Account>"^!;

[4]::DO.OTH.DOCS
""^ggg,
DO{+$(A)AA[aa]CDR[ggg]^ggg "<AcctPrsnlReltn>"^!,
"<PersonnelName>"_&(G)GGG[ggg]|1_"</PersonnelName>"^!,
"<PersonnelUserNumber><![CDATA["_ggg_"]]></PersonnelUserNumber>"^!,
"<Display>Consulting Physician</Display>"^!,
"<AbbrevName>CONSULTDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!};

[5]::DO.OE.CONS
""^Q,
DO{>?(P)POE[aa,Q]^Q Q#1S^po,
IF{"CONS"=$(P)PO[po]|5 $(P)PO[po]|4^ggg,
"<AcctPrsnlReltn>"^!,
"<PersonnelName>"_&(G)GGG[ggg]|1_"</PersonnelName>"^!,
"<PersonnelUserNumber><![CDATA["_ggg_"]]></PersonnelUserNumber>"^!,
"<Display>Consulting Physician</Display>"^!,
"<AbbrevName>CONSULTDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!}};

[6]::TRLR
"</Accounts>"^!,
"</Payload>"^!,
"</ReplyMessage>"^!;
