COMPILE SUBROUTINE REPORT_THRUPUT_ORG(dummy)

* 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
DECLARE FUNCTION Msg, Get_Status, obj_Calendar, Utility, SetInitDirOptions, Dialog_Box, obj_RDS_Test, Environment_Services

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

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\


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

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<1,ROW$TOT_PRIME_TW>     = '  Prime TW'      ;* dkk 12/30/13
* DataArray<1,ROW$TOT_RECLAIM_TW>   = '  Reclaim TW'    ;* dkk 12/30/13

DataArray<1,ROW$CAP_OUT>            = 'Captive Out'
DataArray<1,ROW$CAP_FAB2>           = '  Fab 2'
DataArray<1,ROW$CAP_FOUNDRY>        = '  Foundry'
DataArray<1,ROW$CAP_FAB10>          = '  Fab 10'
DataArray<1,ROW$CAP_FAB11>          = '  Fab 11'
DataArray<1,ROW$CAP_CUST_SCRAP>     = '  Supplier Scrap'
DataArray<1,ROW$CAP_EPI_SCRAP>      = '  IFX Scrap'
DataArray<1,ROW$CAP_OTHER>          = '  Prod Test'
DataArray<1,ROW$CAP_YIELD>          = '  Yield'
DataArray<1,ROW$CAP_SCRAP_RATE>     = '  Scrap/1k'
DataArray<1,ROW$CAP_SHIP>           = '  Shipped'

* DataArray<1,ROW$CAP_PRIME_TW>     = '  Prime TW'      ;* dkk 12/30/13
* DataArray<1,ROW$CAP_RECLAIM_TW>   = '  Reclaim TW'    ;* dkk 12/30/13

DataArray<1,ROW$MER_TOT_OUT>        = 'Merchant Out'
DataArray<1,ROW$MER_BL_OUT>         = '  B/L'
DataArray<1,ROW$MER_BLANKET_OUT>    = '  Blanket'
DataArray<1,ROW$MER_CUST_SCRAP>     = '  Cust Scrap'
DataArray<1,ROW$MER_EPI_SCRAP>      = '  IFX Scrap'
DataArray<1,ROW$MER_OTHER>          = '  Prod Test'
DataArray<1,ROW$MER_YIELD>          = '  Yield'
DataArray<1,ROW$MER_SCRAP_RATE>     = '  Scrap/1k'
DataArray<1,ROW$MER_SHIP>           = '  Shipped'

* DataArray<1,ROW$MER_PRIME_TW>     = '  Prime TW'      ;* dkk 12/30/13
* DataArray<1,ROW$MER_RECLAIM_TW>   = '  Reclaim TW'    ;* dkk 12/30/13

DataArray<1,ROW$ASMP_OUT>           = 'ASM+ Out'
DataArray<1,ROW$ASMP_CUST_SCRAP>    = '  Supplier Scrap'
DataArray<1,ROW$ASMP_EPI_SCRAP>     = '  IFX Scrap'
DataArray<1,ROW$ASMP_OTHER>         = '  Prod Test'
DataArray<1,ROW$ASMP_YIELD>         = '  Yield'
DataArray<1,ROW$ASMP_SCRAP_RATE>    = '  Scrap/1k'

* DataArray<1,ROW$ASMP_PRIME_TW>    = '  Prime TW'      ;* dkk 12/30/13
* DataArray<1,ROW$ASMP_RECLAIM_TW>  = '  Reclaim TW'    ;* dkk 12/30/13

DataArray<1,ROW$ASM_OUT>            = 'ASM Out'
DataArray<1,ROW$ASM_CUST_SCRAP>     = '  Sup/Cust Scrap'
DataArray<1,ROW$ASM_EPI_SCRAP>      = '  IFX Scrap'
DataArray<1,ROW$ASM_OTHER>          = '  Prod Test'
DataArray<1,ROW$ASM_YIELD>          = '  Yield'
DataArray<1,ROW$ASM_SCRAP_RATE>     = '  Scrap/1k'

* DataArray<1,ROW$ASM_PRIME_TW>     = '  Prime TW'      ;* dkk 12/30/13
* DataArray<1,ROW$ASM_RECLAIM_TW>   = '  Reclaim TW'    ;* dkk 12/30/13

DataArray<1,ROW$HTR3_OUT>           = 'HTR3 Out'
DataArray<1,ROW$HTR3_CUST_SCRAP>    = '  Supplier Scrap'
DataArray<1,ROW$HTR3_EPI_SCRAP>     = '  IFX Scrap'
DataArray<1,ROW$HTR3_OTHER>         = '  Prod Test'
DataArray<1,ROW$HTR3_YIELD>         = '  Yield'
DataArray<1,ROW$HTR3_SCRAP_RATE>    = '  Scrap/1k'

* DataArray<1,ROW$HTR3_PRIME_TW>    = '  Prime TW'      ;* dkk 12/30/13
* DataArray<1,ROW$HTR3_RECLAIM_TW>  = '  Reclaim TW'    ;* dkk 12/30/13

DataArray<1,ROW$EPIPRO_OUT>         = 'EpiPRO Out'
DataArray<1,ROW$EPIPRO_CUST_SCRAP>  = '  Supplier Scrap'
DataArray<1,ROW$EPIPRO_EPI_SCRAP>   = '  IFX Scrap'
DataArray<1,ROW$EPIPRO_OTHER>       = '  Prod Test'
DataArray<1,ROW$EPIPRO_YIELD>       = '  Yield'
DataArray<1,ROW$EPIPRO_SCRAP_RATE>  = '  Scrap/1k'

