PK.LABMKEY
A^prefix,
"PATS",@1,
END;

[1]::PATS
/ARGV[1]^DT,
2^nr,
DO{nr</ARGC 1,
   /ARGV[nr]^ACT,
   ?(A)AAAI[ACT]^aa,
   "MIC",@2,
   nr+1^nr};

[2]::MIC
""^Q,
DO{"NEXT.SPEC",@3 "SEND",@4};

[3]::NEXT.SPEC
>(?(SM)SMSP[aa,Q],sms)^Q;

[4]::SEND
IF{$(SM)SMS[sms]|3<DT;
   "<Document>"^!,
   "<InstanceMnemonic>"_prefix_sms_"</InstanceMnemonic>"^!,
   "COMPUTE.KEY",@5,
   "append x to key because t0 may be empty",
   "<Key>"_"x"_/tempkey_"</Key>"^!,
   "<PersonMnemonic>"_$(A)AA[aa]|3_"</PersonMnemonic>"^!,
   "<EventEndDateTime>"^!,
   IF{$(SM)SMS[sms]|13_.'=. $(SM)SMS[sms]|3_$(SM)SMS[sms]|4;
      $(SM)SMS[sms]|14_.'=. $(SM)SMS[sms]|9_$(SM)SMS[sms]|10}^!,
   "</EventEndDateTime>"^!,
   "<EventSetMnemonic>MICRO</EventSetMnemonic>"^!,
   "</Document>"^!};

[5]::COMPUTE.KEY
$(SM)SMS[sms]|23^/tempkey,
""^smp,
DO{+$(SM)SMS[sms]T[smp]^smp /tempkey_"-"_smp^/tempkey,
   "GET.LAST.REPORTABLE.RESULTS",@6,
   "note: cs doesnt have |1 in result, use R itself",
   /tempkey_":"_IF{R;"none"}^/tempkey};

[6]::GET.LAST.REPORTABLE.RESULTS
"Search backwards to get the latest verified results",
""^R^GOT,
DO{IF{GOT "";
   -($(SM)SMS[sms]T[smp]R[R],XX)^R "got result",
      IF{XX|3="V" "and its verified",
            1^GOT;
         R}}};
