compile function REACTORPM_SRPT1() 
declare function msg, send_event, dialog_box, utility, PrintSetup, Set_Printer
declare subroutine rlist, activate_save_select,  or_view
$insert rlist_equates
$insert msg_equates
$insert rpt_cfg_equ
$INSERT PRINTSETUP_EQUATES

TimeX = Oconv(Time(),'MTS')

CrLf = Char(13):Char(10) 
CurDte = OCONV(Date(),"D2/")

ReportID = "REACTORPM001_SRPTCFG"
ReportControl = "REACTORPM_SRPT1"
TableName = 'REACTOR_PM'
ListsID = 'REACTORPM_SRPT1*':@station

TtlLin1 = ''
TtlLin2 = ''
TtlLin3 = ''
TtlLin4 = ''
TtlLin5 = ''
TtlLin6 = ''

open 'CONFIG' to ConfigTable else
    Void = msg( '', 'Unable to open CONFIG...' )
    return 0
end

Read SRptConfig From ConfigTable,ReportID Else
   Void = msg( '', 'Unable to read ': ReportID:' From CONFIG table!' )
   return 0
End

Params = dialog_box( ReportControl, @window, '*CENTER' )

if Params = 'CANCEL' or Params = '' then
    return 0
end
open 'SYSLISTS' to SysListsTable else
   Void = msg( '', 'Unable to open SYSLISTS...' )
   return 0
end

write '' on SysListsTable, ListsID else
   Void = msg( '', 'Unable to clear SYSLISTS ':ListsID:'...' )
   return 0
end

convert '*' to @fm in Params
BegDunDte      = Params< 1>
EndDunDte      = Params< 2>
BegNxtDte      = Params< 3>
EndNxtDte      = Params< 4>
BegCmpDte      = Params< 5>
EndCmpDte      = Params< 6>
ReactServs     = Params< 7>
Reactors       = Params< 8>
NotCompleted   = Params< 9>
FirstSort      = Params<10>
SecondSort     = Params<11>
ThirdSort      = Params<12>

Void = utility( 'CURSOR', 'H' )

WithStmt = 'SELECT ':TableName:' '

Dte = ''
FldNme = ''
TtlLin = ''
WthSwt = 0

Dte<1> = BegDunDte
Dte<2> = EndDunDte
If Dte<1> > '' Then
   If Dte<2> = '' Then Dte<2> = CurDte
End   
FldNme<1> = 'Last Done:'
FldNme<2> = 'WITH LAST_DONE '
GoSub DateFilter
TtlLin2 = TtlLin

Dte<1> = BegNxtDte
Dte<2> = EndNxtDte
If Dte<1> > '' Then
   If Dte<2> = '' Then Dte<2> = CurDte
End  
FldNme<1> = 'Next Done:'
FldNme<2> = 'WITH NEXT_DONE '
GoSub DateFilter
TtlLin2 := '     ':TtlLin

If NotCompleted Then
   TtlLin = 'Not Completed'
   If WthSwt = 1 Then Stmt := ' AND '
   Stmt := "Complete_Date = '' "
   WthSwt = 1
   TtlLin3 = TtlLin
End Else
   Dte<1> = BegCmpDte
   Dte<2> = EndCmpDte
   If Dte<1> > '' Then
      If Dte<2> = '' Then Dte<2> = CurDte
   End  
   FldNme<1> = 'Complete:'
   FldNme<2> = 'WITH COMPLETE_DATE '
   GoSub DateFilter
   TtlLin3 = TtlLin
End

If ReactServs then
   ReactServsCnt = Count(@vm,ReactServs)
   If ReactServsCnt > 10 Then 
      TtlLin = 'ReactServs: More Than 10 Selected'
   End Else
      TtlLin = ReactServs
      swap @vm with ", " in TtlLin
      TtlLin = 'ReactServs: ':TtlLin
   End      
   swap @vm with "' '" in ReactServs
   ReactServs = "'":ReactServs:"'"
   If WthSwt Then WithStmt := ' AND '
   WithStmt := ' WITH REACT_SERV_ID = ':ReactServs
   WthSwt = 1
end else
   TtlLin = 'ReactServs: ALL'
end
TtlLin4 := TtlLin

If Reactors then
   ReactorsCnt = Count(@vm,Reactors)
   If ReactorsCnt > 10 Then 
      TtlLin = 'Reactors: More Than 10 Selected'
   End Else
      TtlLin = Reactors
      swap @vm with ", " in TtlLin
      TtlLin = 'Reactors: ':TtlLin
   End      
   swap @vm with "' '" in Reactors
   Reactors = "'":Reactors:"'"
   If WthSwt Then WithStmt := ' AND '
   WithStmt := ' WITH REACTOR = ':Reactors
   WthSwt = 1
end else
   TtlLin = 'Reactors: ALL'
end
TtlLin4 := '    ':TtlLin

