COMPILE SUBROUTINE REPORT_GM(Dummy)

* General Manager Report - goes straight to Excel

* 9/20/2006 John C. Henry, J.C. Henry & Co.  Initial coding
* 2/13/2009 John C. Henry, J.C. Henry & Co.  Changed to use the WO_MAT table rather than the RDS table.


DECLARE SUBROUTINE ErrMsg, Btree.Extract, Set_Status, SetInitDirOptions, MAKE.LIST
DECLARE FUNCTION Msg, Get_Status, obj_Calendar, Utility, SetInitDirOptions, Dialog_Box, Environment_Services

$INSERT RDS_EQU
$INSERT WO_MAT_EQUATES
$INSERT WM_OUT_EQUATES
$INSERT COMPANY_EQU
$INSERT MSG_EQUATES

EQU ROW$CAPTIVE_QTD			TO 2
EQU ROW$MERCHANT_QTD		TO 3
EQU ROW$CAPTIVE_FAB2		TO 5
EQU ROW$CAPTIVE_FAB3		TO 6
EQU ROW$CAPTIVE_FAB10		TO 7
EQU ROW$CAPTIVE_FAB11		TO 8
EQU ROW$CAPTIVE_FOUNDRY		TO 9

EQU ROW$MERCHANT_BL			TO 11
EQU ROW$MERCHANT_BLANKET	TO 12

EQU COL$WAFERS_IN			TO 2
EQU COL$CUST_PRE_EPI_REJ	TO 3
EQU COL$ACTUAL_STARTS		TO 4
EQU COL$CUST_POST_EPI_REJ	TO 5
EQU COL$FAB_PRE_EPI_REJ		TO 6
EQU COL$FAB_POST_EPI_REJ	TO 7
EQU COL$PROD_TEST			TO 8
EQU COL$ACTUAL_OUTS			TO 9

EQU COL$PERIOD				TO 11
EQU COL$PERIOD_VALUE		TO 12
EQU COL$PERIOD_START		TO 13
EQU COL$PERIOD_END			TO 14

EQU CRLF$ TO \0D0A\

equ xlTrue						to -1
equ xlFalse						to 0
equ xlSheetVisible 				to -1
equ xlRangeAutoFormatSimple		to -4154
equ xlRangeAutoFormatClassic1	to 1
equ xlCSV						to 6
equ xlTextMSDOS					to 21
equ xlCSVWindows				to 23


eXcelCols = 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,AA,BB,CC,DD,EE,FF,GG,HH,II,JJ,KK,LL'
SWAP "','" WITH ',' IN eXcelCols
CONVERT ',' TO @VM IN eXcelCols


NoteText  = 'Select any date in the Fiscal Week of interest.':CRLF$:CRLF$
NoteText := 'The report will calculate the Quarter and Week '
NoteText := 'beginning and ending dates and report on the QTD '
NoteText := 'and the full (Mon - Sun) week the date was in.'

DateLabel = 'Date in Fiscal Week:'

CutOffDt = Dialog_Box("DIALOG_DATE",@WINDOW,NoteText:@FM:DateLabel)
thisCutOffDt = ICONV(CutOffDt,'D4/')
IF thisCutOffDt = '' THEN
	ErrMsg('Invalid Date Entered.')
	RETURN
END

CutOffDt = OCONV(thisCutOffDt,'D4/')

IF CutOffDt = '' THEN RETURN

FWData = obj_Calendar('IRFiscalWeek',CutOffDt)

FiscalWeek	= FWData[1,@FM]
FWStartDt	= FWData[COL2()+1,@FM]
FWEndDt		= FWData[COL2()+1,@FM]

FQData = obj_Calendar('IRFiscalQuarter',CutOffDt)

FiscalQuarter	= FQData<1>
FQStartDt		= FQData<2>
FQEndDt			= FQData<3>

