package com.tenwa.reckon.help;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.amarsoft.app.als.sys.tools.Tools;
import com.amarsoft.app.util.ProductParamUtil;
import com.amarsoft.are.jbo.BizObject;
import com.amarsoft.are.jbo.BizObjectManager;
import com.amarsoft.are.jbo.JBOFactory;
import com.amarsoft.are.jbo.JBOTransaction;
import com.amarsoft.are.util.json.JSONObject;
import com.tenwa.comm.exception.BusinessException;
import com.tenwa.comm.util.jboutil.DataOperatorUtil;
import com.tenwa.reckon.bean.ConditionBean;
import com.tenwa.reckon.bean.FundRentPlanBean;
import com.tenwa.reckon.bean.TabCalBean;
import com.tenwa.reckon.constant.Scale;
public class ConditionHelper {
private JBOTransaction tx;
public JBOTransaction getTx() {
return tx;
}
public void setTx(JBOTransaction tx) {
this.tx = tx;
}
public void run(JSONObject jsonObject,ConditionBean cb,TabCalBean bean,FundRentPlanBean rentPlan,Boolean flag) throws Exception{
Map params = this.runPre(jsonObject,cb, bean, rentPlan,flag);
jsonObject.appendElement("info",params);
//项目粗利
}
public Map runPre(JSONObject jsonObject,ConditionBean cb,TabCalBean bean,FundRentPlanBean rentPlan,Boolean flag) throws Exception{
this.getGrossProfit(rentPlan, cb,bean);
this.getCleancreditmoney(rentPlan, cb,bean);
this.getFirstPaymentTotal(rentPlan, cb, bean);
Map params = new HashMap();
//irr 计算方式修改 (1+irr/100)^12 -1
/*if ("STAGE_IRR".equals(cb.getIrrType())) {
String incomeNumberYear = cb.getIncomeNumberYear() ;
int incomeType = incomeNumberYear.contains("_") == true ? Integer.valueOf(incomeNumberYear.split("_")[1]):Integer.valueOf(incomeNumberYear);
//Integer.valueOf(incomeNumberYear.split("_")[1]);
double irr = new BigDecimal(Double.valueOf(cb.getIrr())/12).setScale(6, BigDecimal.ROUND_HALF_UP).divide(new BigDecimal(100)).add(new BigDecimal(1)).pow(incomeType).subtract(new BigDecimal(1)).multiply(new BigDecimal(100)).setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
params.put("irr", String.valueOf(irr));
}else{*/
params.put("irr", cb.getIrr());
//}
if((!cb.getSettleMethod().equals("even_interest"))&&(!cb.getRentOrRate().equals("rate"))||flag){
params.put("year_rate", cb.getYearRate());
}
if(cb.getSettleMethod().equals("even_interest")&&cb.getRentOrRate().equals("rent")){
if(jsonObject.get("year_rate")!=null)
params.put("year_rate", jsonObject.get("year_rate").getValue().toString());
}
if(cb.getSettleMethod().equals("irregular_rent")){
params.put("INCOME_NUMBER",cb.getIncomeNumber()+"");
}
if((cb.getSettleMethod().equals("even_corpus")&&cb.getRentOrRate().equals("corpus_period"))||cb.getSettleMethod().equals("irregular_rent")||cb.getSettleMethod().equals("even_subsection")||(cb.getSettleMethod().equals("even_rent_day")&&(cb.getRentOrRate().equals("rent_period")||cb.getRentOrRate().equals("rent_period_360")))){
params.put("FIRST_PLAN_DATE", cb.getFirstPlanDate());
params.put("SECOND_PLAN_DATE", cb.getSecondPlanDate());
params.put("INCOME_NUMBER",cb.getIncomeNumber()+"");
}
params.put("gross_profit", cb.getGrossProfit().toString());
params.put("clean_credit_money", cb.getCleanCreditMoney().toString());
params.put("clean_credit_ratio", cb.getCleanCreditRatio().toString());
params.put("income_number", cb.getIncomeNumber() + "");
params.put("lease_term", cb.getLeaseTerm()+"");
params.put("FIRST_PAYMENT_TOTAL",cb.getFirstPaymentTotal());
//更新保证金抵扣
params.put("CAUTION_DEDUCTION_MONEY",cb.getCautionDeductionMoney());
params.put("CAUTION_MONEY_REMAIN",cb.getCautionMoneyRemain());
if( "segmented_financing".equals( cb.getSettleMethod() ) ){
params.put( "COMPREHENSIVE_RATE",cb.getComprehensiveRate() );
}
if ( "Y".equals( cb.getFlexible() ) ) {
// 灵活产品计算其他项
try {
Map> productRevenues = bean.getProductRevenues();
String roaValue = productRevenues.get( "StrategyROA" ).get( "StrategyROA" );
//String roaMax = productRevenues.get( "StrategyROA" ).get( "StrategyROA-MAX" );
if(roaValue == null || roaValue.isEmpty()){
throw new BusinessException( "灵活产品ROA必须配置,请联业务人员!" );
}
BigDecimal irr = new BigDecimal ( cb.getIrr() ) ;
String irrMin = productRevenues.get( "IRR" ).get( "IRR-MIN" );
String irrMan = productRevenues.get( "IRR" ).get( "IRR-MAX" );
if(irrMin == null || irrMin.isEmpty() || irrMan == null || irrMan.isEmpty()){
throw new BusinessException( "灵活产品IRR区间值必须配置,请联业务人员!" );
}
System.out.println("IRR:"+irr);
if( !( irr.compareTo( new BigDecimal( irrMan ) ) <= 0 && irr.compareTo( new BigDecimal( irrMin ) ) >= 0 ) ){
// throw new BusinessException( "IRR: "+irr.setScale( 2, 4 ).toString()+",不在产品配置区间范围内!" );
throw new BusinessException( "不在产品配置区间范围内!" );
}
//修改计算中的税率取值
List
* 公式:净授信额 = 设备款-首付款-保证金-其他收入+其他支出-期初第1期租金+保险费-厂商返利-手续费 - 管理费。
* @author zhangc
* @param rentPlanList 租金计划集合
* @param result 返回前台的MAP集合
* @return
* @throws Exception
*/
private void getCleancreditmoney(FundRentPlanBean rentPlan,ConditionBean cb,TabCalBean bean) throws Exception{
String productId=bean.getProductId();
String formula= ProductParamUtil.getProductParameterValue(productId,"PRD0330","CleanCreditMoney","Formula");
BigDecimal money=BigDecimal.ZERO;
JBOTransaction jbotx=JBOFactory.createJBOTransaction();
try{
if(formula.length()>0){
String sql="select "+formula+" as total from "+Tools.getTable(bean.getCondition_tb())+" where flowunid='"+bean.getDocId()+"' and "+bean.getPlanCName()+"='"+bean.getPlanCValue()+"'";
List> list=DataOperatorUtil.getDataBySql(jbotx, sql, null);
if(list.size()>0){
money=money.add(new BigDecimal(list.get(0).get("TOTAL")));
}
}
}catch(Exception e){
//e.printStackTrace();
}
/*//期初第1期租金
String periodtype = cb.getPeriodType();
BigDecimal firstRent = BigDecimal.ZERO;
if("1".equals(periodtype) || "period_type_1".equals(periodtype)){
if(rentPlan != null && rentPlan.getRentList() != null && 0 < rentPlan.getRentList().size()){
firstRent = new BigDecimal(rentPlan.getRentList().get(0)) ;
}
}
String f= ProductParamUtil.getProductParameterValue(productId,"PRD0330","CleanCreditMoney","FirstRent");
if("Y".equals(f)){
money=money.subtract(firstRent);
}*/
cb.setCleanCreditMoney(money);
cb.setCleanCreditRatio(money.multiply(new BigDecimal(100)).divide(new BigDecimal(cb.getEquipAmt()), Scale.RATE_SCALE, BigDecimal.ROUND_HALF_UP));
jbotx.commit();
}
/**
* 计算期初付款总计
* @param rentPlan
* @param cb
* @param bean
* @throws Exception
*/
private void getFirstPaymentTotal(FundRentPlanBean rentPlan,ConditionBean cb,TabCalBean bean) throws Exception{
String productId=bean.getProductId();
String formula= ProductParamUtil.getProductParameterValue(productId,"PRD0330","PeriodBeginPayMoney","Formula");
BigDecimal money=BigDecimal.ZERO;
JBOTransaction jbotx=JBOFactory.createJBOTransaction();
try{
if(formula.length()>0){
String sql="select "+formula+" as total from "+Tools.getTable(bean.getCondition_tb())+" where flowunid='"+bean.getDocId()+"' and "+bean.getPlanCName()+"='"+bean.getPlanCValue()+"'";
List> list=DataOperatorUtil.getDataBySql(jbotx, sql, null);
if(list.size()>0){
money=money.add(new BigDecimal(list.get(0).get("TOTAL")));
}
}
}catch(Exception e){
//e.printStackTrace();
}
//期初第1期租金
String periodtype = cb.getPeriodType();
BigDecimal firstRent = BigDecimal.ZERO;
if("1".equals(periodtype) || "period_type_1".equals(periodtype)){
if(rentPlan != null && rentPlan.getRentList() != null && 0 < rentPlan.getRentList().size()){
firstRent = new BigDecimal(rentPlan.getRentList().get(0)) ;
}
}
String f= ProductParamUtil.getProductParameterValue(productId,"PRD0330","PeriodBeginPayMoney","FirstRent");
if("Y".equals(f)){
money=money.add(firstRent);
}
cb.setFirstPaymentTotal(money.toString());
jbotx.commit();
}
private void updateCondition(TabCalBean tcb,Map params) throws Exception {
BizObjectManager bm=JBOFactory.getBizObjectManager(tcb.getCondition_tb(), tx);
BizObject condition=bm.createQuery("flowunid=:flowunid and "+tcb.getPlanCName()+"=:value").setParameter("flowunid", tcb.getDocId()).setParameter("value", tcb.getPlanCValue()).getSingleResult(true);
for(Map.Entry entry:params.entrySet()){
condition.setAttributeValue(entry.getKey(), entry.getValue());
}
bm.saveObject(condition);
}
}