COMPILE SUBROUTINE REPORT_THRUPUT(dummy)
#pragma precomp SRP_PreCompiler

* General Manager Report - goes straight to Excel

* 9/23/2006 John C. Henry, J.C. Henry & Co., Inc.  Initial coding

DECLARE SUBROUTINE  ErrMsg, Btree.Extract, Set_Status, SetInitDirOptions, ErrMsg, Logging_Services
DECLARE FUNCTION    Msg, Get_Status, obj_Calendar, Utility, SetInitDirOptions, Dialog_Box, obj_RDS_Test
Declare function    Logging_Services, Environment_Services

$INSERT RDS_EQU
$INSERT COMPANY_EQU
$INSERT REACTOR_EQUATES
$INSERT FISCAL_QTR_EQUATES
$INSERT COC_EQUATES
$INSERT MSG_EQUATES
$INSERT EXCEL_EQU
$INSERT LOGICAL

EQU COMMA$                  TO ','
EQU ROW$TOT_REACTOR_OUTS    TO  2
EQU ROW$TOT_YIELD_OUTS      TO  3
EQU ROW$TOT_CUST_SCRAP      TO  4
EQU ROW$TOT_EPI_SCRAP       TO  5
EQU ROW$TOT_OTHER           TO  6
EQU ROW$TOT_YIELD           TO  7
EQU ROW$TOT_SCRAP_RATE      TO  8
EQU ROW$TOT_SHIP            TO  9
EQU ROW$BLANK_LINE_1        TO 10
EQU ROW$CAP_OUT             TO 11
EQU ROW$CAP_FOUNDRY         TO 12
EQU ROW$CAP_FAB2            TO 13
EQU ROW$CAP_FAB10           TO 14
EQU ROW$CAP_FAB11           TO 15
EQU ROW$CAP_CUST_SCRAP      TO 16
EQU ROW$CAP_EPI_SCRAP       TO 17
EQU ROW$CAP_OTHER           TO 18
EQU ROW$CAP_YIELD           TO 19
EQU ROW$CAP_SCRAP_RATE      TO 20
EQU ROW$CAP_SHIP            TO 21
EQU ROW$BLANK_LINE_2        TO 22
EQU ROW$MER_TOT_OUT         TO 23
EQU ROW$MER_BL_OUT          TO 24
EQU ROW$MER_BLANKET_OUT     TO 25
EQU ROW$MER_CUST_SCRAP      TO 26
EQU ROW$MER_EPI_SCRAP       TO 27
EQU ROW$MER_OTHER           TO 28
EQU ROW$MER_YIELD           TO 29
EQU ROW$MER_SCRAP_RATE      TO 30
EQU ROW$MER_SHIP            TO 31
EQU ROW$BLANK_LINE_3        TO 32
EQU ROW$ASMP_OUT            TO 33
EQU ROW$ASMP_CUST_SCRAP     TO 34
EQU ROW$ASMP_EPI_SCRAP      TO 35
EQU ROW$ASMP_OTHER          TO 36
EQU ROW$ASMP_YIELD          TO 37
EQU ROW$ASMP_SCRAP_RATE     TO 38
EQU ROW$BLANK_LINE_4        TO 39
EQU ROW$ASM_OUT             TO 40
EQU ROW$ASM_CUST_SCRAP      TO 41
EQU ROW$ASM_EPI_SCRAP       TO 42
EQU ROW$ASM_OTHER           TO 43
EQU ROW$ASM_YIELD           TO 44
EQU ROW$ASM_SCRAP_RATE      TO 45
EQU ROW$BLANK_LINE_5        TO 46
EQU ROW$HTR3_OUT            TO 47
EQU ROW$HTR3_CUST_SCRAP     TO 48
EQU ROW$HTR3_EPI_SCRAP      TO 49
EQU ROW$HTR3_OTHER          TO 50
EQU ROW$HTR3_YIELD          TO 51
EQU ROW$HTR3_SCRAP_RATE     TO 52
EQU ROW$BLANK_LINE_6        TO 53
EQU ROW$EPIPRO_OUT          TO 54
EQU ROW$EPIPRO_CUST_SCRAP   TO 55
EQU ROW$EPIPRO_EPI_SCRAP    TO 56
EQU ROW$EPIPRO_OTHER        TO 57
EQU ROW$EPIPRO_YIELD        TO 58
EQU ROW$EPIPRO_SCRAP_RATE   TO 59

EQU COL$MON					TO 2
EQU COL$TUE					TO 3
EQU COL$WED				    TO 4
EQU COL$THU				    TO 5
EQU COL$FRI				    TO 6
EQU COL$SAT				    TO 7
EQU COL$SUN				    TO 8
EQU COL$WTD_ACTUAL		    TO 9
EQU COL$QTD_ACTUAL		    TO 10
EQU COL$QTD_MF_PLAN     	TO 11
EQU COL$QTD_MF_REMAIN       TO 12
EQU COL$ORG_MF_DAILY_PLAN   TO 13
EQU COL$QTD_MF_OVER_UNDER   TO 14

EQU COL$PERIOD				TO 15
EQU COL$PERIOD_VALUE		TO 16
EQU COL$PERIOD_START		TO 17
EQU COL$PERIOD_END			TO 18

EQU CRLF$ TO \0D0A\