RowTitles = ''
RowTitles<1,ROW$CAPTIVE_QTD>		= 'Captive QTD:'
RowTitles<1,ROW$MERCHANT_QTD>		= 'Merchant QTD:'
RowTitles<1,ROW$CAPTIVE_FAB2>		= 'Captive (Fab2)'
RowTitles<1,ROW$CAPTIVE_FAB3>		= 'Captive (Fab3)'
RowTitles<1,ROW$CAPTIVE_FAB10>		= 'Captive (Fab 10)'
RowTitles<1,ROW$CAPTIVE_FAB11>		= 'Captive (Fab 11)'
RowTitles<1,ROW$CAPTIVE_FOUNDRY>	= 'Captive (Foundry)'
RowTitles<1,ROW$MERCHANT_BL>		= 'Merchant B/L'
RowTitles<1,ROW$MERCHANT_BLANKET>	= 'Merchant Blanket'


DataArray = RowTitles
DataArray<COL$WAFERS_IN,1>			= 'Wafers In'
DataArray<COL$CUST_PRE_EPI_REJ,1>	= 'Customer Pre-Epi Rejects'
DataArray<COL$ACTUAL_STARTS,1>		= 'Actual Starts'
DataArray<COL$CUST_POST_EPI_REJ,1>	= 'Customer Post-Epi Rejects'
DataArray<COL$FAB_PRE_EPI_REJ,1>	= 'IREPI Pre-Epi Rejects'
DataArray<COL$FAB_POST_EPI_REJ,1>	= 'IREPI Post-Epi Rejects'
DataArray<COL$PROD_TEST,1>			= 'Product Test'

DataArray<COL$ACTUAL_OUTS,1>		= 'Actual Outs'
DataArray<COL$PERIOD,1>				= 'Fiscal Period'
DataArray<COL$PERIOD_VALUE,1>		= ''
DataArray<COL$PERIOD_START,1>		= 'Start Date'
DataArray<COL$PERIOD_END,1>			= 'End Date'

DataArray<COL$PERIOD,2>				= 'Week'
DataArray<COL$PERIOD_VALUE,2>		= FiscalWeek
DataArray<COL$PERIOD_START,2>		= FWStartDt
DataArray<COL$PERIOD_END,2>			= FWEndDt


DataArray<COL$PERIOD,3>				= 'Quarter'
DataArray<COL$PERIOD_VALUE,3>		= FiscalQuarter
DataArray<COL$PERIOD_START,3>		= FQStartDt
DataArray<COL$PERIOD_END,3>			= FQEndDt


DataArray<COL$PERIOD,5>				= 'Ran At'
DataArray<COL$PERIOD_VALUE,5>		= ''
DataArray<COL$PERIOD_START,5>		= OCONV(Date(),'D4/')
DataArray<COL$PERIOD_END,5>			= OCONV(Time(),'MTH')




OPEN 'DICT.WM_OUT' TO DictWMout ELSE
	ErrMsg('Unable to open "DICT.WM_OUT" for index lookup')
	RETURN
END

OPEN 'DICT.WO_MAT' TO @DICT ELSE
	ErrMsg('Unable to open "DICT.WO_MAT" for index lookup')
	RETURN
END

OPEN 'WO_MAT' TO WOMatTable ELSE
	ErrMsg('Unable to open "WO_MAT" table')
	RETURN
END

/*

OPEN 'DICT.RDS' TO @DICT ELSE
	ErrMsg('Unable to open "DICT.RDS" for index lookup')
	RETURN
END

OPEN 'RDS' TO RDSTable ELSE
	ErrMsg('Unable to open "RDS" table')
	RETURN
END
	
*/

* Fudge the dates - '~' is not inclusive of the end dates

StartDt = ICONV(FQStartDt,'D') - 1
EndDt 	= ICONV(FWEndDt,'D') + 1

SearchString = 'DATE_OUT':@VM:OConv(StartDt, 'D4/'):'~':OConv(EndDt, 'D4/'):@FM

RDSKeys = ''
Option = ''
Flag = ''

WMOutKeys = ''
SearchString = 'SUP_VER_SIG_DT':@VM:OConv(StartDt, 'D4/'):'~':OConv(EndDt, 'D4/'):@FM
Set_Status(0)
Btree.Extract(SearchString, 'WM_OUT', DictWMOut, WMOutKeys, Option, Flag)
IF Get_Status(errCode) THEN
	ErrMsg(errCode)
END

