PK.MARKEY
@OPEN.MED,
@HDR,
@PATS,
@TRLR,
END;

OPEN.MED
@phadir,
O(*,@phadata),
O(\,@phadict),
@pkdir

HDR
"<?xml version="_D(34)_1.0_D(34)_" ?>"^!,
"<ReplyMessage>"^!,
"<TransactionId>"_1234_"</TransactionId>"^!,
"<Status>Success</Status>"^!,
"<Payload>"^!

PATS
/ARGV[1]^MEDODATE,
/ARGV[2]^MARDATE,
3^nr,
DO{nr</ARGC 1,
   /ARGV[nr]^ACT,
   *AAAI[ACT]^aa,
   @PHA,
   nr+1^nr}

PHA
""^Q,
DO{@NEXT.RX @MARS}

NEXT.RX
>(*TRP[aa,Q],tr)^Q

MARS
IF{"if discontinued, check rx end date against date from pk",
   ^*TR[tr]^PHA.MAIN,
   [PHA.MAIN]|8^stat,
   stat="AC" @MARS2;
   IF{stat="DC" [PHA.MAIN]|5;
      stat="CAN" [PHA.MAIN,2]|3}^other.dt,
   [PHA.MAIN]|16^STOP.DT,
   IF{STOP.DT_.=. other.dt;
      other.dt&(other.dt<STOP.DT) other.dt;
      STOP.DT}^STOP.DT,
   (STOP.DT_.'=.)&(STOP.DT<MEDODATE);
   @MARS2}

MARS2
""^traQ,
DO{@NEXT.MAR&('DONE) @SEND}

NEXT.MAR
-(*TRA[tr,traQ],TRA)^traQ,
IF{*TRA[tr,traQ]|1<MARDATE "";traQ}

SEND
\TDO[*TR[tr]|7]|2^TYPE,
IF{TYPE="COMP" DO{+*TR[tr]IN[x]^x tr_"."_x^mnemonic,@SEND.MAR};
   TYPE="SPLIT" DO{+*TR[tr]MD[x]^x tr_"."_x^mnemonic,@SEND.MAR};
   TYPE="ALT" DO{+*TR[tr]BT[x]^x IF{TRA|7'=x;tr_"."_x^mnemonic,@SEND.MAR}};
   tr^mnemonic,@SEND.MAR}

SEND.MAR
"<MedicationResult>"^!,
"<EventMnemonic>"_mnemonic_":"_traQ_"</EventMnemonic>"^!,
"<Key>"_TRA|1_TRA|2_"</Key>"^!,
"<PatientNumber>"_*AA[aa]|3_"</PatientNumber>"^!,
"<OrderMnemonic>"_mnemonic_"</OrderMnemonic>"^!,
"</MedicationResult>"^!

TRLR
"</Payload>"^!,
"</ReplyMessage>"^!
