PK.RADDATA,
A^prefix,
B^examarg,
@OPEN.RAD,
@REPORT,
END;

OPEN.RAD
@admdir,
O(&,@misdict),
O(:,@admdata),
@raddir,
O(*,@raddata),
O(\,@radrpt),
O(&,$.data[*.CDFN]),
@pkdir

REPORT
K(/T.REC),
examarg#0.^rpt,
examarg#1.^rdtarg,
examarg#2.^rexarg,
0^T^THDR,
IF{*RAD.RPT[rpt]_.=. "**REPORT "_rpt_" DOES NOT EXIST**"^/T.HEADER[THDR+1^THDR];
   @REPORT.DATA,
   @REPORT.LINES,
   @SIGNED.INFO}

REPORT.DATA
^*RAD.RPT[rpt]^RAD.MAIN,
[RAD.MAIN,"PT"]^mpi,
"taking the latest time into SEC",
-(*RAD.RPT[rpt]ED[""],XX)^ed,XX|0^SEC1,
*RAD.RPT[rpt]SOV^SEC2,
*RAD.RPT[rpt]S^SEC3,
*RAD.RPT[rpt]ES|0^SEC4,
"data in ES|0 appears to be seconds followed by a counter, at least at one site",
SEC4$L(SEC4,".")^SEC4,
(SEC1)!(SEC2)!(SEC3)!(SEC4)^SEC,
SEC^/T.INFO["UPDATENODE"],
IF{*RAD[mpi]EX[rdtarg]EX[rexarg]AT^arriv.time "got arrival time",
      %Z.date.clin.to.npr(rdtarg)_arriv.time;
   "no arrival time, use TCHTM",
      *RAD[mpi]EX[rdtarg]EX[rexarg]TCHTM^tmp,
      %Z.date.in.magic(tmp)_%Z.time.out(tmp)}^/T.INFO["TESTDATE"],
*RAD[mpi]EX[rdtarg]EX[rexarg]AA^aa,
:AA[aa]|3^/T.INFO["MRN"],
:AA[aa]|0^/T.INFO["ACCT"],
*RAD[mpi]EX[rdtarg]EX[rexarg]EX.ID%2^/T.INFO["TESTNAME"],
"RAD"^/T.INFO["TESTTYPE"],
"Header"^/T.INFO["HEADERSECTION"],
"Results"^/T.INFO["RPTSECTION"],
IF{[RAD.MAIN,"ST"]="S" "Signed";
   [RAD.MAIN,"ST"]="D" "Draft";
   [RAD.MAIN,"ST"]="H" "On Hold";
   [RAD.MAIN,"ST"]="F" "Final";
   [RAD.MAIN,"ST"]="C" "Cancelled";
   [RAD.MAIN,"ST"]}^/T.INFO["STATUS"]^STATUS,
"STATUS: "_STATUS^/T.HEADER[THDR+1^THDR],
" "^/T.HEADER[THDR+1^THDR],
@EXAMS,
" "^/T.HEADER[THDR+1^THDR],
" "^/T.HEADER[THDR+1^THDR],
IF{*RAD.RPT[rpt]ES 1,
   "SIGNED: "_(*RAD.RPT[rpt]ES|1)_"   "_*RAD.RPT[rpt]ES|0^/T.HEADER[THDR+1^THDR]}

EXAMS
"Exam      Procedure"^/T.HEADER[THDR+1^THDR],
""^rdt^rex,
DO{+[RAD.MAIN,"EX",rdt,rex]^rex "got an exam",
      *RAD[mpi]EX[rdt]EX[rex]EX.ID%2^exid,
      *RAD[mpi]EX[rdt]EX[rex]EXN^en,
      *RAD[mpi]EX[rdt]EX[rex]TYP^typ,
      (en:9TL,typ_"/"_exid:31TL)^/T.HEADER[THDR+1^THDR];
   +[RAD.MAIN,"EX",rdt]^rdt}

REPORT.LINES
IF{+[RAD.MAIN,"ADD",""]_.=.;
   " "^/T.REC[T+1^T],
   "**AN ADDENDUM TO THIS REPORT EXISTS**"^/T.REC[T+1^T],
   " "^/T.REC[T+1^T]},
