apzl_leasing/calc/com/tenwa/reckon/help/ConditionHelper.java
2021-04-26 11:33:25 +08:00

337 lines
17 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.tenwa.reckon.help;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.amarsoft.awe.util.SqlObject;
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<String, String> params = this.runPre(jsonObject,cb, bean, rentPlan,flag);
jsonObject.appendElement("info",params);
//ÏîÄ¿´ÖÀû
}
public Map<String, String> 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<String, String> params = new HashMap<String, String>();
//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 ( "Y".equals( cb.getFlexible() ) ) {
// Áé»î²úÆ·¼ÆËãÆäËûÏî
try {
Map<String, Map<String, String>> 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Çø¼äÖµ±ØÐëÅäÖã¬ÇëÁªÒµÎñÈËÔ±£¡" );
}
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<Map<String, String>> pslData = DataOperatorUtil.getDataBySql("select leasform from prd_specific_library where productid='"+bean.getProductId()+"'");
BigDecimal interestRate =null; // ÀûϢ˰ÂÊ
BigDecimal handRate =null; //ÊÖÐø·Ñ˰ÂÊ
BigDecimal gpsInstallFee =null;//GPS˰ÂÊ
BigDecimal rentRate =null;// ×â½ð˰ÂÊ
BigDecimal discountInterestRate =null;// ÌùϢ˰ÂÊ
BigDecimal personalInsuranceRate =null;// ¸öÈËÒâÍâÏÕ˰ÂÊ
BigDecimal percentage = new BigDecimal("100");
if( pslData!=null && pslData.size()>0){
String leasform = pslData.get(0).get("leasform");
List<Map<String, String>> rateData = DataOperatorUtil.getDataBySql("select t.INTEREST_RATE,t.HAND_RATE,t.gps_install_fee,t.rent,t.discount_interest_rate,t.personal_insurance_rate from (select INTEREST_RATE,HAND_RATE,gps_install_fee,rent,CONTRACT_DATE,discount_interest_rate,personal_insurance_rate from TAX_INFO where CONTRACT_DATE<= DATE_FORMAT(NOW(),'%Y/%m/%d') and BEFORE_AFTER='after' and lease_form='"+leasform+"' union all select INTEREST_RATE,HAND_RATE,gps_install_fee,rent,CONTRACT_DATE,discount_interest_rate,personal_insurance_rate from TAX_INFO where CONTRACT_DATE>=DATE_FORMAT(NOW(),'%Y/%m/%d') and BEFORE_AFTER='before' and lease_form='"+leasform+"' ) t order by t.CONTRACT_DATE desc limit 1");
if( rateData!=null && rateData.size()>0 && rateData.get(0).get("discount_interest_rate") != null && rateData.get(0).get("personal_insurance_rate") != null){
interestRate = BigDecimal.ONE.add( new BigDecimal ( rateData.get(0).get("INTEREST_RATE") ).divide(percentage) );
handRate = BigDecimal.ONE.add( new BigDecimal (rateData.get(0).get("HAND_RATE") ).divide(percentage) );
gpsInstallFee = BigDecimal.ONE.add( new BigDecimal (rateData.get(0).get("gps_install_fee") ).divide( percentage ) );
rentRate = BigDecimal.ONE.add( new BigDecimal ( rateData.get(0).get("rent") ).divide( percentage ) );
discountInterestRate = BigDecimal.ONE.add( new BigDecimal ( rateData.get(0).get("discount_interest_rate") ).divide( percentage ) );
personalInsuranceRate = BigDecimal.ONE.add( new BigDecimal ( rateData.get(0).get("personal_insurance_rate") ).divide( percentage ) );
}else{
throw new BusinessException( "δÅäÖöÔӦ˰ÂÊ£¡" );
}
}
// 1 + ˰²î
// BigDecimal tax = BigDecimal.ONE.add( new BigDecimal( cb.getRate() ) );
// ÀûÏ¢ºÍ
List<String> interestBusinessList = rentPlan.getInterestBusinessList();
BigDecimal allInterest = BigDecimal.ZERO;
for ( String interestBusiness : interestBusinessList ) {
allInterest = allInterest.add( new BigDecimal( interestBusiness ) );
}
// ÊÖÐø·Ñ
BigDecimal handlingChargeMoney = new BigDecimal( cb.getHandlingChargeMoney() );
// GPS²î¶î
// BigDecimal gpsDifference = new BigDecimal( cb.getGPSDifference() );
BigDecimal gpsDifference = new BigDecimal( productRevenues.get( "GPSDifference" ).get( "GPSDifference" ) );//È¡²úÆ·ÊÕÒæÖеÄGPS²î¶îÅäÖÃ
String personalInsuranceDifference = "0" ;//»ñÈ¡²úÆ·ÖеĸöÈËÒâÍâÏÕ²î¶îÅäÖÃ
Map<String, String> pidMap = productRevenues.get( "personalInsuranceDifference" );
if( pidMap != null && pidMap.size() > 0){
personalInsuranceDifference = pidMap.get("personalInsuranceDifference");
if( personalInsuranceDifference == null || personalInsuranceDifference.isEmpty() ){
personalInsuranceDifference = "0";
}
}
// ÊÕÈë
BigDecimal in = allInterest.divide( interestRate , 2, 4 ).add( handlingChargeMoney.divide( handRate, 2, 4 ) ).add( gpsDifference.divide( gpsInstallFee, 2, 4 ) ).add( new BigDecimal( cb.getDiscountInterest() ).divide(discountInterestRate , 2 ,4 ) ).add( new BigDecimal( personalInsuranceDifference ).divide( personalInsuranceRate , 2 ,4 ) );
// ×âÁÞÄêÏÞ
int leaseTerm = cb.getLeaseTerm()/12;
// ÈÚ×ʶî
BigDecimal cleanLeaseMoney = new BigDecimal( cb.getCleanLeaseMoney() );
String rentSql = "select corpus,ALL_REMAIN_CORPUS from LC_RENT_PLAN_TEMP where flowunid ='"+cb.getDocId()+"' order by plan_date";
List<Map<String, String>> rentPlanData =DataOperatorUtil.getDataBySql(tx, rentSql, null);
BigDecimal cautionMoney = new BigDecimal( cb.getCautionMoney() );
BigDecimal costMoney = BigDecimal.ZERO;
BigDecimal CostInterestRate = new BigDecimal (productRevenues.get( "CostInterestRate" ).get( "CostInterestRate" ) ).divide( percentage ) ;//²úÆ·Öеijɱ¾ÀûÂÊ
for (Map<String, String> map : rentPlanData) {
//surplus:µ±ÆÚ±¾½ð+Ê£Óà±¾½ð ¡£ cautionMoney £º±£Ö¤½ð
BigDecimal surplus = new BigDecimal(map.get("corpus") ).add(new BigDecimal(map.get("ALL_REMAIN_CORPUS")));
if(surplus.compareTo(cautionMoney)>0){
costMoney = costMoney.add( surplus.subtract(cautionMoney).multiply( CostInterestRate).divide( new BigDecimal("12"), 2 , 4 ) );
}
}
System.out.println(costMoney);
// ³É±¾
BigDecimal cost = costMoney.divide( rentRate, 2, 4 );
// ÔËÓª·ÑÂÊ
String operatingRate = productRevenues.get( "OperatingRate" ).get( "OperatingRate" );
// ÔËÓª·ÑÓÃ
BigDecimal operatingExpenses = cleanLeaseMoney.multiply( new BigDecimal( operatingRate ).divide( percentage ) );
// ·çÏÕ¼ÆÌá±ÈÂÊ
String riskAccrualRatio = productRevenues.get( "RiskAccrualRatio" ).get( "RiskAccrualRatio" );
// ·çÏÕ¼ÆÌá
BigDecimal withdrawalRisk = cleanLeaseMoney.multiply( new BigDecimal( riskAccrualRatio ).divide( percentage ) );
// ROA
BigDecimal roa = in.subtract( cost ).subtract( operatingExpenses ).subtract( withdrawalRisk ).multiply( new BigDecimal( 0.75 ) ).divide( cleanLeaseMoney, 2, 4 ).multiply( new BigDecimal( 2 ) );
StringBuilder sb = new StringBuilder();
sb.append(cb.getDocId()).append("µÄROA¼ÆËã²ÎÊý=ÀûÏ¢ºÍ:").append(allInterest).append(",ÀûϢ˰ÂÊ:").append(interestRate).append(",ÊÖÐø·Ñ:").append(handlingChargeMoney)
.append(",ÊÖÐø·Ñ˰ÂÊ£º").append(handRate).append(",GPS²î¶î:").append(gpsDifference).append(",GPS²î¶î˰ÂÊ:").append(gpsInstallFee)
.append(",×â½ð±íÀï³É±¾ºÍ£º").append(costMoney).append(",±£Ö¤½ð½ð¶î:").append(cb.getCautionMoney()).append(",×âÁÞÄêÏÞ:").append(leaseTerm)
.append(",³É±¾ÀûÂÊ£º").append(CostInterestRate).append(",×â½ð˰ÂÊ:").append(rentRate).append(",ÈÚ×ʽð¶î:").append(cleanLeaseMoney)
.append(",ÔËÓª·ÑÂÊ£º").append(new BigDecimal( operatingRate ).divide( percentage ) ).append(",·çÏÕ¼ÆÌá±ÈÂÊ:").append( new BigDecimal( riskAccrualRatio ).divide( percentage ) )
.append(",ÊÕÈ룺").append(in).append(",³É±¾£º").append(cost).append(",ÔËÓª·ÑÓãº").append(operatingExpenses).append(",·çÏÕ¼ÆÌ᣺").append(withdrawalRisk);
System.out.println(sb.toString());
if ( roa.compareTo( new BigDecimal( roaValue ).divide( new BigDecimal("100") ) ) < 0 ) {
throw new BusinessException( "ROAСÓÚ²úÆ·ÅäÖã¡" );
}
// Õ½ÂÔϵÊý
String strategicCoefficient = productRevenues.get( "StrategicCoefficient" ).get( "StrategicCoefficient" );
params.put( "OPERATING_RATE", operatingRate );
params.put( "RISK_ACCRUAL_RATIO", riskAccrualRatio );
params.put( "STRATEGIC_COEFFICIENT", strategicCoefficient );
params.put( "STRATEGY_ROA", roa.toString() );
} catch ( Exception e ) {
throw new BusinessException( e.getMessage() );
}
}
this.updateCondition(bean, params);
return params;
}
/**
* @throws Exception
*
* @Title: getGrossProfit
* @author zhangc
* @Description: ÏîÄ¿´ÖÀû£º²ÆÎñÀûÏ¢×ܺÍ+ÊÖÐø·Ñ+¹ÜÀí·Ñ+·µµãÊÕÈë+ÀûÏ¢²¹Ìù-±£ÏÕ·Ñ£¨ÎÒ˾£©+×âǰϢ+ÃûÒå»õ¼Û+ÆäËûÊÕÈë-ÆäËûÖ§³ö
* @return
* @return BigDecimal
* @throws
*/
private void getGrossProfit(FundRentPlanBean fundRent,ConditionBean cb,TabCalBean bean) throws Exception{
String productId=bean.getProductId();
String formula= ProductParamUtil.getProductParameterValue(productId,"PRD0330","GrossProfit","Formula");
String FinanceInterest=ProductParamUtil.getProductParameterValue(productId,"PRD0330","GrossProfit","FinanceInterest");
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<Map<String,String>> 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();
}
List<String> interest=fundRent.getInterestBusinessList();
if(FinanceInterest.equals("Y")){
for(int i=0;i<interest.size();i++){
money=money.add(new BigDecimal(interest.get(i)));
}
}
cb.setGrossProfit(money);
jbotx.commit();
}
/**
*
* <p>¼ÆË㾫ÊÚÐŶ</p>
* <p>¹«Ê½£º¾»ÊÚÐŶî = É豸¿î£­Ê׸¶¿î£­±£Ö¤½ð-ÆäËûÊÕÈë+ÆäËûÖ§³ö-ÆÚ³õµÚ1ÆÚ×â½ð+±£ÏÕ·Ñ-³§ÉÌ·µÀû-ÊÖÐø·Ñ - ¹ÜÀí·Ñ¡£</p>
* @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<Map<String,String>> 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<Map<String,String>> 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<String, String> 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<String,String> entry:params.entrySet()){
condition.setAttributeValue(entry.getKey(), entry.getValue());
}
bm.saveObject(condition);
}
}