COMPILE ROUTINE TEST_obj_Calendar(Dummy)
ROWDEF(CHARSTR)

DECLARE SUBROUTINE Set_Status, Send_Info, obj_Calendar, Send_Dyn, ErrMsg
DECLARE FUNCTION Get_Status, obj_Calendar

$INSERT FISCAL_YR_EQUATES
$INSERT FISCAL_QTR_EQUATES

DEBUG
CutOffDt = '6/29/2013'

thisCutOffDt = ICONV(CutOffDt,'D4/')
IF thisCutOffDt = '' THEN
	ErrMsg('Invalid Date Entered.')
	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>

* * * * * 

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



RETURN	;* * * * * * * * *


*DateIn = OCONV(Date(),'D4/')

DateIn = "Feb 18, 2013"

a = obj_Calendar('IRFiscalMonth',DateIn)

DEBUG

RETURN   ; * * * * * * *  * *


Set_Status(0)

debug

MonthNos = ''
MonthStartDts = ''
MonthEndDts = ''

YearRec = XLATE('FISCAL_YR','2014','','X')

FYStartDt	= YearRec<FISCAL_YR_START_DT$>
FYWeeks		= YearRec<FISCAL_YR_WEEKS_IN_YEAR$>

FYQtrKeys = YearRec<FISCAL_YR_FISCAL_QTR_KEY$>

QtrStartDts = XLATE('FISCAL_QTR',FYQtrKeys,FISCAL_QTR_START_DT$,'X')
QtrEndDts	= XLATE('FISCAL_QTR',FYQtrKeys,FISCAL_QTR_END_DT$,'X')

WeekStartDts	= ''
WeekEndDts		= ''

FOR WeekNo = 1 TO FYWeeks
	WeekStartDts<WeekNo> = FYStartDt + ((WeekNo - 1) * 7)
	
	BEGIN CASE
		CASE WeekNo <= 13	; Qtr = 1
		CASE WeekNo <= 27	; Qtr = 2
		CASE WeekNo <= 39	; Qtr = 3
		CASE 1				; Qtr = 4
	END CASE
	
	IF WeekNo =  1 THEN MonthStartDts<1,1> = WeekStartDts<WeekNo>
	IF WeekNo =  5 THEN MonthStartDts<1,2> = WeekStartDts<WeekNo>
	IF WeekNo =  9 THEN MonthStartDts<1,3> = WeekStartDts<WeekNo>
	IF WeekNo = 14 THEN MonthStartDts<1,4> = WeekStartDts<WeekNo>
	IF WeekNo = 18 THEN MonthStartDts<1,5> = WeekStartDts<WeekNo>
	IF WeekNo = 22 THEN MonthStartDts<1,6> = WeekStartDts<WeekNo>
	IF WeekNo = 27 THEN MonthStartDts<1,7> = WeekStartDts<WeekNo>
	IF WeekNo = 31 THEN MonthStartDts<1,8> = WeekStartDts<WeekNo>
	IF WeekNo = 35 THEN MonthStartDts<1,9> = WeekStartDts<WeekNo>
	IF WeekNo = 40 THEN MonthStartDts<1,10> = WeekStartDts<WeekNo>
	IF WeekNo = 44 THEN MonthStartDts<1,11> = WeekStartDts<WeekNo>
	
	IF FYWeeks = 53 THEN
		IF WeekNo = 49 THEN MonthStartDts<1,12> = WeekStartDts<WeekNo>
	END ELSE
		IF WeekNo = 48 THEN MonthStartDts<1,12> = WeekStartDts<WeekNo>
	END
	
	
	CurrMonthStartDt = OCONV(MonthStartDts[-1,'B':@FM],"D4/")
	Send_Dyn(Qtr:' Month: ':CurrMonthStartDt:' ':WeekNo:' ':OCONV(WeekStartDts<WeekNo>,'D4/'):' ':Qtr)

NEXT WeekNo



FOR I = 1 TO 12
	MonthNos<1,I> = I
	IF I < 12 THEN
		MonthEndDts<1,I> = MonthStartDts<1,I+1> - 1
	END ELSE
		MonthEndDts<1,I> = QtrEndDts<1,4>
	END
NEXT I

MonthStartDts = OCONV(MonthStartDts,'D4/')
MonthEndDts = OCONV(MonthEndDts,'D4/')

DEBUG


GOTO Bail
/*

FOR Qtr = 1 TO 4
	QtrRec = XLATE('FISCAL_QTR',FYQtrKeys<1,I>,'','X')
	
	QtrStartDt	= QtrRec<FISCAL_QTR_START_DT$>
	QtrEndDt	= QtrRec<FISCAL_QTR_END_DT$>
	
	QtrDelta = QtrEndDt - QtrStartDt
	
	QtrWeeks = INT(QtrDelta/7) + 1
	
	FOR I = 1 TO QtrWeeks
		WeekStartDt = StartDt - FYStartDt
		WeekNo = INT(Delta/7) + 1
		WeekStartDt = FYStartDate + ((WeekNo - 1) * 7)
	 
NEXT Qtr

*/
RETURN
DateIn = '02/11/2013'

WeekData = obj_Calendar('IRFiscalWeek',DateIn)

IF Get_Status(errCode) THEN 
	DEBUG
END

B = OCONV(DateOut,'D4/')


debug
* * * * * * *
Bail:
* * * * * * *

END