KeyCnt = COUNT(WMOutKeys,@VM) + (WMOutKeys NE '')
WOMatKeys  = ''
FOR I = 1 TO KeyCnt
	WMOutKey = WMOutKEys<1,I>
	WOMatKeys<1,I> = WMOutKey[1,'*']:'*':WMOutKey[-1,'B*']
NEXT I

SearchString = 'RDS_FINAL_SIG_DT':@VM:OConv(StartDt, 'D4/'):'~':OConv(EndDt, 'D4/'):@FM
Set_Status(0)
Btree.Extract(SearchString, 'WO_MAT', @DICT, SelWOMatKeys, Option, Flag)
IF Get_Status(errCode) THEN
	ErrMsg(errCode)
END

WOMatKeys := @VM:SelWOMatKeys
	
/* 
* Dead 2/13/2009
Def = ""
Def<MTEXT$> = "Selecting RDS records..."
Def<MTYPE$> = "U"
MsgUp = Msg(@window, Def)   ;* display the processing message

Set_Status(0)
Btree.Extract(SearchString, 'RDS', @DICT, RDSKeys, Option, Flag)

Msg(@WINDOW, MsgUp)

IF Get_Status(errCode) THEN
	ErrMsg(errCode)
	RETURN
END

IF RDSKeys = '' THEN
	ErrMsg('No RDS Records found for the current quarter!')
	RETURN
END
*/



WOMatCnt	= COUNT(WOMatKeys,@VM) + (WOMatKeys NE '')
LoopCnt	= 0

Def = ""
Def<MCAPTION$>		= "Building Report..."
Def<MTYPE$>			= "GC"
Def<MEXTENT$>		= WOMatCnt
Def<MTEXTWIDTH$>	= 600

MsgUp = Msg(@WINDOW, Def)