Main:

	errCode = ''
	
	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  = 'Enter an ending date for data selection.':CRLF$:CRLF$
	NoteText := 'The report will calculate the Quarter and Week '
	NoteText := 'beginning and ending dates and report on the QTD '
	NoteText := 'through the ending date entered.'
	
	DateLabel = 'Include data through:'
	
	CutOffDt = Dialog_Box("DIALOG_DATE",@WINDOW,NoteText:@FM:DateLabel)
	thisCutOffDt = ICONV(CutOffDt,'D4/')
	IF thisCutOffDt = '' THEN
		RETURN
	END
	
	LogPath     = Environment_Services('GetApplicationRootPath') : '\LogFiles\ThruPut'
	LogDate     = Oconv(Date(), 'D4/')
	LogTime     = Oconv(Time(), 'MTS')
	LogFileName = LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : ' ThruPut Log.csv'
	Headers     = 'RDS' : @FM : 'Work Order' : @FM : 'Date Out' : @FM : 'Time Out' : @FM : 'Wafers'
	objLog      = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, Comma$, Headers, '', False$, True$)
	LoggingDTM  = LogDate : ' ' : LogTime   ; // Logging DTM
	
	CutOffDt = OCONV(thisCutOffDt,'D4/')
	
	IF CutOffDt = '' THEN RETURN
	
	FiscalYear = obj_Calendar('IRFiscalYear',CutOffDt)
	
	FWData = obj_Calendar('IRFiscalWeek',CutOffDt)
	
	FiscalWeek  = FWData[1,@FM]
	FWStartDt   = FWData[COL2()+1,@FM]
	FWEndDt     = FWData[COL2()+1,@FM]
	
	StartDt = ICONV(FWStartDt,'D')
	
	WeekDates = ''
	FOR I = StartDt TO StartDt + 6
		WeekDates<1,-1> = OCONV(I,'D4/')
	NEXT I
	
	FQData = obj_Calendar('IRFiscalQuarter',CutOffDt)
	
	FiscalQuarter   = FQData<1>
	FQStartDt       = FQData<2>
	FQEndDt         = FQData<3>
	
	DataArray = ''
	DataArray<1,ROW$TOT_REACTOR_OUTS>   = 'Reactor Outs'
	DataArray<1,ROW$TOT_YIELD_OUTS>     = '  Yield Outs'
	DataArray<1,ROW$TOT_CUST_SCRAP>     = '  Sup/Cust Scrap'
	DataArray<1,ROW$TOT_EPI_SCRAP>      = '  IFX Scrap'
	DataArray<1,ROW$TOT_OTHER>          = '  Prod Test'
	DataArray<1,ROW$TOT_YIELD>          = '  Yield %'
	DataArray<1,ROW$TOT_SCRAP_RATE>     = '  Scrap/1k'
	DataArray<1,ROW$TOT_SHIP>           = '  Shipped'
	
	DataArray<COL$MON,1>                = 'Monday ':WeekDates<1,1>
	DataArray<COL$TUE,1>                = 'Tuesday ':WeekDates<1,2>
	DataArray<COL$WED,1>                = 'Wednesday ':WeekDates<1,3>
	DataArray<COL$THU,1>                = 'Thursday ':WeekDates<1,4>
	DataArray<COL$FRI,1>                = 'Friday ':WeekDates<1,5>
	DataArray<COL$SAT,1>                = 'Saturday ':WeekDates<1,6>
	DataArray<COL$SUN,1>                = 'Sunday ':WeekDates<1,7>
	DataArray<COL$WTD_ACTUAL,1>         = 'WTD Actual'
	DataArray<COL$QTD_ACTUAL,1>         = 'QTD Actual'
	DataArray<COL$QTD_MF_PLAN,1>    	= 'MF QTY Plan'            ;* dkk 12/30/13
	DataArray<COL$QTD_MF_REMAIN,1>  	= 'MF QTD Remain'          ;* dkk 12/30/13
	DataArray<COL$ORG_MF_DAILY_PLAN,1>  = 'MF Plan Daily'          ;* dkk 12/30/13
	DataArray<COL$QTD_MF_OVER_UNDER,1>  = 'MF QTD   +/-'           ;* dkk 12/30/13
	
	FQRec = XLATE('FISCAL_QTR',FiscalYear:'*':FiscalQuarter,'','X')
	
	TargKeys = ''
	
	FOR I = 1 TO COUNT(FQRec<FISCAL_QTR_THRU_GROUP$>,@VM) + (FQRec<FISCAL_QTR_THRU_GROUP$> NE '')
		TargKey     = FQRec<FISCAL_QTR_THRU_GROUP$,I>:'*':FQRec<FISCAL_QTR_THRU_TARGET$,I>
		TargQty     = OCONV(FQRec<FISCAL_QTR_THRU_QTY$,I>,'MD0,Z')
		TargPcnt    = OCONV(FQRec<FISCAL_QTR_THRU_PCNT$,I>, 'MD2%SZ')
		
		BEGIN CASE
			CASE TargKey = 'TOT*Reactor_Outs'     	  ; DataArray<COL$QTD_MF_PLAN,ROW$TOT_REACTOR_OUTS>  = TargQty
			CASE TargKey = 'TOT*Yield_Outs'             ; DataArray<COL$QTD_MF_PLAN,ROW$TOT_YIELD_OUTS>    = TargQty
			CASE TargKey = 'TOT*Sup/Cust_Scrap'         ; DataArray<COL$QTD_MF_PLAN,ROW$TOT_CUST_SCRAP>    = TargQty
			CASE TargKey = 'TOT*IFX_Scrap'              ; DataArray<COL$QTD_MF_PLAN,ROW$TOT_EPI_SCRAP>     = TargQty
			CASE TargKey = 'TOT*Prod_Test'              ; DataArray<COL$QTD_MF_PLAN,ROW$TOT_OTHER>         = TargQty
			CASE TargKey = 'TOT*Yield'                  ; DataArray<COL$QTD_MF_PLAN,ROW$TOT_YIELD>         = TargPcnt
			CASE TargKey = 'TOT*Shipped'                ; DataArray<COL$QTD_MF_PLAN,ROW$TOT_SHIP>     	 = TargQTY
		END CASE
	NEXT I
	
	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>             = 'QTD As Of'
	DataArray<COL$PERIOD_VALUE,5>       = ''
	DataArray<COL$PERIOD_START,5>       = ''
	DataArray<COL$PERIOD_END,5>         = CutOffDt
	
	DataArray<COL$PERIOD,7>             = 'Ran At'
	DataArray<COL$PERIOD_VALUE,7>       = ''
	DataArray<COL$PERIOD_START,7>       = OCONV(Date(),'D4/')
	DataArray<COL$PERIOD_END,7>         = OCONV(Time(),'MTH')
	
	FQRec           = XLATE('FISCAL_QTR',FiscalYear:'*':FiscalQuarter,'','X')
	QtrStartDt      = FQRec<FISCAL_QTR_START_DT$>
	QtrEndDt        = FQRec<FISCAL_QTR_END_DT$>
	PlanIdleDts     = FQRec<FISCAL_QTR_PLAN_IDLE_DTS$>
	
	IF QtrStartDt = '' THEN
		ErrMsg('Fiscal Quarter ':FiscalYear:'*':FiscalQuarter:' is missing the start date.')
		RETURN
	END
	
	IF QtrEndDt = '' THEN
		ErrMsg('Fiscal Quarter ':FiscalYear:'*':FiscalQuarter:' is missing the end date.')
		RETURN
	END
	
	IF QtrStartDt >= QtrEndDt THEN
		ErrMsg('Fiscal Quarter ':FiscalYear:'*':FiscalQuarter:' has incorrect Start and/or End dates.')
		RETURN
	END
	
	TotQtrDays      = QtrEndDt - QtrStartDt + 1
	QtrDaysRem      = QtrEndDt - thisCutOffDt
	PlanIdleDaysRem = 0
	
	PlanDtCnt = COUNT(PlanIdleDts,@VM) + (PlanIdleDts NE '')
	FOR I = 1 TO PlanDtCnt
		PlanIdleDt = PlanIdleDts<1,I>
		IF PlanIdleDt >= thisCutOffDt THEN PlanIdleDaysRem += 1
	NEXT I
	
	PlanIdleDays    = PlanDtCnt
	
	PlanWorkingDays = TotQtrDays - PlanIdleDays
	
	IF PlanWorkingDays < 0 THEN PlanWorkingDays = 0
	
	PlanDaysRem = QtrDaysRem - PlanIdleDaysRem
	IF PlanDaysRem < 0 THEN PlanDaysRem = 0
	
	BEGIN CASE
		CASE QtrDaysRem = ''
			CalDaysDelta = ICONV(FWEndDt,'D') - ICONV(FQStartDt,'D')
			DaysWorked = INT((CalDaysDelta/TotQtrDays) * PlanWorkingDays)
			
		CASE PlanWorkingDays > PlanDaysRem
			DaysWorked = PlanWorkingDays - PlanDaysRem
			
		CASE 1
			DaysWorked = 0
			
	END CASE
	
	DataArray<COL$PERIOD,9>		= 'Actual Quarter Days'         ;   DataArray<COL$PERIOD_VALUE,9>  = TotQtrDays
	DataArray<COL$PERIOD,10>    = 'Planned Working Days'        ;   DataArray<COL$PERIOD_VALUE,10> = PlanWorkingDays
	DataArray<COL$PERIOD,11>    = 'Actual Days Left'            ;   DataArray<COL$PERIOD_VALUE,11> = QtrDaysRem
	DataArray<COL$PERIOD,12>    = 'Total Idle Days'             ;   DataArray<COL$PERIOD_VALUE,12> = PlanIdleDays
	DataArray<COL$PERIOD,13>    = 'Idle Days Left'              ;   DataArray<COL$PERIOD_VALUE,13> = PlanIdleDaysRem
	DataArray<COL$PERIOD,14>    = 'Planned Days Left'           ;   DataArray<COL$PERIOD_VALUE,14> = PlanDaysRem
	
	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 "DICT.RDS" for index lookup')
		RETURN
	END
	
	* Fudge the dates - '~' is not inclusive of the end dates
	StartDt = ICONV(FQStartDt,'D') - 1
	EndDt   = thisCutOffDt + 1
	
