0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Salesforce_IRR (Internal rate of return) Formula Accuracy

Posted at
    List<SaisankeisanNyushukkinMeisaiRireki__c> totalLst = [
                                                                SELECT
                                                                // SFDC Id
                                                                Id
                                                                // 案件管理番号
                                                                ,Ankenkanribangou__c
                                                                // 採算計算番号
                                                                ,Saisankeisanbangou__c
                                                                // 更新回数
                                                                ,Koshinkaisu__c
                                                                // 回収支払年月
                                                                ,KaishuShiharaiNengetsu__c
                                                                // 入出金金額
                                                                ,KakukaiKingaku__c 
                                                                FROM 
                                                                SaisankeisanNyushukkinMeisaiRireki__c 
                                                                WHERE  
                                                                // [採算計算入出金明細履歴].案件管理番号 = [引数].案件管理番号 関連関係
                                                                Ankenkanribangou__c =: rireki.Ankenkanribangou__c AND 
                                                                // [採算計算入出金明細履歴].採算計算番号 = [引数].採算計算番号 関連関係
                                                                Ankenkanribangou__c =: rireki.Saisankeisanbangou__c 
                                                                // [採算計算入出金明細履歴].更新回数 = [引数].更新回数 かつ		
                                                                // Loopに計算する
                                                                // [採算計算入出金明細履歴].回収支払年月 = [引数].回収支払年月 かつ	
                                                                // Loopに計算する
                                                                //KaishuShiharaiNengetsu__c =: kaishuShiharaiNengetsuStartの(今回ループ回数目 - 1) カ月後
                                                                // [採算計算入出金明細履歴].回収支払サイン = ”回収”	of ”支払”	
                                                                // Loopに計算する
                                                                Order BY KaishuShiharaiNengetsu__c ASC];
    Map<Date,Double> paramMap = new Map<Date,Double>();
    for(Integer i = 0;i < totalLst.size();i++){
        Double min = 0;
        Double max = 0;
        // 回収支払サイン : ”回収” && 更新回数 : 画面変数
        if(kaishu[i].KaishuShiharaiFormula__c == true && kaishu[i].Koshinkaisu__c == koushinKaisuOld && kaishu[i].KaishuShiharaiNengetsu__c == kaishuShiharaiNengetsuStart.addMonths(i)){
            // [採算計算入出金明細履歴].回収支払年月 = [引数].回収支払年月
            min = kaishu[i].KakukaiKingaku__c;
        }

        // 回収支払サイン : ”支払” && 更新回数 : 画面変数
        if(kaishu[i].KaishuShiharaiFormula__c == false && kaishu[i].Koshinkaisu__c == koushinKaisuOld && kaishu[i].KaishuShiharaiNengetsu__c == kaishuShiharaiNengetsuStart.addMonths(i)){
            // [採算計算入出金明細履歴].回収支払年月 = [引数].回収支払年月
            max = kaishu[i].KakukaiKingaku__c;
        }
        paramMap.put(kaishu[i].KaishuShiharaiNengetsu__c,max - min);
    }

    Double IRR = IRRCalc(paramMap, 0.001) * 0.01;
    System.debug(IRR);
    // 6-10.採算計算基本履歴の登録
    update rireki;

}

public Double IRRCalc(Map<Date,Double> paramMap, Double guest){
    // NPV
    Double NPV = 0;
    Double inc = 0.000001;
    do {
        guest += inc;
        NPV = 0;
        // 6-6-3.利回りの算出
        for(Integer j=0; j < totalLst.size(); j++){
            NPV += totalLst[j] / Math.pow((1 + guest), j);
        }
    }while (NPV > 0);
    return guest * 100;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?