* DataArray<1,ROW$EPIPRO_PRIME_TW>  = '  Prime TW'          ;* dkk 12/30/13
* DataArray<1,ROW$EPIPRO_RECLAIM_TW>    = '  Reclaim TW'    ;* dkk 12/30/13


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
      CASE TargKey = 'CAP*Captive_Out'            ; DataArray<COL$QTD_MF_PLAN,ROW$CAP_OUT>           = TargQty
      CASE TargKey = 'CAP*Foundry'                ; DataArray<COL$QTD_MF_PLAN,ROW$CAP_FOUNDRY>       = TargQty
      CASE TargKey = 'CAP*Fab_2'                  ; DataArray<COL$QTD_MF_PLAN,ROW$CAP_FAB2>          = TargQty
      CASE TargKey = 'CAP*Fab_10'                 ; DataArray<COL$QTD_MF_PLAN,ROW$CAP_FAB10>         = TargQty
      CASE TargKey = 'CAP*Fab_11'                 ; DataArray<COL$QTD_MF_PLAN,ROW$CAP_FAB11>         = TargQty
      CASE TargKey = 'CAP*Supplier_Scrap'         ; DataArray<COL$QTD_MF_PLAN,ROW$CAP_CUST_SCRAP>    = TargQty
      CASE TargKey = 'CAP*IFX_Scrap'              ; DataArray<COL$QTD_MF_PLAN,ROW$CAP_EPI_SCRAP>     = TargQty
      CASE TargKey = 'CAP*Prod_Test'              ; DataArray<COL$QTD_MF_PLAN,ROW$CAP_OTHER>         = TargQty
      CASE TargKey = 'CAP*Yield'                  ; DataArray<COL$QTD_MF_PLAN,ROW$CAP_YIELD>         = TargPcnt
      CASE TargKey = 'MER*Merchant_Out'           ; DataArray<COL$QTD_MF_PLAN,ROW$MER_TOT_OUT>       = TargQty
      CASE TargKey = 'MER*B/L'                    ; DataArray<COL$QTD_MF_PLAN,ROW$MER_BL_OUT>        = TargQty
      CASE TargKey = 'MER*Blanket'                ; DataArray<COL$QTD_MF_PLAN,ROW$MER_BLANKET_OUT>   = TargQty
      CASE TargKey = 'MER*Cust_Scrap'             ; DataArray<COL$QTD_MF_PLAN,ROW$MER_CUST_SCRAP>    = TargQty
      CASE TargKey = 'MER*IFX_Scrap'              ; DataArray<COL$QTD_MF_PLAN,ROW$MER_EPI_SCRAP>     = TargQty
      CASE TargKey = 'MER*Prod_Test'              ; DataArray<COL$QTD_MF_PLAN,ROW$MER_OTHER>         = TargQty
      CASE TargKey = 'MER*Yield'                  ; DataArray<COL$QTD_MF_PLAN,ROW$MER_YIELD>         = TargPcnt
      CASE TargKey = 'ASMP*ASM+_Out'              ; DataArray<COL$QTD_MF_PLAN,ROW$ASMP_OUT>          = TargQty
      CASE TargKey = 'ASMP*Supplier_Scrap'        ; DataArray<COL$QTD_MF_PLAN,ROW$ASMP_CUST_SCRAP>   = TargQty
      CASE TargKey = 'ASMP*IFX_Scrap'       	  ; DataArray<COL$QTD_MF_PLAN,ROW$ASMP_EPI_SCRAP>    = TargQty
      CASE TargKey = 'ASMP*Prod_Test'             ; DataArray<COL$QTD_MF_PLAN,ROW$ASMP_OTHER>        = TargQty
      CASE TargKey = 'ASMP*Yield'                 ; DataArray<COL$QTD_MF_PLAN,ROW$ASMP_YIELD>        = TargPcnt
      CASE TargKey = 'ASM*ASM_Out'                ; DataArray<COL$QTD_MF_PLAN,ROW$ASM_OUT>           = TargQty
      CASE TargKey = 'ASMSup/Cust_Scrap'          ; DataArray<COL$QTD_MF_PLAN,ROW$ASM_CUST_SCRAP>    = TargQty
      CASE TargKey = 'ASM*IFX_Scrap'              ; DataArray<COL$QTD_MF_PLAN,ROW$ASM_EPI_SCRAP>     = TargQty
      CASE TargKey = 'ASM*Prod_Test'              ; DataArray<COL$QTD_MF_PLAN,ROW$ASM_OTHER>         = TargQty
      CASE TargKey = 'ASM*Yield'                  ; DataArray<COL$QTD_MF_PLAN,ROW$ASM_YIELD>         = TargPcnt
      CASE TargKey = 'HTR3*HTR3_Out'              ; DataArray<COL$QTD_MF_PLAN,ROW$HTR3_OUT>          = TargQty
      CASE TargKey = 'HTR3*Supplier_Scrap'        ; DataArray<COL$QTD_MF_PLAN,ROW$HTR3_CUST_SCRAP>   = TargQty
      CASE TargKey = 'HTR3*IFX_Scrap'             ; DataArray<COL$QTD_MF_PLAN,ROW$HTR3_EPI_SCRAP>    = TargQty
      CASE TargKey = 'HTR3*Prod_Test'             ; DataArray<COL$QTD_MF_PLAN,ROW$HTR3_OTHER>        = TargQty
      CASE TargKey = 'HTR3*Yield'                 ; DataArray<COL$QTD_MF_PLAN,ROW$HTR3_YIELD>        = TargPcnt
      CASE TargKey = 'EPIPRO*EpiPRO_Out'          ; DataArray<COL$QTD_MF_PLAN,ROW$EPIPRO_OUT>        = TargQty
      CASE TargKey = 'EPIPRO*Supplier_Scrap'      ; DataArray<COL$QTD_MF_PLAN,ROW$EPIPRO_CUST_SCRAP> = TargQty
      CASE TargKey = 'EPIPRO*IFX_Scrap'           ; DataArray<COL$QTD_MF_PLAN,ROW$EPIPRO_EPI_SCRAP>  = TargQty
      CASE TargKey = 'EPIPRO*Prod_Test'           ; DataArray<COL$QTD_MF_PLAN,ROW$EPIPRO_OTHER>      = TargQty
      CASE TargKey = 'EPIPRO*Yield'               ; DataArray<COL$QTD_MF_PLAN,ROW$EPIPRO_YIELD>      = TargPcnt
  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  = ICONV(FWEndDt,'D') + 1        ;*  12/2/2015 JCH -  Cut Off date used as EndDt  * * * * * * *
EndDt   = thisCutOffDt + 1

