PK.ORDKEY
@OPEN.OE,
@HDR,
@PATS,
@TRLR,
END;

OPEN.OE
@admdir,
O(&,@misdict),
O(:,@admdata),
@oedir,
O(*,@oedata),
O(\,@oedict),
@pkdir

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

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

PATS
/ARGV[1]^DT,
2^nr,
DO{nr</ARGC 1,
   /ARGV[nr]^ACT,
   :AAAI[ACT]^aa,
   @ORD,
   nr+1^nr}

ORD
""^ORD.DT,
DO{@NEXT.DATE ""^po,
DO{@NEXT.ORD @SEND}}

NEXT.DATE
+*POE[aa,ORD.DT]^ORD.DT

NEXT.ORD
+*POE[aa,ORD.DT,po]^po

SEND
K(/T.REC),
K(/T.TEXT),
^*PO[po]^ORDR.MAIN,
[ORDR.MAIN]|5^pc,
IF{[ORDR.MAIN]_.=. "empty, ignore";
   /CONF[.orders.cats]["all"] @SEND2;
   /CONF[.orders.cats][pc] @SEND2;
   "notonlist"}

SEND2
[ORDR.MAIN]|3^aa,
[ORDR.MAIN]|6^pp,
[ORDR.MAIN]|2^stat,
[ORDR.MAIN]|0^orddate,
[ORDR.MAIN]|1^ordtime,
[ORDR.MAIN]|10^svcdate,
[ORDR.MAIN]|11^svctime,
orddate_ordtime_"-"_svcdate_"-"_(-[ORDR.MAIN,"E",""])^synckey,
"dont emit inactive orders before the given day",
IF{((stat="X")!(stat="C"))&((svcdate_.'=.)&(svcdate<DT));
   IF{stat="X" "Cancelled";
      stat="U" "Unverified";
      stat="V" "Verified";
      stat="T" "S on cs, T on magic","Transmitted";
      stat="L" "Logged";
      stat="I" "In progress";
      stat="xxxT" "T on cs, missing on magic","Taken";
      stat="C" "Completed";
      stat="R" "Result";
      stat}^statdisp,
   0^TXT,
   DO{+([ORDR.MAIN,"NT",nt],LN)^nt LN^/T.TEXT[TXT+1^TXT]},
   IF{[ORDR.MAIN,"R"]|0^gxx @HANDLE.CDS},
   IF{[ORDR.MAIN,"R"]|1^gxx @HANDLE.CDS},
   IF{[ORDR.MAIN,"R"]|2^gxx @HANDLE.CDS},
   "<Order>"^!,
   "<OrderMnemonic>"_po_"</OrderMnemonic>"^!,
   "<Key>"_synckey_"</Key>"^!,
   "<AccountNumber>"_:AA[aa]|0_"</AccountNumber>"^!,
   "<PatientNumber>"_:AA[aa]|3_"</PatientNumber>"^!,
   "<HnaOrderMnemonic><![CDATA["_\PP[pc,pp]|1_"]]></HnaOrderMnemonic>"^!,
   "<CatalogType><ValueMnemonic>"_pc_"</ValueMnemonic></CatalogType>"^!,
   0^CD,
   "Order number: "_[ORDR.MAIN]|12^/T.REC[CD+1^CD],
   "Category: "_pc_" - "_\PC[pc]|1^/T.REC[CD+1^CD],
   "Order status: "_statdisp^/T.REC[CD+1^CD],
   " "^/T.REC[CD+1^CD],
   ""^nt,
   IF{+/T.TEXT[nt] "Details:"^/T.REC[CD+1^CD],
      DO{+(/T.TEXT[nt],LN)^nt LN^/T.REC[CD+1^CD]},
      " "^/T.REC[CD+1^CD]},
   "Ordered by: "_&GGG[[ORDR.MAIN]|4]|1_" "_orddate_" "_ordtime^/T.REC[CD+1^CD],
   [ORDR.MAIN]|16^gu,
   IF{&GU[gu]|13 &GGG[&GU[gu]|13]|1;&GU[gu]|1;gu}^user,
   IF{user "Entered by: "_user^/T.REC[CD+1^CD]},
   " "^/T.REC[CD+1^CD],
   IF{svcdate "Service date: "_svcdate_" "_svctime^/T.REC[CD+1^CD]},
   "<ClinicalDisplayLine><![CDATA["^!,
   ""^T,
   DO{+(/T.REC[T],XX)^T XX_D(10)^!},
   "]]></ClinicalDisplayLine>"^!,
   "<OrderStatus>"^!,
   "<Mnemonic>"_stat_"</Mnemonic>"^!,
   "<Display>"_statdisp_"</Display>"^!,
   "</OrderStatus>"^!,
   "<OriginalOrderDateTime>"_orddate_ordtime_"</OriginalOrderDateTime>"^!,
   "<ServiceDateTime>"_svcdate_svctime_"</ServiceDateTime>"^!,
   "</Order>"^!}

HANDLE.CDS
"for each screen field, show a value if we have one",
DO{+&GXX[gxx]F[ggv]^ggv "got a field",
   &GGV[ggv]|1^prompt,
   &GGV[ggv]|2^fldtype,
   IF{[ORDR.MAIN,"R",ggv]|0^val "got a single value",
         IF{fldtype="G" &GGV[ggv]|4^ggl,&GGL[ggl]E[val]|0^val},
         prompt_"  "_val^/T.TEXT[TXT+1^TXT];
      +[ORDR.MAIN,"R",ggv,"M",""] "got multiple values",
         prompt^/T.TEXT[TXT+1^TXT],
         DO{+([ORDR.MAIN,"R",ggv,"M",m],LN)^m LN|0^val,
            IF{fldtype="G" &GGV[ggv]|4^ggl,&GGL[ggl]E[val]|0^val},
            "  "_val^/T.TEXT[TXT+1^TXT]}}}