* 	SearchString = 'DATE_OUT':@VM:StartDt:'~':EndDt:@FM     ;**************
	
	SearchString = 'DATE_OUT':@VM:OConv(StartDt, 'D4/'):'~':OConv(EndDt, 'D4/'):@FM
	
	RDSKeys = ''
	Option = ''
	Flag = ''
	
	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
	
	RDSCnt  = COUNT(RDSKeys,@VM) + (RDSKeys NE '')
	LoopCnt = 0
	LastCheck = 0
	
	Def = ""
	Def<MCAPTION$>      = "Building Report..."
	Def<MTYPE$>         = "GC"
	Def<MEXTENT$>       = RDSCnt
	Def<MTEXTWIDTH$>    = 600
	
	AArray = ''
	AATot = 0
	MerProdRow = ''     ;* Moved from line 588 - dkk 1/13/14
	CapFabRow = 9       ;* Moved from line 589 - dkk 1/13/14
	
	MsgUp = Msg(@WINDOW, Def)
	
	LOOP
		@ID = RDSKeys[1,@VM]
		RDSKeys[1,COL2()] = ''
		LoopCnt += 1
		
		READ @RECORD FROM RDSTable,@ID THEN
			
			ReactorsOut = {WAFERS_IN}
			AATot = AATot + ReactorsOut
			
			AArray<LoopCnt,1> = @ID
			AArray<LoopCnt,2> = AATot
			AArray<LoopCnt,3> = ReactorsOut
			
			NCRKeys = @RECORD<RDS_NCR_KEYS$>
			CustScrap   = {CUST_TOT_REJ}
			IFXEpiScrap  = {LSL_TOT_REJ}
			
			TestKeys = XLATE('RDS_LAYER',@RECORD<RDS_RDS_LAYER_KEYS$>,3,'X')
			TWProd = obj_RDS_Test('ProdTestCount',TestKeys)
			
			SRPBillable = @RECORD<RDS_RESEARCH_SRP_BILLABLE$>
			
			OtherWafers = TWProd + SRPBillable
			
			Reactor = @RECORD<RDS_REACTOR$>
			ReactorType = XLATE('REACTOR',Reactor,REACTOR_REACT_TYPE$ ,'X')
			
			WafersOut = ReactorsOut - CustScrap - IFXEpiScrap - TWProd
			
			BEGIN CASE
				CASE ReactorType = 'ASM+'
					ReactTotRow         = ROW$ASMP_OUT
					ReactCustScrapRow   = ROW$ASMP_CUST_SCRAP
					ReactIFXEpiScrapRow  = ROW$ASMP_EPI_SCRAP
					ReactOtherRow       = ROW$ASMP_OTHER
					ReactYieldRow       = ROW$ASMP_YIELD
					ReactScrapRateRow   = ROW$ASMP_SCRAP_RATE
					
				CASE ReactorType = 'HTR'
					ReactTotRow         = ROW$HTR3_OUT
					ReactCustScrapRow   = ROW$HTR3_CUST_SCRAP
					ReactIFXEpiScrapRow  = ROW$HTR3_EPI_SCRAP
					ReactOtherRow       = ROW$HTR3_OTHER
					ReactYieldRow       = ROW$HTR3_YIELD
					ReactScrapRateRow   = ROW$HTR3_SCRAP_RATE
					
				CASE ReactorType = 'EPP'
					ReactTotRow         = ROW$EPIPRO_OUT
					ReactCustScrapRow   = ROW$EPIPRO_CUST_SCRAP
					ReactIFXEpiScrapRow  = ROW$EPIPRO_EPI_SCRAP
					ReactOtherRow       = ROW$EPIPRO_OTHER
					ReactYieldRow       = ROW$EPIPRO_YIELD
					ReactScrapRateRow   = ROW$EPIPRO_SCRAP_RATE
					
				CASE 1
					ReactTotRow         = ROW$ASM_OUT
					ReactCustScrapRow   = ROW$ASM_CUST_SCRAP
					ReactIFXEpiScrapRow  = ROW$ASM_EPI_SCRAP
					ReactOtherRow       = ROW$ASM_OTHER
					ReactYieldRow       = ROW$ASM_YIELD
					ReactScrapRateRow   = ROW$ASM_SCRAP_RATE
					
			END CASE
			
			CustNo = @RECORD<RDS_CUST_NO$>
			
			Captive   = 0 ; // Disable Captive
			CustReportExclude = XLATE('COMPANY',CustNo,COMPANY_REPORT_EXCLUDE$,'X')
			
			Product = {PRODUCT}
			DateOut = {DATE_OUT}
			TimeOut = {TIME_OUT}
			Day = MOD(DateOut,7)
			
			BEGIN CASE
				CASE Day = 0 ; DayCol = COL$SUN
				CASE Day = 1 ; DayCol = COL$MON
				CASE Day = 2 ; DayCol = COL$TUE
				CASE Day = 3 ; DayCol = COL$WED
				CASE Day = 4 ; DayCol = COL$THU
				CASE Day = 5 ; DayCol = COL$FRI
				CASE Day = 6 ; DayCol = COL$SAT
			END CASE
			
			DateOut = OCONV(DateOut,'D4/')
			TimeOut = OCONV(TimeOut,'MTS')
			
			LogData     = ''
			LogData<1>  = @ID
			LogData<2>  = {WO}
			LogData<3>  = DateOut
			LogData<4>  = TimeOut
			If ReactorsOut NE '' then            
				LogData<5>  = ReactorsOut
			end else
				LogData<5> = 0
			end
			Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
			
			
			RDSFiscalWeek = obj_Calendar('IRFiscalWeek',DateOut)<1> ;* 2nd and 3rd fields are the start and end dates of the week
			
			BEGIN CASE
				CASE Captive AND CustNo = '6593'
					CapFabRow = ROW$CAP_FAB2
					
				CASE Captive AND (CustNo = '7053' OR CustNo = '7092' OR CustNo = '7102' OR CustNo = '7103' OR CustNo = '7108' )
					CapFabRow = ROW$CAP_FOUNDRY
					
				CASE Captive AND CustNo = '6874' OR CustNo = '7055'
					IF {WAFER_MM} = '200 mm' THEN
						CapFabRow = ROW$CAP_FAB11
					END ELSE
						CapFabRow = ROW$CAP_FAB10
					END
					
				CASE Captive AND CustNo = '7087'
					CapFabRow = ROW$CAP_FAB11
					
				CASE NOT(Captive) AND Product _EQC '041-B/L'
					MerProdRow = ROW$MER_BL_OUT
					
				CASE NOT(Captive) AND Product _EQC '044-Blanket'
					MerProdRow = ROW$MER_BLANKET_OUT
					
				CASE 1
					NULL        
					
			END CASE
			
			DataArray<COL$QTD_ACTUAL,ROW$TOT_REACTOR_OUTS>  = DataArray<COL$QTD_ACTUAL,ROW$TOT_REACTOR_OUTS> + ReactorsOut
			DataArray<COL$QTD_ACTUAL,ROW$TOT_YIELD_OUTS>    = DataArray<COL$QTD_ACTUAL,ROW$TOT_YIELD_OUTS> + WafersOut
			DataArray<COL$QTD_ACTUAL,ROW$TOT_CUST_SCRAP>    = DataArray<COL$QTD_ACTUAL,ROW$TOT_CUST_SCRAP> + CustScrap
			DataArray<COL$QTD_ACTUAL,ROW$TOT_EPI_SCRAP>     = DataArray<COL$QTD_ACTUAL,ROW$TOT_EPI_SCRAP> + IFXEpiScrap
			DataArray<COL$QTD_ACTUAL,ROW$TOT_OTHER>         = DataArray<COL$QTD_ACTUAL,ROW$TOT_OTHER> + OtherWafers
			
			Yield = DataArray<COL$QTD_ACTUAL,ROW$TOT_YIELD_OUTS> / (DataArray<COL$QTD_ACTUAL,ROW$TOT_YIELD_OUTS> + DataArray<COL$QTD_ACTUAL,ROW$TOT_EPI_SCRAP>)
			
			DataArray<COL$QTD_ACTUAL,ROW$TOT_YIELD> = OCONV(ICONV(Yield*100,'MD2'),'MD2S%')
			
			**********************
			* IFX Scrap Rate Row *
			**********************
			IF DataArray<COL$QTD_ACTUAL,ROW$TOT_YIELD_OUTS> > 0 THEN
				ScrapRate = (DataArray<COL$QTD_ACTUAL,ROW$TOT_EPI_SCRAP> / DataArray<COL$QTD_ACTUAL,ROW$TOT_YIELD_OUTS>) * 1000
			END ELSE
				ScrapRate = 0
			END
			DataArray<COL$QTD_ACTUAL,ROW$TOT_SCRAP_RATE> = OCONV(ICONV(ScrapRate,'MD2'),'MD2')
			
			**********************************
			* Begin of Planning Calculations *
			**********************************
			
			*************
			* Scrap Row *
			*************
			QTDRem = (ICONV(DataArray<COL$QTD_MF_PLAN,ROW$TOT_EPI_SCRAP>,'MD0') - ICONV(DataArray<COL$QTD_ACTUAL,ROW$TOT_EPI_SCRAP>,'MD0'))
			DataArray<COL$QTD_MF_REMAIN,ROW$TOT_EPI_SCRAP> = OCONV(QTDRem,'MD0,')
			
			IF PlanWorkingDays = 0 THEN
				OrgDailyPlan = 0
			END ELSE
			    If PlanDaysRem GT 0 then 
				    OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_REMAIN,ROW$TOT_EPI_SCRAP>,'MD0')/PlanDaysRem
				end else
				    OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_REMAIN,ROW$TOT_EPI_SCRAP>,'MD0')
				end
			END
			DataArray<COL$ORG_MF_DAILY_PLAN,ROW$TOT_EPI_SCRAP> = OCONV(ICONV(OrgDailyPlan,'MD0'),'MD0')
			
			IF OrgDailyPlan NE 0 THEN
				PlanQTD = ICONV((ICONV(DataArray<COL$QTD_MF_PLAN,ROW$TOT_EPI_SCRAP>,'MD0')/PlanWorkingDays)*DaysWorked,'MD0')
				QTDOverUnder = ICONV(DataArray<COL$QTD_ACTUAL,ROW$TOT_EPI_SCRAP> - PlanQTD,'MD0')
				DataArray<COL$QTD_MF_OVER_UNDER,ROW$TOT_EPI_SCRAP> = OCONV(QTDOverUnder,'MD0,')
			END
			
			******************
			* Yield Outs Row *
			******************
			QTDRem = (ICONV(DataArray<COL$QTD_MF_PLAN,ROW$TOT_YIELD_OUTS>,'MD0') - ICONV(DataArray<COL$QTD_ACTUAL,ROW$TOT_YIELD_OUTS>,'MD0'))
			DataArray<COL$QTD_MF_REMAIN,ROW$TOT_YIELD_OUTS> = OCONV(QTDRem,'MD0,')
			
			IF PlanWorkingDays = 0 THEN
				OrgDailyPlan = 0
			END ELSE
			    If PlanDaysRem GT 0 then
				    OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_REMAIN,ROW$TOT_YIELD_OUTS>,'MD0')/PlanDaysRem
				end else
				    OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_REMAIN,ROW$TOT_YIELD_OUTS>,'MD0')
				end
			END
			DataArray<COL$ORG_MF_DAILY_PLAN,ROW$TOT_YIELD_OUTS> = OCONV(ICONV(OrgDailyPlan,'MD0'),'MD0')
			
			IF OrgDailyPlan NE 0 THEN
				PlanQTD = ICONV((ICONV(DataArray<COL$QTD_MF_PLAN,ROW$TOT_YIELD_OUTS>,'MD0')/PlanWorkingDays)*DaysWorked,'MD0')
				QTDOverUnder = ICONV(DataArray<COL$QTD_ACTUAL,ROW$TOT_YIELD_OUTS> - PlanQTD,'MD0')
				DataArray<COL$QTD_MF_OVER_UNDER,ROW$TOT_YIELD_OUTS> = OCONV(QTDOverUnder,'MD0,')
			END
			
			********************************
			* End of Planning Calculations *
			********************************
			
			IF RDSFiscalWeek = FiscalWeek THEN
				
				
				IF DayCol NE '' THEN
					DataArray<DayCol,ROW$TOT_REACTOR_OUTS>   = DataArray<DayCol,ROW$TOT_REACTOR_OUTS> + ReactorsOut
					DataArray<DayCol,ROW$TOT_YIELD_OUTS>     = DataArray<DayCol,ROW$TOT_YIELD_OUTS> + WafersOut
					DataArray<DayCol,ROW$TOT_CUST_SCRAP>    = DataArray<DayCol,ROW$TOT_CUST_SCRAP> + CustScrap
					DataArray<DayCol,ROW$TOT_EPI_SCRAP>     = DataArray<DayCol,ROW$TOT_EPI_SCRAP> + IFXEpiScrap
					DataArray<DayCol,ROW$TOT_OTHER>         = DataArray<DayCol,ROW$TOT_OTHER> + OtherWafers
					
					IF DataArray<DayCol,ROW$TOT_YIELD_OUTS> > 0 THEN
						Yield = DataArray<DayCol,ROW$TOT_YIELD_OUTS> / (DataArray<DayCol,ROW$TOT_YIELD_OUTS> + DataArray<DayCol,ROW$TOT_EPI_SCRAP>)
					END ELSE
						Yield = 0
					END
					
					DataArray<DayCol,ROW$TOT_YIELD> = OCONV(ICONV(Yield*100,'MD2'),'MD2S%')
				END
				
				DataArray<COL$WTD_ACTUAL,ROW$TOT_REACTOR_OUTS>   = DataArray<COL$WTD_ACTUAL,ROW$TOT_REACTOR_OUTS> + ReactorsOut
				DataArray<COL$WTD_ACTUAL,ROW$TOT_YIELD_OUTS>     = DataArray<COL$WTD_ACTUAL,ROW$TOT_YIELD_OUTS> + WafersOut
				DataArray<COL$WTD_ACTUAL,ROW$TOT_CUST_SCRAP>    = DataArray<COL$WTD_ACTUAL,ROW$TOT_CUST_SCRAP> + CustScrap
				DataArray<COL$WTD_ACTUAL,ROW$TOT_EPI_SCRAP>     = DataArray<COL$WTD_ACTUAL,ROW$TOT_EPI_SCRAP> + IFXEpiScrap
				DataArray<COL$WTD_ACTUAL,ROW$TOT_OTHER>         = DataArray<COL$WTD_ACTUAL,ROW$TOT_OTHER> + OtherWafers
				
				IF DataArray<COL$WTD_ACTUAL,ROW$TOT_YIELD_OUTS> > 0 THEN
					Yield = DataArray<COL$WTD_ACTUAL,ROW$TOT_YIELD_OUTS> / (DataArray<COL$WTD_ACTUAL,ROW$TOT_YIELD_OUTS> + DataArray<COL$WTD_ACTUAL,ROW$TOT_EPI_SCRAP>)
				END ELSE
					Yield = 0
				END
				DataArray<COL$WTD_ACTUAL,ROW$TOT_YIELD> = OCONV(ICONV(Yield*100,'MD2'),'MD2S%')
				
				* Scrap Rate
				IF DataArray<COL$WTD_ACTUAL,ROW$TOT_YIELD_OUTS> > 0 THEN
					ScrapRate = (DataArray<COL$WTD_ACTUAL,ROW$TOT_EPI_SCRAP>/DataArray<COL$WTD_ACTUAL,ROW$TOT_YIELD_OUTS>)*1000
				END ELSE
					ScrapRate = 0
				END
				DataArray<COL$WTD_ACTUAL,ROW$TOT_SCRAP_RATE> = OCONV(ICONV(ScrapRate,'MD2'),'MD2')
				
			END     ;*  End of check for current fiscal week
			
		END ;* End of RDS Record read
		
		Running = Msg(@WINDOW, MsgUp, LoopCnt, MSGINSTUPDATE$) 
		
		
	UNTIL RDSKeys = '' OR NOT(Running)
	REPEAT
	
	Msg(@WINDOW,MsgUp)      ;* Take message down
	
	* * * * New Section to calculate Shipments - JCH 3/26/2014  ********************************************************
	
	OPEN 'COC' TO ShipTable ELSE
		ErrMsg('Unable to open COC for report.')
		RETURN
	END
	
	OPEN 'DICT.COC' TO @DICT ELSE
		ErrMsg('Unable to open DICT.COC for report.')
		RETURN
	END
	
	StartDt = ICONV(FQStartDt,'D') - 1
	EndDt  = ICONV(FWEndDt,'D') + 1        ;* 12/2/2015 JCH - Use cutoff date for data selection rather than week ending date
	EndDt   = thisCutOffDt + 1
	
