PK.PAT.LAB.M,
A^prefix,
B^specimen,
"FORMAT.SPEC",@1,
END;

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

[2]::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}}};

[3]::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]}};

[4]::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: "_(&(SM)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{&(SM)SMQ[smqP]|13;&(SM)SMQ[smqP]|1}_": "^QQ,
         QQ_XX|1_" "_&(SM)SMQ[smqP]|11_" "_IF{XX|2 "ABN: "_XX|2}^/T.REC[T+1^T]},
      "SUSCEPTIBILITY.PROC",@5}};                                            

[5]::SUSCEPTIBILITY.PROC
""^S^A^Q,
&(SM)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: "_(&(SM)SMA[A]|1)_" "_(A.NODE|0)^QQ,
         QQ_" "_(A.NODE|1)_IF{XX|4 "ABN: "_XX|4}^/T.REC[T+1^T],
         ""^r,
         DO{+(&(SM)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: "_(&(SM)SMP[S]|1)^/T.REC[T+1^T]};

[6]::RESULT.PROMPTS
"handling result prompts",
""^smq,
DO{+([RESULT.MAIN,"Q",smq],XX)^smq "got a prompt",
   IF{&(SM)SMQ[smq]|9'="Y" "not reportable, dont emit";
      IF{&(SM)SMQ[smq]|13;&(SM)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]}}};

[7]::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]}}};

[8]::SPECIAL.RESULT
IF{[RESULT.MAIN]|5^sr "got one",
   "different structures between magic and c/s",
   IF{&(SM)SMP[smp]NG[sr]^NG "got one",
         IF{[RESULT.MAIN]|2="P" NG|0;NG|1}^scp,
         IF{&(S)SCP[scp]T[1];&(S)SCP[scp]|1};
      &(S1)SPARAM["R",sr]SR;
      &(S)SCP[sr]|1;
      sr}^result,
   "SPECIAL RESULT: "_result^/T.REC[T+1^T]};

[9]::SPECIMEN.DATA
[MAIN]|1^aa,
$(A)AA[aa]|3^/T.INFO["MRN"],
$(A)AA[aa]|0^/T.INFO["ACCT"],
"Source and Description",
&(SM)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"],
&(SM)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)_"-"_(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)_"-"_(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]};

[10]::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";"  Verified"}^/T.REC[T+1^T];

[11]::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];

