PK.LABNDATA,
@OPEN.LAB,
@GET.DELTAFLAG,
@HDR,
@PATS,
@TRLR,
END;

OPEN.LAB
@admdir,
O(:,@admdata),
IF{/CONF[.labs.source]="lab" "regular labs",
  @labdir,
  O(*,@labdata),
  O(\,@labdict);
"if not regular labs, assume mock labs",
  @mocklabdir,
  O(*,@mocklabdata),
  O(&,@mocklabindx),
  O(\,@mocklabdict)},
@pkdir

GET.DELTAFLAG
IF{/CONF[.labs.source]="lab" "regular labs",
  \SPARAM|12^LABNORMS.DELTA;
"if not regular labs, assume mock labs",
  "#"^LABNORMS.DELTA}

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

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

PATS
1^nr,
DO{nr</ARGC 1,
   /ARGV[nr]^sls,
   @SPEC,
   nr+1^nr}

SPEC
K(/T.SPEC),
K(/T.COMPONENTS),
IF{/CONF[.labs.source]="lab" "regular labs",
      ^*SLS[sls]^SPECIMEN,
      [SPECIMEN]|1^aa,
      [SPECIMEN]|3^/T.SPEC["COLLDT"],
      [SPECIMEN]|4^/T.SPEC["COLLTM"],
      [SPECIMEN]|2^/T.SPEC["COMPSTATUS"],
      [SPECIMEN]|23^/T.SPEC["KEY"];
   "if not regular labs, assume mock labs",
      ^*L[sls]^SPECIMEN,
      :AAAI[[SPECIMEN]|1]^aa,
      %Z.date.in.magic([SPECIMEN]|8)^/T.SPEC["COLLDT"],
      %Z.time.out([SPECIMEN]|8)^/T.SPEC["COLLTM"],
      [SPECIMEN]|2^/T.SPEC["COMPSTATUS"],
      [SPECIMEN]|9_[SPECIMEN]|10^/T.SPEC["KEY"]},
:AA[aa]|3^/T.SPEC["MRN"],
""^ordtest,
DO{@NEXT.ORD.TEST @BUILD.PANEL},
"remove panels that are subsets of other panels",
@FILTER.PANELS,
"do this after FILTER.PANELS because we filter panels based on paneldefs, not results",
@FILTER.OUT.UNRESULTED.TESTS,
""^ordtest,
DO{@NEXT.ORD.TEST @SEND.PANEL},
K(/T.SPEC),
K(/T.COMPONENTS)

NEXT.ORD.TEST
"both regular and mock labs use the same structure here",
+[SPECIMEN,"O",ordtest]^ordtest

BUILD.PANEL
@GET.TEST.TYPE,
IF{@IS.PROFILE.OR.GROUP "got a group/profile",
      %PK.LABSETLIST(ordtest,ordtest);
   @IS.TTEST.OR.C "got an individual ttest, or C, we might ignore C later",
      ordtest^/T.COMPONENTS[ordtest][ordtest]}

GET.TEST.TYPE
IF{/CONF[.labs.source]="lab" "regular labs",
      \SLT[ordtest]|3;
   "mock labs",
      \LAB.TE[ordtest]A|3}^typ

IS.PROFILE.OR.GROUP
("P"=typ)!("G"=typ)

IS.TTEST.OR.C
("T"=typ)!("C"=typ)

GET.RESTEST
"both regular and mock labs use the same structure here",
^[SPECIMEN,"T",restest]^RESTEST

FILTER.PANELS
"goal here is to remove any panels that are complete subsets of other panels",
""^slt1,
DO{+/T.COMPONENTS[slt1]^slt1 "got one",
   ""^slt2,
   DO{+/T.COMPONENTS[slt2]^slt2 "got one",
      IF{slt1=slt2 "dont test against self";
         "see if all components of slt2 are in slt1",
            ""^slt2c,
            1^is.subset,
            DO{is.subset&(+/T.COMPONENTS[slt2][slt2c]^slt2c) "got one",
               IF{/T.COMPONENTS[slt1][slt2c]_.=. "not found in slt1",
                  ""^is.subset}},
            IF{is.subset "remove it from the list",
               K(/T.COMPONENTS[slt2])}}}}

FILTER.OUT.UNRESULTED.TESTS
"now filter out any restests that arent resulted",
""^ordtest,
DO{+/T.COMPONENTS[ordtest]^ordtest "got one",
   ""^restest,
   DO{@NEXT.COMPONENT "got one",
      @GET.RESTEST,
      IF{[RESTEST]_.=. "not resulted",
         K(/T.COMPONENTS[ordtest][restest])}}}

SEND.PANEL
""^restest,
DO{@NEXT.COMPONENT @SEND.COMPONENT}

NEXT.COMPONENT
+/T.COMPONENTS[ordtest][restest]^restest