SearchString = 'DATE_OUT':@VM:StartDt:'~':EndDt:@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

      WafersOut = {WFRS_OUT_THRUPUT}
    
      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}
      
      * PrimeTW   = {TW_PRIME}        ;* dkk 12/30/13
      * ReclaimTW = {TW_RECLAIM}      ;* dkk 12/30/13
      
      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')
      
      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
              * ReactPrimeTWRow   = ROW$ASMP_PRIME_TW         ;* dkk 12/30/13
              * ReactReclaimTWRow = ROW$ASMP_RECLAIM_TW       ;* dkk 12/30/13
              
          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
              * ReactPrimeTWRow   = ROW$HTR3_PRIME_TW         ;* dkk 12/30/13
              * ReactReclaimTWRow = ROW$HTR3_RECLAIM_TW       ;* dkk 12/30/13
              
          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
              * ReactPrimeTWRow   = ROW$EPIPRO_PRIME_TW       ;* dkk 12/30/13
              * ReactReclaimTWRow = ROW$EPIPRO_RECLAIM_TW     ;* dkk 12/30/13
              
          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
              * ReactPrimeTWRow   = ROW$ASM_PRIME_TW          ;* dkk 12/30/13
              * ReactReclaimTWRow = ROW$ASM_RECLAIM_TW        ;* dkk 12/30/13
              
      END CASE
              
      CustNo = @RECORD<RDS_CUST_NO$>
      
      Captive = {CUST_CAPTIVE}
      CustReportExclude = XLATE('COMPANY',CustNo,COMPANY_REPORT_EXCLUDE$,'X')
      
      Product = {PRODUCT}
      DateOut = {DATE_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/')
      
      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'
                  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
          * DataArray<COL$QTD_ACTUAL,ROW$TOT_PRIME_TW>    = DataArray<COL$QTD_ACTUAL,ROW$TOT_PRIME_TW> + PrimeTW          ;* dkk 12/30/13
          * DataArray<COL$QTD_ACTUAL,ROW$TOT_RECLAIM_TW>  = DataArray<COL$QTD_ACTUAL,ROW$TOT_RECLAIM_TW> + ReclaimTW      ;* dkk 12/30/13
          
          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
              OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_REMAIN,ROW$TOT_EPI_SCRAP>,'MD0')/PlanDaysRem
          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
              OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_REMAIN,ROW$TOT_YIELD_OUTS>,'MD0')/PlanDaysRem
          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
                  * DataArray<DayCol,ROW$TOT_PRIME_TW>    = DataArray<DayCol,ROW$TOT_PRIME_TW> + PrimeTW          ;* dkk 12/30/13
                  * DataArray<DayCol,ROW$TOT_RECLAIM_TW>  = DataArray<DayCol,ROW$TOT_RECLAIM_TW> + ReclaimTW      ;* dkk 12/30/13
                  
                  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
              * DataArray<COL$WTD_ACTUAL,ROW$TOT_PRIME_TW>    = DataArray<COL$WTD_ACTUAL,ROW$TOT_PRIME_TW> + PrimeTW              ;* dkk 12/30/13
              * DataArray<COL$WTD_ACTUAL,ROW$TOT_RECLAIM_TW>  = DataArray<COL$WTD_ACTUAL,ROW$TOT_RECLAIM_TW> + ReclaimTW          ;* dkk 12/30/13
              
              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
          
          
          IF Captive THEN
          
              DataArray<COL$QTD_ACTUAL,ROW$CAP_OUT>           = DataArray<COL$QTD_ACTUAL,ROW$CAP_OUT> + WafersOut
              DataArray<COL$QTD_ACTUAL,CapFabRow>             = DataArray<COL$QTD_ACTUAL,CapFabRow> + WafersOut
              DataArray<COL$QTD_ACTUAL,ROW$CAP_CUST_SCRAP>    = DataArray<COL$QTD_ACTUAL,ROW$CAP_CUST_SCRAP> + CustScrap
              DataArray<COL$QTD_ACTUAL,ROW$CAP_EPI_SCRAP>     = DataArray<COL$QTD_ACTUAL,ROW$CAP_EPI_SCRAP> + IFXEpiScrap
              DataArray<COL$QTD_ACTUAL,ROW$CAP_OTHER>         = DataArray<COL$QTD_ACTUAL,ROW$CAP_OTHER> + OtherWafers
              * DataArray<COL$QTD_ACTUAL,ROW$CAP_PRIME_TW>        = DataArray<COL$QTD_ACTUAL,ROW$CAP_PRIME_TW> + PrimeTW              ;* dkk 12/30/13
              * DataArray<COL$QTD_ACTUAL,ROW$CAP_RECLAIM_TW>  = DataArray<COL$QTD_ACTUAL,ROW$CAP_RECLAIM_TW> + ReclaimTW              ;* dkk 12/30/13
                  
              IF DataArray<COL$QTD_ACTUAL,ROW$CAP_OUT> > 0 THEN
                  Yield = DataArray<COL$QTD_ACTUAL,ROW$CAP_OUT> / ( DataArray<COL$QTD_ACTUAL,ROW$CAP_OUT> + DataArray<COL$QTD_ACTUAL,ROW$CAP_EPI_SCRAP> )
              END ELSE
                  Yield = 0
              END
              
              DataArray<COL$QTD_ACTUAL,ROW$CAP_YIELD> = OCONV(ICONV(Yield*100,'MD2'),'MD2S%')
              
              * Scrap Rate
              
              IF DataArray<COL$QTD_ACTUAL,ROW$CAP_OUT> > 0 THEN
                  ScrapRate = (DataArray<COL$QTD_ACTUAL,ROW$CAP_EPI_SCRAP>/DataArray<COL$QTD_ACTUAL,ROW$CAP_OUT>)*1000
              END ELSE
                  ScrapRate = 0
              END
              
              DataArray<COL$QTD_ACTUAL,ROW$CAP_SCRAP_RATE> = OCONV(ICONV(ScrapRate,'MD2'),'MD2')
              
              * Planning Calcs
          
              QTDRem = (ICONV(DataArray<COL$QTD_MF_PLAN,ROW$CAP_EPI_SCRAP>,'MD0') - ICONV(DataArray<COL$QTD_ACTUAL,ROW$CAP_EPI_SCRAP>,'MD0'))
              
              IF PlanWorkingDays = 0 THEN
                  OrgDailyPlan = 0
              END ELSE
                  *OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_PLAN,ROW$CAP_EPI_SCRAP>,'MD0')/PlanWorkingDays
                  OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_REMAIN,ROW$CAP_EPI_SCRAP>,'MD0')/PlanDaysRem ;* <- 1/27/2014 JCH **********
              END
              
              DataArray<COL$QTD_MF_REMAIN,ROW$CAP_EPI_SCRAP> = OCONV(QTDRem,'MD0,')
              
              DataArray<COL$ORG_MF_DAILY_PLAN,ROW$CAP_EPI_SCRAP> = OCONV(ICONV(OrgDailyPlan,'MD0'),'MD0')
              
              IF OrgDailyPlan NE 0 THEN
                  PlanQTD = ICONV((ICONV(DataArray<COL$QTD_MF_PLAN,ROW$CAP_EPI_SCRAP>,'MD0')/PlanWorkingDays)*DaysWorked,'MD0')  ;* <- 3/3/2014 JCH
                  QTDOverUnder = ICONV(DataArray<COL$QTD_ACTUAL,ROW$CAP_EPI_SCRAP> - PlanQTD,'MD0')                               ;* <- 3/3/2014 JCH
                  DataArray<COL$QTD_MF_OVER_UNDER,ROW$CAP_EPI_SCRAP> = OCONV(QTDOverUnder,'MD0,')                                ;* <- 1/15/2008 JCH
              END
          
              QTDRem = (ICONV(DataArray<COL$QTD_MF_PLAN,ROW$CAP_OUT>,'MD0') - ICONV(DataArray<COL$QTD_ACTUAL,ROW$CAP_OUT>,'MD0'))
             
              IF PlanWorkingDays = 0 THEN
                  OrgDailyPlan = 0
              END ELSE
                  *OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_PLAN,ROW$CAP_OUT>,'MD0')/PlanWorkingDays
                  OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_REMAIN,ROW$CAP_OUT>,'MD0')/PlanDaysRem   ;* <- 1/27/2014 JCH **********
              END
              
              DataArray<COL$QTD_MF_REMAIN,ROW$CAP_OUT>       = OCONV(QTDRem,'MD0,')
              DataArray<COL$ORG_MF_DAILY_PLAN,ROW$CAP_OUT>   = OCONV(ICONV(OrgDailyPlan,'MD0'),'MD0')
              
              IF OrgDailyPlan NE 0 THEN
                  PlanQTD = ICONV((ICONV(DataArray<COL$QTD_MF_PLAN,ROW$CAP_OUT>,'MD0')/PlanWorkingDays)*DaysWorked,'MD0')    ;* <- 3/3/2014 JCH
                  QTDOverUnder = ICONV(DataArray<COL$QTD_ACTUAL,ROW$CAP_OUT> - PlanQTD,'MD0')                                 ;* <- 3/3/2014 JCH
                  DataArray<COL$QTD_MF_OVER_UNDER,ROW$CAP_OUT> = OCONV(QTDOverUnder,'MD0,')                                  ;* <- 1/15/2008 JCH
              END
              
              
              QTDRem = (ICONV(DataArray<COL$QTD_MF_PLAN,CapFabRow>,'MD0') - ICONV(DataArray<COL$QTD_ACTUAL,CapFabRow>,'MD0'))
              
              IF PlanWorkingDays > 0 THEN
                  OrgDailyPlan = 0
              END ELSE
                  *OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_PLAN,CapFabRow>,'MD0')/PlanWorkingDays
                  OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_REMAIN,CapFabRow>,'MD0')/PlanDaysRem ;* <- 1/27/2014 JCH **********
              END
              
              
              DataArray<COL$QTD_MF_REMAIN,CapFabRow>     = OCONV(QTDRem,'MD0,')
              DataArray<COL$ORG_MF_DAILY_PLAN,CapFabRow> = OCONV(ICONV(OrgDailyPlan,'MD0'),'MD0')
              
              IF OrgDailyPlan NE 0 THEN
                  PlanQTD = ICONV((ICONV(DataArray<COL$QTD_MF_PLAN,CapFabRow>,'MD0')/PlanWorkingDays)*DaysWorked,'MD0')  ;* <- 3/3/2014 JCH
                  QTDOverUnder = ICONV(DataArray<COL$QTD_ACTUAL,CapFabRow> - PlanQTD,'MD0')                               ;* <- 3/3/2014 JCH
                  DataArray<COL$QTD_MF_OVER_UNDER,CapFabRow> = OCONV(QTDOverUnder,'MD0,')                                ;* <- 1/15/2008 JCH
              END
              
              * End of Planning Calcs
              
              IF RDSFiscalWeek = FiscalWeek THEN
              
                  DataArray<DayCol,ROW$CAP_OUT>           = DataArray<DayCol,ROW$CAP_OUT> + WafersOut
                  DataArray<DayCol,CapFabRow>             = DataArray<DayCol,CapFabRow> + WafersOut
                  DataArray<DayCol,ROW$CAP_CUST_SCRAP>    = DataArray<DayCol,ROW$CAP_CUST_SCRAP> + CustScrap
                  DataArray<DayCol,ROW$CAP_EPI_SCRAP>     = DataArray<DayCol,ROW$CAP_EPI_SCRAP> + IFXEpiScrap
                  DataArray<DayCol,ROW$CAP_OTHER>         = DataArray<DayCol,ROW$CAP_OTHER> + OtherWafers
                  * DataArray<DayCol,ROW$CAP_PRIME_TW>        = DataArray<DayCol,ROW$CAP_PRIME_TW> + PrimeTW                              ;* dkk 12/30/13
                  * DataArray<DayCol,ROW$CAP_RECLAIM_TW>  = DataArray<DayCol,ROW$CAP_RECLAIM_TW> + ReclaimTW                              ;* dkk 12/30/13
                  
                  IF DataArray<DayCol,ROW$CAP_OUT> > 0 THEN
                      Yield = DataArray<DayCol,ROW$CAP_OUT> / ( DataArray<DayCol,ROW$CAP_OUT> + DataArray<DayCol,ROW$CAP_EPI_SCRAP> )
                  END ELSE
                      Yield = 0
                  END
                  
                  DataArray<DayCol,ROW$CAP_YIELD> = OCONV(ICONV(Yield*100,'MD2'),'MD2S%')
                      
                  DataArray<COL$WTD_ACTUAL,ROW$CAP_OUT>           = DataArray<COL$WTD_ACTUAL,ROW$CAP_OUT> + WafersOut
                  DataArray<COL$WTD_ACTUAL,CapFabRow>             = DataArray<COL$WTD_ACTUAL,CapFabRow> + WafersOut
                  DataArray<COL$WTD_ACTUAL,ROW$CAP_CUST_SCRAP>    = DataArray<COL$WTD_ACTUAL,ROW$CAP_CUST_SCRAP> + CustScrap
                  DataArray<COL$WTD_ACTUAL,ROW$CAP_EPI_SCRAP>     = DataArray<COL$WTD_ACTUAL,ROW$CAP_EPI_SCRAP> + IFXEpiScrap
                  DataArray<COL$WTD_ACTUAL,ROW$CAP_OTHER>         = DataArray<COL$WTD_ACTUAL,ROW$CAP_OTHER> + OtherWafers
                  * DataArray<COL$WTD_ACTUAL,ROW$CAP_PRIME_TW>        = DataArray<COL$WTD_ACTUAL,ROW$CAP_PRIME_TW> + PrimeTW              ;* dkk 12/30/13
                  * DataArray<COL$WTD_ACTUAL,ROW$CAP_RECLAIM_TW>  = DataArray<COL$WTD_ACTUAL,ROW$CAP_RECLAIM_TW> + ReclaimTW              ;* dkk 12/30/13
                  
                  
                  IF DataArray<COL$WTD_ACTUAL,ROW$CAP_OUT> > 0 THEN
                      Yield = DataArray<COL$WTD_ACTUAL,ROW$CAP_OUT> / ( DataArray<COL$WTD_ACTUAL,ROW$CAP_OUT> + DataArray<COL$WTD_ACTUAL,ROW$CAP_EPI_SCRAP> )
                  END ELSE
                      Yield = 0
                  END
                  
                  DataArray<COL$WTD_ACTUAL,ROW$CAP_YIELD> = OCONV(ICONV(Yield*100,'MD2'),'MD2S%')
                  
                  * Scrap Rate
                  
                  IF DataArray<COL$WTD_ACTUAL,ROW$CAP_OUT> > 0 THEN
                      ScrapRate = (DataArray<COL$WTD_ACTUAL,ROW$CAP_EPI_SCRAP>/DataArray<COL$WTD_ACTUAL,ROW$CAP_OUT>)*1000
                  END ELSE
                      ScrapRate = 0
                  END
                  
                  DataArray<COL$WTD_ACTUAL,ROW$CAP_SCRAP_RATE> = OCONV(ICONV(ScrapRate,'MD2'),'MD2')
                                                              
              END     ;*  End of check for current fiscal week
          
          END ELSE
          
              DataArray<COL$QTD_ACTUAL,ROW$MER_TOT_OUT>       = DataArray<COL$QTD_ACTUAL,ROW$MER_TOT_OUT> + WafersOut
              DataArray<COL$QTD_ACTUAL,MerProdRow>            = DataArray<COL$QTD_ACTUAL,MerProdRow> + WafersOut
              DataArray<COL$QTD_ACTUAL,ROW$MER_CUST_SCRAP>    = DataArray<COL$QTD_ACTUAL,ROW$MER_CUST_SCRAP> + CustScrap
              DataArray<COL$QTD_ACTUAL,ROW$MER_EPI_SCRAP>     = DataArray<COL$QTD_ACTUAL,ROW$MER_EPI_SCRAP> + IFXEpiScrap
              DataArray<COL$QTD_ACTUAL,ROW$MER_OTHER>         = DataArray<COL$QTD_ACTUAL,ROW$MER_OTHER> + OtherWafers
              * DataArray<COL$QTD_ACTUAL,ROW$MER_PRIME_TW>    = DataArray<COL$QTD_ACTUAL,ROW$MER_PRIME_TW> + PrimeTW                  ;* dkk 12/30/13
              * DataArray<COL$QTD_ACTUAL,ROW$MER_RECLAIM_TW>  = DataArray<COL$QTD_ACTUAL,ROW$MER_RECLAIM_TW> + ReclaimTW              ;* dkk 12/30/13
              
              IF DataArray<COL$QTD_ACTUAL,ROW$MER_TOT_OUT> > 0 THEN
                  Yield = DataArray<COL$QTD_ACTUAL,ROW$MER_TOT_OUT> / ( DataArray<COL$QTD_ACTUAL,ROW$MER_TOT_OUT> + DataArray<COL$QTD_ACTUAL,ROW$MER_EPI_SCRAP> )
              END ELSE
                  Yield = 0
              END
              
              DataArray<COL$QTD_ACTUAL,ROW$MER_YIELD>     = OCONV(ICONV(Yield*100,'MD2'),'MD2S%')
              
              * Scrap Rate
                  
              IF DataArray<COL$QTD_ACTUAL,ROW$MER_TOT_OUT> > 0 THEN
                  ScrapRate = (DataArray<COL$QTD_ACTUAL,ROW$MER_EPI_SCRAP>/DataArray<COL$QTD_ACTUAL,ROW$MER_TOT_OUT>)*1000
              END ELSE
                  ScrapRate = 0
              END
              
              DataArray<COL$QTD_ACTUAL,ROW$MER_SCRAP_RATE> = OCONV(ICONV(ScrapRate,'MD2'),'MD2')
              
              
              * Planning Calcs
              
              
              QTDRem = (ICONV(DataArray<COL$QTD_MF_PLAN,ROW$MER_EPI_SCRAP>,'MD0') - ICONV(DataArray<COL$QTD_ACTUAL,ROW$MER_EPI_SCRAP>,'MD0'))
              
              IF PlanWorkingDays = 0 THEN
                  OrgDailyPlan = 0
              END ELSE
                  *OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_PLAN,ROW$MER_EPI_SCRAP>,'MD0')/PlanWorkingDays
                  OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_REMAIN,ROW$MER_EPI_SCRAP>,'MD0')/PlanDaysRem ;* <- 1/27/2014 JCH **********
                  
              END
              
              DataArray<COL$QTD_MF_REMAIN,ROW$MER_EPI_SCRAP> = OCONV(QTDRem,'MD0,')
              DataArray<COL$ORG_MF_DAILY_PLAN,ROW$MER_EPI_SCRAP> = OCONV(ICONV(OrgDailyPlan,'MD0'),'MD0')
              
              IF OrgDailyPlan NE 0 THEN
                  PlanQTD = ICONV((ICONV(DataArray<COL$QTD_MF_PLAN,ROW$MER_EPI_SCRAP>,'MD0')/PlanWorkingDays)*DaysWorked,'MD0')  ;* <- 3/3/2014 JCH
                  QTDOverUnder = ICONV(DataArray<COL$QTD_ACTUAL,ROW$MER_EPI_SCRAP> - PlanQTD,'MD0')                               ;* <- 3/3/2014 JCH
                  DataArray<COL$QTD_MF_OVER_UNDER,ROW$MER_EPI_SCRAP> = OCONV(QTDOverUnder,'MD0,')                                ;* <- 1/15/2008 JCH 
              END
          
              QTDRem = (ICONV(DataArray<COL$QTD_MF_PLAN,ROW$MER_TOT_OUT>,'MD0') - ICONV(DataArray<COL$QTD_ACTUAL,ROW$MER_TOT_OUT>,'MD0'))
              
              IF PlanWorkingDays = 0 THEN
                  OrgDailyPlan = 0
              END ELSE
                  *OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_PLAN,ROW$MER_TOT_OUT>,'MD0')/PlanWorkingDays
                  OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_REMAIN,ROW$MER_TOT_OUT>,'MD0')/PlanDaysRem   ;* <- 1/27/2014 JCH **********
              END
              
              DataArray<COL$QTD_MF_REMAIN,ROW$MER_TOT_OUT> = OCONV(QTDRem,'MD0,')

              DataArray<COL$ORG_MF_DAILY_PLAN,ROW$MER_TOT_OUT> = OCONV(ICONV(OrgDailyPlan,'MD0'),'MD0')
              
              IF OrgDailyPlan NE 0 THEN
                  PlanQTD = ICONV((ICONV(DataArray<COL$QTD_MF_PLAN,ROW$MER_TOT_OUT>,'MD0')/PlanWorkingDays)*DaysWorked,'MD0')    ;* <- 3/3/2014 JCH
                  QTDOverUnder = ICONV(DataArray<COL$QTD_ACTUAL,ROW$MER_TOT_OUT> - PlanQTD,'MD0')                                 ;* <- 3/3/2014 JCH
                  DataArray<COL$QTD_MF_OVER_UNDER,ROW$MER_TOT_OUT> = OCONV(QTDOverUnder,'MD0,')                                  ;* <- 1/15/2008 JCH 
              END
                              
              QTDRem = (ICONV(DataArray<COL$QTD_MF_PLAN,MerProdRow>,'MD0') - ICONV(DataArray<COL$QTD_ACTUAL,MerProdRow>,'MD0'))
              
              IF PlanWorkingDays = 0 THEN
                  OrgDailyPlan = 0

              END ELSE
                  *OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_PLAN,MerProdRow>,'MD0')/PlanWorkingDays
                  OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_REMAIN,MerProdRow>,'MD0')/PlanDaysRem    ;* <- 1/27/2014 JCH **********
              END
              
              DataArray<COL$QTD_MF_REMAIN,MerProdRow> = OCONV(QTDRem,'MD0,')
              
              DataArray<COL$ORG_MF_DAILY_PLAN,MerProdRow> = OCONV(ICONV(OrgDailyPlan,'MD0'),'MD0')
              
              IF OrgDailyPlan NE 0 THEN
                  PlanQTD = ICONV((ICONV(DataArray<COL$QTD_MF_PLAN,MerProdRow>,'MD0')/PlanWorkingDays)*DaysWorked,'MD0') ;* <- 3/3/2014 JCH
                  QTDOverUnder = ICONV(DataArray<COL$QTD_ACTUAL,MerProdRow> - PlanQTD,'MD0')                              ;* <- 3/3/2014 JCH
                  DataArray<COL$QTD_MF_OVER_UNDER,MerProdRow> = OCONV(QTDOverUnder,'MD0,')                               ;* <- 1/15/2008 JCH
              END
                              
              
              * End of Planning Calcs
              
              IF RDSFiscalWeek = FiscalWeek THEN
              
                  DataArray<DayCol,ROW$MER_TOT_OUT>       = DataArray<DayCol,ROW$MER_TOT_OUT> + WafersOut
                  DataArray<DayCol,MerProdRow>            = DataArray<DayCol,MerProdRow> + WafersOut
                  DataArray<DayCol,ROW$MER_CUST_SCRAP>    = DataArray<DayCol,ROW$MER_CUST_SCRAP> + CustScrap
                  DataArray<DayCol,ROW$MER_EPI_SCRAP>     = DataArray<DayCol,ROW$MER_EPI_SCRAP> + IFXEpiScrap
                  DataArray<DayCol,ROW$MER_OTHER>         = DataArray<DayCol,ROW$MER_OTHER> + OtherWafers
                  * DataArray<DayCol,ROW$MER_PRIME_TW>    = DataArray<DayCol,ROW$MER_PRIME_TW> + PrimeTW                                          ;* dkk 12/30/13
                  * DataArray<DayCol,ROW$MER_RECLAIM_TW>  = DataArray<DayCol,ROW$MER_RECLAIM_TW> + ReclaimTW                                      ;* dkk 12/30/13
                  
                  IF DataArray<DayCol,ROW$MER_TOT_OUT> > 0 THEN
                      Yield = DataArray<DayCol,ROW$MER_TOT_OUT> / ( DataArray<DayCol,ROW$MER_TOT_OUT> + DataArray<DayCol,ROW$MER_EPI_SCRAP> )
                  END ELSE
                      Yield = 0
                  END
                  
                  DataArray<DayCol,ROW$MER_YIELD>     = OCONV(ICONV(Yield*100,'MD2'),'MD2S%')
                      
                  DataArray<COL$WTD_ACTUAL,ROW$MER_TOT_OUT>       = DataArray<COL$WTD_ACTUAL,ROW$MER_TOT_OUT> + WafersOut
                  DataArray<COL$WTD_ACTUAL,MerProdRow>            = DataArray<COL$WTD_ACTUAL,MerProdRow> + WafersOut
                  DataArray<COL$WTD_ACTUAL,ROW$MER_CUST_SCRAP>    = DataArray<COL$WTD_ACTUAL,ROW$MER_CUST_SCRAP> + CustScrap
                  DataArray<COL$WTD_ACTUAL,ROW$MER_EPI_SCRAP>     = DataArray<COL$WTD_ACTUAL,ROW$MER_EPI_SCRAP> + IFXEpiScrap
                  DataArray<COL$WTD_ACTUAL,ROW$MER_OTHER>         = DataArray<COL$WTD_ACTUAL,ROW$MER_OTHER> + OtherWafers
                  * DataArray<COL$WTD_ACTUAL,ROW$MER_PRIME_TW>    = DataArray<COL$WTD_ACTUAL,ROW$MER_PRIME_TW> + PrimeTW                          ;* dkk 12/30/13
                  * DataArray<COL$WTD_ACTUAL,ROW$MER_RECLAIM_TW>  = DataArray<COL$WTD_ACTUAL,ROW$MER_RECLAIM_TW> + ReclaimTW                      ;* dkk 12/30/13
                  
                  IF DataArray<COL$WTD_ACTUAL,ROW$MER_TOT_OUT> > 0 THEN
                      Yield = DataArray<COL$WTD_ACTUAL,ROW$MER_TOT_OUT> / ( DataArray<COL$WTD_ACTUAL,ROW$MER_TOT_OUT> + DataArray<COL$WTD_ACTUAL,ROW$MER_EPI_SCRAP> )
                  END ELSE
                      Yield = 0
                  END
                  
                  DataArray<COL$WTD_ACTUAL,ROW$MER_YIELD>     = OCONV(ICONV(Yield*100,'MD2'),'MD2S%')
                  
                  * Scrap Rate
                  
                  IF DataArray<COL$WTD_ACTUAL,ROW$MER_TOT_OUT> > 0 THEN
                      ScrapRate = (DataArray<COL$WTD_ACTUAL,ROW$MER_EPI_SCRAP>/DataArray<COL$WTD_ACTUAL,ROW$MER_TOT_OUT>)*1000
                  END ELSE
                      ScrapRate = 0
                  END
          
                  DataArray<COL$WTD_ACTUAL,ROW$MER_SCRAP_RATE> = OCONV(ICONV(ScrapRate,'MD2'),'MD2')
                                                              
              END     ;*  End of check for current fiscal week
          END
          
          * Calculations for Reactor Subtotals
          
          DataArray<COL$QTD_ACTUAL,ReactTotRow>           = DataArray<COL$QTD_ACTUAL,ReactTotRow> + WafersOut
          DataArray<COL$QTD_ACTUAL,ReactCustScrapRow>     = DataArray<COL$QTD_ACTUAL,ReactCustScrapRow> + CustScrap
          DataArray<COL$QTD_ACTUAL,ReactIFXEpiScrapRow>    = DataArray<COL$QTD_ACTUAL,ReactIFXEpiScrapRow> + IFXEpiScrap
          DataArray<COL$QTD_ACTUAL,ReactOtherRow>         = DataArray<COL$QTD_ACTUAL,ReactOtherRow> + OtherWafers
          * DataArray<COL$QTD_ACTUAL,ReactPrimeTWRow>     = DataArray<COL$QTD_ACTUAL,ReactPrimeTWRow> + PrimeTW                           ;* dkk 12/30/13
          * DataArray<COL$QTD_ACTUAL,ReactReclaimTWRow>       = DataArray<COL$QTD_ACTUAL,ReactReclaimTWRow> + ReclaimTW                   ;* dkk 12/30/13
          
          
          IF DataArray<COL$QTD_ACTUAL,ReactTotRow> > 0 THEN
              Yield = DataArray<COL$QTD_ACTUAL,ReactTotRow> / ( DataArray<COL$QTD_ACTUAL,ReactTotRow> + DataArray<COL$QTD_ACTUAL,ReactIFXEpiScrapRow> )
          END ELSE
              Yield = 0
          END
          
          DataArray<COL$QTD_ACTUAL,ReactYieldRow> = OCONV(ICONV(Yield*100,'MD2'),'MD2S%')
          
          
          * Scrap Rate
          
          IF DataArray<COL$QTD_ACTUAL,ReactTotRow> > 0 THEN
              ScrapRate = (DataArray<COL$QTD_ACTUAL,ReactIFXEpiScrapRow>/DataArray<COL$QTD_ACTUAL,ReactTotRow>)*1000
          END ELSE
              ScrapRate = 0
          END
          
          DataArray<COL$QTD_ACTUAL,ReactScrapRateRow> = OCONV(ICONV(ScrapRate,'MD2'),'MD2')
          
          * Planning Calcs
          
          QTDRem = (ICONV(DataArray<COL$QTD_MF_PLAN,ReactTotRow>,'MD0') - ICONV(DataArray<COL$QTD_ACTUAL,ReactTotRow>,'MD0'))
          

          
          IF PlanWorkingDays = 0 THEN
              OrgDailyPlan = 0
          END ELSE
              OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_PLAN,ReactTotRow>,'MD0')/PlanWorkingDays
          END
          
          DataArray<COL$QTD_MF_REMAIN,ReactTotRow> = OCONV(QTDRem,'MD0,')
          
          
          DataArray<COL$ORG_MF_DAILY_PLAN,ReactTotRow> = OCONV(ICONV(OrgDailyPlan,'MD0'),'MD0')
          
          IF OrgDailyPlan NE '' THEN
              QTDOverUnder = ICONV(DataArray<COL$QTD_ACTUAL,ReactTotRow> - (DataArray<COL$ORG_MF_DAILY_PLAN,ReactTotRow>*DaysWorked),'MD0')  ;* <- 1/15/2008 JCH
              DataArray<COL$QTD_MF_OVER_UNDER,ReactTotRow> = OCONV(QTDOverUnder,'MD0,')                                                      ;* <- 1/15/2008 JCH
          END
          
          QTDRem = (ICONV(DataArray<COL$QTD_MF_PLAN,ReactIFXEpiScrapRow>,'MD0') - ICONV(DataArray<COL$QTD_ACTUAL,ReactIFXEpiScrapRow>,'MD0'))
          
          
          IF PlanWorkingDays = 0 THEN
              OrgDailyPlan = 0
          END ELSE
              OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_REMAIN,ReactIFXEpiScrapRow>,'MD0')/PlanDaysRem    ;* <- 1/27/2014 JCH **********
          END
          
          DataArray<COL$QTD_MF_REMAIN,ReactIFXEpiScrapRow> = OCONV(QTDRem,'MD0,')
          
          DataArray<COL$ORG_MF_DAILY_PLAN,ReactIFXEpiScrapRow> = OCONV(ICONV(OrgDailyPlan,'MD0'),'MD0')
          
          IF OrgDailyPlan NE '' THEN
              QTDOverUnder = ICONV(DataArray<COL$QTD_ACTUAL,ReactIFXEpiScrapRow> - (DataArray<COL$ORG_MF_DAILY_PLAN,ReactIFXEpiScrapRow>*DaysWorked),'MD0')    ;* <- 1/15/2008 JCH
              DataArray<COL$QTD_MF_OVER_UNDER,ReactIFXEpiScrapRow> = OCONV(QTDOverUnder,'MD0,')                                                               ;* <- 1/15/2008 JCH
          END
          
          * End of Planning Calcs
          
          
          IF RDSFiscalWeek = FiscalWeek THEN
              
              DataArray<DayCol,ReactTotRow>           = DataArray<DayCol,ReactTotRow> + WafersOut
              DataArray<DayCol,ReactCustScrapRow>     = DataArray<DayCol,ReactCustScrapRow> + CustScrap
              DataArray<DayCol,ReactIFXEpiScrapRow>    = DataArray<DayCol,ReactIFXEpiScrapRow> + IFXEpiScrap
              DataArray<DayCol,ReactOtherRow>         = DataArray<DayCol,ReactOtherRow> + OtherWafers
              * DataArray<DayCol,ReactPrimeTWRow>     = DataArray<DayCol,ReactPrimeTWRow> + PrimeTW                               ;* dkk 12/30/13
              * DataArray<DayCol,ReactReclaimTWRow>   = DataArray<DayCol,ReactReclaimTWRow> + ReclaimTW                       ;* dkk 12/30/13
              
              IF DataArray<DayCol,ReactTotRow> > 0 THEN
                  Yield = DataArray<DayCol,ReactTotRow> / ( DataArray<DayCol,ReactTotRow> + DataArray<DayCol,ReactIFXEpiScrapRow> )
              END ELSE
                  Yield = 0
              END
              
              DataArray<DayCol,ReactYieldRow> = OCONV(ICONV(Yield*100,'MD2'),'MD2S%')
                  
              DataArray<COL$WTD_ACTUAL,ReactTotRow>           = DataArray<COL$WTD_ACTUAL,ReactTotRow> + WafersOut
              DataArray<COL$WTD_ACTUAL,ReactCustScrapRow>     = DataArray<COL$WTD_ACTUAL,ReactCustScrapRow> + CustScrap
              DataArray<COL$WTD_ACTUAL,ReactIFXEpiScrapRow>    = DataArray<COL$WTD_ACTUAL,ReactIFXEpiScrapRow> + IFXEpiScrap
              DataArray<COL$WTD_ACTUAL,ReactOtherRow>         = DataArray<COL$WTD_ACTUAL,ReactOtherRow> + OtherWafers
              * DataArray<COL$WTD_ACTUAL,ReactPrimeTWRow>     = DataArray<COL$WTD_ACTUAL,ReactPrimeTWRow> + PrimeTW               ;* dkk 12/30/13
              * DataArray<COL$WTD_ACTUAL,ReactReclaimTWRow>       = DataArray<COL$WTD_ACTUAL,ReactReclaimTWRow> + ReclaimTW       ;* dkk 12/30/13
                  
              IF DataArray<COL$WTD_ACTUAL,ReactTotRow> > 0 THEN
                  Yield = DataArray<COL$WTD_ACTUAL,ReactTotRow> / ( DataArray<COL$WTD_ACTUAL,ReactTotRow>  + DataArray<COL$WTD_ACTUAL,ReactIFXEpiScrapRow> )
              END ELSE
                  Yield = 0
              END
              
              DataArray<COL$WTD_ACTUAL,ReactYieldRow> = OCONV(ICONV(Yield*100,'MD2'),'MD2S%')
              
              * Scrap Rate
          
              IF DataArray<COL$WTD_ACTUAL,ReactTotRow> > 0 THEN
                  ScrapRate = (DataArray<COL$WTD_ACTUAL,ReactIFXEpiScrapRow>/DataArray<COL$WTD_ACTUAL,ReactTotRow>)*1000
              END ELSE
                  ScrapRate = 0
              END
              
              DataArray<COL$WTD_ACTUAL,ReactScrapRateRow> = OCONV(ICONV(ScrapRate,'MD2'),'MD2')
                                                          
          END     ;*  End of check for current fiscal week
          
      END     ;* End of check for Report Exclude flag on customer

  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

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

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
      
      IF Captive THEN
          Row = ROW$CAP_SHIP
      END ELSE
          Row = ROW$MER_SHIP
      END
      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
	  OrgDailyPlan = ICONV(DataArray<COL$QTD_MF_REMAIN,ROW$TOT_SHIP>,'MD0')/PlanDaysRem
	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

