PK.LABMDATA,
A^prefix,
B^specimen,
@OPEN.LAB,
@FORMAT.SPEC,
END;

OPEN.LAB
@admdir,
O(&,@misdict),
O(:,@admdata),
@labdir,
O(*,@labdata),
O(\,@labdict),
@pkdir

FORMAT.SPEC
0^T^THDR,
D(64)^atsign,
^*SMS[specimen]^MAIN,
*SMS[specimen]^MAIN.VAR,
@SPECIMEN.DATA,
IF{MAIN.VAR|2="CAN" "**SPECIMEN CANCELLED***"^/T.HEADER[THDR+1^THDR];
""^smp,
DO{+[MAIN,"T",smp]^smp "got "_smp,
   "Procedure: "_\SMP[smp]|1^/T.REC[T+1^T],
   "none"^key,
   IF{"check if can report",
      ^*SMS[specimen,"T",smp]^PROC.MAIN,
      [PROC.MAIN]|5_.'=. 1,
         " ***PROCEDURE DELETED***"^/T.REC[T+1^T];
      @GET.LAST.REPORTABLE.RESULTS,
         R_.=. "No verified results to report"^/T.REC[T+1^T];
      "have a result",
         [RESULT.MAIN]|1^key,
         @RESULT.STATUS,
         @SPECIAL.RESULT,
         @RESULT.PROMPTS,
         @RESULT.INTERPRETATION,
         @RESULT.LINES,
         @ORGS.AND.SUSCEPTIBILITIES},
   " "^/T.REC[T+1^T],
   /T.INFO["UPDATENODE"]_"-"_smp_":"_key^/T.INFO["UPDATENODE"]}}

GET.LAST.REPORTABLE.RESULTS
"Search backwards to get the latest verified results",
""^R^GOT,
DO{IF{GOT "";
   -([PROC.MAIN,"R",R],XX)^R "got result",
      IF{XX|3="V" "and its verified",
            ^[PROC.MAIN,"R",R]^RESULT.MAIN,
            1^GOT;
         R}}}

RESULT.LINES
""^F,
DO{+([RESULT.MAIN,"F",F],RL)^F IF{RL:0S#0=atsign "Internal Only, Dont Use";
   RL^/T.REC[T+1^T]}}

ORGS.AND.SUSCEPTIBILITIES
""^K,
DO{+([RESULT.MAIN,"K",K],ORG.NODE)^K "got an org",
   IF{ORG.NODE|2 "ORG IS NP";
      ORG.NODE|0^ORG#"0#"^ORG.MNE,ORG#"1#"^ORG.NUM,
      "Organism: "_(\SMO[ORG.MNE]|1_IF{ORG.NUM "#"_ORG.NUM})^/T.REC[T+1^T],
      DO{+([RESULT.MAIN,"K",K,"P",smqP],XX)^smqP "got a culture result",
         "  "_IF{\SMQ[smqP]|13;\SMQ[smqP]|1}_": "^QQ,
         QQ_XX|1_" "_\SMQ[smqP]|11_" "_IF{XX|2 "ABN: "_XX|2}^/T.REC[T+1^T]},
      @SUSCEPTIBILITY.PROC}}

SUSCEPTIBILITY.PROC
""^S^A^Q,
\SMT[[MAIN,"A"]|0]|6^SUSC.TGT,
DO{+([RESULT.MAIN,"O",ORG,"S",S,"A",A],A.NODE)^A "got a node",
   IF{A.NODE|3 "no print";
      A.NODE|0_A.NODE|1_.=.;
      " Anti: "_(\SMA[A]|3)_" "_(A.NODE|0)^QQ,
         QQ_" "_(A.NODE|1)_IF{XX|4 "ABN: "_XX|4}^/T.REC[T+1^T],
         ""^r,
         DO{+(\SMA[A]S["M"]O[".DEFAULT"]T[SUSC.TGT]V[A.NODE|1]R[r],RR)^r "got result",
            IF{RR|2=A.NODE|0 "   "_RR|0_" "_RR|1_" "_RR|3^/T.REC[T+1^T]}},
         "Antibiotic text comments",
         DO{+([RESULT.MAIN,"O",ORG,"S",S,"A",A,"T",Q],XX)^Q 1,
            "   "_XX^/T.REC[T+1^T]}};
   +([RESULT.MAIN,"O",ORG,"S",S],SUS.NODE)^S 1,
      "SUSCEPTIBLITY PROCEDURE: "_(\SMP[S]|1)^/T.REC[T+1^T]}

