175 lines
6.1 KiB
Java
175 lines
6.1 KiB
Java
package com.tenwa.reckon.help;
|
||
|
||
import java.math.BigDecimal;
|
||
import java.text.ParseException;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
|
||
import com.tenwa.reckon.bean.ConditionBean;
|
||
import com.tenwa.reckon.bean.KnowingConfigBean;
|
||
import com.tenwa.reckon.bean.KnowingTableBean;
|
||
import com.tenwa.reckon.bean.TabCalBean;
|
||
import com.tenwa.reckon.constant.Scale;
|
||
import com.tenwa.reckon.util.IRRCalculateUtil;
|
||
|
||
public class CalYearRateFromRent {
|
||
|
||
private static CalYearRateFromRent rent;
|
||
private CalYearRateFromRent(){}
|
||
|
||
public static CalYearRateFromRent getInstance(){
|
||
if(rent == null){
|
||
rent = new CalYearRateFromRent();
|
||
}
|
||
return rent;
|
||
}
|
||
|
||
public void calDo(ConditionBean cb,TabCalBean tcb) throws Exception{
|
||
String rentOrRate = cb.getRentOrRate();
|
||
String settleMethod = cb.getSettleMethod();
|
||
if("rent".equals(rentOrRate) || "knowing_rent".equals(rentOrRate)){
|
||
if("even_interest".equals(settleMethod)){
|
||
calDoByEvenInterest(cb);//均息法
|
||
}
|
||
if("even_rent".equals(settleMethod)){
|
||
calDoByEvenRent(cb,tcb);//等额租金
|
||
}
|
||
if("even_rent_day".equals(settleMethod)){
|
||
calDoByEvenRentDay2(cb, tcb);
|
||
}
|
||
}
|
||
if("rent_period".equals(rentOrRate)||"rent_period_360".equals(rentOrRate)){//已知租金表
|
||
calDoByEvenRentDay2(cb,tcb);
|
||
}
|
||
|
||
}
|
||
/**
|
||
* 等额租金算IRR 做为年利率
|
||
* @param cb
|
||
* @param tcb
|
||
*/
|
||
private void calDoByEvenRent(ConditionBean cb,TabCalBean tcb){
|
||
//period_type_1 前收 期初1/期末0
|
||
String periodType = cb.getPeriodType();
|
||
//每期租金
|
||
String rent = cb.getRentValue();
|
||
//还款间隔 月
|
||
int yearIncomeNumber = cb.getIncomeIntervalMonth();
|
||
//还款期次
|
||
String incomenumber = String.valueOf(cb.getIncomeNumber());
|
||
List<BigDecimal> rentList = new ArrayList<BigDecimal>();
|
||
if(cb.getRentOrRate().equals("knowing_rent")){
|
||
List<KnowingConfigBean> beans = tcb.getKnowingConfigs();
|
||
rentList = KnowingRentTools.convertKnowingBeanToList(beans);
|
||
}else{
|
||
for (int i = 0; i < Integer.parseInt(incomenumber); i++) {
|
||
rentList.add( new BigDecimal(rent) );
|
||
}
|
||
}
|
||
//融资额
|
||
String leaseMoney = cb.getCleanLeaseMoney();
|
||
//期末余值
|
||
String equipendvalue = cb.getEquipEndValue();
|
||
//循环封装租金用于构建现金流
|
||
if("period_type_1".equals(periodType) || "1".equals(periodType)){// 期初
|
||
BigDecimal leaseAmt = new BigDecimal(leaseMoney).multiply(new BigDecimal(-1));
|
||
BigDecimal first = rentList.get(0);
|
||
rentList.set(0,leaseAmt.add(first));
|
||
}
|
||
if("period_type_0".equals(periodType) || "0".equals(periodType)){// 期末
|
||
rentList.add(0,new BigDecimal(leaseMoney).multiply(new BigDecimal(-1)));
|
||
}
|
||
//期末余值处理:计算年利率时,该值加在最后一期上
|
||
BigDecimal endMoney = rentList.get(rentList.size()-1);
|
||
endMoney = endMoney.add(new BigDecimal(equipendvalue));
|
||
if("period_type_1".equals(periodType) || "1".equals(periodType)){// 期初
|
||
rentList.add(rentList.size(),new BigDecimal(equipendvalue));
|
||
}
|
||
if("period_type_0".equals(periodType) || "0".equals(periodType)){// 期末
|
||
rentList.set(rentList.size()-1,endMoney);
|
||
}
|
||
//计算期IRR
|
||
BigDecimal issueRate = IRRCalculateUtil.getIRR(rentList);
|
||
//期IRR乘以还款间隔即为年利率
|
||
BigDecimal newYearRate = issueRate.multiply(new BigDecimal(1200/yearIncomeNumber));
|
||
//返回年利率
|
||
cb.setYearRate(newYearRate.toString());
|
||
}
|
||
|
||
/**
|
||
* 等额租金按
|
||
* @param cb
|
||
* @param tcb
|
||
* @throws Exception
|
||
*/
|
||
private void calDoByEvenRentDay2(ConditionBean cb,TabCalBean tcb) throws Exception{
|
||
//period_type_1 前收 期初1/期末0
|
||
//String periodType = cb.getPeriodType();
|
||
//每期租金
|
||
String rent = cb.getRentValue();
|
||
//还款期次
|
||
String incomenumber = String.valueOf(cb.getIncomeNumber());
|
||
List<String> rentList = new ArrayList<String>();
|
||
// 加载日期列表
|
||
PlanDateServiceImpl pdsi = new PlanDateServiceImpl();
|
||
List<String> dateList = pdsi.getPlanDateList(cb,"0");
|
||
dateList=pdsi.getAdjustDate(dateList, cb.getPlanDateHoilday());
|
||
if("interest_date".equals(cb.getInterestDayType()))
|
||
{
|
||
dateList=pdsi.getInterestDateList(cb);
|
||
dateList=pdsi.getAdjustDate(dateList, cb.getInterestDateHoliday());
|
||
}
|
||
if(cb.getRentOrRate().equals("knowing_rent")){
|
||
List<KnowingConfigBean> beans = tcb.getKnowingConfigs();
|
||
rentList = KnowingRentTools.convertKnowingBeanToStrList(beans);
|
||
}else if("rent_period".equals(cb.getRentOrRate())||"rent_period_360".equals(cb.getRentOrRate())){
|
||
List<KnowingTableBean> beans = tcb.getKnowingTables();
|
||
rentList = KnowingTableTools.convertKnowingBeanToStrList(beans);
|
||
dateList=KnowingTableTools.convertKnowingBeanToDateList(beans);
|
||
} else{
|
||
for (int i = 0; i < Integer.parseInt(incomenumber); i++) {
|
||
rentList.add(rent);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
//融资额
|
||
String leaseMoney = cb.getCleanLeaseMoney();
|
||
//期末余值
|
||
String equipendvalue = cb.getEquipEndValue();
|
||
//XIRR为年利率
|
||
String newYearRate =IRRCalculateUtil.getIRRNew(leaseMoney,equipendvalue,cb.getGrace(),rentList, dateList,cb.getStartDate(),cb.getRentOrRate());
|
||
//返回年利率
|
||
cb.setYearRate(new BigDecimal(newYearRate).multiply(new BigDecimal(100)).toString());
|
||
}
|
||
|
||
/**
|
||
* 均息法 算年利率
|
||
* @param cb
|
||
*/
|
||
private void calDoByEvenInterest(ConditionBean cb){
|
||
//每一期租金
|
||
String rent = cb.getRentValue();
|
||
//还款周期
|
||
String leaseTerm = String.valueOf(cb.getLeaseTerm());
|
||
//还款次数
|
||
Integer incomeNumber = cb.getIncomeNumber();
|
||
//净融资额
|
||
String leaseMoney = cb.getCleanLeaseMoney();
|
||
//宽限期数
|
||
Integer grace = cb.getGrace() ;
|
||
//计算每期本金:总融资额/还租次数
|
||
BigDecimal issueCorpus = new BigDecimal(leaseMoney).subtract(new BigDecimal(cb.getEquipEndValue())).divide(new BigDecimal(incomeNumber), Scale.RATE_SCALE, BigDecimal.ROUND_HALF_UP);
|
||
//利息 = 租金 - 本金
|
||
BigDecimal issueInterest = new BigDecimal(rent).subtract(issueCorpus);
|
||
//计算利息总额
|
||
BigDecimal allInterest = issueInterest.multiply(new BigDecimal(incomeNumber).add(new BigDecimal(grace)));
|
||
//总利息/总本金/年限(租赁期限/12)
|
||
BigDecimal newYearRate = allInterest.multiply(new BigDecimal(100)).divide(new BigDecimal(leaseMoney), Scale.RATE_SCALE, BigDecimal.ROUND_HALF_UP).divide(new BigDecimal(leaseTerm).divide(new BigDecimal(12), 20, BigDecimal.ROUND_HALF_UP), Scale.RATE_SCALE, BigDecimal.ROUND_HALF_UP);
|
||
//返回均息法情况下的年利率
|
||
cb.setYearRate(newYearRate.toString());
|
||
}
|
||
}
|