COMPILE SUBROUTINE Print_Manifest(WONo, CassNo)
#pragma precomp SRP_PreCompiler

/*
	Print Manifest Report
	08/27/2021 - Daniel Stieber - Adapted from PRINT_DEMAND subroutine.
*/

DECLARE SUBROUTINE Utility, ErrMsg, Set_Status, Set_Printer, RList, SRP_Stopwatch
DECLARE FUNCTION   Set_Printer, Get_Printer, Msg, Get_Status, Printer_Select, obj_Install, Dialog_Box, obj_Location

$INSERT OIPRINT_EQUATES
$INSERT WO_LOG_EQUATES
$INSERT APPCOLORS
$INSERT MSG_EQUATES
$INSERT LOGICAL
$INSERT SCHEDULE_EQU
$INSERT WO_WFR_EQUATES

EQU TAB$				TO \09\
EQU TARGET_ACTIVELIST$	TO 5

Main: 
    ErrorTitle  =  'Error in stored procedure Print_Manifest'
    ErrCode		= ''
    ErrorMsg	= ''

    OPEN 'WO_WFR' TO WOWfrTable ELSE
        ErrorMsg  =  'Unable to open "SCHED_DET" table.'
        ErrMsg(ErrorTitle:@SVM:ErrorMsg)
        RETURN
    END

    OPEN 'DICT.WO_WFR' TO @DICT ELSE
        ErrorMsg = 'Unable to open "DICT.WO_WFR" table.'
        ErrMsg(ErrorTitle:@SVM:ErrorMsg)
        RETURN
    END

    TotalWafers = Xlate('WO_MAT_WFR', WONo:'*':CassNo, 'OUT_WFR_QTY', 'X')
    WOWfrKeys   = Xlate('WO_MAT_WFR', WONo:'*':CassNo, 'OUT_WFR_ID', 'X')

    Swap @VM with @FM in WOWfrKeys
    CALL Make.List(0, WOWfrKeys, WOWfrTable, @DICT)
    
    CustNo      = Xlate('WO_LOG', WONo, 'CUST_NO', 'X')
    CustName    = Xlate('COMPANY', CustNo, 'CO_NAME', 'X')
    PartNo      = Xlate('WO_LOG', WONo, 'EPI_PART_NO', 'X')
    PONo        = Xlate('WO_LOG', WONo, 'PROD_ORD_NO', 'X')
    
    Header    = ''
    Header<1> = 'Work Order # ':WONo:@VM:'Shipping Destination: ':CustName:' - Box ':CassNo:@VM
    Header<2> = @VM:'Part # ':PartNo:@VM
    Header<3> = 'Total Wafers = ':TotalWafers:@VM:'PO # ':PONo:@VM
    
    Set_Status(0)

    * Start of printing process

    FileName		= 'Print Manifest'
    Title			= 'Printing Manifest'

    TopMargin    = 1.5
    BottomMargin = 1.0
    LeftMargin   = 1.0
    RightMargin  = 1.0

    Margins = LeftMargin:@FM:TopMargin:@FM:RightMargin:@FM:BottomMargin

    PageSetup       = '1'	;* LandScape
    PrintSetup      = '2'	;* Preview Normal
    PrintSetup<1,2> = '0'	;* All buttons
    PrintSetup<1,5> = '1'	;* Page Range
    PrintSetup<1,6> = 7		;* full mouse and keyboard support

    PrintPath = ''
    stat      = Set_Printer('INIT',FileName,Title,Margins,PageSetup,PrintSetup,PrintPath)

    IF stat < 0 THEN
        ErrorMsg	= 'Stat = ' : Stat : ', PrintPath = ' : PrintPath
        GOTO OIPrintErr
    end
    DateRange = 'Effective ':OCONV(Date(),'D4')

    Header<-1> = ''

    font	= 'Arial'
    font<2>	= '14'
    font<4>	= '1'		;* Bold

    stat = Set_Printer('FONTHEADFOOT',font)	; IF stat < 0 THEN GOTO OIPrintErr
    stat = Set_Printer('HEADER',Header)		; IF stat < 0 THEN GOTO OIPrintErr

    @RECCOUNT	= 0
    FirstPass	= 1
    LastRecord	= 0
    FirstLine 	= 1
    fontSpacing	= 100

    * Make Column Heading

    ColHead      = ''							; ColFmt = ''
    ColHead<1,1> = 'Wafer ID'					; ColFmt<1,1>	= '+^2500'
    ColHead<1,2> = 'Scribe'                     ; ColFmt<1,2>	= '+^2500'
    ColHead<1,3> = 'Epi Structure'				; ColFmt<1,3>	= '+^2500'
    ColHead<1,4> = 'Grade'						; ColFmt<1,4>	= '+^2500'
    ColHead<1,5> = 'WaferThickness (um)'		; ColFmt<1,5>	= '+^2500'
    ColHead<1,6> = 'Slot'		                ; ColFmt<1,6>	= '+^700'

    * Zero Accumulators For Each Break

    Last.Break   = 1