OrderBy = ''
OrderBy<1,1> = 'BY REACTOR '
OrderBy<1,2> = 'Reactor'
OrderBy<2,1> = 'BY LAST_DONE '
OrderBy<2,2> = 'Last Done Date'
OrderBy<3,1> = 'BY NEXT_DONE '
OrderBy<3,2> = 'Next Done Date'
OrderBy<4,1> = 'BY COMPLETE_DATE '
OrderBy<4,2> = 'Complete Date'
OrderBy<5,1> = 'BY REACT_SERV_ID '
OrderBy<5,2> = 'Reactor Service ID'

OrderString = ''
TtlLin5 = 'Sorted By '
SrtSwt = 0
If FirstSort Then
   OrderString := OrderBy<FirstSort,1>
   TtlLin5 := OrderBy<FirstSort,2>:", "  
   SrtSwt = 1
End
If SecondSort Then
   OrderString := OrderBy<SecondSort,1>
   TtlLin5 := OrderBy<SecondSort,2>:", "  
   SrtSwt = 1
End
If ThirdSort Then
   OrderString := OrderBy<ThirdSort,1>
   TtlLin5 := OrderBy<ThirdSort,2>:", "  
   SrtSwt = 1   
End
If SrtSwt Then TtlLin5[-2,2] = '' 
 
If NotCompleted Then 
   If WthSwt Then WithStmt := ' AND '
   WithStmt := " WITH COMPLETE_DATE = ''"
End

*OsWrite WithStmt To 'C:\WithStmt.Txt'

rlist( WithStmt, target_savelist$, ListsID, '', '' )
activate_save_select( ListsID )

Script = SRptConfig<SlistScript$>

*** Report titles
TitleLine2 = SRptConfig<Title2$ >
TitleLine3 = SRptConfig<Title3$ >
TitleLine4 = SRptConfig<Title4$ >
RptWdt = SRptConfig<Width$>
TtlMid = RptWdt/2
Swap '~CurrDate~' With CurDte In Script

If TitleLine2[1,1] = '~' Then
   Filler = TtlMid - (Len(TtlLin2)/2)
   If Filler < 0 Then Filler = 0
   TtlLin2 = Space(Filler):TtlLin2
   If TtlLin2 Then Swap '~SecondTitleLine~' With TtlLin2 In Script
End
If TitleLine3[1,1] = '~' Then
   Filler = TtlMid - (Len(TtlLin3))/2
   If Filler < 0 Then Filler = 0
   If TtlLin3 Then Swap '~ThirdTitleLine~' With  Space(Filler):TtlLin3 In Script
End
If TitleLine4[1,1] = '~' Then
   Filler = TtlMid - (Len(TtlLin4))/2
   If Filler < 0 Then Filler = 0
   If TtlLin4 Then TtlLin4 = Space(Filler):TtlLin4
   
   Filler = TtlMid - (Len(TtlLin5))/2
   If Filler < 0 Then Filler = 0
   If TtlLin5 Then TtlLin5 = Space(Filler):TtlLin5
   
   TtlLin4 := "'L'":TtlLin5
   
   Swap '~FourthTitleLine~' With  TtlLin4 In Script
End

Swap '~ORDERBY~' With OrderString In Script

OsWrite Script To "C:\SLIST.txt"

* Added after upgrade to OI4.13a to reset default printer in OIPI32 engine.

PrintCount	= PrintSetup(PRN_GET$,'',Printers,DefaultPrinter)
CONVERT ',' TO @FM IN DefaultPrinter
DefPrintPath = DefaultPrinter<1>:@FM:DefaultPrinter<3>

Void = set_printer( 'INIT', '', '', .63:@fm:.63:@fm:.25:@fm:'', 0,'',DefPrintPath )
Void = Set_Printer('TERM')

call slist( Script )
Void = utility( 'CURSOR', 'A' )

write '' on SysListsTable, ListsID else
    Void = msg( '', 'Unable to clear SYSLISTS ':ListsID:'...' )
    return 0
end

RETURN 0

*==============================
DateFilter:
*==============================
Stmt = ''
TtlLin = ''
Begin Case
   Case Dte<1> and Dte<2>
      TtlLin = FldNme<1>:' ':Dte<1>:' To ':Dte<2>
      If WthSwt = 1 Then Stmt := ' AND '
      Stmt     := FldNme<2>:" FROM '": Dte<1>:"' TO '":Dte<2>:"' "
      WthSwt = 1
   Case Dte<2>
      TtlLin = FldNme<1>:' Earliest To ':Dte<2>
      If WthSwt = 1 Then Stmt := ' AND '
      Stmt     := FldNme<2>:" LE '":Dte<2>:"' "
      WthSwt = 1
   Case 1
      TtlLin = FldNme<1>:' ALL '
End Case
WithStmt := Stmt

Return