" "^/T.REC[T+1^T],
IF{O(?S,*RAD.RPT[rpt])^ftyp'=1'=0 C(?U);
   ^/RAD.TEXT[rpt]^atype,^?RAD.TEXT[rpt]^etype,
   K(/RAD.TEXT[rpt]),
   ftyp=0 IF{+?_.=.;
             ""^tlc,
             DO{+? tlc+1^tlc,IF{?^sl#0P=D(1) sl|2^q,IF{sl|1;" "};sl;" "}^[atype]T[tlc],
                   IF{'q D(5):L([atype]T[tlc])^[atype]T[tlc]A|0;
                      ""^a,DO{U(q)^x[1],U(q)^x[2],
                              x[1]&x[2] IF{x[1]=5 a_(D(5):x[2])^a;
                                           a_(D(24):x[2])^a}},
                      a^[atype]T[tlc]A|0},
                   IF{1=sl|3 1^[atype]T[tlc]A|5}},
             ""^x[1]^x[2]^q^sl^a^tlc},C(?U);
   M([etype],[atype]),
   [etype]^[atype],C(?U)},
@MOVE.RPT.TO.TREC,
@ADDENDUM

MOVE.RPT.TO.TREC
^/RAD.TEXT[rpt]T^FILE,""^XXX,
DO{+([FILE,XXX],YYY)^XXX 1,
   YYY:0X^/T.REC[T+1^T]}

ADDENDUM
""^add,
DO{+[RAD.MAIN,"ADD",add]^add " "^/T.REC[T+1^T],
                             "**ADDENDUM REPORT**"^/T.REC[T+1^T]," "^/T.REC[T+1^T],
                             IF{*RAD.ADD[add]ST="H" "ADDENDUM IS ON HOLD"^/T.REC[T+1^T];
                                *RAD.ADD[add]ST="C" "ADDENDUM IS CANCELLED"^/T.REC[T+1^T];
                                "Addendum Status: "_(IF{*RAD.ADD[add]ST="D" "Draft";*RAD.ADD[add]="F" "Final";
                                                        *RAD.ADD[add]ST})^/T.REC[T+1^T],
                                @ADD.RESULTS,
                                @ADD.SIGNED.INFO}}

ADD.RESULTS
" "^/T.REC[T+1^T],
IF{O(?S,*RAD.ADD[add])^ftyp'=1'=0 C(?U);
   ^/RAD.TEXT[add]^atype,^?RAD.TEXT[add]^etype,
   K(/RAD.TEXT[add]),
   ftyp=0 IF{+?_.=.;
             ""^tlc,
             DO{+? tlc+1^tlc,IF{?^sl#0P=D(1) sl|2^q,IF{sl|1;" "};sl;" "}^[atype]T[tlc],
                   IF{'q D(5):L([atype]T[tlc])^[atype]T[tlc]A|0;
                      ""^a,DO{U(q)^x[1],U(q)^x[2],
                              x[1]&x[2] IF{x[1]=5 a_(D(5):x[2])^a;
                                           a_(D(24):x[2])^a}},
                      a^[atype]T[tlc]A|0},
                   IF{1=sl|3 1^[atype]T[tlc]A|5}},
             ""^x[1]^x[2]^q^sl^a^tlc},C(?U);
   M([etype],[atype]),
   [etype]^[atype],C(?U)},
^/RAD.TEXT[add]T^FILE,""^XXX,
DO{+([FILE,XXX],YYY)^XXX YYY^/T.REC[T+1^T]}

ADD.SIGNED.INFO
" "^/T.REC[T+1^T],
" "^/T.REC[T+1^T],
IF{*RAD.ADD[add,"DR"]^rdr &RAD.RDR[rdr]ID%2^rpt.by,
                          "Addendum Reported by: "_(rpt.by)^/T.REC[T+1^T]},
IF{*RAD.ADD[add,"ES"]|1^us IF{&RAD.RDR[us]ID;&.US[us]ID}%2^sig.by,
                           "Addendum Signed by: "_(sig.by)^/T.REC[T+1^T]}

SIGNED.INFO
" "^/T.REC[T+1^T],
" "^/T.REC[T+1^T],
IF{[RAD.MAIN,"DR"]^rdr &RAD.RDR[rdr]ID%2^rpt.by,
"Reported by: "_(rpt.by)^/T.REC[T+1^T]},
IF{[RAD.MAIN,"ES"]|1^us IF{&RAD.RDR[us]ID;&.US[us]ID}%2^sig.by,
"Signed by: "_(sig.by)^/T.REC[T+1^T]}
