apzl_leasing/calc/com/tenwa/reckon/executor/CashFlowExecutor.java

238 lines
13 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.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.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 = this.getIrr(cb,tcb);
cb.setIrr(irr);
return irr;
}
private String getIrr(ConditionBean cb ,TabCalBean tcb) 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"));
}
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 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') ";
//Èç¹ûÊÇÁé»î²úÆ·¼õÈ¥²úÆ·ÖÐÅäÖõÄGPS²î¶î
if("Y".equals( cb.getFlexible() ) ){
Map<String, Map<String, String>> productRevenue = tcb.getProductRevenues();
String GPSDifference = productRevenues.get( "GPSDifference" ).get( "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);
}
}
}