salesforce——Apex查询公司财年和财季开始和结束时间(SOQL)

salesforce——Apex查询公司财年和财季开始和结束时间

关于财年和财季

每个公司一般都会有自己的财年(fiscal year)和财季(fiscal quarter),财年即当前年份便是今年的财年,而财季会因为公司体制等原因各有不同,每三个月份便是一个财季,比如一个公司的财季从2月开始,那么第一个财季就是2-4月,一个财季开始的时间也就是2月1日,结束时间就是4月30日,以此类推。

SOQL查询

下面的类可以返回当前财年、当前财季以及当前季度(1,2,3,4)或当前财季开始时间和结束时间,具体的实现也有注释可看。

public with sharing class GetFiscalYearStartDate {
    //财年开始时间
    private Date fiscalYearStartDate;
    //财年开始月份
    private Integer FiscalYearStartMonth;
    //当前季度
    private Integer currentyFiscalQuarter;
    //当前季度开始时间
    private Date currentyDateOfThisQuarter;
    //当前季度最后一天
    private Date lastDateOfThisQuarter;
    

    public GetFiscalYearStartDate() {
        //获取财年开始月份
        FiscalYearStartMonth = [select FiscalYearStartMonth from Organization where id=:Userinfo.getOrganizationId()].FiscalYearStartMonth;
        System.debug('FiscalYearStartMonth:' + FiscalYearStartMonth);
        //计算当前财季
        if(system.today().month() >= fiscalYearStartMonth){
            fiscalYearStartDate = date.newinstance(system.today().year(), FiscalYearStartMonth, 1);
            currentyFiscalQuarter = ((system.today().month() - FiscalYearStartMonth) / 3) + 1;
        }
        else{
            fiscalYearStartDate = date.newinstance(system.today().year() - 1, FiscalYearStartMonth, 1);
            currentyFiscalQuarter = ((12 + system.today().month() - FiscalYearStartMonth) / 3) + 1;
        } 
        Integer addMonths = currentyFiscalQuarter * 3;
        System.debug('currentyFiscalQuarter:' + currentyFiscalQuarter + ';  addMonths:' + addMonths);
        lastDateOfThisQuarter = fiscalYearStartDate ;
        //季度最后一天(比如财年从1月1日开始,现在是第4季度,那么就加12,就是明年的1月1日,再减1,就是12月31日);
        lastDateOfThisQuarter = lastDateOfThisQuarter.addMonths(addMonths).addDays(-1);//addDays(-1)
        //季度最开始的那天
        currentyDateOfThisQuarter = fiscalYearStartDate.addMonths(addMonths-3);

    }

    /***
    返回季度开始的第一天 
    no problem*/
    public Date getBeginDateOfThisQuarter(){  
        return currentyDateOfThisQuarter;
    }

    /***
    返回当前季度最后一天 */
    public Date getlastDateOfThisQuarter(){  
        return lastDateOfThisQuarter;
    }

    /***
    返回当前季度 */
    public Integer getcurrentFiscalQuarter(){
        return currentyFiscalQuarter;
    }

    /**
    返回当前季度已经过去天数的百分比(分子)
    no problem */
    public Integer getDaysElaspsedByCurrentQuarter(){
        //季度天数
        Integer quarterDays = currentyDateOfThisQuarter.daysBetween(lastDateOfThisQuarter);
        //当前季度过去的天数除以季度天数,再取整数
        Date today = System.today();
        Integer daysElaspsedByCurrentQuarter = ( (currentyDateOfThisQuarter.daysBetween(today))  * 100 / quarterDays );
        System.debug('季度天数:' + quarterDays + '季度过去多少天(百分比):' + daysElaspsedByCurrentQuarter);
        if(daysElaspsedByCurrentQuarter <= 30){
            return 30;
        }else if(daysElaspsedByCurrentQuarter >= 80){
            return 80;
        }else{
            return daysElaspsedByCurrentQuarter;
        }
    } 
}

若有异议欢迎留言讨论,共同学习,共勉!!
Thx!!

发布了93 篇原创文章 · 获赞 26 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_38261445/article/details/103548010