=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialAprilPeriodType.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialAprilPeriodType.java 2010-11-17 03:17:16 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialAprilPeriodType.java 2010-11-17 10:58:45 +0000 @@ -72,12 +72,13 @@ private Period createPeriod( Calendar cal ) { cal.set( Calendar.YEAR, cal.get( Calendar.YEAR ) + cal.get( Calendar.MONDAY ) / 7 - 1); - cal.set( Calendar.DAY_OF_YEAR, cal.getActualMinimum( Calendar.DAY_OF_YEAR ) + 90 ); + cal.set( Calendar.MONTH, Calendar.APRIL ); + cal.set( Calendar.DATE, 1 ); Date startDate = cal.getTime(); cal.add( Calendar.YEAR, 1 ); - cal.set( Calendar.DAY_OF_YEAR, cal.getActualMinimum( Calendar.DAY_OF_YEAR ) + 89 ); + cal.set( Calendar.DAY_OF_YEAR, cal.get( Calendar.DAY_OF_YEAR ) - 1 ); return new Period( this, startDate, cal.getTime() ); } @@ -97,7 +98,7 @@ { Calendar cal = createCalendarInstance( period.getStartDate() ); - cal.set( Calendar.YEAR, cal.get( Calendar.YEAR ) - cal.get( Calendar.YEAR ) % 2 + 10 ); + cal.set( Calendar.YEAR, cal.get( Calendar.YEAR ) - cal.get( Calendar.YEAR ) % 2 + 12 ); Date startDate = cal.getTime(); cal.add( Calendar.YEAR, 1 ); @@ -110,7 +111,7 @@ public Period getPreviousPeriod( Period period ) { Calendar cal = createCalendarInstance( period.getStartDate() ); - cal.set( Calendar.YEAR, cal.get( Calendar.YEAR ) - cal.get( Calendar.YEAR ) % 2 - 1 ); + cal.set( Calendar.YEAR, cal.get( Calendar.YEAR ) - cal.get( Calendar.YEAR ) % 2 + 2 ); Date startDate = cal.getTime(); cal.add( Calendar.YEAR, 1 ); @@ -128,7 +129,8 @@ ArrayList years = new ArrayList(); Calendar cal = createCalendarInstance( date ); - cal.add( Calendar.YEAR, cal.get( Calendar.YEAR ) % 2 == 0 ? -10 : -9 ); + cal.set( Calendar.YEAR, cal.get( Calendar.YEAR ) + cal.get( Calendar.MONDAY ) / 7 - 11); + cal.set( Calendar.DAY_OF_YEAR, cal.getActualMinimum( Calendar.DAY_OF_YEAR ) + 90 ); for ( int i = 0; i < 11; ++i ) { === added file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/FinancialAprilPeriodTypeTest.java' --- dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/FinancialAprilPeriodTypeTest.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/FinancialAprilPeriodTypeTest.java 2010-11-17 10:58:45 +0000 @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2004-2010, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.hisp.dhis.period; + +import static junit.framework.Assert.assertEquals; + +import java.util.Calendar; +import java.util.List; + +import org.junit.Test; + +/** + * @author Lars Helge Overland + */ + +public class FinancialAprilPeriodTypeTest +{ + @Test + public void testCreatePeriod() + { + FinancialAprilPeriodType periodType = new FinancialAprilPeriodType(); + + Calendar testCal = PeriodType.createCalendarInstance(); + testCal.set( 2009, Calendar.FEBRUARY, 15 ); + + Calendar startCal = PeriodType.createCalendarInstance(); + startCal.set( 2008, Calendar.APRIL, 1 ); + Calendar endCal = PeriodType.createCalendarInstance(); + endCal.set( 2009, Calendar.MARCH, 31 ); + + Period period = periodType.createPeriod( testCal.getTime() ); + + assertEquals( startCal.getTime(), period.getStartDate() ); + assertEquals( endCal.getTime(), period.getEndDate() ); + + + testCal.set( 2009, Calendar.SEPTEMBER, 12 ); + + period = periodType.createPeriod( testCal.getTime() ); + + startCal.set( 2009, Calendar.APRIL, 1 ); + endCal.set( 2010, Calendar.MARCH , 31 ); + + assertEquals( startCal.getTime(), period.getStartDate() ); + assertEquals( endCal.getTime(), period.getEndDate() ); + } + + @Test + public void testGeneratePeriods() + { + FinancialAprilPeriodType periodType = new FinancialAprilPeriodType(); + + Calendar testCal = PeriodType.createCalendarInstance(); + testCal.set( 2009, 1, 15 ); + + List periods = periodType.generatePeriods( testCal.getTime() ); + + Calendar startCal = PeriodType.createCalendarInstance(); + startCal.set( 1998, Calendar.APRIL, 1 ); + Calendar endCal = PeriodType.createCalendarInstance(); + endCal.set( 1999, Calendar.MARCH, 31 ); + + Period startPeriod = periods.get( 0 ); + assertEquals( startCal.getTime(), startPeriod.getStartDate() ); + assertEquals( endCal.getTime(), startPeriod.getEndDate() ); + + startCal = PeriodType.createCalendarInstance(); + startCal.set( 2008, Calendar.APRIL, 1 ); + endCal = PeriodType.createCalendarInstance(); + endCal.set( 2009, Calendar.MARCH, 31 ); + + Period endPeriod = periods.get( periods.size() - 1 ); + assertEquals( startCal.getTime(), endPeriod.getStartDate() ); + assertEquals( endCal.getTime(), endPeriod.getEndDate() ); + + } +}