SEND.COMPONENT
K(/T.TEST),
K(/T.COMMENTS),
@GET.RESTEST,
"non-resulted ttests have already been filtered out",
IF{/CONF[.labs.source]="lab" "regular labs",
   IF{"Y"'=\SLT[restest]|8 "not reportable, ignore";
      "otherwise, get data",
         [RESTEST]|0^/T.TEST["RESULT"],
         [RESTEST]|8^/T.TEST["NORMRANGE"],
         [RESTEST]|12^/T.TEST["UNITS"],
         [RESTEST]|7^/T.TEST["NORMSTATUS"],
         ^*SLS[[RESTEST]|9]^PREVSPEC};
   "if not regular labs, assume mock labs",
      IF{"Y"'=\LAB.TE[restest]A|8 "not reportable, ignore";
         "otherwise, get data",
         [RESTEST]|0^/T.TEST["RESULT"],
         [RESTEST]|6^tmp,
         IF{tmp$1="(" tmp'$1^tmp},
         IF{tmp#(L(tmp)-1)=")" tmp$(L(tmp)-1)^tmp},
         tmp^/T.TEST["NORMRANGE"],
         \LAB.TE[restest]RG[\LAB.TE[restest]|4]|1^/T.TEST["UNITS"],
         [RESTEST]|7^/T.TEST["NORMSTATUS"],
         ^*L[[RESTEST]|8]^PREVSPEC}},
0^cmtln,
@GET.COMMENTS,
IF{(/T.TEST["RESULT"]_"."=".")&(cmtln=0);
   /T.TEST["RESULT"]_"."="NP.";
   "<Component>"^!,
   "<Specimen>"_sls_"</Specimen>"^!,
   "<PanelDefMnemonic>"_ordtest_"</PanelDefMnemonic>"^!,
   "<Panel>"_sls_"."_ordtest_"</Panel>"^!,
   "<EventNumber>"_sls_"."_ordtest_"."_restest_"</EventNumber>"^!,
   "<ComponentDefinitionMnemonic>"_restest_"</ComponentDefinitionMnemonic>"^!,
   "<EventCodeMnemonic>"_restest_"</EventCodeMnemonic>"^!,
   "<NormalRange><![CDATA["_/T.TEST["NORMRANGE"]_"]]></NormalRange>"^!,
   "<Units>"_/T.TEST["UNITS"]_"</Units>"^!,
   "<Value><![CDATA["_/T.TEST["RESULT"]_"]]></Value>"^!,
   "<ClinicalSignificantTime>"_/T.SPEC["COLLDT"]_/T.SPEC["COLLTM"]_"</ClinicalSignificantTime>"^!,
   "<CompletedStatus>"^!,
   "<Display>"_/T.SPEC["COMPSTATUS"]_"</Display>"^!,
   "<Mnemonic>COMPLETED."_/T.SPEC["COMPSTATUS"]_"</Mnemonic>"^!,
   "</CompletedStatus>"^!,
   "<NormalcyStatus>"^!,
   "<Display>"_/T.TEST["NORMSTATUS"]_"</Display>"^!,
   "<Mnemonic>NORMALCY."_IF{/T.TEST["NORMSTATUS"];"NORMAL"}_"</Mnemonic>"^!,
   "</NormalcyStatus>"^!,
   "<Key>"_"x"_/T.SPEC["KEY"]_"</Key>"^!,
   "<PatientNumber>"_/T.SPEC["MRN"]_"</PatientNumber>"^!,
   @SEND.COMMENTS,
   "</Component>"^!},
K(/T.COMMENTS),
K(/T.TEST)

GET.COMMENTS
D(64)^atsign,
"handle deltas by putting them in the comments",
IF{(L(/T.TEST["NORMSTATUS"])>0)&(L(/T.TEST["NORMSTATUS"],LABNORMS.DELTA)=0) "got a delta flag",
   IF{/CONF[.labs.source]="lab" "regular labs",
         [PREVSPEC,"T",restest]|0^prevresult,
         [PREVSPEC]|3^prevdate,
         [PREVSPEC]|4^prevtime;
      "if not regular labs, assume mock labs",
         [PREVSPEC,"T",restest]|0^prevresult,
         %Z.date.in.magic([PREVSPEC]|8)^prevdate,
         %Z.time.out([PREVSPEC]|8)^prevtime},
   prevdate$4^yr,prevdate$6'$4^mn,prevdate'$6^day,
   mn_"/"_day_"/"_yr^prevdate,
   "Delta: "_prevresult_" at "_prevdate_"-"_prevtime^/T.COMMENTS[cmtln+1^cmtln]},
"note: do the at-sign tests in the population because querydefs dont handle empty cdatas well",
DO{@NEXT.COMMENT IF{ln:0S#0=atsign;ln^/T.COMMENTS[cmtln+1^cmtln]}},
IF{/CONF[.labs.source]="lab" "standard labs",
   "look for canned text in standard labs only",
      [RESTEST]|14^ct,""^ctl,
      DO{+(\SCP[ct]T[ctl],ln)^ctl IF{ln:0S#0=atsign;ln^/T.COMMENTS[cmtln+1^cmtln]}},
      "pull canned text comments from test definition, based on result.group",
      IF{[RESTEST]|1;\SLT[restest]|15}^sltG,
      \SLT[restest]G[sltG]|8^ct,""^ctl,
      DO{+(\SCP[ct]T[ctl],ln)^ctl IF{ln:0S#0=atsign;ln^/T.COMMENTS[cmtln+1^cmtln]}}}

NEXT.COMMENT
"both regular and mock labs use the same structure here",
+([RESTEST,"C",cq],ln)^cq

SEND.COMMENTS
IF{cmtln>0 "got at least one",
   "<Comment><Body><![CDATA["^!,
   ""^cq,
   DO{+(/T.COMMENTS[cq],ln)^cq ln_D(10)^!},
   "]]></Body></Comment>"^!}
