PK.MEDODATA
@OPEN.MED,
@HDR,
@ARGS,
@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>"^!

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

ARGS
1^nr,
DO{nr</ARGC 1,
   /ARGV[nr]^ARG,
   @RX,
   nr+1^nr}

RX
K(/T.MED),
ARG$L(ARG,.)^tr,
ARG%L(ARG,.)^trsub,
"Check that RX Exists",
IF{*TR[tr]_.=. "<Error>MED "_tr_" does not exist</Error>"^!;
   ^*TR[tr]^PHA.MAIN,
   [PHA.MAIN]|1^aa,
   "Look to ADM for Pat Info",
   *AA[aa]|0^/T.MED["ACCT"],
   *AA[aa]|3^/T.MED["MRN"],
   "Get rest of data from PHA",
   [PHA.MAIN]|18^/T.MED["PROV"],
   [PHA.MAIN]|14_[PHA.MAIN]|15^/T.MED["START.DT"],
   [PHA.MAIN]|16_[PHA.MAIN]|17^stop.dt,
   [PHA.MAIN]|8^stat,
   IF{stat="DC" [PHA.MAIN]|5_[PHA.MAIN]|6;
      stat="CAN" [PHA.MAIN,2]|3_[PHA.MAIN,2]|4}^other.dt,
   IF{stop.dt_.=. other.dt;
      other.dt&(other.dt<stop.dt) other.dt;
      stop.dt}^/T.MED["STOP.DT"],
   IF{(stat'="AC")&(stat'="UNV") "DC";stat}^/T.MED["ORDER.STAT"],
   [PHA.MAIN,2]|0_[PHA.MAIN,2]|1_/T.MED["START.DT"]_/T.MED["STOP.DT"]^/T.MED["KEY"],
   IF{/T.MED["ORDER.STAT"]="AC" "false";"true"}^/T.MED["DI"],
   [PHA.MAIN]|10^/T.MED["SCH"],
   ;"when sch is 'ONE', .STK-MED appears in sig, but it also shows in MT UI",
   ;IF{/T.MED["SCH"]="ONE" "ONCE";[PHA.MAIN]|9}^/T.MED["FREQ"],
   [PHA.MAIN]|9^/T.MED["FREQ"],   
   IF{(/T.MED["SCH"]="SCH")&(L(/T.MED["FREQ"],"PRN")<L(/T.MED["FREQ"])) "really a PRN",
      "PRN"^/T.MED["SCH"]},
   IF{/T.MED["SCH"]="PRN" "true";"false"}^/T.MED["PRN"],
   [PHA.MAIN]|11^/T.MED["ROUTE"],
   0^cmtln^tqln,
   IF{[PHA.MAIN,"RC",1]_.=.;
      "RX COMMENTS: "^/T.MED["COMMENTS",cmtln+1^cmtln],
      DO{+([PHA.MAIN,"RC",TRC],XX)^TRC XX^/T.MED["COMMENTS",cmtln+1^cmtln]}},
   IF{[PHA.MAIN,"CM",1]_.=.;
      "LABEL COMMENTS: "^/T.MED["COMMENTS",cmtln+1^cmtln],
      DO{+([PHA.MAIN,"CM",TCM],XX)^TCM XX^/T.MED["COMMENTS",cmtln+1^cmtln]}},
   \TDO[[PHA.MAIN]|7]|2^TYPE,
   IF{TYPE="MED" @SINGLE.MED;
      TYPE="COMP" @COMPOUND.MEDS;
      TYPE="SPLIT" @SPLIT.MEDS;
      TYPE="IV"!(T="IVPB") @IV.MEDS;
      TYPE="ALT" @ALT.IV.MEDS},
   @RATE.DURATION,
   IF{TYPE="ALT" 4^/T.MED["ORDER.TYPE"],
         DO{+([PHA.MAIN,"DI",TRD],XX)^TRD XX^/T.MED["TQ",tqln+1^tqln]};
      /T.MED["DOSE"]_.=.!(/T.MED["DOSE"]=0) 2^/T.MED["ORDER.TYPE"],
         DO{+([PHA.MAIN,"DI",TRD],XX)^TRD XX^/T.MED["TQ",tqln+1^tqln]};
      /T.MED["SCH"]="PRN" 1^/T.MED["ORDER.TYPE"],
         IF{[PHA.MAIN,3]|15 \TPR[[PHA.MAIN,3]|15]|1^/T.MED["TQ",tqln+1^tqln]};
      0^/T.MED["ORDER.TYPE"]},
   @FORMAT.RX}

