PK.MARDATA
"HDR",@1,
"ARGS",@3,
"TRLR",@2,
END;

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

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

[3]::ARGS
1^nr,
DO{nr</ARGC 1,
   /ARGV[nr]^ARG,
   "RX",@4,
   nr+1^nr};

[4]::RX
ARG$L(ARG,":")^ORDER,
ORDER$L(ORDER,.)^tr,
ORDER%L(ORDER,.)^trsub,
ARG%L(ARG,":")^traQ,
"GET.UNITS",@5,
?(T)TRA[tr,traQ]^TRA,
$(T)TR[tr]|1^aa,
"<MedicationResult>"^!,
"<EventMnemonic>"_ARG_"</EventMnemonic>"^!,
"<Key>"_TRA|1_TRA|2_"</Key>"^!,
"<PatientNumber>"_$(A)AA[aa]|3_"</PatientNumber>"^!,
"<OrderMnemonic>"_ORDER_"</OrderMnemonic>"^!,
"<AdministrationDateTime>"_TRA|1_TRA|2_"</AdministrationDateTime>"^!,
IF{TRA|14="Y" "check not given",
   TRA|17_" "_units^dose,
   0^statusflags;
   "NOT GIVEN"^dose,
   4^statusflags},
"<StatusFlags>"_statusflags_"</StatusFlags>"^!,
"<AdministrationDoseAndUnit>"_dose_"</AdministrationDoseAndUnit>"^!,
"<AdministrationNote><![CDATA["^!,
DO{+?(T)TRA[tr,traQ]RC[tdrc]^tdrc &(T)TDRC[tdrc]|1_D(10)^!},
DO{+(?(T)TRA[tr,traQ]T[t],TXT)^t TXT_D(10)^!},
"]]></AdministrationNote>"^!,
TRA|3^USERNAME,
"<AdministrationProvider>"_USERNAME_"</AdministrationProvider>"^!,
"</MedicationResult>"^!;

[5]::GET.UNITS
"need a comment here because we cant assign a ptr in the first statement",
^$(T)TR[tr]^PHA.MAIN,
&(T)TDO[[PHA.MAIN]|7]|2^TYPE,
IF{TYPE="MED" "SINGLE.MED",@6;
   TYPE="COMP" "COMPOUND.MEDS",@7;
   TYPE="SPLIT" "SPLIT.MEDS",@8;
   TYPE="IV"!(TYPE="IVPB") "IV.MEDS",@9;
   TYPE="ALT" "ALT.IV.MEDS",@10},
IF{tf &(T1)TF[tf]|5^units};

[6]::SINGLE.MED
[PHA.MAIN,"M"]|0^tf;

[7]::COMPOUND.MEDS
[PHA.MAIN,"IN",trsub]|0^tf;

[8]::SPLIT.MEDS
[PHA.MAIN,"MD",trsub]|0^tf;

[9]::IV.MEDS
""^tf,
DO{(tf_.=.)&(+([PHA.MAIN,"AD",TRA],XX)^TRA) "got additive",
   XX|0^tf},
DO{(tf_.=.)&(+([PHA.MAIN,"IC",TRC],XX)^TRC) "got carrier",
   XX|0^tf};

[10]::ALT.IV.MEDS
""^tf,
DO{(tf_.=.)&(+([PHA.MAIN,"BT",trsub,"AD",TRAA],XX)^TRAA) "got additive",
   XX|0^tf},
DO{(tf_.=.)&(+([PHA.MAIN,"BT",trsub,"IC",TRAC],XX)^TRAC) "got carrier",
   XX|0^tf};
