Jul 30, 2012

Is There Better Way To Get Last Date Of Fiscal Quarter?

Recently it was requested to get the "Last Date" of present Fiscal Quarter. For e.g. if "Fiscal Year Start Month" is set to January (Setup > Administration Setup > Company Profile > Fiscal Year) and present month is Feb then date returned should be "31 March 2012"


SOQL is not the option as in the end I need to get the Date so after lot of brain storming end up with this solution, if this is not the best solution then other solutions suggestions are most welcomed.


So here is my apex method which returns me the last date of present fiscal quarter


//Returns last date of present fiscal quarter
public Date LastDateofCurrentFQ()
    {
        // Info : There are 4 Fiscal Quarter FQ( each of 3 months) in fiscal year 
        Organization  org = new Organization() ;
        org = [select FiscalYearStartMonth from Organization where id=:Userinfo.getOrganizationId()];
        Integer FiscalYearStartMonthIndex = org.FiscalYearStartMonth ; 

        // Month Index like january : 1 , february: 2 and so on 
        Integer CurrentMonthIndex = system.today().month();

        if(CurrentMonthIndex < FiscalYearStartMonthIndex )
            CurrentMonthIndex  = 12+ CurrentMonthIndex ;

        Integer DifferenceofBothMonths = CurrentMonthIndex - FiscalYearStartMonthIndex  ;
        Integer CurrentFiscalQuarterLastMonth  = 0; 

 if(0 < = DifferenceofBothMonths && DifferenceofBothMonths <= 2 ) //For first quarter
            CurrentFiscalQuarterLastMonth = FiscalYearStartMonthIndex  +2 ; 
        else if( 3 <= DifferenceofBothMonths && DifferenceofBothMonths <= 5) //For second quarter 
            CurrentFiscalQuarterLastMonth = FiscalYearStartMonthIndex  +5 ;
        else if( 6 <= DifferenceofBothMonths && DifferenceofBothMonths <= 8) // For third quarter
            CurrentFiscalQuarterLastMonth = FiscalYearStartMonthIndex  +8 ;
        else if( 9 <= DifferenceofBothMonths && DifferenceofBothMonths <= 11) // For fourth quarter
            CurrentFiscalQuarterLastMonth = FiscalYearStartMonthIndex  +11 ;

        if(CurrentFiscalQuarterLastMonth > 12)
            CurrentFiscalQuarterLastMonth  = CurrentFiscalQuarterLastMonth  - 12 ;         

        Date firstDateOFyear = date.newInstance(system.today().year(), 1, 1);
        Date LastDateofCurrentFQ  = date.newInstance(system.today().year(), 1, 1); 
        LastDateofCurrentFQ   = firstDateOFyear.addMonths(CurrentFiscalQuarterLastMonth).addDays(-1) ;
        return LastDateofCurrentFQ ;        
    }

Code is self explanatory, still if anything is not clear do reach me out.

5 comments:

  1. Recently had to do something similar. (Word of warning: to query the organization, the user will need to have the view all data permission. We used a 'without sharing' class to query the org.)

    // get fiscal year start month
    Integer FiscalYearStartMonthIndex = [select FiscalYearStartMonth
    from Organization
    where id=:Userinfo.getOrganizationId()].FiscalYearStartMonth;

    // find end of first quarter of current year
    Date quarterEnd = Date.newInstance(system.today().year(), FiscalYearStartMonthIndex, 1).addMonths(3).addDays(-1);

    // if we are past the current quarter, move to the next one.
    while (system.today() >= quarterEnd) {
    quarterEnd = quarterEnd.addMonths(3);
    }

    return quarterEnd;

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I think you can also use the Math.mod to do the required transformation:

    public class FinancialQuarterTool {

    public static void example(){

    Integer fiscalYearStartMonth = [select FiscalYearStartMonth from Organization where id=:Userinfo.getOrganizationId()].FiscalYearStartMonth;
    System.debug(lastDateOfFq(Date.today(),fiscalYearStartMonth));

    }

    public static Date firstDateOfFq(Date currentDate, Integer fiscalYearStartMonth ){

    Integer offset = currentDate.month() + 12 - fiscalYearStartMonth;
    offset = Math.mod(offset, 3);
    return currentDate.addMonths(-offset).toStartOfMonth();

    }

    public static Date lastDateOfFq(Date currentDate, Integer fiscalYearStartMonth ){

    Date d = firstDateOfFq(currentDate, fiscalYearStartMonth );
    return d.addMonths(3).addDays(-1);

    }

    }

    ReplyDelete
  4. Date dt = date.newInstance(1014,12, 17);
    Integer currentMnt =dt.month();
    Integer currentQ =((currentMnt-1)/3) + 1;
    Date endOfQDate = date.newInstance(dt.year(),currentMnt + (4 - (currentMnt - ((currentQ -1)*3))) , 1).addDays(-1);
    system.debug(endOfQDate);

    ReplyDelete