LOOP
	@ID = WOMatKeys[1,@VM]
	WOMatKeys[1,COL2()] = ''
	LoopCnt += 1
	
		READ @RECORD FROM WOMatTable,@ID THEN
	
		WafersIn		= {REP_SCH_QTY}
		
		IF WafersIn = '' OR WafersIn = 0 ELSE
			
			CustPreEpiRej	= {CUST_PRE_EPI_REJ}		;* This is the total for the RDS
			CustPostEpiRej	= {CUST_POST_EPI_REJ}
			FabPreEpiRej	= {LSL_PRE_EPI_REJ}
			FabPostEpiRej	= {LSL_POST_EPI_REJ}
			ProdTest		= {REP_TW_PROD}
			WaferSize		= {WAFER_INCHES}
		
			ActualStarts = WafersIn - CustPreEpiRej - CustPostEpiRej - ProdTest
			ActualOuts	= ActualStarts - FabPreEpiRej - FabPostEpiRej
			
			CustNo = {CUST_NO}
			
			Captive = {CAPTIVE}
			CustReportExclude = XLATE('COMPANY',CustNo,COMPANY_REPORT_EXCLUDE$,'X')
			
			Product = {PRODUCT}
			
			IF @RECORD<WO_MAT_WMO_KEY$> = '' THEN
				FinalSigDTM = @RECORD<WO_MAT_RDS_FINAL_SIG_DTM$>[-1,'B':@VM]
				DateOut = OCONV(FinalSigDTM[1,'.'],'D4/')
			END ELSE
				FinalSigDTM = XLATE('WM_OUT',@RECORD<WO_MAT_WMO_KEY$>,WM_OUT_SUP_VER_SIG_DTM$,'X')
				DateOut = OCONV(FinalSigDTM[1,'.'],'D4/')
			END
			
			

			RDSFiscalWeek = obj_Calendar('IRFiscalWeek',DateOut)<1>	;* 2nd and 3rd fields are the start and end dates of the week
			
			IF NOT(CustReportExclude) THEN
				BEGIN CASE
					CASE Captive AND CustNo = '6593'
						QTDRow = ROW$CAPTIVE_QTD
						WeekRow = ROW$CAPTIVE_FAB2
						
					CASE Captive AND CustNo = '408'
						QTDRow = ROW$CAPTIVE_QTD
						WeekRow = ROW$CAPTIVE_FAB3
									
					CASE Captive AND CustNo = '6874'
						QTDRow = ROW$CAPTIVE_QTD
						
						IF WaferSize[1,' '] = '8' THEN
							WeekRow = ROW$CAPTIVE_FAB11
						END ELSE
							WeekRow = ROW$CAPTIVE_FAB10
						END
						
					CASE Captive
						QTDRow = ROW$CAPTIVE_QTD
						WeekRow = ROW$CAPTIVE_FOUNDRY
					
					CASE NOT(Captive) AND Product = '041-B/L'
						QTDRow = ROW$MERCHANT_QTD
						WeekRow = ROW$MERCHANT_BL
						
					CASE NOT(Captive) AND Product = '044-Blanket'
						QTDRow = ROW$MERCHANT_QTD
						WeekRow = ROW$MERCHANT_BLANKET
						
					CASE 1
						QTDRow = ''
						WeekRow = ''
						
				END CASE
										
				IF QTDRow NE '' THEN
					DataArray<COL$WAFERS_IN,QTDRow>			= DataArray<COL$WAFERS_IN,QTDRow> + WafersIn
					DataArray<COL$CUST_PRE_EPI_REJ,QTDRow>	= DataArray<COL$CUST_PRE_EPI_REJ,QTDRow> + CustPreEpiRej
					DataArray<COL$ACTUAL_STARTS,QTDRow>		= DataArray<COL$ACTUAL_STARTS,QTDRow> + ActualStarts
					DataArray<COL$CUST_POST_EPI_REJ,QTDRow>	= DataArray<COL$CUST_POST_EPI_REJ,QTDRow> + CustPostEpiRej
					DataArray<COL$FAB_PRE_EPI_REJ,QTDRow>	= DataArray<COL$FAB_PRE_EPI_REJ,QTDRow> + FabPreEpiRej
					DataArray<COL$FAB_POST_EPI_REJ,QTDRow>	= DataArray<COL$FAB_POST_EPI_REJ,QTDRow> + FabPostEpiRej
					DataArray<COL$PROD_TEST,QTDRow>			= DataArray<COL$PROD_TEST,QTDRow> + ProdTest
					DataArray<COL$ACTUAL_OUTS,QTDRow>		= DataArray<COL$ACTUAL_OUTS,QTDRow> + ActualOuts
				END
				
				IF WeekRow NE '' AND RDSFiscalWeek = FiscalWeek THEN
					DataArray<COL$WAFERS_IN,WeekRow>			= DataArray<COL$WAFERS_IN,WeekRow> + WafersIn
					DataArray<COL$CUST_PRE_EPI_REJ,WeekRow>		= DataArray<COL$CUST_PRE_EPI_REJ,WeekRow> + CustPreEpiRej
					DataArray<COL$ACTUAL_STARTS,WeekRow>		= DataArray<COL$ACTUAL_STARTS,WeekRow> + ActualStarts
					DataArray<COL$CUST_POST_EPI_REJ,WeekRow>	= DataArray<COL$CUST_POST_EPI_REJ,WeekRow> + CustPostEpiRej
					DataArray<COL$FAB_PRE_EPI_REJ,WeekRow>		= DataArray<COL$FAB_PRE_EPI_REJ,WeekRow> + FabPreEpiRej
					DataArray<COL$FAB_POST_EPI_REJ,WeekRow>		= DataArray<COL$FAB_POST_EPI_REJ,WeekRow> + FabPostEpiRej
					DataArray<COL$PROD_TEST,WeekRow>			= DataArray<COL$PROD_TEST,WeekRow> + ProdTest
					DataArray<COL$ACTUAL_OUTS,WeekRow>			= DataArray<COL$ACTUAL_OUTS,WeekRow> + ActualOuts
				END					
				
			END	;* End of check for Report Exclude flag on customer
		END	;* End of check for WafersIn
		
		
	
	END	;* End of WO_Mat Record read
	
	Running = Msg(@WINDOW, MsgUp, LoopCnt, MSGINSTUPDATE$) 
	
UNTIL WOMatKeys = '' OR NOT(Running)
REPEAT