FOR I = ROW$CAP_OUT TO ROW$CAP_SHIP
  IF I NE ROW$CAP_YIELD THEN
      FOR J = COL$MON TO COL$QTD_ACTUAL
          DataArray<J,I> = OCONV(DataArray<J,I>,'MD0,Z')
      NEXT J
  END
NEXT I

FOR I = ROW$MER_TOT_OUT TO ROW$MER_SHIP
  IF I NE ROW$MER_YIELD THEN
      FOR J = COL$MON TO COL$QTD_ACTUAL
          DataArray<J,I> = OCONV(DataArray<J,I>,'MD0,Z')
      NEXT J
  END
NEXT I

FOR I = ROW$ASMP_OUT TO ROW$ASMP_OTHER
  FOR J = COL$MON TO COL$QTD_ACTUAL
      DataArray<J,I> = OCONV(DataArray<J,I>,'MD0,Z')
  NEXT J
NEXT I

FOR I = ROW$ASM_OUT TO ROW$ASM_OTHER
  FOR J = COL$MON TO COL$QTD_ACTUAL
      DataArray<J,I> = OCONV(DataArray<J,I>,'MD0,Z')
  NEXT J
NEXT I

FOR I = ROW$HTR3_OUT TO ROW$HTR3_OTHER
  FOR J = COL$MON TO COL$QTD_ACTUAL
      DataArray<J,I> = OCONV(DataArray<J,I>,'MD0,Z')
  NEXT J