* 	SearchString = 'SHIP_DT':@VM:StartDt:'~':EndDt:@FM
	
	SearchString = 'SHIP_DT':@VM:OConv(StartDt, 'D4/'):'~':OConv(EndDt, 'D4/'):@FM
	
	ShipKeys = ''
	Option = ''
	Flag = ''
	
	Def = ""
	Def<MTEXT$> = "Selecting Shipment records..."
	Def<MTYPE$> = "U"
	MsgUp = Msg(@window, Def)   ;* display the processing message
	
	Set_Status(0)
	Btree.Extract(SearchString, 'COC', @DICT, ShipKeys, Option, Flag)
	
	Msg(@WINDOW, MsgUp)
	
	IF Get_Status(errCode) THEN
		ErrMsg(errCode)
		RETURN
	END
	
	*    IF ShipKeys = '' THEN
	*      ErrMsg('No Shipment Records found for the current quarter!')
	*      RETURN
	*    END
	
	If ShipKeys NE '' then
		ShipCnt = COUNT(ShipKeys,@VM) + (ShipKeys NE '')
		LoopCnt = 0
		LastCheck = 0
		
		Def = ""
		Def<MCAPTION$>      = "Building Report Shipments..."
		Def<MTYPE$>         = "GC"
		Def<MEXTENT$>       = ShipCnt
		Def<MTEXTWIDTH$>    = 600
		
		MsgUp = Msg(@WINDOW, Def)
		
		LOOP
			@ID = ShipKeys[1,@VM]
			ShipKeys[1,COL2()] = ''
			LoopCnt += 1
			
			READ @RECORD FROM ShipTable,@ID THEN
				
				ShipQty = SUM({CASS_WAFER_CNT})     ;* Only product wafers (no test wafers being returned )
				Captive = {CUST_CAPTIVE}
				
				ShipDt  = @RECORD<COC_SHIP_DT$>
				Day     = MOD(ShipDt,7)
				
				BEGIN CASE
					CASE Day = 0 ; DayCol = COL$SUN
					CASE Day = 1 ; DayCol = COL$MON
					CASE Day = 2 ; DayCol = COL$TUE
					CASE Day = 3 ; DayCol = COL$WED
					CASE Day = 4 ; DayCol = COL$THU
					CASE Day = 5 ; DayCol = COL$FRI
					CASE Day = 6 ; DayCol = COL$SAT
				END CASE
				
				ShipDt = OCONV(ShipDt,'D4/')
				ShipFiscalWeek = obj_Calendar('IRFiscalWeek',ShipDt)<1> ;* 2nd and 3rd fields are the start and end dates of the week
				
				Row = ROW$TOT_SHIP
				DataArray<COL$QTD_ACTUAL,Row> = DataArray<COL$QTD_ACTUAL,Row> + ShipQty
				
				IF ShipFiscalWeek = FiscalWeek THEN
					DataArray<COL$WTD_ACTUAL,Row> = DataArray<COL$WTD_ACTUAL,Row> + ShipQty 
					DataArray<DayCol,Row> = DataArray<DayCol,Row> + ShipQty
				END
				
			END ;* End of Shipment Record Read
			
			Running = Msg(@WINDOW, MsgUp, LoopCnt, MSGINSTUPDATE$) 
			
		UNTIL ShipKeys = '' OR NOT(Running)
			
		REPEAT
		
		**************************************
		* Beginning Of Planning Calculations *
		**************************************
		
		***************
		* Shipped Row *
		***************
		QTDRem = (ICONV(DataArray<COL$QTD_MF_PLAN,ROW$TOT_SHIP>,'MD0') - ICONV(DataArray<COL$QTD_ACTUAL,ROW$TOT_SHIP>,'MD0'))
		DataArray<COL$QTD_MF_REMAIN,ROW$TOT_SHIP> = OCONV(QTDRem,'MD0,')
		
		IF PlanWorkingDays = 0 THEN
			OrgDailyPlan = 0
		END ELSE
		    If PlanDaysRem GT 0 then 
			    OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_REMAIN,ROW$TOT_SHIP>,'MD0')/PlanDaysRem
		    end else
		        OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_REMAIN,ROW$TOT_SHIP>,'MD0')
		    end
		END
		DataArray<COL$ORG_MF_DAILY_PLAN,ROW$TOT_SHIP> = OCONV(ICONV(OrgDailyPlan,'MD0'),'MD0')
		
		IF OrgDailyPlan NE 0 THEN
			PlanQTD = ICONV((ICONV(DataArray<COL$QTD_MF_PLAN,ROW$TOT_SHIP>,'MD0')/PlanWorkingDays)*DaysWorked,'MD0')
			QTDOverUnder = ICONV(DataArray<COL$QTD_ACTUAL,ROW$TOT_SHIP> - PlanQTD,'MD0')
			DataArray<COL$QTD_MF_OVER_UNDER,ROW$TOT_SHIP> = OCONV(QTDOverUnder,'MD0,')
		END
		
		********************************
		* End Of Planning Calculations *
		********************************
		
		* * * * * End of shipment section   
		
		FOR I = ROW$TOT_REACTOR_OUTS TO ROW$TOT_SHIP
			IF I NE ROW$TOT_YIELD THEN
				FOR J = COL$MON TO COL$QTD_ACTUAL
					DataArray<J,I> = OCONV(DataArray<J,I>,'MD0,Z')
				NEXT J
			END
		NEXT I
	end

	Msg(@WINDOW,MsgUp)
	
	xlApp = OleCreateInstance("excel.Application")                              ;* Create instance of Excel
	OlePutProperty(XlApp, 'Visible', xlSheetVisible)
	if OleStatus() then Goto HadError
	
	xlWorkBooks = OleGetProperty(xlApp, "Workbooks")                             ;* Create workbook
	xlWkb       = OleCallMethod(xlWorkbooks,"Add")
	if OleStatus() then Goto HadError
	
	xlSht = OleGetProperty(xlWkb, "Worksheets",1)                               ;* Select worksheet 1
	if OleStatus() then Goto HadError
	
	xlPageSetup = OleGetProperty(xlSht,"PageSetup")                             ;* Get PageSetup object for Page/Printing stuff
	if OleStatus() then GOTO HadError
	
	OlePutProperty( xlPageSetup , 'PrintGridLines', xlTrue )                                        ;* Print Gridlines on
	OlePutProperty( xlPageSetup , 'Orientation' , xlLandscape )                                     ;* Orientation to Landscape
	OlePutProperty( xlPageSetup , 'HeaderMargin', '20')                                             ;* HeaderMargin to 20pts
	
	OlePutProperty( xlPageSetup , 'CenterHeader' , '&14Epi SERVICES - Thruput Report - w/o Makeup Activity' )       ;* Set Center Header
	OlePutProperty( xlPageSetup , 'LeftHeader' , '&G' )                                             ;* Set graphic in Left Header
	
	graphic = OleGetProperty( xlPageSetup , 'LeftHeaderPicture' )                                   ;* Get graphic object for Left Header Picture
	OlePutProperty( graphic , 'FileName' , 'R:\Oinsight\BMPS\EpiSvcs.png' )                         ;* Set filename for graphic
	OlePutProperty( graphic , 'Height' , '51.75' )                                                  ;* Scale graphic to fit header      
	OlePutProperty( graphic , 'Width' , '81')
	
	OlePutProperty( xlPageSetup , 'RightHeader' , 'Page &P' )                   ;* Set Right Header to show current page number
	
	
	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>)            ;* Load report array into spreadsheet cells
			IF OleStatus() THEN GOTO HadError
		NEXT Column
	NEXT LineCnt
	
	LastColumn = eXcelCols<1,ColCnt>
	range = OleGetProperty( xlSht, 'Range','B1:':LastColumn:'1')                ;* Turn on word wrap in columns from B through end
	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 )      ;* Turn on autoformat (FormatClassic1)
	IF OleStatus() THEN GOTO HadError
	
	FOR I = COL$MON TO COL$SUN
		eXcelCol = eXcelCols<1,I>
		column = OleGetProperty( xlSht, 'Range' , eXcelCol:':':exCelCol )       ;* Set the day columns to a standard width
		OlePutProperty( column , 'ColumnWidth' , '10.6' )                       ;* Originally 10.3 - changed to 10.6 to fit Wednesday - dkk 12/30/13
		a = OleStatus()
	NEXT I
	
	EdgeList = xlDiagonalDown:@VM:xlDiagonalup:@VM:xlEdgeLeft:@VM:xlEdgeTop:@VM:xlEdgeRight:@VM:xlInsideVertical
	
	* Put border on the BOTTOM of this row
	
	range = OleGetProperty( xlSht, 'Range', 'A':ROW$TOT_SHIP:':':RangeColumn:ROW$TOT_SHIP)
	
	FOR I = 1 TO COUNT(EdgeList,@VM) + (EdgeList NE '')
		edge = OleGetProperty( range, 'Borders' , EdgeList<1,I> )
		OlePutProperty( edge , 'LineStyle' , xlLineStyleNone )                  ;* Clear border segments EXCEPT bottom
	NEXT I
	* Set border segment for bottom to medium weight line
	edge = OleGetProperty( range, 'Borders' , xlEdgeBottom )                    
	olePutProperty( edge , 'LineStyle', xlContinuous )
	olePutProperty( edge , 'Weight' , xlMedium )
	olePutProperty( edge , 'ColorIndex', xlColorIndexAutomatic )
	* Save to local drive
	Void = OleCallMethod( xlWkb, 'SaveAs',Environment_Services('GetReportsRootPath') : '\ThruPut.xls', xlWorkbookNormal)
	
RETURN


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