RESULT.PROMPTS
"handling result prompts",
""^smq,
DO{+([RESULT.MAIN,"Q",smq],XX)^smq "got a prompt",
   IF{\SMQ[smq]|9'="Y" "not reportable, dont emit";
      IF{\SMQ[smq]|13;\SMQ[smq]|1}^prompt,
      prompt_(XX|1)_IF{XX|2 "ABN: "_XX|2}^/T.REC[T+1^T],
      DO{+([RESULT.MAIN,"Q",smq,smsRQ],XX)^smsRQ "got a multi-response",
         "  "_(XX|1)_IF{XX|2 "ABN: "_XX|2}^/T.REC[T+1^T]}}}

RESULT.INTERPRETATION
"handling interpretation",
""^smsIQ,
IF{+[RESULT.MAIN,"I",""]_.=.;
   "Interpretation:"^/T.REC[T+1^T],
   DO{+([RESULT.MAIN,"I",smqIQ],XX)^smqIQ "got an interpretation",
      IF{XX:0S#0=atsign;XX^/T.REC[T+1^T]}}}

SPECIAL.RESULT
IF{[RESULT.MAIN]|5^sr "got one",
   IF{\SMP[smp]NG[sr] "got one",
         IF{[RESULT.MAIN]|2="P" "prelim result",
               IF{\SMP[smp]NG[sr]T[1]|3;\SMP[smp]B|6};
            "final result",
               IF{\SMP[smp]NG[sr]T[1]|1;\SMP[smp]B|7}}^scp,
         \SCP[scp]|1;
      \SPARAM["R",sr]SR;
      \SCP[sr]|1;
      sr}^result,
   "SPECIAL RESULT: "_result^/T.REC[T+1^T]}

SPECIMEN.DATA
[MAIN]|1^aa,
:AA[aa]|3^/T.INFO["MRN"],
:AA[aa]|0^/T.INFO["ACCT"],
"Source and Description",
\SMT[[MAIN,"A"]|0]|2^SRC,
SRC^/T.INFO["TESTNAME"],
IF{MAIN.VAR|13_.'=. MAIN.VAR|3_MAIN.VAR|4;
   MAIN.VAR|14_.'=. MAIN.VAR|9_MAIN.VAR|10}^/T.INFO["TESTDATE"],
"MICRO"^/T.INFO["TESTTYPE"],
"Specimen"^/T.INFO["HEADERSECTION"],
"Results"^/T.INFO["RPTSECTION"],
MAIN.VAR|23^/T.INFO["UPDATENODE"],"may be updated by result",
MAIN.VAR|2^/T.INFO["STATUS"],
\SMF[[MAIN,"A"]|1]|2^SPDESC,
"Specimen: "_MAIN.VAR|0_" ("_MAIN.VAR|2_")"^/T.HEADER[THDR+1^THDR],
"Source: "_(SRC)^/T.HEADER[THDR+1^THDR],
IF{SPDESC " Specimen Desc: "_SPDESC}^/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|14_.=. "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],
0^GOT,
DO{(GOT=0)&(+([MAIN,"C",smsC],XX)^smsC) IF{XX:0S#0'=atsign 1^GOT}},
IF{GOT=1 "Comments:"^/T.HEADER[THDR+1^THDR],
   ""^smsC,
   DO{+([MAIN,"C",smsC],XX)^smsC IF{XX:0S#0=atsign;"  "_XX^/T.HEADER[THDR+1^THDR]}},
" "^/T.HEADER[THDR+1^THDR]}

RESULT.STATUS
[RESULT.MAIN]|2^PF,
IF{PF'="P"'="F" "Unknown Status";
   PF="P" "Preliminary";
   PF="F" "Final"}^tmp,
tmp_IF{[RESULT.MAIN]|3^ST'="V" "  Not Verified"}^/T.REC[T+1^T]

SHOW.RESULT.TIMES
IF{[RESULT.MAIN]|7^VT 1,
      %Z.date.out(%Z.date.in.magic(VT+[MAIN]|16))^QQ,
      QQ_"-"_%Z.time.out(VT+[MAIN]|16)_"  "^QQ,
      QQ_[RESULT.MAIN]|6;
   "NOT AVAILABLE"}^QQ,
"Ver Time: "_QQ^/T.REC[T+1^T],
"Res Time: "_(IF{[RESULT.MAIN]|1^RT 1,
              %Z.date.out(%Z.date.in.magic(RT+[MAIN]|16))^QQ,
              QQ_"-"_%Z.time.out(RT+[MAIN]|16)_"  "^QQ,
              QQ_[RESULT.MAIN]|6;"NOT AVAILABLE"})^/T.REC[T+1^T]
