2019-12-24 14:58:51 +08:00

509 lines
22 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.lease.app.quartzmession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import jbo.com.tenwa.lease.comm.CONTRACT_RENT_MONTH;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.amarsoft.app.awe.config.InitDBType;
import com.amarsoft.are.ARE;
import com.amarsoft.are.jbo.JBOException;
import com.amarsoft.are.jbo.JBOFactory;
import com.amarsoft.are.log.Log;
import com.amarsoft.are.util.StringFunction;
import com.amarsoft.awe.util.ASResultSet;
import com.amarsoft.awe.util.SqlObject;
import com.amarsoft.awe.util.Transaction;
import com.amarsoft.dict.als.cache.CacheLoaderFactory;
import com.base.util.QuartzUtil;
import com.tenwa.leasing.service.voucher.CreateVoucherService;
import com.tenwa.reckon.constant.Scale;
import com.tenwa.reckon.util.Conn;
import com.tenwa.reckon.util.DateTools;
import com.tenwa.reckon.util.DateUtils;
public class SplitInts implements Job{
/*
* (non-Javadoc)
* @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
* 月末利息拆分、手续费均摊。
*/
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
Transaction Sqlca =null;
String startime = StringFunction.getTodayNow();
// 未计提或发生变更的合同重新计提
try {
Sqlca = Transaction.createTransaction(CacheLoaderFactory.getDataSource());
String sql="select ci.contract_id,ci.payment_number,cc.period_type,cc.start_date,cc.handling_charge_money, "
+" cc.income_interval_month,cc.clean_lease_money,crm.amount from lc_calc_condition_status ci "
+ " left join lc_calc_condition cc on cc.payment_number=ci.payment_number "
+ " left join (select count(1) amount,crm.payment_number "
+ " from CONTRACT_RENT_MONTH crm group by crm.payment_number) crm "
+ " on crm.payment_number=ci.payment_number where ci.plan_status > 30 and ci.plan_status < 100";
SqlObject asql = new SqlObject(sql);
ASResultSet rs = null;
rs = Sqlca.getASResultSet(asql);
//初始化计提表
truncateContractRentMonth();
while(rs.next()){
String payment_number=rs.getStringValue("payment_number");
String period_type=rs.getStringValue("period_type");
String contract_id=rs.getStringValue("contract_id");
String handling_charge_money=rs.getStringValue("handling_charge_money");
String clean_lease_money=rs.getStringValue("clean_lease_money");
String income_number_year=rs.getStringValue("INCOME_INTERVAL_MONTH");
String start_date=rs.getStringValue("start_date");
if(start_date==null||"".equals(start_date)){
continue;
}
List<String> dateList=new ArrayList<String>();
List<String> interestlist=new ArrayList<String>();
List<String> planidList=new ArrayList<String>();
List<BigDecimal> handMoneyList=new ArrayList<BigDecimal>();
List<String> AllRemainCorpus=new ArrayList<String>();
AllRemainCorpus.add(clean_lease_money);
Conn conn =new Conn(Sqlca);
String sql1="select * from lc_rent_plan cfrp where cfrp.payment_number=? order by cfrp.plan_list";
List<Map<String,String>> rentPlans = conn.executeQuery(sql1, payment_number);
for(int i=0;i<rentPlans.size();i++ ){
Map<String,String> rentplan=rentPlans.get(i);
dateList.add(rentplan.get("plan_date"));
interestlist.add(rentplan.get("interest"));
planidList.add(rentplan.get("id"));
if(i<rentPlans.size()-1){
AllRemainCorpus.add(rentplan.get("all_remain_corpus"));
}
}
//handMoneyList=gethandMoneyList(handling_charge_money, AllRemainCorpus);
handMoneyList=gethandMoneyList(handling_charge_money, dateList, start_date, period_type, income_number_year);
List<Map<String,String>> contractRentMonth=null;
//if("period_type_0".equals(period_type)){
contractRentMonth=splitInterest0(contract_id,payment_number, start_date, dateList, interestlist,handMoneyList, planidList);
/*}else{
contractRentMonth=splitInterest1(contract_id, start_date,income_number_year ,dateList, interestlist,handMoneyList, planidList);
}*/
//插入数据并生成凭证
insertContractRentMonth(contractRentMonth,Sqlca);
}
rs.close();
Sqlca.commit();
QuartzUtil.insertLog(startime,"com.tenwa.lease.app.quartzmession.SplitInts", "success", "成功", "admin");
} catch (Exception e) {
QuartzUtil.insertLog(startime,"com.tenwa.lease.app.quartzmession.SplitInts", "success", "失败", "admin");
logger.error(e.getMessage());
e.printStackTrace();
}finally{
if(Sqlca!=null){
try {
Sqlca.disConnect();
} catch (JBOException e) {
e.printStackTrace();
}
Sqlca = null;
}
}
}
private static Log logger=ARE.getLog();
//期末付款利息拆分
public List<Map<String,String>> splitInterest0(String contract_id,String payment_number,String startDate,List<String> dateList,List<String> interestlist,List<BigDecimal> handMoneyList,List<String> planidList){
try {
List<Map<String,String>> contractRentMonth=new ArrayList<Map<String,String>>();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
for(int i=0;i<dateList.size();i++){
BigDecimal interest=new BigDecimal(interestlist.get(i));//当期利息
BigDecimal handMoney=handMoneyList.get(i);//当期手续费
String planid=planidList.get(i);//租金计划期次ID;
String endDate=dateList.get(i);//计划日期
Calendar start=Calendar.getInstance();
start.setTime(sdf.parse(startDate));
Calendar end=Calendar.getInstance();
end.setTime(sdf.parse(endDate));
//月份差
int numberYear=DateTools.getMonthDiff(startDate, endDate);
//分母
long down=DateTools.getDateDiff(endDate, startDate);
//第一期第一笔利息拆分,只有起租日后的一部分
if(i==0){
Map<String,String> map=new HashMap<String, String>();
BigDecimal monthInterest=BigDecimal.ZERO;
BigDecimal monthhandMoney=BigDecimal.ZERO;
int beginday=start.get(Calendar.DAY_OF_MONTH);
int monthEndDay=Integer.parseInt(DateUtils.getLastDayOfMonth(start.get(Calendar.YEAR)+"", start.get(Calendar.MONTH)+1+""));
int up=monthEndDay-beginday;
if(down != 0){
monthInterest=monthInterest.add(interest.multiply(new BigDecimal(up)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP));
monthhandMoney=monthhandMoney.add(handMoney.multiply(new BigDecimal(up)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP));
}
map.put("rent_list", i+1+"");
map.put("interest", monthInterest+"");
map.put("fee", monthhandMoney+"");
map.put("payment_number", payment_number);
map.put("contract_id", contract_id);
map.put("plan_id", planid);
map.put("start_date", sdf.format(start.getTime()));
start.set(Calendar.DAY_OF_MONTH, monthEndDay);
map.put("end_date", sdf.format(start.getTime()));
map.put("accrued_date", start.get(Calendar.MONTH)+1+"");
map.put("create_date", DateTools.getSystemDate());
contractRentMonth.add(map);
start.set(Calendar.DAY_OF_MONTH, 1);
start.set(Calendar.MONTH, start.get(Calendar.MONTH)+1);
}else{//前部分利息已拆分
start.set(Calendar.DAY_OF_MONTH, 1);
start.set(Calendar.MONTH, start.get(Calendar.MONTH)+1);
}
//每期利息按月拆分
for(int j=0;j<numberYear;j++){
Map<String,String> map=new HashMap<String, String>();
BigDecimal monthInterest=BigDecimal.ZERO;
BigDecimal monthhandMoney=BigDecimal.ZERO;
int endday=end.get(Calendar.DAY_OF_MONTH);
int monthEndDay=Integer.parseInt(DateUtils.getLastDayOfMonth(start.get(Calendar.YEAR)+"", start.get(Calendar.MONTH)+1+""));
//当期最后一个月份时,下半段利息为次期一部分
if(j<numberYear-1){
int up=monthEndDay;
monthInterest=interest.multiply(new BigDecimal(up)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP);
monthhandMoney=handMoney.multiply(new BigDecimal(up)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP);
}else{//当期最后一个月份时,下半段利息为次期一部分
int up1=endday;
int up2=monthEndDay-endday;
monthInterest=monthInterest.add(interest.multiply(new BigDecimal(up1)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP));
monthhandMoney=monthhandMoney.add(handMoney.multiply(new BigDecimal(up1)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP));
//如果不是最后一期
if(i<dateList.size()-1){
long down2=DateTools.getDateDiff(dateList.get(i+1), dateList.get(i));
BigDecimal interest2=new BigDecimal(interestlist.get(i+1));//次期利息
BigDecimal handMoney2=handMoneyList.get(i+1);//次期利息
if(down2<=0){
monthInterest=monthInterest.add(interest2);
monthhandMoney=monthhandMoney.add(handMoney2);
}else{
monthInterest=monthInterest.add(interest2.multiply(new BigDecimal(up2)).divide(new BigDecimal(down2),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP));
monthhandMoney=monthhandMoney.add(handMoney2.multiply(new BigDecimal(up2)).divide(new BigDecimal(down2),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP));
}
}else{
monthEndDay=endday;
}
}
map.put("rent_list", i+1+"");
map.put("interest", monthInterest+"");
map.put("fee", monthhandMoney+"");
map.put("payment_number", payment_number);
map.put("contract_id", contract_id);
map.put("plan_id", planid);
start.set(Calendar.DAY_OF_MONTH, 1);
map.put("start_date", sdf.format(start.getTime()));
start.set(Calendar.DAY_OF_MONTH, monthEndDay);
map.put("end_date", sdf.format(start.getTime()));
map.put("accrued_date", start.get(Calendar.MONTH)+1+"");
map.put("create_date", DateTools.getSystemDate());
contractRentMonth.add(map);
start.set(Calendar.DAY_OF_MONTH, 1);
start.set(Calendar.MONTH, start.get(Calendar.MONTH)+1);
}
startDate=endDate;
}
return contractRentMonth;
} catch (Exception e) {
logger.error("利息拆分出错!!");
e.printStackTrace();
}
return null;
}
//期初付款利息拆分
public List<Map<String,String>> splitInterest1(String contractId,String startDate,String incomeNumberyear,List<String> dateList,List<String> interestlist,List<BigDecimal> handMoneyList,List<String> planidList){
try {
List<Map<String,String>> contractRentMonth=new ArrayList<Map<String,String>>();
int number=Integer.parseInt(incomeNumberyear.substring(7));
SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
for(int i=0;i<dateList.size();i++){
BigDecimal interest=new BigDecimal(interestlist.get(i));//当期利息
BigDecimal handMoney=handMoneyList.get(i);//当期手续费
String planid=planidList.get(i);//租金计划期次ID;
String endDate=dateList.get(i);//计划日期
Calendar start=Calendar.getInstance();
start.setTime(sdf.parse(startDate));
Calendar end=Calendar.getInstance();
end.setTime(sdf.parse(endDate));
startDate=endDate;
//最后一次结束时间设置
if(i<dateList.size()-1){
endDate=dateList.get(i+1);
end.setTime(sdf.parse(dateList.get(i+1)));
}else{
end.set(Calendar.MONTH, end.get(Calendar.MONTH)+number);
endDate=sdf.format(end.getTime());
}
//月份差
int numberYear=DateTools.getMonthDiff(startDate, endDate);
//分母
long down=DateTools.getDateDiff(endDate, startDate);
//第一期第一笔利息拆分,只有起租日后的一部分
if(i==0){
Map<String,String> map=new HashMap<String, String>();
BigDecimal monthInterest=BigDecimal.ZERO;
BigDecimal monthhandMoney=BigDecimal.ZERO;
int beginday=start.get(Calendar.DAY_OF_MONTH);
int monthEndDay=Integer.parseInt(DateUtils.getLastDayOfMonth(start.get(Calendar.YEAR)+"", start.get(Calendar.MONTH)+1+""));
int up=monthEndDay-beginday;
monthhandMoney=monthhandMoney.add(handMoney.multiply(new BigDecimal(up)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP));
map.put("rent_list", i+1+"");
map.put("interest", monthInterest+"");
map.put("fee", monthhandMoney+"");
map.put("contract_id", contractId);
map.put("plan_id", planid);
map.put("start_date", sdf.format(start.getTime()));
start.set(Calendar.DAY_OF_MONTH, monthEndDay);
map.put("end_date", sdf.format(start.getTime()));
map.put("accrued_date", start.get(Calendar.MONTH)+1+"");
map.put("create_date", DateTools.getSystemDate());
contractRentMonth.add(map);
start.set(Calendar.DAY_OF_MONTH, 1);
start.set(Calendar.MONTH, start.get(Calendar.MONTH)+1);
}
//每期利息按月拆分
for(int j=0;j<numberYear;j++){
Map<String,String> map=new HashMap<String, String>();
BigDecimal monthInterest=BigDecimal.ZERO;
BigDecimal monthhandMoney=BigDecimal.ZERO;
int endday=end.get(Calendar.DAY_OF_MONTH);
int monthEndDay=Integer.parseInt(DateUtils.getLastDayOfMonth(end.get(Calendar.YEAR)+"", end.get(Calendar.MONTH)+1+""));
if(j>0){
int up=monthEndDay;
monthInterest=interest.multiply(new BigDecimal(up)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP);
monthhandMoney=handMoney.multiply(new BigDecimal(up)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP);
//如果是最后一期最后一月
if(i==dateList.size()-1){
int up2=endday;
monthInterest=monthInterest.add(interest.multiply(new BigDecimal(up2)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP));
monthhandMoney=monthhandMoney.add(handMoney.multiply(new BigDecimal(up2)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP));
monthEndDay=endday;
}
}else{//当期第一个月份时,下半段利息为次期一部分
//如果是最后一期最后一月
if(i==dateList.size()-1){
int up3=endday;
monthInterest=monthInterest.add(interest.multiply(new BigDecimal(up3)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP));
monthhandMoney=monthhandMoney.add(handMoney.multiply(new BigDecimal(up3)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP));
monthEndDay=endday;
}else{
int up1=endday;
int up2=monthEndDay-endday;
Calendar sd=Calendar.getInstance();
sd.setTime(sdf.parse(startDate));
sd.add(Calendar.MONTH, number);
Calendar ed=Calendar.getInstance();
ed.setTime(sdf.parse(endDate));
ed.add(Calendar.MONTH, number);
long down2=DateTools.getDateDiff(sdf.format(ed.getTime()), sdf.format(sd.getTime()));
BigDecimal interest2=new BigDecimal(interestlist.get(i+1));//次期利息
BigDecimal handMoney2=handMoneyList.get(i+1);//次期手续费
if(down2<=0){
monthInterest=monthInterest.add(interest2);
monthhandMoney=monthhandMoney.add(handMoney2);
}else{
monthInterest=monthInterest.add(interest2.multiply(new BigDecimal(up2)).divide(new BigDecimal(down2),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP));
monthhandMoney=monthhandMoney.add(handMoney2.multiply(new BigDecimal(up2)).divide(new BigDecimal(down2),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP));
}
monthInterest=monthInterest.add(interest.multiply(new BigDecimal(up1)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP));
monthhandMoney=monthhandMoney.add(handMoney.multiply(new BigDecimal(up1)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP));
}
}
map.put("rent_list", i+1+"");
map.put("interest", monthInterest+"");
map.put("fee", monthhandMoney+"");
map.put("contract_id", contractId);
map.put("plan_id", planid);
end.set(Calendar.DAY_OF_MONTH, 1);
map.put("start_date", sdf.format(end.getTime()));
end.set(Calendar.DAY_OF_MONTH, monthEndDay);
map.put("end_date", sdf.format(end.getTime()));
map.put("accrued_date", start.get(Calendar.MONTH)+1+"");
map.put("create_date", DateTools.getSystemDate());
contractRentMonth.add(map);
end.set(Calendar.MONTH, end.get(Calendar.MONTH)+1);
}
}
return contractRentMonth;
} catch (Exception e) {
logger.error("利息拆分出错!!");
e.printStackTrace();
}
return null;
}
/**
* 手续费按期拆分
* @param handMoney
* @param remainCorpusList
* @return
*/
public List<BigDecimal> gethandMoneyList(String handMoney,List<String> remainCorpusList){
List<BigDecimal> handMoneyList=new ArrayList<BigDecimal>();
BigDecimal hmoney=new BigDecimal(handMoney);
BigDecimal money=BigDecimal.ZERO;
BigDecimal all=BigDecimal.ZERO;
for(int i=0;i<remainCorpusList.size();i++){
BigDecimal cur=new BigDecimal(remainCorpusList.get(i));
all=all.add(cur);
}
for(int i=0;i<remainCorpusList.size();i++){
BigDecimal mon=BigDecimal.ZERO;
if(i<remainCorpusList.size()-1){
BigDecimal cur=new BigDecimal(remainCorpusList.get(i));
mon=hmoney.multiply(cur).divide(all,Scale.CORPUS_SCALE,BigDecimal.ROUND_HALF_UP);
money=money.add(mon);
}else{
mon=hmoney.subtract(money);
}
handMoneyList.add(mon);
}
return handMoneyList;
}
/**
* 手续费按每期天数进行拆分
* @param handMoney
* @param dateList
* @param startDate
* @param period_type
* @param incomeNumberyear
* @return handMoneyList
*/
public List<BigDecimal> gethandMoneyList(String handMoney,List<String> dateList,String startDate,String period_type,String incomeNumberyear){
List<BigDecimal> handMoneyList=new ArrayList<BigDecimal>();
try {
BigDecimal hmoney=new BigDecimal(handMoney);
int number=Integer.parseInt(incomeNumberyear);
if("period_type_1".equals(period_type)){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
dateList.remove(0);
Calendar enddate=Calendar.getInstance();
enddate.setTime(sdf.parse(dateList.get(dateList.size()-1)));
enddate.add(Calendar.MONTH, number);
dateList.add(sdf.format(enddate.getTime()));
}
BigDecimal money=BigDecimal.ZERO;
BigDecimal all=BigDecimal.ZERO;
all=new BigDecimal(DateTools.getDateDiff(dateList.get(dateList.size()-1), startDate));
for(int i=0;i<dateList.size();i++){
BigDecimal mon=BigDecimal.ZERO;
long dateDiff=DateTools.getDateDiff(dateList.get(i), startDate);
if(i<dateList.size()-1){
BigDecimal cur=new BigDecimal(dateDiff);
mon=hmoney.multiply(cur).divide(all,Scale.CORPUS_SCALE,BigDecimal.ROUND_HALF_UP);
money=money.add(mon);
}else{
mon=hmoney.subtract(money);
}
handMoneyList.add(mon);
startDate=dateList.get(i);
}
}catch (Exception e) {
logger.error("手续费拆分出错!");
e.printStackTrace();
}
return handMoneyList;
}
//插入计提表,当月计提利息计入凭证
public void insertContractRentMonth(List<Map<String,String>> contractRentMonth,Transaction tx) throws Exception{
Conn conn =new Conn(tx);
for(Map<String,String> map:contractRentMonth){
String sql1="insert into contract_rent_month( id ";
String sql2=" values ( replace(UUID(),'-','') ";
if("ORACLE".equals(InitDBType.DBTYPE)){
sql2=" values ( sys_guid() ";
}
Set<String> keys = map.keySet();
for(String key :keys){
sql1+=","+key;
sql2+=",'"+map.get(key)+"'";
}
String sql=sql1+") "+sql2+") ";
conn.executeUpdate(sql);
//若为当月,则生成凭证
SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
Calendar current=Calendar.getInstance();
current.setTime(sdf.parse(DateTools.getSystemDate()));
Calendar plan=Calendar.getInstance();
plan.setTime(sdf.parse(map.get("end_date")));
if(plan.get(Calendar.YEAR)==current.get(Calendar.YEAR)&&plan.get(Calendar.MONTH)==current.get(Calendar.MONTH)){
//map.get("interest");为当月利息fee为当月手续费,contract_id;
// TODO 在此生成凭证
Transaction sqlca1 = Transaction.createTransaction(CacheLoaderFactory.getDataSource());
/*String interest = map.get("interest");//利息
String payment_number = map.get("payment_number");//合同id
String handling_fee = map.get("fee");//手续费
String account_date = map.get("end_date");//生成日期
CreateVoucherService c = new CreateVoucherServiceImpl();*/
try{
/* c.createVoucher_rentmonth(payment_number, handling_fee, interest,account_date, null, sqlca1);
*/ }catch(Exception e){
e.printStackTrace();
sqlca1.rollback();
}finally{
sqlca1.disConnect();
}
}
}
}
//初始化计提表
public void truncateContractRentMonth() throws Exception{
JBOFactory.createBizObjectQuery(CONTRACT_RENT_MONTH.CLASS_NAME, "delete from O").executeUpdate();
}
public static void main(String[] args) {
String contractid="11213";
String startDate="2017/4/11";
List<String> dateList=new ArrayList<String>();
List<String> interestlist=new ArrayList<String>();
List<String> planidList=new ArrayList<String>();
List<BigDecimal> handMoneyList=new ArrayList<BigDecimal>();
dateList.add("2017/5/11");
dateList.add("2017/6/11");
dateList.add("2017/7/11");
interestlist.add("10000");
interestlist.add("20000");
interestlist.add("30000");
handMoneyList.add(new BigDecimal("2000"));
handMoneyList.add(new BigDecimal("3000"));
handMoneyList.add(new BigDecimal("4000"));
planidList.add("1111111");
planidList.add("1111111");
planidList.add("1111111");
/* System.out.println(new SplitInts().splitInterest0(contractid,startDate,dateList,interestlist,handMoneyList,planidList));
*/ }
}