284 lines
15 KiB
Java
284 lines
15 KiB
Java
package com.tenwa.reckon.executor;
|
||
|
||
import java.math.BigDecimal;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.Map.Entry;
|
||
import java.util.Set;
|
||
|
||
import com.amarsoft.app.als.sys.tools.Tools;
|
||
import com.amarsoft.app.awe.config.InitDBType;
|
||
import com.amarsoft.app.util.ProductParamUtil;
|
||
import com.amarsoft.are.jbo.BizObjectManager;
|
||
import com.amarsoft.are.jbo.JBOFactory;
|
||
import com.amarsoft.are.jbo.JBOTransaction;
|
||
import com.amarsoft.awe.util.SqlObject;
|
||
import com.amarsoft.awe.util.Transaction;
|
||
import com.gnete.bc.util.Strings;
|
||
import com.tenwa.comm.util.jboutil.DataOperatorUtil;
|
||
import com.tenwa.reckon.bean.ConditionBean;
|
||
import com.tenwa.reckon.bean.TabCalBean;
|
||
import com.tenwa.reckon.util.DateUtils;
|
||
import com.tenwa.reckon.util.IRRCalculateUtil;
|
||
import com.tenwa.reckon.util.IrrTools;
|
||
|
||
public class CashFlowExecutor {
|
||
|
||
private JBOTransaction tx;
|
||
|
||
public CashFlowExecutor(JBOTransaction tx){
|
||
this.tx=tx;
|
||
}
|
||
|
||
|
||
public String run(ConditionBean cb , TabCalBean tcb) throws Exception{
|
||
this.delete(tcb, cb);
|
||
this.add(cb, tcb);
|
||
String irr = "";
|
||
if( "segmented_financing".equals( cb.getSettleMethod() ) ){
|
||
irr = this.getIrr(cb,tcb,"subsectionIRR");
|
||
String subsectionRate = this.getIrr(cb, tcb, "subsectionRate");
|
||
cb.setComprehensiveRate( subsectionRate );
|
||
}else{
|
||
irr = this.getIrr(cb,tcb,"normal");
|
||
}
|
||
cb.setIrr(irr);
|
||
return irr;
|
||
}
|
||
//calcType 计算类型 normal正常合同计算IRR;subsectionIRR:分段融计算IRR,subsectionRate:分段融计算综合利率
|
||
private String getIrr(ConditionBean cb , TabCalBean tcb, String calcType) throws Exception{
|
||
String cashSql = "select NET_FLOW,PLAN_DATE from "+Tools.getTable(tcb.getContractCashTb())+" where flowunid ='"+tcb.getDocId()+"' and "+tcb.getPlanCName()+"='"+tcb.getPlanCValue()+"' order by plan_date";
|
||
List<Map<String, String>> cashes =DataOperatorUtil.getDataBySql(tx, cashSql, null);
|
||
List<String > netList = new ArrayList<String>();
|
||
List<String > dateList = new ArrayList<String>();
|
||
for(Map<String, String> cash :cashes){
|
||
netList.add(cash.get("NET_FLOW"));
|
||
dateList.add(cash.get("PLAN_DATE"));
|
||
}
|
||
if ("subsectionRate".equals(calcType) ){
|
||
String subsectionSql = "select IS_BALANCE_LENDING,DISCOUNT_INTEREST,DISCOUNT_COLLECTION_PERIOD from lc_calc_subsection_info_temp where FLOWUNID='"+tcb.getDocId()+"'";
|
||
List<Map<String, String>> subsectionData =DataOperatorUtil.getDataBySql(tx, subsectionSql, null);
|
||
BigDecimal tempMoney = BigDecimal.ZERO;
|
||
int coss = 0;
|
||
for (Map<String, String> subsection :subsectionData) {
|
||
String ibl = subsection.get("IS_BALANCE_LENDING");
|
||
if(!"Y".equals(ibl)){
|
||
coss = Integer.parseInt( subsection.get("DISCOUNT_COLLECTION_PERIOD") );
|
||
}
|
||
tempMoney = new BigDecimal( netList.get(coss) );
|
||
netList.set(coss, ( tempMoney.subtract( new BigDecimal( subsection.get("DISCOUNT_INTEREST") ) ) ).toString() );
|
||
}
|
||
}
|
||
String irr="";
|
||
if("STAGE_IRR".equals(cb.getIrrType())){//按期IRR
|
||
BigDecimal issueRate=IRRCalculateUtil.getIRR2(netList);
|
||
irr=issueRate.multiply( new BigDecimal(1200/cb.getIncomeIntervalMonth())).setScale(6,BigDecimal.ROUND_HALF_UP).toString();
|
||
}else if("MONTH_IRR".equals(cb.getIrrType())){//按月IRR
|
||
String upperListDate="";
|
||
List<BigDecimal> newNetList=new ArrayList<BigDecimal>();
|
||
List<String> newDateList=new ArrayList<String>();
|
||
for(int i=0;i<dateList.size();i++){
|
||
String monthFistDay=dateList.get(i).substring(0, dateList.get(i).length()-2)+"01";
|
||
if(monthFistDay.equals(upperListDate)){
|
||
newNetList.set(newNetList.size()-1, newNetList.get(newNetList.size()-1).add(new BigDecimal(netList.get(i))));
|
||
}else{
|
||
newNetList.add(new BigDecimal(netList.get(i)));
|
||
newDateList.add(monthFistDay);
|
||
}
|
||
upperListDate=monthFistDay;
|
||
}
|
||
List<BigDecimal> inflowPour=new ArrayList<BigDecimal>();
|
||
String plandate="";
|
||
for(int i=0;i<newDateList.size();i++){
|
||
if(i==0){
|
||
inflowPour.add(newNetList.get(i));
|
||
}else{
|
||
int month=DateUtils.getBetweenMonths(plandate, newDateList.get(i),"yyyy/MM/dd");
|
||
if(month>1){
|
||
for(int y=1;y<month;y++){
|
||
inflowPour.add(BigDecimal.ZERO);
|
||
}
|
||
}
|
||
inflowPour.add(newNetList.get(i));
|
||
}
|
||
plandate=newDateList.get(i);
|
||
}
|
||
irr=IRRCalculateUtil.getIRR(inflowPour, BigDecimal.ONE).multiply(new BigDecimal(100)).toString();
|
||
}else{
|
||
irr = IrrTools.getIrrNew(netList,dateList, tcb.getCalType());
|
||
}
|
||
return irr;
|
||
}
|
||
|
||
private void delete(TabCalBean tcb,ConditionBean cb) throws Exception{
|
||
BizObjectManager bm=JBOFactory.getBizObjectManager(tcb.getContractCashTb(), tx);
|
||
String sql = " delete from O where flowunid='" + tcb.getDocId() + "' and "+tcb.getPlanCName()+"='"+tcb.getPlanCValue()+"'";
|
||
bm.createQuery(sql).executeUpdate();
|
||
}
|
||
|
||
private void add(ConditionBean cb,TabCalBean tcb) throws Exception{
|
||
|
||
Map<String, Map<String, String>> productRevenues = tcb.getProductRevenues();
|
||
String GPSDifference = "0";//获取产品中的GPS差额配置
|
||
Map<String, String> GPSMap = productRevenues == null ? null : productRevenues.get( "GPSDifference" );
|
||
if( GPSMap != null && GPSMap.size() > 0){
|
||
GPSDifference = GPSMap.get("GPSDifference");
|
||
if( GPSDifference == null || GPSDifference.isEmpty() ){
|
||
GPSDifference = "0";
|
||
}
|
||
}
|
||
/*
|
||
* 根据客户(张文竹)要求,将个人意外险差额去掉
|
||
String personalInsuranceDifference = "0" ;//获取产品中的个人意外险差额配置
|
||
Map<String, String> pidMap = productRevenues == null ? null : productRevenues.get( "personalInsuranceDifference" );
|
||
if( pidMap != null && pidMap.size() > 0){
|
||
personalInsuranceDifference = pidMap.get("personalInsuranceDifference");
|
||
if( personalInsuranceDifference == null || personalInsuranceDifference.isEmpty() ){
|
||
personalInsuranceDifference = "0";
|
||
}
|
||
}*/
|
||
|
||
String sql = "";
|
||
if("MYSQL".equals(InitDBType.DBTYPE)){
|
||
sql += "INSERT INTO " +Tools.getTable(tcb.getContractCashTb())+ " (id," + tcb.getPlanCName() + "";
|
||
sql += ",plan_date";
|
||
sql += ",fund_in";
|
||
sql += ",fund_in_details";
|
||
sql += ",fund_out";
|
||
sql += ",fund_out_details";
|
||
sql += ",net_flow";
|
||
sql += ",flowunid";//create_date,creator_ "+DateUtil.getSystemDate()+"' create_date ,'"+SecurityUtil.getPrincipal().getId()+"' creator_"
|
||
sql += " )";
|
||
|
||
sql +=" select replace(uuid(),'-','') id,'"+tcb.getPlanCValue()+"' "+tcb.getPlanCName()+",t.plan_date,sum(t.flowin) fundin,ifnull(group_concat(if(t.flowindetail='',null,t.flowindetail)),'-') fundindetails, ";
|
||
sql +=" sum(t.flowout) fundout ,ifnull(group_concat(if(t.flowoutdetail='',null,t.flowoutdetail)),'-')fundoutdetails,sum(t.cleanfow)netflow ,'"+tcb.getDocId()+"' flowunid " ;
|
||
sql +=" from (" ;
|
||
sql +=" select cfrp.plan_date,cfrp.rent flowin,concat('第',cfrp.plan_list,'期租金:',format(cfrp.rent,2)) flowindetail,0 flowout,'' flowoutdetail,cfrp.rent-0 cleanfow " ;
|
||
sql +=" from "+Tools.getTable(tcb.getRentPlan_tb()) +" cfrp" ;
|
||
sql +=" where cfrp.flowunid = '"+tcb.getDocId()+"' and cfrp."+tcb.getPlanCName()+"='"+tcb.getPlanCValue()+"'" ;
|
||
sql +=" union all " ;
|
||
sql +=" select fundplan.plan_date,if(fundplan.pay_type='pay_type_in',fundplan.plan_money,0)flowin, ";
|
||
sql +=" if(fundplan.pay_type='pay_type_in',concat(tdd.itemname,':',format(fundplan.plan_money,2)),''), " ;
|
||
sql +=" if(fundplan.pay_type='pay_type_out',fundplan.plan_money,0)flowout, " ;
|
||
if("business_product".equals(tcb.getProductType())) {
|
||
sql +=" if(fundplan.pay_type='pay_type_out',concat(if(tdd.itemno = 'feetype10','设备款',tdd.itemname),':',format(fundplan.plan_money,2)),'')flowoutdetail, " ;
|
||
} else {
|
||
sql +=" if(fundplan.pay_type='pay_type_out',concat(tdd.itemname,':',format(fundplan.plan_money,2)),'')flowoutdetail, " ;
|
||
}
|
||
sql +=" if(fundplan.pay_type='pay_type_in',fundplan.plan_money,-fundplan.plan_money) cleanfow " ;
|
||
sql +=" from "+Tools.getTable(tcb.getFundFundPlan_tb())+" fundplan " ;
|
||
sql +=" left join code_library tdd on fundplan.fee_type = tdd.itemno and tdd.codeno='FeeType' " ;
|
||
sql +=" where fundplan.flowunid = '"+ tcb.getDocId()+"' and fundplan."+tcb.getPlanCName()+"='"+tcb.getPlanCValue()+"' and fundplan.fee_type in ('feetype2','feetype1','feetype10','feetype16','feetype17','feetype33','feetype24','feetype27') ";
|
||
//根据客户(张文竹)不管是否灵活产品,配置的GPS差额和个人意外险差额添加到IRR计算
|
||
/*
|
||
if( !"0".equals(personalInsuranceDifference) ){
|
||
sql +=" union all select plan_date,'"+personalInsuranceDifference+"' flowin,'个人意外险差额:"+personalInsuranceDifference+"' flowindetail,'' flowout,'' flowoutdetil,'"+personalInsuranceDifference+"' cleanfow from lc_fund_plan_temp where flowunid = '"+tcb.getDocId()+"' and fee_type='feetype10' ";
|
||
}
|
||
*/
|
||
if( !"0".equals(GPSDifference) ){
|
||
sql += "union all select plan_date,'"+GPSDifference+"' flowin,'GPS差额:"+GPSDifference+"' flowindetail,'' flowout,'' flowoutdetil,'"+GPSDifference+"' cleanfow from lc_fund_plan_temp where flowunid = '"+tcb.getDocId()+"' and fee_type='feetype10' ";
|
||
}
|
||
|
||
}else if("ORACLE".equals(InitDBType.DBTYPE)){
|
||
sql += "INSERT INTO " +Tools.getTable(tcb.getContractCashTb())+ " (id," + tcb.getPlanCName() + "";
|
||
sql += ",plan_date";
|
||
sql += ",fund_in";
|
||
sql += ",fund_in_details";
|
||
sql += ",fund_out";
|
||
sql += ",fund_out_details";
|
||
sql += ",net_flow";
|
||
sql += ",flowunid";//create_date,creator_ "+DateUtil.getSystemDate()+"' create_date ,'"+SecurityUtil.getPrincipal().getId()+"' creator_"
|
||
sql += " )";
|
||
|
||
sql +=" select sys_guid() id,'"+tcb.getPlanCValue()+"' "+tcb.getPlanCName()+",t.plan_date,sum(t.flowin) fundin,nvl(wmsys.wm_concat(case when t.flowindetail='' then null else t.flowindetail end ),'-') fundindetails, ";
|
||
sql +=" sum(t.flowout) fundout ,nvl(wmsys.wm_concat(case when t.flowoutdetail='' then null else t.flowoutdetail end),'-') fundoutdetails,sum(t.cleanfow)netflow ,'"+tcb.getDocId()+"' flowunid " ;
|
||
sql +=" from (" ;
|
||
sql +=" select cfrp.plan_date,cfrp.rent flowin,'第'||cfrp.plan_list||'期租金:'||to_char(cfrp.rent,'FM9,999,999,999,999,999,999,999,990.00') flowindetail,0 flowout,'' flowoutdetail,cfrp.rent-0 cleanfow " ;
|
||
sql +=" from "+Tools.getTable(tcb.getRentPlan_tb()) +" cfrp" ;
|
||
sql +=" where cfrp.flowunid = '"+tcb.getDocId()+"' and cfrp."+tcb.getPlanCName()+"='"+tcb.getPlanCValue()+"'" ;
|
||
sql +=" union all " ;
|
||
sql +=" select fundplan.plan_date,case when fundplan.pay_type='pay_type_in'then fundplan.plan_money else 0 end flowin, ";
|
||
sql +=" case when fundplan.pay_type='pay_type_in'then concat(concat(tdd.itemname,':'),to_char(fundplan.plan_money,'FM9,999,999,999,999,999,999,999,990.00')) else ''end, " ;
|
||
sql +=" case when fundplan.pay_type='pay_type_out'then fundplan.plan_money else 0 end flowout, " ;
|
||
if("business_product".equals(tcb.getProductType())) {
|
||
sql +=" case when fundplan.pay_type='pay_type_out' then (case then tdd.itemno = 'feetype10' then '设备款' else tdd.itemname end) ||':'||to_char(fundplan.plan_money,'FM9,999,999,999,999,999,999,999,990.00') else ''end flowoutdetail, " ;
|
||
} else {
|
||
sql +=" case when fundplan.pay_type='pay_type_out' then tdd.itemname||':'||to_char(fundplan.plan_money,'FM9,999,999,999,999,999,999,999,990.00') else ''end flowoutdetail, " ;
|
||
}
|
||
sql +=" case when fundplan.pay_type='pay_type_in' then fundplan.plan_money else -fundplan.plan_money end cleanfow " ;
|
||
sql +=" from "+Tools.getTable(tcb.getFundFundPlan_tb())+" fundplan " ;
|
||
sql +=" left join code_library tdd on fundplan.fee_type = tdd.itemno " ;
|
||
sql +=" where fundplan.flowunid = '"+ tcb.getDocId()+"' and fundplan."+tcb.getPlanCName()+"='"+tcb.getPlanCValue()+"'";
|
||
}
|
||
String productId = tcb.getProductId();
|
||
if(productId != null){
|
||
Map<String,Map<String,String>> productCashInIRRList = ProductParamUtil.getProductComponentType(productId, "PRD0315");
|
||
Set<Entry<String,Map<String,String>>> entry = productCashInIRRList.entrySet();
|
||
for(Entry<String,Map<String,String>> e : entry){
|
||
Map<String,String> parameMap = e.getValue();
|
||
boolean flag = false;
|
||
if("N".equals(parameMap.get("CostType10"))){
|
||
flag = true;
|
||
}
|
||
if(flag){
|
||
sql +=" and tdd.relativecode<>'"+e.getKey()+"' ";
|
||
}
|
||
}
|
||
}
|
||
//sql +=" and fundplan."+tcb.getContOrProjCName()+" = '"+tcb.getContOrProjCValue()+"'" ;
|
||
sql +=" )t group by t.plan_date ";
|
||
Transaction Sqlca =null;
|
||
Sqlca = Transaction.createTransaction(tx);
|
||
SqlObject asql = new SqlObject("");
|
||
asql.setOriginalSql(sql);
|
||
Sqlca.executeSQL(asql);
|
||
|
||
String depositStyle = "";
|
||
Map<String, Map<String, String>> productCost = ProductParamUtil.getProductComponentType(productId, "PRD0315");
|
||
if( productCost != null && productCost.size() > 0 ){
|
||
Map<String, String> cautionMoney = productCost.get("CAUTION_MONEY");
|
||
if( cautionMoney != null && cautionMoney.size() > 0 ){
|
||
depositStyle = cautionMoney.get("DepositStyle");
|
||
}
|
||
}
|
||
String lcftSql = "select ID,PLAN_DATE,FUND_IN,FUND_OUT from lc_cash_flow_temp where flowunid ='"+tcb.getDocId()+"' and "+tcb.getPlanCName()+"='"+tcb.getPlanCValue()+"' order by plan_date desc";
|
||
List<Map<String, String>> lcftDate =DataOperatorUtil.getDataBySql(tx, lcftSql, null);
|
||
BigDecimal outMoney = null ;//保证金
|
||
BigDecimal inMoney = null ;//每期租金
|
||
String updateSql = "" ;
|
||
if( "caution_money_method01".equals( depositStyle ) ){//保证金抵扣
|
||
for (int i=0 ; i < lcftDate.size() ; i++ ) {
|
||
if(i==0){
|
||
outMoney = new BigDecimal(cb.getCautionMoney() );
|
||
inMoney = new BigDecimal(lcftDate.get(i).get("FUND_IN") );
|
||
}else{
|
||
inMoney = new BigDecimal(lcftDate.get(i).get("FUND_IN") );
|
||
}
|
||
if(outMoney.compareTo(inMoney)>=0){
|
||
updateSql = " update lc_cash_flow_temp set FUND_OUT='"+inMoney+"',FUND_OUT_DETAILS='抵扣保证金:"+inMoney+"',NET_FLOW='0.00' where id='"+lcftDate.get(i).get("ID")+"' ";
|
||
asql.setOriginalSql(updateSql);
|
||
Sqlca.executeSQL(asql);
|
||
outMoney = outMoney.subtract(inMoney);
|
||
}else{
|
||
updateSql = " update lc_cash_flow_temp set FUND_OUT='"+outMoney+"',FUND_OUT_DETAILS='抵扣保证金:"+outMoney+"',NET_FLOW='"+inMoney.subtract(outMoney)+"' where id='"+lcftDate.get(i).get("ID")+"' ";
|
||
asql.setOriginalSql(updateSql);
|
||
Sqlca.executeSQL(asql);
|
||
break;
|
||
}
|
||
}
|
||
|
||
}else if ("caution_money_method02".equals( depositStyle )){//保证金退回
|
||
outMoney = new BigDecimal(cb.getCautionMoney() );
|
||
inMoney = new BigDecimal(lcftDate.get(0).get("FUND_IN") );
|
||
updateSql = " update lc_cash_flow_temp set FUND_OUT='"+outMoney+"',FUND_OUT_DETAILS='抵扣保证金:"+outMoney+"',NET_FLOW='"+inMoney.subtract(outMoney)+"' where id='"+lcftDate.get(0).get("ID")+"' ";
|
||
asql.setOriginalSql(updateSql);
|
||
Sqlca.executeSQL(asql);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
}
|