PK.LABNDATA
"GET.DELTAFLAG",@1,
"HDR",@2,
"PATS",@4,
"TRLR",@3,
END;

[1]::GET.DELTAFLAG
&(S1)SPARAM|11^LABNORMS.DELTA;

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

[3]::TRLR
"</Payload>"^!,
"</ReplyMessage>"^!;

[4]::PATS
1^nr,
DO{nr</ARGC 1,
   /ARGV[nr]^sls,
   "SPEC",@5,
   nr+1^nr};

[5]::SPEC
K(/T.SPEC),
K(/T.COMPONENTS),
^$(SL)SLS[sls]^SPECIMEN,
[SPECIMEN]|1^aa,
$(A)AA[aa]|3^/T.SPEC["MRN"],
[SPECIMEN]|3^/T.SPEC["COLLDT"],
[SPECIMEN]|4^/T.SPEC["COLLTM"],
[SPECIMEN]|2^/T.SPEC["COMPSTATUS"],
[SPECIMEN]|23^/T.SPEC["KEY"],
""^ordtest,
DO{"NEXT.ORD.TEST",@6 "BUILD.PANEL",@7},
"remove panels that are subsets of other panels",
"FILTER.PANELS",@12,
"do this after FILTER.PANELS because we filter panels based on paneldefs, not results",
"FILTER.OUT.UNRESULTED.TESTS",@13,
""^ordtest,
DO{"NEXT.ORD.TEST",@6 "SEND.PANEL",@14},
K(/T.SPEC),
K(/T.COMPONENTS);

[6]::NEXT.ORD.TEST
+[SPECIMEN,"O",ordtest]^ordtest;

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

[8]::GET.TEST.TYPE
&(SL)SLT[ordtest]|3^typ;

[9]::IS.PROFILE.OR.GROUP
("P"=typ)!("G"=typ);

[10]::IS.TTEST.OR.C
("T"=typ)!("C"=typ);

[11]::GET.RESTEST
"cant assign a pointer as first statement in subroutine",
^[SPECIMEN,"T",restest]^RESTEST;

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

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

[14]::SEND.PANEL
""^restest,
DO{"NEXT.COMPONENT",@15 "SEND.COMPONENT",@16};

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

[16]::SEND.COMPONENT
K(/T.TEST),
K(/T.COMMENTS),
"GET.RESTEST",@11,
"non-resulted ttests have already been filtered out",
IF{"Y"'=&(SL)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"],
      ^$(SL)SLS[[RESTEST]|9]^PREVSPEC},
0^cmtln,
"GET.COMMENTS",@17,
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",@19,
   "</Component>"^!},
K(/T.COMMENTS),
K(/T.TEST);

[17]::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",
   [PREVSPEC,"T",restest]|0^prevresult,
   [PREVSPEC]|3^prevdate,
   [PREVSPEC]|4^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",@18 IF{ln:0S#0=atsign;ln^/T.COMMENTS[cmtln+1^cmtln]}},
   [RESTEST]|14^ct,""^ctl,
   DO{+(&(S)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;&(SL)SLT[restest]|15}^sltG,
   &(SL)SLT[restest]G[sltG]|8^ct,""^ctl,
   DO{+(&(S)SCP[ct]T[ctl],ln)^ctl IF{ln:0S#0=atsign;ln^/T.COMMENTS[cmtln+1^cmtln]}};

[18]::NEXT.COMMENT
+([RESTEST,"C",cq],ln)^cq;

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