FORMAT.RX
IF{/T.MED["MEDNAME"]_.=. "no text in medname, dont send it";
   "<Order>"^!,
   "<OrderMnemonic>"_ARG_"</OrderMnemonic>"^!,
   "<Key>"_/T.MED["KEY"]_"</Key>"^!,
   "<AccountNumber>"_/T.MED["ACCT"]_"</AccountNumber>"^!,
   "<PatientNumber>"_/T.MED["MRN"]_"</PatientNumber>"^!,
   "<HnaOrderMnemonic><![CDATA["_/T.MED["MEDNAME"]_"]]></HnaOrderMnemonic>"^!,
   "<OrderingProviderMnemonic>"_/T.MED["PROV"]_"</OrderingProviderMnemonic>"^!,
   "<!-- present if med has been discontinued -->",
   IF{/T.MED["DI"] "<DiscontinueIndicator>"_/T.MED["DI"]_"</DiscontinueIndicator>"^!},
   "<SuspendIndicator>false</SuspendIndicator>"^!,
   "<OriginalOrderDateTime>"_/T.MED["START.DT"]_"</OriginalOrderDateTime>"^!,
   "<StopDateTime>"_/T.MED["STOP.DT"]_"</StopDateTime>"^!,
   IF{/T.MED["PRN"] "<PrnIndicator>"_/T.MED["PRN"]_"</PrnIndicator>"^!},
   IF{/T.MED["DOSE"] "<Dose><![CDATA["_/T.MED["DOSE"]_"]]></Dose>"^!},
   IF{/T.MED["UNITS"] "<Units><![CDATA["_/T.MED["UNITS"]_"]]></Units>"^!},
   IF{/T.MED["ROUTE"] "<Route><![CDATA["_/T.MED["ROUTE"]_"]]></Route>"^!},
   IF{/T.MED["FREQ"] "<Frequency><![CDATA["_/T.MED["FREQ"]_"]]></Frequency>"^!},
   "<Type>"_/T.MED["ORDER.TYPE"]_"</Type>"^!,
   IF{(/T.MED["ORDER.TYPE"]'=0)&((tqln+cmtln)>0) "<TypeQualifier><![CDATA["^!,
         ""^x,
         DO{+(/T.MED["TQ",x],XX)^x XX_D(10)^!},
         DO{+(/T.MED["COMMENTS",x],XX)^x XX_D(10)^!},
         "]]></TypeQualifier>"^!;
      cmtln>0 "<Note><![CDATA["^!,
         ""^x,
         DO{+(/T.MED["COMMENTS",x],XX)^x XX_D(10)^!},
         "]]></Note>"^!},
  @LINKS.AND.SPLITS,
  @RX.ORDER.STATUS,
  "</Order>"^!}

LINKS.AND.SPLITS
"<LinkedOrders>"^!,
""^temp,
DO{+([PHA.MAIN,"LI",temp],XX)^temp IF{XX|0=tr "dont emit self";
   "<LinkedOrder><OrderMnemonic>"_XX|0_"</OrderMnemonic></LinkedOrder>"^!}},
"</LinkedOrders>"^!,
IF{trsub_.=.;
   "<SplitOrders>"^!,
   ""^temp,
   DO{+([PHA.MAIN,"IN",temp],XX)^temp IF{temp=trsub "dont emit self";
      "<SplitOrder><OrderMnemonic>"_tr_"."_temp_"</OrderMnemonic></SplitOrder>"^!}},
   ""^temp,
   DO{+([PHA.MAIN,"MD",temp],XX)^temp IF{temp=trsub "dont emit self";
      "<SplitOrder><OrderMnemonic>"_tr_"."_temp_"</OrderMnemonic></SplitOrder>"^!}},
   ""^temp,
   DO{+([PHA.MAIN,"BT",temp],XX)^temp IF{temp=trsub "dont emit self";
      "<SplitOrder><OrderMnemonic>"_tr_"."_temp_"</OrderMnemonic></SplitOrder>"^!}},
   "</SplitOrders>"^!}

RX.ORDER.STATUS
"<OrderStatus>"^!,
"<Mnemonic>MEDORDSTAT."_/T.MED["ORDER.STAT"]_"</Mnemonic>"^!,
"<Display>"_/T.MED["ORDER.STAT"]_"</Display>"^!,
"</OrderStatus>"^!

SINGLE.MED
[PHA.MAIN,"M"]|0^tf,@GET.MEDREFINFO,
[PHA.MAIN,"M"]|1^/T.MED["DOSE"]

COMPOUND.MEDS
[PHA.MAIN,"IN",trsub]^XX,
XX|0^tf,
@GET.MEDREFINFO,
XX|1^/T.MED["DOSE"]

SPLIT.MEDS
[PHA.MAIN,"MD",trsub]^XX,
XX|0^tf,
@GET.MEDREFINFO,
XX|1^/T.MED["DOSE"]

IV.MEDS
""^firstname,
DO{+([PHA.MAIN,"AD",TRA],XX)^TRA "got additive",
   XX|0^tf,@GET.MEDREFINFO,
   XX|1^dose,
   \TF[XX|0]|5^units,
   IF{firstname_.=. /T.MED["MEDNAME"]^firstname,dose^firstdose,units^firstunits},
   "Med: "_/T.MED["MEDNAME"]^TEMP,
   TEMP_" "_dose_" "_units^/T.MED["COMMENTS",cmtln+1^cmtln]},
IF{firstname "in"^/T.MED["COMMENTS",cmtln+1^cmtln]},
DO{+([PHA.MAIN,"IC",TRC],XX)^TRC "got carrier",
   XX|0^tf,@GET.MEDREFINFO,
   IF{XX|5 XX|5^dose,"%"^units;
      XX|1^dose,\TF[XX|0]|7^units},
   IF{firstname_.=. /T.MED["MEDNAME"]^firstname,dose^firstdose,units^firstunits},
   "Med: "_/T.MED["MEDNAME"]^TEMP,
   TEMP_" - "_dose_IF{XX|5;" "}_units^/T.MED["COMMENTS",cmtln+1^cmtln]},
" "^/T.MED["COMMENTS",cmtln+1^cmtln],
"Total Volume: "_[PHA.MAIN,"I"]|1_" ML"^/T.MED["COMMENTS",cmtln+1^cmtln],
firstname^/T.MED["MEDNAME"],
firstdose^/T.MED["DOSE"],
firstunits^/T.MED["UNITS"]

ALT.IV.MEDS
""^firstname,
"Bottle Type: "_trsub^/T.MED["COMMENTS",cmtln+1^cmtln],
DO{+([PHA.MAIN,"BT",trsub,"AD",TRAA],XX)^TRAA "got additive",
   XX|0^tf,@GET.MEDREFINFO,
   XX|1^dose,
   \TF[XX|0]|5^units,
   IF{firstname_.=. /T.MED["MEDNAME"]^firstname,dose^firstdose,units^firstunits},
   "Med: "_/T.MED["MEDNAME"]_" "_\TF[XX|0]|3^TEMP,
   TEMP_" "_dose_"  "_units^/T.MED["COMMENTS",cmtln+1^cmtln]},
IF{firstname "in"^/T.MED["COMMENTS",cmtln+1^cmtln]},
DO{+([PHA.MAIN,"BT",trsub,"IC",TRAC],XX)^TRAC "got carrier",
   XX|0^tf,@GET.MEDREFINFO,
   XX|1^dose,
   \TF[XX|0]|5^units,
   IF{firstname_.=. /T.MED["MEDNAME"]^firstname,dose^firstdose,units^firstunits},
   "Med: "_/T.MED["MEDNAME"]_" "_\TF[XX|0]|3^TEMP,
   TEMP_" "_dose_"  "_units^/T.MED["COMMENTS",cmtln+1^cmtln]},
"Total Volume: "_XX|0_" ML"^/T.MED["COMMENTS",cmtln+1^cmtln],
"Bottle "_trsub_": "_firstname^/T.MED["MEDNAME"],
firstdose^/T.MED["DOSE"],
firstunits^/T.MED["UNITS"]

RATE.DURATION
IF{[PHA.MAIN,"I"]|3_[PHA.MAIN,"I"]|4 " "^/T.MED["COMMENTS",cmtln+1^cmtln]},
IF{[PHA.MAIN,"I"]|3 "Ordered Rate: "_[PHA.MAIN,"I"]|3^/T.MED["COMMENTS",cmtln+1^cmtln]},
IF{[PHA.MAIN,"I"]|4 "Duration: "_[PHA.MAIN,"I"]|4^/T.MED["COMMENTS",cmtln+1^cmtln]}

GET.MEDREFINFO
IF{\TF[tf]D|0;
   \TDG[\TF[tf]|2]|1;
   \TF[tf]|2;tf}^/T.MED["MEDNAME"],
\TF[tf]|5^/T.MED["UNITS"]