NEXT I

FOR I = ROW$EPIPRO_OUT TO ROW$EPIPRO_OTHER
  FOR J = COL$MON TO COL$QTD_ACTUAL
      DataArray<J,I> = OCONV(DataArray<J,I>,'MD0,Z')
  NEXT J
NEXT I

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
  IF OleStatus() THEN debug
NEXT I

edge = OleGetProperty( range, 'Borders' , xlEdgeBottom )                    ;* Set border segment for bottom to medium weight line
olePutProperty( edge , 'LineStyle', xlContinuous )
olePutProperty( edge , 'Weight' , xlMedium )
olePutProperty( edge , 'ColorIndex', xlColorIndexAutomatic )

range = OleGetProperty( xlSht, 'Range', 'A':ROW$MER_SHIP:':':RangeColumn:ROW$MER_SHIP)  ;* Set border for range (underline entire row)

FOR I = 1 TO COUNT(EdgeList,@VM) + (EdgeList NE '')
  edge = OleGetProperty( range, 'Borders' , EdgeList<1,I> )               
  OlePutProperty( edge , 'LineStyle' , xlLineStyleNone )
NEXT I

edge = OleGetProperty( range, 'Borders' , xlEdgeBottom )
olePutProperty( edge , 'LineStyle', xlContinuous )
olePutProperty( edge , 'Weight' , xlMedium )
olePutProperty( edge , 'ColorIndex', xlColorIndexAutomatic )