* * * * * * *
ReadRecord:
* * * * * * *
    
    * Zero Break Flags To False
    
    Break = 0
    
    READNEXT @ID, Which.Value ELSE LastRecord = 1
    
    IF FirstPass AND LastRecord THEN
        GOTO Bail
    END
    
    IF LastRecord THEN GOTO BREAKS
    
    READO @RECORD FROM WOWfrTable,@ID ELSE
        GOTO ReadRecord
    END
    
    @RECCOUNT += 1
    
    * Calculate Value(s) For Column(s)
    
    WaferID   = {GAN_WFR_ID}
    Scribe    = {SCRIBE}
    EpiStruct = {EPI_STRUCTURE}
    Grade     = {GRADE}
    WfrThick  = {SUB_THICK}
    Slot      = {SHIP_SLOT}

    IF FirstPass THEN
        FirstPass = 0
        GOTO DETAIL
    END
    
* * * * * * *
BREAKS:
* * * * * * *

    * Perform Last Record Output If Done
    
    IF LastRecord THEN
        colData = ''
        GOTO Bail
    END
    
    
* * * * * * *
DETAIL:
* * * * * * *
    
    * PRINT DETAIL LINE
    
    COLDATA   = ''
    RowNumber = 1      

    COLDATA<RowNumber,1> = WaferID
    COLDATA<RowNumber,2> = Scribe
    COLDATA<RowNumber,3> = EpiStruct
    COLDATA<RowNumber,4> = Grade
    COLDATA<RowNumber,5> = WfrThick
    COLDATA<RowNumber,6> = Slot
    
    If COLDATA NE '' then GoSub PrintTable
    
    GOTO ReadRecord
    
    
* * * * * * *
Bail:
* * * * * * *
    
    stat = Set_Printer('TERM',1)

RETURN


* * * * * * *
OIPrintErr:
* * * * * * *
    
    ErrMsg(ErrorTitle:@SVM:ErrorMsg)
    ErrMsg(ErrorTitle:@SVM:'Set_Printer returned errorcode ':stat)
    stat = Set_Printer('TERM',1)
    
RETURN


* * * * * *
PrintTable:
* * * * * * 
    
    PageHeight			= Get_Printer('PAGESIZE')<2>
    PrintableHeight		= PageHeight - TopMargin - BottomMargin
    PrinterHeight		= Get_Printer('POS')<2>
    
    stat			= Set_Printer('CALCTABLE',ColFmt:@FM:ColData)
    TableSize		= Get_Printer('CALCTABLE')
    
    
    TableHeight		= TableSize<2>
    
    fontSpacing			= 120
    
    IF ( TableHeight + PrinterHeight >= PrintableHeight ) OR FirstLine THEN
        IF NOT(FirstLine) THEN
            stat = Set_Printer('PAGEBREAK')
        END
        FirstLine = 0
        font<2> = 10
        font<4> = 1		;* Bold
        stat = Set_Printer('FONT',font,'100')
        
        stat = Set_Printer('ADDTABLE',colFmt,colHead,'',LTGREY$,'',0,TB_ALL)
        
        
        font<4> = 0
        stat = Set_Printer('FONT',font,fontSpacing)
        
        stat = Set_Printer('ADDTABLE',colFmt,'',colData,LTGREY$,'',0,7)
        
    END ELSE
        font<2> = 10
        font<4> = 0
        stat = Set_Printer('FONT',font,fontSpacing)
        stat = Set_Printer('ADDTABLE',colFmt,'',colData,LTGREY$,'',1,TB_ALL)
        
    END
    
    
RETURN


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ClearCursors:
    For counter = 0 to 8
        ClearSelect counter
    Next counter
return


