apzl_leasing/calc/com/tenwa/reckon/executor/RentCalHelper.java
2018-08-23 21:20:40 +08:00

352 lines
15 KiB
Java

package com.tenwa.reckon.executor;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import jbo.app.tenwa.calc.LC_CALC_CONDITION;
import jbo.app.tenwa.calc.LC_RENT_INCOME;
import jbo.app.tenwa.calc.LC_RENT_PLAN;
import jbo.com.tenwa.entity.comm.flow.FLOW_BUSSINESS_OBJECT;
import jbo.com.tenwa.lease.comm.LC_CAR_DISPOSE;
import com.amarsoft.app.awe.config.InitDBType;
import com.amarsoft.are.jbo.BizObject;
import com.amarsoft.are.jbo.BizObjectManager;
import com.amarsoft.are.jbo.BizObjectQuery;
import com.amarsoft.are.jbo.JBOException;
import com.amarsoft.are.jbo.JBOFactory;
import com.amarsoft.are.jbo.JBOTransaction;
import com.amarsoft.are.util.json.JSONEncoder;
import com.amarsoft.are.util.json.JSONObject;
import com.tenwa.reckon.util.Conn;
import com.tenwa.reckon.util.DateUtil;
import com.tenwa.reckon.util.DateUtils;
/**
* 合同中途中止
* @author tenwapc
*
*/
public class RentCalHelper {
private String contractId;
private String startList;
private String paydayAdjust;
private String paymentnumber;
/**
* 获取开始期次是否回笼
* @return
*/
public String findPlanDateByRentList() {
JSONObject jsonObject = JSONObject.createObject();
try {
BizObject rentplan=JBOFactory.createBizObjectQuery(LC_RENT_PLAN.CLASS_NAME,"PAYMENT_NUMBER=:paymentnumber and plan_list=:planlist")
.setParameter("paymentnumber", paymentnumber).setParameter("planlist", startList).getSingleResult(false);
@SuppressWarnings("unchecked")
List<BizObject> rentincome=JBOFactory.createBizObjectQuery(LC_RENT_INCOME.CLASS_NAME,"plan_id=:planid and ROLL_BACK='0'").setParameter("planid", rentplan.getAttribute("ID").getString()).getResultList(false);
//先去判断当前期次是否已回笼
if(rentincome.size()>0){
jsonObject.appendElement("result", "false");
jsonObject.appendElement("message","当前期次已回笼或部分回笼,请重新选择回笼期次!");
}else{
jsonObject.appendElement("result", "true");
jsonObject.appendElement("message",rentplan.getAttribute("plan_date").getString());
}
} catch (JBOException e) {
jsonObject.appendElement("result", "false");
jsonObject.appendElement("message","服务器繁忙,请稍后再试!");
e.printStackTrace();
}
return JSONEncoder.encode(jsonObject);
}
public String findConditionInfo() {
JSONObject jsonObject = JSONObject.createObject();
try {
JBOFactory f = JBOFactory.getFactory();
BizObjectManager m = f.getManager(LC_CALC_CONDITION.CLASS_NAME);
BizObjectQuery q = m.createQuery("CONTRACT_ID = :contractId").setParameter("contractId", this.contractId);
BizObject bo = q.getSingleResult(true);
if(bo!=null){
jsonObject.appendElement("ADJUSTLIST", bo.getAttribute("INCOMENUMBER").getString());
jsonObject.appendElement("YEARRATE", bo.getAttribute("YEARRATE").getString());
return JSONEncoder.encode(jsonObject);
}
return "";
} catch (JBOException e) {
e.printStackTrace();
return "";
}
}
/**
* 校验中止日期是否合法
* @return
* @throws Exception
*/
public String checkPaydayAdjust() throws Exception {
JSONObject jsonObject = JSONObject.createObject();
JBOTransaction tx=null;
try{
tx=JBOFactory.createJBOTransaction();
String checkSql = " select max(plan_date) maxdate,min(plan_date) mindate from LC_RENT_PLAN where contract_id = ? and payment_number=?";
Conn conn = new Conn(tx);
List<Map<String, String>> results = conn.executeQuery(checkSql, this.contractId,this.getPaymentnumber());
String maxdate = results.get(0).get("maxdate");
String mindate = results.get(0).get("mindate");
Date max = DateUtil.getTimeByFormat(maxdate, "yyyy/MM/dd");
Date min = DateUtil.getTimeByFormat(mindate, "yyyy/MM/dd");
Date payDayAdjust = DateUtil.getTimeByFormat(this.paydayAdjust, "yyyy/MM/dd");
if(payDayAdjust.compareTo(min) >= 0 && payDayAdjust.compareTo(max) <= 0){
//约定终止日不能落在已汇款的租金计划的区间段
checkSql = "select max(rp.plan_date) maxdate from LC_RENT_PLAN rp left join lc_rent_income ci on rp.id=ci.plan_id where ci.id is not null and rp.contract_id = ? and rp.payment_number=?";
results = conn.executeQuery(checkSql, this.contractId,this.paymentnumber);
String maxDate = results.get(0).get("maxdate");
if(maxDate != null){
max = DateUtil.getTimeByFormat(maxDate, "yyyy/MM/dd");
if(payDayAdjust.compareTo(max) > 0 ){
jsonObject.appendElement("result", "true");
}else{
jsonObject.appendElement("result", "false");
jsonObject.appendElement("msg","约定终止日不能落在已汇款的租金计划的区间段!!!");
}
}else{
jsonObject.appendElement("result", "true");
}
}else{
jsonObject.appendElement("result", "false");
jsonObject.appendElement("msg","预定终止日应不能大于最后一期租金支付日期,也不能小于第一期租金支付日期!!!");
}
}catch(Exception e){
tx.rollback();
e.printStackTrace();
jsonObject.appendElement("result", "false");
jsonObject.appendElement("msg","服务器正忙,请稍后再试!!!");
}finally{
tx.commit();
}
return JSONEncoder.encode(jsonObject);
}
public String getLcCarDispose() throws Exception{
BizObjectManager lcdManage = JBOFactory.getBizObjectManager(LC_CAR_DISPOSE.CLASS_NAME);
BizObject lcd = lcdManage.createQuery("select * from O left join jbo.com.tenwa.lease.comm.LB_CONTRACT_INFO lci on lci.contract_no=O.contract_no where lci.id=:id ").setParameter("id", contractId).getSingleResult(false);
if(lcd!=null){
return lcd.getAttribute("collect_fees").getString()+"@"+lcd.getAttribute("dispose_money").getString();
}
return "";
}
/**
* 根据中止日期算金额
* @return
* @throws Exception
*/
public String getTerminateInfo() throws Exception {
JSONObject jsonObject = JSONObject.createObject();
JBOTransaction tx=null;
try{
tx=JBOFactory.createJBOTransaction();
BizObjectManager bm = JBOFactory.getBizObjectManager(LC_CALC_CONDITION.CLASS_NAME);
Conn conn = new Conn(tx);
String sql = "select sum(case when DATE_FORMAT(t.plandate,'%Y/%m/%d') > DATE_FORMAT('"+this.paydayAdjust+"','%Y/%m/%d') then t.remaincorpus else 0 end ) remaincorpus,"
+ " sum(case when DATE_FORMAT(t.plandate,'%Y/%m/%d') > DATE_FORMAT('"+this.paydayAdjust+"','%Y/%m/%d') then t.remaininterest else 0 end ) remaininterest, "
+" sum(case when DATE_FORMAT(t.plandate,'%Y/%m/%d') > DATE_FORMAT('"+this.paydayAdjust+"','%Y/%m/%d') then t.corpus else 0 end ) remainfincorpus,"
+ "sum(case when DATE_FORMAT(t.plandate,'%Y/%m/%d') > DATE_FORMAT('"+this.paydayAdjust+"','%Y/%m/%d') then t.penalty else 0 end ) remainfpenalty,"
+" sum(case when DATE_FORMAT( t.plandate,'%Y/%m/%d') > DATE_FORMAT('"+this.paydayAdjust+"','%Y/%m/%d') then t.interest else 0 end ) remainfininterest,"
+" sum(case when DATE_FORMAT( t.plandate,'%Y/%m/%d') < DATE_FORMAT('"+this.paydayAdjust+"','%Y/%m/%d') then t.overduerent else 0 end ) overduerent,sum(case when t.cou< t.planlist and t.planlist-t.cou<=12-t.cou then t.interest else 0 end) fist_rent,"
+ " sum(case when t.cou< t.planlist and t.planlist-t.cou>12-t.cou then t.corpus else 0 end) fist_corpus,"
+ " max( case when DATE_FORMAT(t.plandate, '%Y/%m/%d') < DATE_FORMAT('"+this.paydayAdjust+"','%Y/%m/%d') then t.plandate else '0' end) plandate"
+ " from ("
+ " select "
+ " t1.corpus-ifnull(t2.sumincomecorpus,0) remaincorpus,"
+ " t1.interestbusiness - ifnull(t2.sumincomeinterest,0) remaininterest,"
+ " t1.rent - ifnull(t2.sumincomerent,0) overduerent, t1.rent,"
+ " t1.corpus,"
+ " t1.interest,"
+ " IFNULL(t1.penalty,0)-ifnull(t2.sumincomepenalty,0) penalty,"
+ " t1.plandate,"
+ " t1.planlist,IFNULL(t2.planlist,0) cou"
+ " from ("
+ " select cfrp.plan_date plandate,"
+ " cfrp.plan_list planlist,"
+ " cfrp.rent,"
+ " cfrp.interest,"
+ " cfrp.corpus,"
+ " cfrp.penalty,"
+ " cfrp.corpus_business corpusbusiness,"
+ " cfrp.interest_business interestbusiness,"
+ " cfrp.contract_id contractid"
+ " from LC_RENT_PLAN cfrp "
+ " where cfrp.contract_id = ? and payment_number=?"
+ " ) t1"
+ " left join "
+ " (select sum( cfri.corpus ) sumincomecorpus"
+ " ,sum(cfri.interest ) sumincomeinterest,"
+ " sum( cfri.rent ) sumincomerent,"
+ " sum( cfri.penalty ) sumincomepenalty,"
+ " min(cfri.contract_id) contractid,"
+ " cfri.plan_list planlist "
+ " from LC_RENT_INCOME cfri "
+ " where cfri.contract_id = ? and payment_number=?"
+ " group by cfri.plan_list"
+ " )t2 on t2.contractid = t1.contractid and t2.planlist = t1.planlist"
+ " ) t ";
if("ORACLE".equals(InitDBType.DBTYPE)){
sql=sql.replaceAll("DATE_FORMAT", "to_date");
sql=sql.replaceAll("ifnull", "nvl");
}
List<Map<String, String>> results = conn.executeQuery(sql, this.contractId,this.getPaymentnumber(),this.contractId,this.getPaymentnumber());
Map<String, String> detailInfo = results.get(0);
for(String key : detailInfo.keySet()){
jsonObject.appendElement(key, detailInfo.get(key));
}
if(detailInfo.size()>0){
String planDate=detailInfo.get("plandate");
BigDecimal fistCorpus= new BigDecimal(detailInfo.get("fist_corpus"));
long numberDay=DateUtils.DateDays(this.paydayAdjust, planDate);
BizObject bo=bm.createQuery(" select * from O where contract_id=:contract_id").setParameter("contract_id", this.contractId).getSingleResult(false);
BigDecimal yearRate= new BigDecimal(bo!=null?bo.getAttribute(LC_CALC_CONDITION.YEAR_RATE).toString():"");
BigDecimal repaymentInterest=fistCorpus.multiply(yearRate).divide(new BigDecimal(360), 20, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(numberDay));
jsonObject.appendElement("REPAYMENT_INTEREST", repaymentInterest.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
}
return JSONEncoder.encode(jsonObject);
}catch(Exception e){
tx.rollback();
e.printStackTrace();
return "";
}finally{
tx.commit();
}
}
/**
* 获取投放方案 应收 应退
* @return
* @throws Exception
*/
public String getTerminateOtherInfo() throws Exception {
JSONObject jsonObject = JSONObject.createObject();
String fundSql = "select t2.sumplanin-ifnull(t1.sumchargein,0) fundin,t2.sumplanout- ifnull(t1.sumchargeout,0) fundout,max(lc.NOMINAL_PRICE) nominalprice from ( "
+ " select sum(case ffc.pay_type when 'pay_type_in' then ffc.fact_money else 0 end )sumchargein, "
+ " sum(case ffc.pay_type when 'pay_type_out' then ffc.fact_money else 0 end )sumchargeout, "
+ " min(ffc.contract_id) contractid,min(ffc.payment_number) paymentnumber "
+ " from LC_FUND_INCOME ffc where ffc.contract_id = ? and ffc.payment_number=? "
+ " )t1 right join "
+ " (select sum(case ffp.pay_type when 'pay_type_in' then ffp.plan_money else 0 end )sumplanin, "
+ " sum(case ffp.pay_type when 'pay_type_out' then ffp.plan_money else 0 end)sumplanout, "
+ " min(ffp.contract_id) contractid,min(ffp.payment_number) paymentnumber "
+ " from LC_FUND_PLAN ffp "
+ " where ffp.contract_id = ? and ffp.payment_number=? "
+ " )t2 on t1.contractid = t2.contractid and t1.paymentnumber=t2.paymentnumber left join lc_calc_condition lc on lc.CONTRACT_ID=t2.contractid";
if("ORACLE".equals(InitDBType.DBTYPE)){
fundSql=fundSql.replaceAll("ifnull", "nvl");
}
JBOTransaction tx=null;
try {
tx=JBOFactory.createJBOTransaction();
Conn conn = new Conn(tx);
List<Map<String, String>> list = conn.executeQuery(fundSql, this.contractId ,this.getPaymentnumber(), this.contractId,this.getPaymentnumber());
if(null != list && list.size() > 0 ){
Map<String, String> fundMap = list.get(0);
jsonObject.appendElement("OTHER_IN",fundMap.get("fundin"));
jsonObject.appendElement("OTHER_OUT",fundMap.get("fundout"));
jsonObject.appendElement("NOMINAL_PRICE",fundMap.get("nominalprice"));
}else{
jsonObject.appendElement("OTHER_IN","0");
jsonObject.appendElement("OTHER_OUT","0");
jsonObject.appendElement("NOMINAL_PRICE","0");
}
} catch (JBOException e) {
tx.rollback();
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(tx!=null){
tx.commit();
}
}
return JSONEncoder.encode(jsonObject);
}
public String getTerminateOtherInfoFinish() throws Exception {
JSONObject jsonObject = JSONObject.createObject();
String fundSql = "select t2.sumplanin,t2.sumplanout,max(lc.NOMINAL_PRICE) nominalprice from ( "
+ " select sum(case ffc.pay_type when 'pay_type_in' then ffc.fact_money else 0 end )sumchargein, "
+ " sum(case ffc.pay_type when 'pay_type_out' then ffc.fact_money else 0 end )sumchargeout, "
+ " min(ffc.contract_id) contractid,min(ffc.payment_number) paymentnumber "
+ " from LC_FUND_INCOME ffc where ffc.contract_id = ? and ffc.payment_number=? "
+ " )t1 right join "
+ " (select sum(case ffp.pay_type when 'pay_type_in' then ffp.plan_money else 0 end )sumplanin, "
+ " sum(case ffp.pay_type when 'pay_type_out' then ffp.plan_money else 0 end)sumplanout, "
+ " min(ffp.contract_id) contractid,min(ffp.payment_number) paymentnumber "
+ " from LC_FUND_PLAN ffp "
+ " where ffp.contract_id = ? and ffp.payment_number=? and ffp.FEE_TYPE='feetype16' "
+ " )t2 on t1.contractid = t2.contractid and t1.paymentnumber=t2.paymentnumber left join lc_calc_condition lc on lc.CONTRACT_ID=t2.contractid";
if("ORACLE".equals(InitDBType.DBTYPE)){
fundSql=fundSql.replaceAll("ifnull", "nvl");
}
JBOTransaction tx=null;
try {
tx=JBOFactory.createJBOTransaction();
Conn conn = new Conn(tx);
List<Map<String, String>> list = conn.executeQuery(fundSql, this.contractId ,this.getPaymentnumber(), this.contractId,this.getPaymentnumber());
if(null != list && list.size() > 0 ){
Map<String, String> fundMap = list.get(0);
jsonObject.appendElement("OTHER_IN",fundMap.get("sumplanin"));
jsonObject.appendElement("OTHER_OUT",fundMap.get("sumplanout"));
jsonObject.appendElement("NOMINAL_PRICE",fundMap.get("nominalprice"));
}else{
jsonObject.appendElement("OTHER_IN","0");
jsonObject.appendElement("OTHER_OUT","0");
jsonObject.appendElement("NOMINAL_PRICE","0");
}
} catch (JBOException e) {
tx.rollback();
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(tx!=null){
tx.commit();
}
}
return JSONEncoder.encode(jsonObject);
}
public String getContractId() {
return contractId;
}
public void setContractId(String contractId) {
this.contractId = contractId;
}
public String getStartList() {
return startList;
}
public void setStartList(String startList) {
this.startList = startList;
}
public String getPaydayAdjust() {
return paydayAdjust;
}
public void setPaydayAdjust(String paydayAdjust) {
this.paydayAdjust = paydayAdjust;
}
public String getPaymentnumber() {
return paymentnumber;
}
public void setPaymentnumber(String paymentnumber) {
this.paymentnumber = paymentnumber;
}
}