/*

* Dead on 2/13/2009 JCH


	RDSCnt	= COUNT(RDSKeys,@VM) + (RDSKeys NE '')
	LoopCnt	= 0

	Def = ""
	Def<MCAPTION$>		= "Building Report..."
	Def<MTYPE$>			= "GC"
	Def<MEXTENT$>		= RDSCnt
	Def<MTEXTWIDTH$>	= 600


	MsgUp = Msg(@WINDOW, Def)

	LOOP
		@ID = RDSKeys[1,@VM]
		RDSKeys[1,COL2()] = ''
		LoopCnt += 1
		
		
			READ @RECORD FROM RDSTable,@ID THEN
		
			WafersIn		= {WAFERS_IN}
			
			IF WafersIn = '' OR WafersIn = 0 ELSE
				
				CustPreEpiRej	= {CUST_PRE_EPI_REJ}		;* This is the total for the RDS
				CustPostEpiRej	= {CUST_POST_EPI_REJ}
				FabPreEpiRej	= {LSL_PRE_EPI_REJ}
				FabPostEpiRej	= {LSL_POST_EPI_REJ}
				ProdTest		= {TW_PROD}
				WaferSize		= {WAFER_INCHES}
			
				ActualStarts = WafersIn - CustPreEpiRej - CustPostEpiRej - ProdTest
				ActualOuts	= ActualStarts - FabPreEpiRej - FabPostEpiRej
				
				CustNo = @RECORD<RDS_CUST_NO$>
				
				Captive = {CUST_CAPTIVE}
				CustReportExclude = XLATE('COMPANY',CustNo,COMPANY_REPORT_EXCLUDE$,'X')
				
				Product = {PRODUCT}
				DateOut = OCONV({DATE_OUT},'D4/')
				
				
		
		
				RDSFiscalWeek = obj_Calendar('IRFiscalWeek',DateOut)<1>	;* 2nd and 3rd fields are the start and end dates of the week
				
				IF NOT(CustReportExclude) THEN
					BEGIN CASE
						CASE Captive AND CustNo = '6593'
							QTDRow = ROW$CAPTIVE_QTD
							WeekRow = ROW$CAPTIVE_FAB2
							
						CASE Captive AND CustNo = '408'
							QTDRow = ROW$CAPTIVE_QTD
							WeekRow = ROW$CAPTIVE_FAB3
										
						CASE Captive AND CustNo = '6874'
							QTDRow = ROW$CAPTIVE_QTD
							
							IF WaferSize[1,' '] = '8' THEN
								WeekRow = ROW$CAPTIVE_FAB11
							END ELSE
								WeekRow = ROW$CAPTIVE_FAB10
							END
							
						CASE Captive
							QTDRow = ROW$CAPTIVE_QTD
							WeekRow = ROW$CAPTIVE_FOUNDRY
						
						CASE NOT(Captive) AND Product = '041-B/L'
							QTDRow = ROW$MERCHANT_QTD
							WeekRow = ROW$MERCHANT_BL
							
						CASE NOT(Captive) AND Product = '044-Blanket'
							QTDRow = ROW$MERCHANT_QTD
							WeekRow = ROW$MERCHANT_BLANKET
							
						CASE 1
							QTDRow = ''
							WeekRow = ''
							
					END CASE
											
					IF QTDRow NE '' THEN
						DataArray<COL$WAFERS_IN,QTDRow>			= DataArray<COL$WAFERS_IN,QTDRow> + WafersIn
						DataArray<COL$CUST_PRE_EPI_REJ,QTDRow>	= DataArray<COL$CUST_PRE_EPI_REJ,QTDRow> + CustPreEpiRej
						DataArray<COL$ACTUAL_STARTS,QTDRow>		= DataArray<COL$ACTUAL_STARTS,QTDRow> + ActualStarts
						DataArray<COL$CUST_POST_EPI_REJ,QTDRow>	= DataArray<COL$CUST_POST_EPI_REJ,QTDRow> + CustPostEpiRej
						DataArray<COL$FAB_PRE_EPI_REJ,QTDRow>	= DataArray<COL$FAB_PRE_EPI_REJ,QTDRow> + FabPreEpiRej
						DataArray<COL$FAB_POST_EPI_REJ,QTDRow>	= DataArray<COL$FAB_POST_EPI_REJ,QTDRow> + FabPostEpiRej
						DataArray<COL$PROD_TEST,QTDRow>			= DataArray<COL$PROD_TEST,QTDRow> + ProdTest
						DataArray<COL$ACTUAL_OUTS,QTDRow>		= DataArray<COL$ACTUAL_OUTS,QTDRow> + ActualOuts
					END
					
					IF WeekRow NE '' AND RDSFiscalWeek = FiscalWeek THEN
						DataArray<COL$WAFERS_IN,WeekRow>			= DataArray<COL$WAFERS_IN,WeekRow> + WafersIn
						DataArray<COL$CUST_PRE_EPI_REJ,WeekRow>		= DataArray<COL$CUST_PRE_EPI_REJ,WeekRow> + CustPreEpiRej
						DataArray<COL$ACTUAL_STARTS,WeekRow>		= DataArray<COL$ACTUAL_STARTS,WeekRow> + ActualStarts
						DataArray<COL$CUST_POST_EPI_REJ,WeekRow>	= DataArray<COL$CUST_POST_EPI_REJ,WeekRow> + CustPostEpiRej
						DataArray<COL$FAB_PRE_EPI_REJ,WeekRow>		= DataArray<COL$FAB_PRE_EPI_REJ,WeekRow> + FabPreEpiRej
						DataArray<COL$FAB_POST_EPI_REJ,WeekRow>		= DataArray<COL$FAB_POST_EPI_REJ,WeekRow> + FabPostEpiRej
						DataArray<COL$PROD_TEST,WeekRow>			= DataArray<COL$PROD_TEST,WeekRow> + ProdTest
						DataArray<COL$ACTUAL_OUTS,WeekRow>			= DataArray<COL$ACTUAL_OUTS,WeekRow> + ActualOuts
					END					
					
				END	;* End of check for Report Exclude flag on customer
			END	;* End of check for WafersIn
			
			
		
		END	;* End of RDS Record read
		
		Running = Msg(@WINDOW, MsgUp, LoopCnt, MSGINSTUPDATE$) 
		
	UNTIL RDSKeys = '' OR NOT(Running)
	REPEAT
*/




