compile function SCHED_RPT4() 
begin condition
pre:
post:
end condition
declare function getprinterlist, getprofilestring, writeprofilestring, extdevicemode
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
$insert logical
$insert rlist_equates
$insert msg_equates
$insert schedule_equ
$insert schedule_srl_equ

Params = dialog_box( 'SCHED_RPT4', @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 = 'SCHED_RPT4*':@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
SSFrom       = iconv( Params<1>, 'D' )
SSThru       = iconv( Params<2>, 'D' )
DirToPrinter = Params<3>
Custs        = Params<4>
POs          = Params<5>
WOs          = Params<6>
LotNums      = Params<7>
PartNums     = Params<8>
SchedTable = 'SCHEDULE'

open 'DICT.':SchedTable to @dict else
    MsgInfo = ''
    MsgInfo<mcol$>  = -2
    MsgInfo<mrow$>  = -2
    MsgInfo<mtext$> = 'Unable to open DICT.':SchedTable 
    Void = msg( '', MsgInfo )
    return 0
end
open SchedTable to SchedTableHandle else
    MsgInfo = ''
    MsgInfo<mcol$>  = -2
    MsgInfo<mrow$>  = -2
    MsgInfo<mtext$> = 'Unable to open ':SchedTable
    Void = msg( '', MsgInfo )
    return 0
end
Stat = utility( 'CURSOR', 'H' )
Stmt = 'SELECT ':SchedTable:' BY CUST_CITY BY WO '
if SchedTable = 'SCHEDULE' then
    Stmt := 'BY LOT_NUM '
end 
Stmt:= 'WITH STATUS = "O" '
if Custs then
    swap @vm with "' '" in Custs
    Custs = "'":Custs:"'"
    Stmt := ' AND WITH CUST_NO = ':Custs
end
if POs then
    swap @vm with "' '" in POs
    POs = "'":POs:"'"
    Stmt := ' AND WITH PO = ':POs
end
if WOs then
    swap @vm with "' '" in WOs
    WOs = "'":WOs:"'"
    Stmt := ' AND WITH WO = ':WOs
end
if LotNums then
    swap @vm with "' '" in LotNums
    LotNums = "'":LotNums:"'"
    Stmt := ' AND WITH LOT_NUM = ':LotNums
end
if PartNums then
    swap @vm with "' '" in PartNums
    PartNums = "'":PartNums:"'"
    Stmt := ' AND WITH PART_NUM = ':PartNums
end

rlist( Stmt, target_savelist$, ListsId, '', '' )
activate_save_select( ListsId )
*debug
if @reccount then
   if SchedTable = 'SCHEDULE' then
      * set up equates for M&S
	  ESchedQty   = schedule_sched_qty$
	  ESchedDate  = schedule_sched_date$
	  EPo         = schedule_po$
	  EWo         = schedule_wo$
	  ELotNum     = schedule_lot_num$
	  EUnprocessed= schedule_unprocessed$
	  EPartNum    = schedule_part_num$
	  ERejects    = schedule_rejects$
   end else
      * set up equates for SRL
	  ESchedQty   = schedule_srl_sched_qty$
	  ESchedDate  = schedule_srl_sched_date$
	  EPo         = schedule_srl_po$
	  EWo         = schedule_srl_wo$
	  ELotNum     = 99 ;* there is no lot num in schedule srl so set to 99 for null
	  EUnprocessed= schedule_srl_unprocessed$
	  EPartNum    = 99 ;* there is no part num in schedule srl set to 99 for null
	  ERejects    = schedule_srl_rejects$
   end
   Eof = false$
   PrintedOne = false$
   loop
      readnext @id else Eof = true$
    until Eof
      read @record from SchedTableHandle, @id else
          MsgInfo = ''
          MsgInfo<mcol$>  = -2
          MsgInfo<mrow$>  = -2
          MsgInfo<mtext$> = 'Unable to read record id ':@id:' from ':SchedTable
          Void = msg( '', MsgInfo )
          return 0
      end
      TotNotShipQty = ( {TOTAL_NOT_SHIP_QTY} - ( sum( @record<EUnprocessed> ) ) )      
      if TotNotShipQty > 0 then
          TotShipQty = {TOTAL_SHIP_QTY} + sum( @record<ERejects> )
          CustCity   = {CUST_CITY}
          * could be included in the report if the dates are inclusive
          SchedQty  = @record<ESchedQty>
          SchedDate = @record<ESchedDate>
          USchedQty  = '' ;* unshipped Scheduled Quantities
          USchedDate = '' ;* unshipped Scheduled Dates
          SQCnt = fieldcount( SchedQty, @vm )
          AllAccountedFor = false$
          AccountedWafers = 0
          for i = 1 to SQCnt
              ThisSchedQty = SchedQty<1,i>
              SumNextLine = AccountedWafers + ThisSchedQty
              AccountedWafers += ThisSchedQty
              if SumNextLine > TotShipQty then
                  USchedQty = SumNextLine - TotShipQty
                  USchedDate = SchedDate<1,i>
                  USchedQty := @vm:field( SchedQty, @vm, i+1, 9999 )
                  USchedDate := @vm:field( SchedDate, @vm, i+1, 9999 ) 
                  AllAccountedFor = true$
              end else
                 if SumNextLine = TotShipQty then
                     AllAccountedFor = true$
                     USchedQty  = field( SchedQty, @vm, i+1, 9999 )
                     USchedDate = field( SchedDate, @vm, i+1, 9999 )
                     AllAccountedFor = true$                      
                 end
              end
            Until AllAccountedFor
          next i
          if USchedQty[-1,1] = @vm then USchedQty[-1,1] = ''
          if USchedDate[-1,1] = @vm then USchedDate[-1,1] = ''
          USCnt = fieldcount( USchedDate, @vm )
          for i = 1 to USCnt
              ThisUSchedDate = USchedDate<1,i>
              if ThisUSchedDate >= SSFrom and ThisUSchedDate <= SSThru then
                  * print it
                  if PrintedOne else
                      gosub SetUpPrinter
                  end
                  PrintLine = ""
			      PrintLine := @id:@vm
				  PrintLine := CustCity:@vm
				  PrintLine := @record<EPo>:@vm
				  PrintLine := @record<EWo>:@vm
				  PrintLine := @record<EPartNum>:@vm
				  PrintLine := @record<ELotNum>:@vm
				  PrintLine := fmt( oconv( USchedQty<1,i>, 'MD0,' ), 'R#9' ):@vm
				  PrintLine := oconv( ThisUSchedDate, 'D2/' ) 
				  Statx = set_printer( 'TEXTCOL', PrintLine )
              end 
          next i
      end
   repeat
   if PrintedOne then
       Void = set_printer( 'TERM', 1 )
   end else
       Void = set_printer( 'TERM', 1 )   
       MsgInfo = ''
       MsgInfo<mcol$>  = -2
       MsgInfo<mrow$>  = -2
       MsgInfo<mtext$> = 'There were no unshipped wafers in your date range.' 
       MsgInfo<micon$> = '!'
       Void = msg( '', MsgInfo )
   end
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:
    Stat = utility( 'CURSOR', 'A' )
    if DirToPrinter = 'Yes' then
        OutputParam = 0
    end else
        OutputParam = 3 ;* maximized print preview
    end 
    *Void = set_printer( 'INIT', '', '', '':@fm:1.25, 0, OutputParam )
    
    PrintPath = Printer_Select('',1)	;* Gets default printer path
   
    Void = set_printer( 'INIT', '', '', .31:@fm:.5:@fm:.25:@fm:.5, 1, OutputParam ,PrintPath) 
                
	TheHeader = " Customer Wafer Shipment Request report from ":oconv( SSFrom, 'D2/' ):' - ':oconv( SSThru, 'D2/' ):" by Customer by Work Order    Page # 'P'"
	**** for Selected Dates From (":oconv( FromDate, 'D2/' ):") Thru (":oconv( ThruDate, 'D2/' ):")"
	HeadPos   = '.5,3,1.25,1.25,1.25,1.25,1,1'
	convert ',' to @fm in HeadPos
	ColHeader = "Sched.":@vm:"Customer/City....................." : @vm : "PO..........." : @vm : "WO..........." : @vm : "Part Num.........":@vm:"Lot Num......" : @vm : "Qty............" : @vm : "Req Ship"
	***ColHeader<-1> = str( '-', 10 ):@vm:str( '-', 25 ):@vm:str( '-', 9 ):@vm:str( '-', 9 ):@vm:str( '-', 9 ):@vm:str( '-', 6 ):@vm:str( '-', 9 )
	Void = set_printer( 'FONTHEADFOOT' )
	Void = set_printer( 'HEADER', TheHeader, HeadPos, ColHeader )
	PrintedOne = true$
return
*============================================================================*


