PK.LABBDATA,
A^prefix,
B^specimen,
"FORMAT.SPEC",@1,
END;

[1]::FORMAT.SPEC
0^T^THDR,
D(64)^atsign,
^$(SB)SBS[specimen]^MAIN,
$(SB)SBS[specimen]^MAIN.VAR,
"SPECIMEN.DATA",@2,
IF{MAIN.VAR|2="CAN" "**SPECIMEN CANCELLED***"^/T.REC[T+1^T];
   "RESULTS",@5};

[2]::SPECIMEN.DATA
[MAIN]|1^aa,
$(A)AA[aa]|3^/T.INFO["MRN"],
$(A)AA[aa]|0^/T.INFO["ACCT"],
"PRODUCTS",@3,
"TESTS",@4,
IF{prodname;testname;"No products"}^/T.INFO["TESTNAME"],
MAIN.VAR|23^/T.INFO["UPDATENODE"],
IF{MAIN.VAR|13_.'=. MAIN.VAR|3_MAIN.VAR|4;
   MAIN.VAR|14_.'=. MAIN.VAR|9_MAIN.VAR|10}^/T.INFO["TESTDATE"],
"BBK"^/T.INFO["TESTTYPE"],
"Specimen"^/T.INFO["HEADERSECTION"],
"Results"^/T.INFO["RPTSECTION"],
MAIN.VAR|2^/T.INFO["STATUS"],
"Specimen: "_MAIN.VAR|0_" ("_MAIN.VAR|2_")"^/T.HEADER[THDR+1^THDR],
"Coll Date: "_IF{MAIN.VAR|13_.=. "Not Collected";%(Z)date.out(MAIN.VAR|3)^QQ,
                 QQ_"-"_(MAIN.VAR|4)_"  "_(MAIN.VAR|13)}^/T.HEADER[THDR+1^THDR],
"Recv Date: "_IF{MAIN.VAR|12'="Y" "Not Received";%(Z)date.out(MAIN.VAR|9)^QQ,
                 QQ_"-"_(MAIN.VAR|10)_"  "_(MAIN.VAR|14)}^/T.HEADER[THDR+1^THDR],
"Submitted By: "_MAIN.VAR|11^/T.HEADER[THDR+1^THDR];

[3]::PRODUCTS
"Products:"^/T.REC[T+1^T],
IF{+[MAIN,"OP",""] "got at least one",
      0^i,
      DO{+[MAIN,"OP",sbp]^sbp i+1^i,
         "  "_&(SB)SBP[sbp]|1^QQ,
         QQ_IF{([MAIN,"OP",sbp]|0^cnt)>1 " / "_cnt}^QQ,
         QQ_IF{([MAIN,"OP",sbp]|1^cnt)_.'=. " / -"_cnt}^/T.REC[T+1^T],
         IF{i=1 &(SB)SBP[sbp]|1^prodname;
            i=2 prodname_"..."^prodname}};
   "no products",
      "  None"^/T.REC[T+1^T]},
" "^/T.REC[T+1^T];

[4]::TESTS
"Tests:"^/T.REC[T+1^T],
IF{+[MAIN,"O",""] "got at least one",
      0^i,
      DO{+[MAIN,"O",sbt]^sbt i+1^i,
         "  "_&(SB)SBT[sbt]|1_IF{([MAIN,"O",sbt]|0^cnt)>1 " / "_cnt}^/T.REC[T+1^T],
         IF{i=1 &(SB)SBT[sbt]|1^testname;
            i=2 testname_"..."^testname}};
   "no tests",
      "  None"^/T.REC[T+1^T]},
" "^/T.REC[T+1^T];

[5]::RESULTS
"PRODUCT.DETAILS",@6,
"TEST.DETAILS",@10,
"COMMENTS",@11;

[6]::PRODUCT.DETAILS
DO{+[MAIN,"OP",sbp]^sbp "got one",
   "Product: "_&(SB)SBP[sbp]|1^/T.REC[T+1^T],
   "PRODUCT.UNITS",@7};

[7]::PRODUCT.UNITS
IF{+[MAIN,"I",""] "got some units",
   " Units:"^/T.REC[T+1^T],
   ""^sbu,
   DO{+[MAIN,"I",sbp,"U",sbu]^sbu "got one",
      $(SB1)SBU[sbu]^UNIT,
      "  "_UNIT|0_": "_&(SB)SBY[UNIT|9]|2^QQ,
      "UNIT.STATUS",@8^tmp,
      QQ_"  St: "_tmp^QQ,
      "UNIT.LASTACTION",@9^tmp,
      QQ_" "_tmp^/T.REC[T+1^T]},
" "^/T.REC[T+1^T]};

[8]::UNIT.STATUS
IF{UNIT|2="ISS" "Issued";
   UNIT|2="PTX" "Presumed Transfused";
   UNIT|2="TRN" "Transfused";
   UNIT|2="TRR" "Reaction";
   UNIT|2="XMC" "Cross-matched";
   UNIT|2};

[9]::UNIT.LASTACTION
IF{UNIT|2="ISS" $(SB1)SBU[sbu]SO|0_" "_$(SB1)SBU[sbu]SO|1;
   $(SB1)SBU[sbu]A|11_" "_$(SB1)SBU[sbu]A|12};

[10]::TEST.DETAILS
IF{+[MAIN,"T",""] "got some tests",
   "Tests:"^/T.REC[T+1^T],
   ""^sbt,
   DO{+([MAIN,"T",sbt],TT)^sbt "got one",
      IF{"NP"=TT|0 "not printable";
         "Y"'=&(SB)SBT[sbt]|8 "SBT["_sbt_"] is not reportable";
         "printable result on reportable test",
            IF{&(SB)SBT[sbt]|3="B" "blood type",
                  &(SB)SBY[TT|0]|2;
               "default",
                  TT|0}^res,
            IF{(res_.=.)&(>[MAIN,"T",sbt,"X",""]_.=.) "Pending"^res},
            " "_&(SB)SBT[sbt]|1_":  "_res_IF{TT|7 "  ABN: "_TT|7}^/T.REC[T+1^T],
            ""^sbsXQ^sbp,
            DO{+([MAIN,"T",sbt,"X",sbp,sbsXQ],XM)^sbsXQ "got a cross-match",
                  &(SB)SBP[sbp]|1^prod,
                  IF{XM|0_.=. "Pending";
                     XM|0="TNP" "Test not performed";
                     $(SB1)SBU[XM|0]^UNIT,
                     UNIT|0_" "_&(SB)SBY[UNIT|9]|2_" Comp? "_XM|1}^res,
                  "  "_&(SB)SBP[sbp]|1_": "_res^/T.REC[T+1^T];
               +[MAIN,"T",sbt,"X",sbp]^sbp}}},
   " "^/T.REC[T+1^T]};

[11]::COMMENTS
0^GOT,
DO{(GOT=0)&(+([MAIN,"C",sbsC],XX)^sbsC) IF{XX:0S#0'=atsign 1^GOT}},
IF{GOT=1 "Comments:"^/T.REC[T+1^T],
   ""^sbsC,
   DO{+([MAIN,"C",sbsC],XX)^sbsC IF{XX:0S#0=atsign;"  "_XX^/T.REC[T+1^T]}},
" "^/T.REC[T+1^T]};