* Void = OleCallMethod( xlWkb, 'SaveAs','C:\OIReports\ThruPut.xls', xlWorkbookNormal )        ;* Save to local drive
Void = OleCallMethod( xlWkb, 'SaveAs',Environment_Services('GetReportsRootPath') : '\ThruPut.xls', xlWorkbookNormal )        ;* Save to local drive

RETURN

* * * * * * *
HadError:
* * * * * * *

xlChart = ''
Charts = ''
range = ''
xlSht = ''
xlWkb=''
xlWorkBooks = ''
x = OleCallMethod(xlApp, 'Quit')

RETURN 

/* end of code */

*


/*
* Insert 4 blank rows at the top

range = OleGetProperty( xlSht, 'Range','1:1')
rows = OleGetProperty( range, 'Rows' )

void = OleCallMethod( rows, 'Insert', xlShiftDown )
void = OleCallMethod( rows, 'Insert', xlShiftDown )
void = OleCallMethod( rows, 'Insert', xlShiftDown )
void = OleCallMethod( rows, 'Insert', xlShiftDown )

* Merge the 4 rows and setup for header text

range = OleGetProperty( xlSht, 'Range','A1:':LastColumn:'4')
OlePutProperty( range , 'HorizontalAlignment' , xlCenter )
OlePutProperty( range , 'VerticalAlignment' , xlCenter )
OlePutProperty( range , 'WrapText' , xlFalse )
OlePutProperty( range , 'Orientation' , '0' )
OlePutProperty( range , 'AddIndent' , xlFalse )
OlePutProperty( range , 'IndentLevel' , '0' )
OlePutProperty( range , 'ShrinkToFit' , xlFalse )
OlePutProperty( range , 'ReadingOrder' , xlContext )
OlePutProperty( range , 'MergeCells' , xlTrue )

font = OleGetProperty( range , 'Font' )
OlePutProperty( font , 'Name' , 'Arial' )
OlePutProperty( font , 'FontStyle' , 'Bold' )
OlePutProperty( font , 'Size' , '12' )
OlePutProperty( font , 'Strikethrough' , xlFalse )
OlePutProperty( font , 'Superscript' , xlFalse )
OlePutProperty( font , 'Subscript' , xlFalse )
OlePutProperty( font , 'OutlineFont' , xlFalse )
OlePutProperty( font , 'Shadow' , xlFalse )
OlePutProperty( font , 'Underline' , xlUnderLineStyleNone )
OlePutProperty( font , 'ColorIndex' , xlAutomatic )


range = OleGetProperty( xlSht, 'Range','A1:':LastColumn:'4')
OlePutProperty(range, 'Value', 'Thruput Report - w/o Makeup Activity')                          ;* Put the title text in

pictures = OleGetProperty( xlSht , 'Pictures' )
void = OleCallMethod( pictures , 'Insert' , 'R:\Oinsight\BMPS\EpiSvcs.png' )                    ;* Insert graphic in u/l corner

shapes = OleGetProperty( xlSht , 'Shapes' )                                                     ;* This is a collection
shaperange = OleGetProperty( shapes , 'Range' , '1' )                                           ;* This selects 1 or more of the collection to work with
void = OleCallMethod( shaperange , 'ScaleWidth' , '0.41' , msoFalse , msoScaleFromTopLeft )     ;* Scale the graphic to fit in the 4 line header
void = OleCallMethod( shaperange , 'ScaleHeight' , '0.41' , msoFalse , msoScaleFromTopLeft )
*/
