PK.PATDEM
@PATDEM.OPEN.PREFIXES,
@PATDEM.HDR,
@PATDEM.PATS,
@PATDEM.TRLR,
END;

PATDEM.OPEN.PREFIXES
@admdir,
O(*,@admdata),
IF{/CONF[.usemsm] @msmdir,
   O(:,@msmdata)},
@pkdir

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

PATDEM.PATS
0^nr,
DO{nr+1^nr</ARGC 1,
   /ARGV[nr]^ACT,
   @PATDEM.SWITCH}

PATDEM.SWITCH
IF{ACT$3="MSM" 1,
   ACT^sch,
   @PATDEM.MSM.PAT;
   ""^aa,
   IF{/CONF[.patient.outpat.exp] 1,
      L(ACT,/CONF[.patient.outpat.exp])^POS,
      IF{POS'=L(ACT) 1,
         *AAAI[ACT$POS]^aa,
         ACT%(POS+L(/CONF[.patient.outpat.exp])-1)^appt}},
   IF{'aa 1,
      *AAAI[ACT]^aa,
      ""^appt},
   IF{aa 1,
      @PATDEM.PAT}}

PATDEM.PAT
IF{appt 1,
   *AA[aa]|0_/CONF[.patient.outpat.exp]_appt^Q1;
   *AA[aa]|0^Q1},
*AA[aa]|3^Q3,
*AA[aa]|2^STATTYPE,
STATTYPE%(L(STATTYPE," "))^TYPE,
IF{/CONF[.patient.outpat.exp]&(appt!(TYPE'="IN")) 1,
   Q3_/CONF[.patient.outpat.exp]_Q1^Q3},
"<Person>"^!,
"<PatientNumber>"_Q3_"</PatientNumber>"^!,
"<VisitNumber>"_Q1_"</VisitNumber>"^!,
*AA[aa]B|0^Q2,
<*AA[aa]EV[""]^/tmp,
Q2_(/tmp#0S)_(/tmp#1S)^Q2,
""^ggg,
DO{+*AA[aa]CDR[ggg]^ggg 1,
   Q2_ggg^Q2},
"<Key>"_Q2_"</Key>"^!,
"<BirthDate>"_*AA[aa]|5_"</BirthDate>"^!,
"<Gender>"_*AA[aa]|7_"</Gender>"^!,
"<Race>"_*AA[aa]A|5_"</Race>"^!,
*AA[aa]|1^NM,
NM$(L(NM,","))^LNM,
NM%(L(NM,","))^NM,
NM$(L(NM," "))^FNM,
NM%(L(NM," "))^MNM,
"<FirstName>"_FNM_"</FirstName>"^!,
"<MiddleName>"_MNM_"</MiddleName>"^!,
"<LastName>"_LNM_"</LastName>"^!,
"<SocialSecurityNumber>"_*AA[aa]A|2_"</SocialSecurityNumber>"^!,
"<MedicalRecordNumber>"_Q3_"</MedicalRecordNumber>"^!,
"<MothersMaidenName></MothersMaidenName>"^!,
"<DynamicProperty>"^!,
"<DyPropName>Address</DyPropName>"^!,
"<DyPropValue><![CDATA["_*AA[aa]AD|0_"]]></DyPropValue>"^!,
"<DyPropMeaning>Street1</DyPropMeaning>"^!,
"<DyPropId>41</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>Address</DyPropName>"^!,
"<DyPropValue><![CDATA["_*AA[aa]AD|1_"]]></DyPropValue>"^!,
"<DyPropMeaning>Street2</DyPropMeaning>"^!,
"<DyPropId>42</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>Address</DyPropName>"^!,
"<DyPropValue><![CDATA["_*AA[aa]AD|2_"]]></DyPropValue>"^!,
"<DyPropMeaning>City</DyPropMeaning>"^!,
"<DyPropId>43</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>Address</DyPropName>"^!,
"<DyPropValue><![CDATA["_*AA[aa]AD|3_"]]></DyPropValue>"^!,
"<DyPropMeaning>State</DyPropMeaning>"^!,
"<DyPropId>44</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>Address</DyPropName>"^!,
"<DyPropValue><![CDATA["_*AA[aa]AD|4_"]]></DyPropValue>"^!,
"<DyPropMeaning>ZipCode</DyPropMeaning>"^!,
"<DyPropId>45</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>Home Phone</DyPropName>"^!,
"<DyPropValue><![CDATA["_*AA[aa]AD|5_"]]></DyPropValue>"^!,
"<DyPropMeaning>HomePhone</DyPropMeaning>"^!,
"<DyPropId>46</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>Work Phone</DyPropName>"^!,
"<DyPropValue><![CDATA["_*AA[aa]E|7_"]]></DyPropValue>"^!,
"<DyPropMeaning>WorkPhone</DyPropMeaning>"^!,
"<DyPropId>47</DyPropId>"^!,
"</DynamicProperty>"^!,
"<DynamicProperty>"^!,
"<DyPropName>Other Phone</DyPropName>"^!,
"<DyPropValue><![CDATA["_*AA[aa]AD|8_"]]></DyPropValue>"^!,
"<DyPropMeaning>OtherPhone</DyPropMeaning>"^!,
"<DyPropId>48</DyPropId>"^!,
"</DynamicProperty>"^!,
"</Person>"^!

PATDEM.TRLR
"</Persons>"^!,
"</Payload>"^!,
"</ReplyMessage>"^!

PATDEM.MSM.PAT
sch%2^sch,
:SCH[sch,"PB","unum"]^Q3,
"MSM"_sch^Q1,
IF{/CONF[.patient.outpat.exp] 1,
   Q3_/CONF[.patient.outpat.exp]_Q1^Q3},
"<Person>"^!,
"<PatientNumber>"_Q3_"</PatientNumber>"^!,
"<VisitNumber>"_Q1_"</VisitNumber>"^!,
:SCH[sch]D|0_"_"_-:SCH[sch]AT[""]^Q2,
""^ggg^Q,
DO{+(:SCH[sch]DR[Q],ggg)^Q 1,
   ggg|0^ggg,
   Q2_ggg^Q2},
"<Key>"_Q2_"</Key>"^!,
:SCH[sch,"PB","pre.dob"]^dt,
"<BirthDate>"_(dt#"2/")_(dt#"0/")_(dt#"1/")_"</BirthDate>"^!,
"<Gender>"_:SCH[sch,"PB","pre.sex"]_"</Gender>"^!,
:SCH[sch,"PB","nm"]^NM,
NM$(L(NM,","))^LNM,
NM%(L(NM,","))^NM,
NM$(L(NM," "))^FNM,
NM%(L(NM," "))^MNM,
"<FirstName>"_FNM_"</FirstName>"^!,
"<MiddleName>"_MNM_"</MiddleName>"^!,
"<LastName>"_LNM_"</LastName>"^!,
"<MedicalRecordNumber>"_Q3_"</MedicalRecordNumber>"^!,
"<DynamicProperty>"^!,
"<DyPropName>Home Phone</DyPropName>"^!,
"<DyPropValue><![CDATA["_:SCH[sch,"PB","HOMEPHONE"]_"]]></DyPropValue>"^!,
"<DyPropMeaning>HomePhone</DyPropMeaning>"^!,
"<DyPropId>46</DyPropId>"^!,
"</DynamicProperty>"^!,
"</Person>"^!
