PK.PATKEY
@PATKEY.OPEN.PREFIXES,
@PATKEY.PARSEOPTIONAL,
@PATKEY.HDR,
@PATKEY.ACCT,
@PATKEY.PATS,
@PATKEY.OPT.PATS,
@PATKEY.TRLR,
END;

PATKEY.OPEN.PREFIXES
@admdir,
O(*,@admdata),
@pkdir

PATKEY.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>"^!

PATKEY.PARSEOPTIONAL
1^nr,
0^NUMLOCS,
IF{/CONF[.patient.locationfiltering] @PATKEY.LOADLOCATIONS}

PATKEY.LOADLOCATIONS
K(/LOCS),
"Copy locations into structure",
/ARGV[nr]^NUMLOCS,
nr+1^nr,
0^i,
DO{i<NUMLOCS 1,
   /ARGV[nr+i]^loc,
   1^/LOCS[loc%3],
   loc%3^/LOCLIST[i],
   i+1^i},
nr+i^nr

PATKEY.ACCT
""^Q,
"<HEL>"^!,
DO{>(*AARB[Q],QQ)^Q 1,
   IF{(*AAAI[*AA[QQ]|0]=QQ)&(NUMLOCS=0!/LOCS[*AA[QQ]|12]) 1,
      *AA[QQ]|0^Q1,
      *AA[QQ]|3^Q3,
      IF{Q3_.'=. 1,
         *AA[QQ]B|0^Q2,
         <*AA[QQ]EV[""]^/tmp,
         Q2_(/tmp#0S)_(/tmp#1S)^Q2,
         *AA[QQ]|2^STATTYPE,
         STATTYPE%(L(STATTYPE," "))^TYPE,
         IF{/CONF[.patient.outpat.exp]&(TYPE'="IN") 1,
            Q3_/CONF[.patient.outpat.exp]_Q1^Q3},
         ""^ggg,
         DO{+*AA[QQ]CDR[ggg]^ggg 1,
            Q2_ggg^Q2},
         "<Encounter>"^!,
         "<VisitNumber>"_Q1_"</VisitNumber>"^!,
         "<PatientNumber>"_Q3_"</PatientNumber>"^!,
         "<Key>"_Q2_"</Key>"^!,
         "</Encounter>"^!}}},
"</HEL>"^!

PATKEY.PATS
""^Q,
"<HPL>"^!,
DO{>(*AARB[Q],QQ)^Q 1,
   IF{(*AAAI[*AA[QQ]|0]=QQ)&(NUMLOCS=0!/LOCS[*AA[QQ]|12]) 1,
      *AA[QQ]|0^Q1,
      *AA[QQ]|3^Q3,
      IF{Q3_.'=. 1,
         *AA[QQ]B|0^Q2,
         <*AA[QQ]EV[""]^/tmp,
         Q2_(/tmp#0S)_(/tmp#1S)^Q2,
         *AA[QQ]|2^STATTYPE,
         STATTYPE%(L(STATTYPE," "))^TYPE,
         IF{/CONF[.patient.outpat.exp]&(TYPE'="IN") 1,
            Q3_/CONF[.patient.outpat.exp]_Q1^Q3},
         ""^ggg,
         DO{+*AA[QQ]CDR[ggg]^ggg Q2_ggg^Q2},
         "<Person>"^!,
         "<VisitNumber>"_Q1_"</VisitNumber>"^!,
         "<PatientNumber>"_Q3_"</PatientNumber>"^!,
         "<Key>"_Q2_"</Key>"^!,
         "</Person>"^!}}},
"</HPL>"^!

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

PATKEY.OPT.BYTYPE
BEGIN.END.DT$L(BEGIN.END.DT,"-")^BDT,
BEGIN.END.DT%L(BEGIN.END.DT,"-")^EDT,
IF{TYP="DIS" @PATKEY.DIS.PATS;
   TYP="MSM" IF{/CONF[.usemsm]&(NUMLOCS=0!/LOCS["OR"]) @PATKEY.MSM};
   TYP="SCHEDULING" IF{/CONF[.usesch] @PATKEY.SCHEDULING.PATS};
   @PATKEY.OUT.PATS,@PATKEY.SCH.PATS}

PATKEY.DIS.PATS
""^FAC,
"<HPL>"^!,
DO{+*AADI[FAC]^FAC 1,
   BDT^DT,
   DO{>*AADI[FAC,DT]^DT&(DT#0S'>EDT) 1,
      DT#2S^aa,
      IF{(*AAAI[*AA[aa]|0]=aa)&(NUMLOCS=0!/LOCS[*AA[aa]|12]) 1,
         @PATKEY.HPL.ENTRY}}},
"</HPL>"^!,
""^FAC,
"<HEL>"^!,
DO{+*AADI[FAC]^FAC 1,
   BDT^DT,
   DO{>*AADI[FAC,DT]^DT&(DT#0S'>EDT) 1,
      DT#2S^aa,
      IF{(*AAAI[*AA[aa]|0]=aa)&(NUMLOCS=0!/LOCS[*AA[aa]|12]) 1,
         @PATKEY.HEL.ENTRY}}},
"</HEL>"^!

PATKEY.OUT.PATS
"<HPL>"^!,
""^FAC,
DO{+*AAEI[FAC]^FAC 1,
   BDT^DT,
   DO{>*AAEI[FAC,TYP,DT]^DT&(DT#0S'>EDT) 1,
      DT#2S^aa,
      IF{(*AAAI[*AA[aa]|0]=aa)&(NUMLOCS=0!/LOCS[*AA[aa]|12]) 1,
         @PATKEY.HPL.ENTRY}}},
"</HPL>"^!,
"<HEL>"^!,
""^FAC,
DO{+*AAEI[FAC]^FAC 1,
   BDT^DT,
   DO{>*AAEI[FAC,TYP,DT]^DT&(DT#0S'>EDT) 1,
      DT#2S^aa,
      IF{(*AAAI[*AA[aa]|0]=aa)&(NUMLOCS=0!/LOCS[*AA[aa]|12]) 1,
         @PATKEY.HEL.ENTRY}}},
"</HEL>"^!

PATKEY.SCH.PATS
"<HPL>"^!,
""^FAC,
DO{+*AASI[FAC]^FAC 1,
   BDT^DT,
   DO{>*AASI[FAC,TYP,DT]^DT&(DT#0S'>EDT) 1,
      DT#1S^aa,
      IF{(*AAAI[*AA[aa]|0]=aa)&(*AA[aa]|3&(NUMLOCS=0!/LOCS[*AA[aa]|12])) 1,
         @PATKEY.HPL.ENTRY}}},
"</HPL>"^!,
"<HEL>"^!,
""^FAC,
DO{+*AASI[FAC]^FAC 1,
   BDT^DT,
   DO{>*AASI[FAC,TYP,DT]^DT&(DT#0S'>EDT) 1,
      DT#1S^aa,
      IF{(*AAAI[*AA[aa]|0]=aa)&(*AA[aa]|3&(NUMLOCS=0!/LOCS[*AA[aa]|12])) 1,
         @PATKEY.HEL.ENTRY}}},
"</HEL>"^!

PATKEY.SCHEDULING.PATS
@schdir,
O(:,@schdata),
O(?,@schdict),
@pkdir,
"<HPL>"^!,
0^i,
DO{i<NUMLOCS 1,
   /LOCLIST[i]^ggn,ggn^lcur,
   DO{>?WACD[lcur]^lcur&(lcur#0S=ggn) 1,
      lcur#1S^wac,
      wac^gcur,
      DO{>?WATG[gcur]^gcur&(gcur#0S=wac) 1,
         gcur#1S^wat,
         BDT^DT,
         DO{>:WZT[wat,DT]^DT&(DT#0S'>EDT) 1,
            DT#3S^wz,
            :WZ[wz]#7S^aa,
            IF{/CONF[.patient.outpat.exp] 1,
               (/CONF[.patient.outpat.exp])_(DT#0S)_(DT#1S)^AAPP},
            IF{(':WZ[wz]|8)&(*AA[aa]|3)&(*AAAI[*AA[aa]|0]=aa) 1,
               @PATKEY.HPL.ENTRY}}}},i+1^i},
"</HPL>"^!,
"<HEL>"^!,
0^i,
DO{i<NUMLOCS 1,
   /LOCLIST[i]^ggn,
   ggn^lcur,
   DO{>?WACD[lcur]^lcur&(lcur#0S=ggn) 1,
      lcur#1S^wac,
      wac^gcur,
      DO{>?WATG[gcur]^gcur&(gcur#0S=wac) 1,
         gcur#1S^wat,
         BDT^DT,
         DO{>:WZT[wat,DT]^DT&(DT#0S'>EDT) 1,
            DT#3S^wz,
            :WZ[wz]#7S^aa,
            IF{/CONF[.patient.outpat.exp] 1,
               (/CONF[.patient.outpat.exp])_(DT#0S)_(DT#1S)^AAPP},
            IF{(':WZ[wz]|8)&(*AA[aa]|3)&(*AAAI[*AA[aa]|0]=aa) 1,
               @PATKEY.HEL.ENTRY}}}},i+1^i},
"</HEL>"^!,
""^AAPP,
C(:),
C(?)

PATKEY.HPL.ENTRY
(*AA[aa]|0)_AAPP^Q1,
*AA[aa]|3^Q3,
IF{Q3_.=.;
   *AA[aa]B|0^Q2,
   <*AA[aa]EV[""]^/tmp,
   Q2_(/tmp#0S)_(/tmp#1S)^Q2,
   *AA[aa]|2^STATTYPE,
   STATTYPE%(L(STATTYPE," "))^TYPE,
   ""^ggg,
   DO{+*AA[aa]CDR[ggg]^ggg 1,
      Q2_ggg^Q2},
   IF{/CONF[.patient.outpat.exp]&(AAPP!(TYPE'="IN")) 1,
      Q3_/CONF[.patient.outpat.exp]_Q1^Q3},
   "<Person>"^!,
   "<VisitNumber>"_Q1_"</VisitNumber>"^!,
   "<PatientNumber>"_Q3_"</PatientNumber>"^!,
   "<Key>"_Q2_"</Key>"^!,
   "</Person>"^!}

PATKEY.HEL.ENTRY
(*AA[aa]|0)_AAPP^Q1,
*AA[aa]|3^Q3,
IF{Q3_.=.;
   *AA[aa]B|0^Q2,
   <*AA[aa]EV[""]^/tmp,
   Q2_(/tmp#0S)_(/tmp#1S)^Q2,
   *AA[aa]|2^STATTYPE,
   STATTYPE%(L(STATTYP," "))^TYPE,
   ""^ggg,
   DO{+*AA[aa]CDR[ggg]^ggg Q2_ggg^Q2},
   IF{/CONF[.patient.outpat.exp]&(AAPP!(TYPE'="IN")) 1,
      Q3_/CONF[.patient.outpat.exp]_Q1^Q3},
      "<Encounter>"^!,
      "<VisitNumber>"_Q1_"</VisitNumber>"^!,
      "<PatientNumber>"_Q3_"</PatientNumber>"^!,
      "<Key>"_Q2_"</Key>"^!,
      "</Encounter>"^!}

PATKEY.TRLR
"</PatientManagerList>"^!,
"</Payload>"^!,
"</ReplyMessage>"^!

PATKEY.MSM
@msmdir,
O(&S,@msmdata),
@pkdir,
"<HPL>"^!,
%Z.date.clinical(BDT)-1^DT^\DT,
%Z.date.clinical(EDT)^EDT^\EDT,
DO{+&SCHDI[DT]^DT'>EDT 1,
   ""^sch,
   DO{+&SCHDI[DT,sch]^sch 1,
   IF{'&SCH[sch,"CAN"]&&SCH[sch,"PB","unum"] 1,
      @PATKEY.MSM.HPL.ENTRY}}},
"</HPL>"^!,
""^FAC,
"<HEL>"^!,
%Z.date.clinical(BDT)-1^DT,
%Z.date.clinical(EDT)^EDT,
DO{+&SCHDI[DT]^DT'>EDT 1,
   ""^sch,
   DO{+&SCHDI[DT,sch]^sch 1,
   IF{'&SCH[sch,"CAN"]&&SCH[sch,"PB","unum"] 1,
      @PATKEY.MSM.HEL.ENTRY}}},
"</HEL>"^!,
C(&U)

PATKEY.MSM.HPL.ENTRY
"MSM"_sch^Q1,
&SCH[sch,"PB","unum"]^Q3,
&SCH[sch]D|0_"_"_-&SCH[sch]AT[""]^Q2,
IF{/CONF[.patient.outpat.exp] 1,
   Q3_/CONF[.patient.outpat.exp]_Q1^Q3},
"<Person>"^!,
"<VisitNumber>"_Q1_"</VisitNumber>"^!,
"<PatientNumber>"_Q3_"</PatientNumber>"^!,
"<Key>"_Q2_"</Key>"^!,
"</Person>"^!

PATKEY.MSM.HEL.ENTRY
"MSM"_sch^Q1,
&SCH[sch,"PB","unum"]^Q3,
&SCH[sch]D|0_"_"_-&SCH[sch]AT[""]^Q2,
IF{/CONF[.patient.outpat.exp] 1,
   Q3_/CONF[.patient.outpat.exp]_Q1^Q3},
"<Encounter>"^!,
"<VisitNumber>"_Q1_"</VisitNumber>"^!,
"<PatientNumber>"_Q3_"</PatientNumber>"^!,
"<Key>"_Q2_"</Key>"^!,
"</Encounter>"^!
