PK.PATDOC,
"HDR",@1,
"ACCT",@2,
"OPT.PATS",@4,
"TRLR",@11,
END;

[1]::HDR
"<?xml version="_D(34)_1.0_D(34)_" ?>"^!,
"<ReplyMessage>"^!,
"<TransactionId>0</TransactionId>"^!,
"<Status>Success</Status>"^!,
"<StatusDescription>Step returned a Success status.</StatusDescription>"^!,
"<Payload>"^!,
"<PatientManagerList>"^!;

[2]::ACCT
""^Q,
DO{>(?(A)AARB[Q],aa)^Q $(A)AA[aa]^/aa,/aa|0^Q1,
/aa|3^Q3,
IF{Q1="NEW";Q3_.=.;
$(A)AA[aa]B|0^Q2,
<$(A)AA[aa]EV[""]^/tmp,
Q2_(/tmp#0S)_(/tmp#1S)^Q2,
/aa^ZQ,
$(A)AA[aa]DR^DQ,
""^ggg,
DO{+$(A)AA[aa]CDR[ggg]^ggg Q2_ggg^Q2},
"<HUPL>"^!,
"<Person>"^!,
"<VisitNumber>"_Q1_"</VisitNumber>"^!,
"<PatientNumber>"_Q3_"</PatientNumber>"^!,
"<Key>"_Q2_"</Key>"^!,
"</Person>"^!,
"</HUPL>"^!,
"<HEL>"^!,
"<Encounter>"^!,
"<VisitNumber>"_Q1_"</VisitNumber>"^!,
"<PatientNumber>"_Q3_"</PatientNumber>"^!,
"<Key>"_Q2_"</Key>"^!,
"<NurseUnitMnemonic>LOC."_ZQ|12_"</NurseUnitMnemonic>"^!,
"<DynamicProperty>"^!,
"<DyPropMeaning>SVC."_ZQ|9_"</DyPropMeaning>"^!,
"</DynamicProperty>"^!,
"<AcctPrsnlReltns>"^!,
IF{DQ|0 "<AcctPrsnlReltn>"^!,
"<PersonnelUserNumber><![CDATA["_DQ|0_"]]></PersonnelUserNumber>"^!,
"<AbbrevName>ADMITDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
IF{DQ|1 "<AcctPrsnlReltn>"^!,
"<PersonnelUserNumber><![CDATA["_DQ|1_"]]></PersonnelUserNumber>"^!,
"<AbbrevName>ATTENDDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
IF{DQ|2 "<AcctPrsnlReltn>"^!,
"<PersonnelUserNumber><![CDATA["_DQ|2_"]]></PersonnelUserNumber>"^!,
"<AbbrevName>OTHERDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
IF{DQ|3 "<AcctPrsnlReltn>"^!,
"<PersonnelUserNumber><![CDATA["_DQ|3_"]]></PersonnelUserNumber>"^!,
"<AbbrevName>FAMILYDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
IF{DQ|4 "<AcctPrsnlReltn>"^!,
"<PersonnelUserNumber><![CDATA["_DQ|4_"]]></PersonnelUserNumber>"^!,
"<AbbrevName>ERDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
IF{DQ|5 "<AcctPrsnlReltn>"^!,
"<PersonnelUserNumber><![CDATA["_DQ|5_"]]></PersonnelUserNumber>"^!,
"<AbbrevName>PRIMCAREDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
"DO.OTH.DOCS",@10,
"</AcctPrsnlReltns>"^!,
"</Encounter>"^!,
"</HEL>"^!}};

[3]::PATS
"placeholder"^!;

[4]::OPT.PATS
1^nr,
DO{nr+1</ARGC 1,
   /ARGV[nr]^TYP,
   /ARGV[nr+1]^BEGIN.END.DT,
   @5,"OPT.BYTYPE",
   nr+2^nr};

[5]::OPT.BYTYPE
BEGIN.END.DT$L(BEGIN.END.DT,"-")^BDT,
BEGIN.END.DT%L(BEGIN.END.DT,"-")^EDT,
IF{TYP="DIS" @6,"DIS.PATS";@7,"OUT.PATS"};

[6]::DIS.PATS
""^FAC,
DO{+?(A)AADI[FAC]^FAC BDT^DT,
DO{>?(A)AADI[FAC,DT]^DT&(DT#0S'>EDT) DT#2S^aa,
$(A)AA[aa]^/aa,
/aa|0^Q1,
/aa|3^Q3,
IF{Q1="NEW";Q3_.=.;
$(A)AA[aa]B|0^Q2,
<$(A)AA[aa]EV[""]^/tmp,
Q2_(/tmp#0S)_(/tmp#1S)^Q2,
/aa^ZQ,
$(A)AA[aa]DR^DQ,
"<HEL>"^!,
@9,"HEL.ENTRY",
"</HEL>"^!,
"<HUPL>"^!,
@8,"HUPL.ENTRY",
"</HUPL>"^!}}};

[7]::OUT.PATS
""^FAC,
DO{+?(A)AAEI[FAC]^FAC BDT^DT,
DO{>?(A)AAEI[FAC,TYP,DT]^DT&(DT#0S'>EDT) DT#2S^aa,
$(A)AA[aa]^/aa,
/aa|0^Q1,
/aa|3^Q3,
IF{Q1="NEW";Q3_.=.;
$(A)AA[aa]B|0^Q2,
<$(A)AA[aa]EV[""]^/tmp,
Q2_(/tmp#0S)_(/tmp#1S)^Q2,
/aa^ZQ,
$(A)AA[aa]DR^DQ,
"<HEL>"^!,
@9,"HEL.ENTRY",
"</HEL>"^!,
"<HUPL>"^!,
@8,"HUPL.ENTRY",
"</HUPL>"^!}}};

[8]::HUPL.ENTRY
"<Person>"^!,
"<VisitNumber>"_Q1_"</VisitNumber>"^!,
"<PatientNumber>"_Q3_"</PatientNumber>"^!,
"<Key>"_Q2_"</Key>"^!,
"</Person>"^!;

[9]::HEL.ENTRY
"<Encounter>"^!,
"<VisitNumber>"_Q1_"</VisitNumber>"^!,
"<PatientNumber>"_Q3_"</PatientNumber>"^!,
"<DischargeDate>"_ZQ|17_ZQ|18_"</DischargeDate>"^!,
"<NurseUnitMnemonic>LOC."_ZQ|12_"</NurseUnitMnemonic>"^!,
"<DynamicProperty>"^!,
"<DyPropMeaning>SVC."_ZQ|9_"</DyPropMeaning>"^!,
"</DynamicProperty>"^!,
"<AcctPrsnlReltns>"^!,
IF{DQ|0 "<AcctPrsnlReltn>"^!,
"<PersonnelUserNumber><![CDATA["_DQ|0_"]]></PersonnelUserNumber>"^!,
"<AbbrevName>ADMITDOC</AbbrevName>"^!,
IF{DQ|1 "</AcctPrsnlReltn>"^!},
"<AcctPrsnlReltn>"^!,
"<PersonnelUserNumber><![CDATA["_DQ|1_"]]></PersonnelUserNumber>"^!,
"<AbbrevName>ATTENDDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
IF{DQ|2 "<AcctPrsnlReltn>"^!,
"<PersonnelUserNumber><![CDATA["_DQ|2_"]]></PersonnelUserNumber>"^!,
"<AbbrevName>OTHERDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
IF{DQ|3 "<AcctPrsnlReltn>"^!,
"<PersonnelUserNumber><![CDATA["_DQ|3_"]]></PersonnelUserNumber>"^!,
"<AbbrevName>FAMILYDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
IF{DQ|4 "<AcctPrsnlReltn>"^!,
"<PersonnelUserNumber><![CDATA["_DQ|4_"]]></PersonnelUserNumber>"^!,
"<AbbrevName>ERDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
IF{DQ|5 "<AcctPrsnlReltn>"^!,
"<PersonnelUserNumber><![CDATA["_DQ|5_"]]></PersonnelUserNumber>"^!,
"<AbbrevName>PRIMCAREDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!},
"DO.OTH.DOCS",@10,
"</AcctPrsnlReltns>"^!,
"<Key>"_Q2_"</Key>"^!,
"</Encounter>"^!;

[10]::DO.OTH.DOCS
""^ggg,
DO{+$(A)AA[aa]CDR[ggg]^ggg Q2_ggg^Q2,
"<AcctPrsnlReltn>"^!,
"<PersonnelUserNumber><![CDATA["_ggg_"]]></PersonnelUserNumber>"^!,
"<AbbrevName>CONSULTDOC</AbbrevName>"^!,
"</AcctPrsnlReltn>"^!};

[11]::TRLR
"</PatientManagerList>"^!,
"</Payload>"^!,
"</ReplyMessage>"^!;