Msg(@WINDOW,MsgUp)

xlApp = OleCreateInstance("excel.Application")
OlePutProperty(XlApp, 'Visible', xlSheetVisible)
if OleStatus() then Goto HadError


xlWorkBooks =OleGetProperty(xlApp, "Workbooks")
xlWkb = OleCallMethod(xlWorkbooks,"Add")
if OleStatus() then Goto HadError


xlSht = OleGetProperty(xlWkb, "Worksheets",1)
if OleStatus() then Goto HadError

ColCnt = COUNT(DataArray,@FM) + (DataArray NE '')
LineCnt = COUNT(DataArray<1>,@VM) + (DataArray<1> NE '')
FOR LineNo = 1 TO LineCnt
	FOR Column = 1 to ColCnt
		RangeColumn = eXcelCols<1,Column>
		range = OleGetProperty( xlSht, 'Range',RangeColumn:LineNo)
		OlePutProperty(range, 'Value', DataArray<Column,LineNo>)
		IF OleStatus() THEN GOTO HadError
	NEXT Column
	
NEXT LineCnt


range = OleGetProperty( xlSht, 'Range','B1:':RangeColumn:'1')
OlePutProperty( range, 'WrapText', xlTrue)
IF OleStatus() THEN GOTO HadError

RangeColumn = eXcelCols<1,ColCnt>
range = OleGetProperty( xlSht, 'Range','A1:':RangeColumn:LineCnt)

Void = OleCallMethod( range, 'AutoFormat', xlRangeAutoFormatClassic1 )

* Void = OleCallMethod( xlWkb, 'SaveAs','C:\OIReports\GM Report1.CSV', xlCSVWindows )
Void = OleCallMethod( xlWkb, 'SaveAs',Environment_Services('GetReportsRootPath') : '\GM Report1.CSV', xlCSVWindows )
IF OleStatus() THEN GOTO HadError

RETURN

* * * * * * *
HadError:
* * * * * * *
debug
xlChart = ''
Charts = ''
range = ''
xlSht = ''
xlWkb=''
xlWorkBooks = ''
x = OleCallMethod(xlApp, 'Quit')

RETURN 

/* end of code */





