compile function RDS_RPT3() 
begin condition
pre:
post:
end condition

declare function msg, set_property, send_event, dialog_box, utility, get_property
declare function set_printer, fieldcount, Printer_Select
declare subroutine rlist, or_view, activate_save_select, yield, array_sort
$insert logical
$insert rlist_equates
$insert msg_equates
$insert rds_equ

Params = dialog_box( 'RDS_RPT3', @window, '*CENTER' )
if Params = 'CANCEL' or Params = '' then
    return 0
end
open 'SYSLISTS' to SysListsTable else
    MsgInfo = ''
    MsgInfo<mcol$> = -2
    MsgInfo<mrow$> = -2
    MsgInfo<mtext$> = 'Unable to open SYSLISTS...' 
    Void = msg( '', MsgInfo )
    return 0
end
ListsId = 'RDS_RPT3*':@station
write '' on SysListsTable, ListsId else
    MsgInfo = ''
    MsgInfo<mcol$> = -2
    MsgInfo<mrow$> = -2
    MsgInfo<mtext$> = 'Unable to clear SYSLISTS ':ListsId:'...' 
    Void = msg( '', MsgInfo )
    return 0
end
convert '*' to @fm in Params
Recipe       = Params<1>
Reactor      = Params<2>
Offsets      = Params<3>
Injectors    = Params<4>
DateInFrom   = Params<5>
DateInThru   = Params<6>
DirToPrinter = Params<7>

open 'DICT.RDS' to @dict else
    MsgInfo = ''
    MsgInfo<mcol$> = -2
    MsgInfo<mrow$> = -2
    MsgInfo<mtext$> = 'Unable to open DICT.RDS' 
    Void = msg( '', MsgInfo )
    return 0
end
open 'RDS' to RDSTable else
    MsgInfo = ''
    MsgInfo<mcol$> = -2
    MsgInfo<mrow$> = -2
    MsgInfo<mtext$> = 'Unable to open RDS' 
    Void = msg( '', MsgInfo )
    return 0
end
Stat = utility( 'CURSOR', 'H' )
Stmt = 'SELECT RDS'
* all params are required
Stmt:= ' WITH DATE_IN FROM ':quote(DateInFrom):' TO ':quote(DateInThru)
Stmt:= ' AND WITH REACTOR = ':Reactor
if Recipe <> '' then
   Stmt:= ' AND WITH RECIPE_NO = ':Recipe
end

rlist( Stmt, target_savelist$, ListsId, '', '' )
activate_save_select( ListsId )

if Offsets then
   HeadStart = 'Offset '
   ColStart  = 'Offsets..........'
end else
   HeadStart = 'Injector '
   ColStart  = 'Injectors........'
end
CustArray = ''
ValArray  = ''
CntArray  = ''
if @reccount then
   gosub SetUpPrinter
   Eof = false$
   loop
      readnext @id else Eof = true$
    until Eof
      read @record from RDSTable, @id else
    	  MsgInfo = ''
	      MsgInfo<mcol$> = -2
	      MsgInfo<mrow$> = -2
          MsgInfo<mtext$> = 'Unable to read record id ':@id:' from RDS' 
          Void = msg( '', MsgInfo )
          return 0
      end
      if Offsets then
         ValToUse = {ALL_OFFSETS}
      end else
         ValToUse = @record<rds_injectors$>
         if ValToUse = '' then
            ValToUse = 'N/A'
         end
      end
      CustId = @record<rds_cust_no$>
      locate CustId in CustArray using @fm setting CPos then
         * do nothing this customer found
      end else
         CustArray = insert( CustArray, -1, 0, 0, CustId )
      end
      locate ValToUse in ValArray<CPos> using @vm setting VPos then
         CntArray<CPos,VPos> = CntArray<CPos,VPos> + 1   
      end else
         ValArray = insert( ValArray, CPos, -1, 0, ValToUse )
         CntArray = insert( CntArray, CPos, -1, 0, 1 )
      end
   repeat
   * now loop through the custarray
   CustCnt = fieldcount( CustArray, @fm )
   for i = 1 to CustCnt
      ThisCust = oconv( CustArray<i>, '[XLATE_CONV,COMPANY*CO_NAME]' )
      TheseVals = ValArray<i>
      TheseCnts = CntArray<i>
      ArraysToPass = TheseCnts:@fm:TheseVals
      array_sort( ArraysToPass, 'D', 'R' )
      Vcnt = fieldcount( TheseVals, @vm )
      for j = 1 to Vcnt
         PrintLine = ''
         PrintLine:= ThisCust:@vm
         PrintLine:= fmt( ArraysToPass<2,j>, 'R#17' ):@vm ;* these are the vals
         PrintLine:= fmt( ArraysToPass<1,j>, 'R#8' )      ;* these are the cnts
         Statx = set_printer( 'TEXTCOL', PrintLine )
      next j      
   next i
   Stat = utility( 'CURSOR', 'A' )   
   Void = set_printer( 'TERM', 1 )
end else
   MsgInfo = ''
   MsgInfo<mcol$>  = -2
   MsgInfo<mrow$>  = -2
   MsgInfo<mtext$> = 'NO Records meeting your date criteria!!'
   MsgInfo<micon$> = '!'
   Void = msg( '', MsgInfo )
end
write '' on SysListsTable, ListsId else
    MsgInfo = ''
    MsgInfo<mcol$>  = -2
    MsgInfo<mrow$>  = -2
    MsgInfo<mtext$> = 'Unable to clear SYSLISTS ':ListsId:'...'
    Void = msg( '', MsgInfo )
    return 0
end
return 0

*============================================================================*
SetUpPrinter:
    if DirToPrinter = 'Yes' then
        OutputParam = 0
    end else
        OutputParam = 3 ;* maximized print preview
    end 
    PrintPath = Printer_Select('',1)		;* Get default printer path
    Void = set_printer( 'INIT', '', '', .31:@fm:.75:@fm:.25:@fm:.5, 0, OutputParam,PrintPath )             
    
	TheHeader = HeadStart:' report for  reactor# ':Reactor:' '
	if Recipe <> '' then
	   TheHeader:= 'Recipe#':Recipe:' '
	end
   TheHeader:= "from ":DateInFrom:' - ':DateInThru:"      Page # 'P'"	
	HeadPos   = '3,2,1'
	convert ',' to @fm in HeadPos
	ColHeader = "Customer.........................":@vm:ColStart:@vm:"Total..."
	Void = set_printer( 'FONTHEADFOOT' )
	Void = set_printer( 'HEADER', TheHeader, HeadPos, ColHeader )
return
*============================================================================*


