/**
 *
 * @(#)author  [ok]
 * @(#)version 1.0
 * @(#)date    2014. 06. 25
 * @(#)since   JDK 1.7.2
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 * All rights reserved.
 * This software is the proprietary information of udapsoft, Inc.
 */
package kr.co.udapsoft.ebid.buyer.payment.outside.biz;


import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.base.WAFMessages;
import kr.co.hsnc.common.sql.RowSet;
import kr.co.hsnc.common.sql.persistent.Persistent;
import kr.co.hsnc.common.sql.persistent.PersistentImpl;
import kr.co.hsnc.common.sql.search.Search;
import kr.co.hsnc.common.sql.search.SearchImpl;
import kr.co.hsnc.common.sql.util.RowSetUtility;
import kr.co.hsnc.common.util.ValueObject;
import kr.co.udapsoft.common.sender.MessageSend;

import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

public class AdvencePaymentSaveBiz extends Biz {
	
	public AdvencePaymentSaveBiz(HandlerStorage storage) {
		super(storage);
	}

	public void doBiz() throws Exception {

		WAFLogger.debug("선급금 신청 상세 저장 처리");
		
		boolean success = false;
		if(storage.compareEvent("SAVE_DATA")){	//데이터 저장
			success = saveData(params);
		}else if(storage.compareEvent("DELETE_DATA")){	//데이터 삭제
			success = deleteData(params);
		}else if(storage.compareEvent("REQ_DATA")){		//선급금신청 요청
			
			success = reqData(params);
			
			String resUserName = params.get("resUserName");			//계약 담당자명
			String resHp = params.get("resHp").replaceAll("-", "");	//계약 담당자 핸드폰번호
			String resEmail = params.get("resEmail");				//계약 담당자 이메일
			String resDivision = params.get("resDivision");			

			String hp = params.get("hp").replaceAll("-", "");	//업체 담당자 핸드폰번호
			String eMail = params.get("eMail");					//업체 담당자 이메일
        	String memberName = params.get("memberName");		//업체명
        	String userName = params.get("userName");			//업체 담당자명
        	String deptName = params.get("deptName");			//현장명
        	String conDocNo = params.get("conDocNo");			//계약번호
        	String buildName = params.get("buildName");			//계약명

			//SMS
	      	MessageSend messageSend = new MessageSend(storage);
	      	String recvAddress = hp;
        	String smsContent = WAFMessages.getMessage("Prepayments.Application.Request");
        	WAFLogger.debug(smsContent);
        	messageSend.sendSMS(recvAddress, storage.getUser().get("USER_PHONE"), storage.getUser().get("ACCT_ID"), smsContent);  //2015.9.21 resHp
			
        	//E-Mail
        	MessageSend mailSend = new MessageSend(storage);
        	StringBuffer mailContent = new StringBuffer();
        	mailContent.append(memberName+ "님은 선급금 신청바랍니다. </BR>");
        	mailContent.append("협력업체포탈시스템에 접속하시어 선급금정보 확인후 신청바랍니다.</BR>");
        	mailContent.append("<font style='width:auto; font-size:12px; line-height:18px; padding:5px; text-align:left;'>");
        	mailContent.append("</BR>");
        	mailContent.append("1.프로젝트: "+deptName+"</BR>");
        	mailContent.append("2.계약번호: "+conDocNo+"</BR>");
        	mailContent.append("3.계약명: " +buildName+"</BR>");
        	mailContent.append("4.계약담당자: "+resDivision+"/" +resUserName+"(연락처 :"+resHp+")</BR>");
        	mailContent.append("</font>");
        	messageSend.sendEmail(eMail, resEmail, "선급금 신청 요청 메일입니다.", "선급금 신청 요청 메일입니다.", mailContent);			
			
		}else if(storage.compareEvent("APPROVAL")){		//선급 결재 완료
			success = approvalData(params);
		}else if(storage.compareEvent("CHK_REJECT")){
			
			String resultChk = "1";	//불가
			String status = "";
			
			status = getRejectionCheck();
			
			if(!status.equals("04")){	//결재중
				resultChk = "0";	//가능
			} 
    		ValueObject vo = new ValueObject();
    		vo.set("checkFlag", resultChk);
    		storage.addValueObject("VALUEOBJECT", vo);
    		storage.setSuccess(true);
    		
		}else if(storage.compareEvent("SAVE_SEGUM")){
			success = segumData(params);
		}else if(storage.compareEvent("CHK_SEGUM")){
			
			String resultChk = "1";	//불가
			String status = "";
			
			status = getRejectionCheck();
			
			if(!status.equals("04")){	//결재중
				resultChk = "0";	//가능
			} 
    		ValueObject vo = new ValueObject();
    		vo.set("checkFlag", resultChk);
    		storage.addValueObject("VALUEOBJECT", vo);
    		storage.setSuccess(true);
		
		} else if(storage.compareEvent("CHK_APPVAR")){
			
			String resultChk = "1";	//불가
			String status = "";
			
			status = getRejectionCheck();
			
			if(!status.equals("04")){	//결재중
				resultChk = "0";	//가능
			} 
    		ValueObject vo = new ValueObject();
    		vo.set("checkFlag", resultChk);
    		storage.addValueObject("VALUEOBJECT", vo);
    		storage.setSuccess(true);

		} else if(storage.compareEvent("GET_RATE")){
			
			String rateEx = "0";
			String rateAmt = "0";
			String rateVamt = "0";
			String rateDy = params.get("date1").replace("-", "");
			String curGb = params.get("conCurCd");
			String reqPrepaySupplyAmt = params.get("reqPrepaySupplyTamt").replace(",", "");
			String inputVat = params.get("bugase");
			
			ValueObject vo = new ValueObject();
			
			if(!"KRW".equals(curGb)){
				
				ValueObject RageExValue = getRageEx(rateDy, curGb, reqPrepaySupplyAmt, inputVat);
				//rateEx = RageExValue.get("RATE");
				//rateAmt = rowObject.get("RATE_AMT");
				//rateVamt = rowObject.get("RATE_VAMT");
				
				vo.set("rate", RageExValue.get("RATE"));
				vo.set("rateAmt", RageExValue.get("RATE_AMT"));
				vo.set("rateVamt", RageExValue.get("RATE_VAMT"));
				
			}else{
	
				vo.set("rate", rateEx);
				vo.set("rateAmt", rateAmt);
				vo.set("rateVamt", rateVamt);
			}
			
    		storage.addValueObject("VALUEOBJECT", vo);
    		storage.setSuccess(true);

		}else if(storage.compareEvent("SAVE_GUA")){
			success = false;
			success = guaData(params);
			storage.setSuccess(success);
			
		}else if(storage.compareEvent("RNNC_DATA")){
			success = false;
			success = rnncData(params);
			
		}
		
		storage.setSuccess(success);
		
	}

	/**
	 * Persistent 를 이용한 Save 처리
	 * @param params
	 * @return
	 * * 입력받은 선급금 상세 정보를 등록합니다.
     * @param connection
     * @param pgmName
     * @param screenName
     * @param systemCode
     * @throws Exception
	 */
	private boolean saveData(ValueObject params) {
		
		
		Persistent persistent_u = null;
		String sqlstr_u = "";

		Persistent persistent_u1 = null;
		String sqlstr_u1 = "";

		Persistent persistent_u2 = null;
		String sqlstr_u2 = "";
		
		try {
		
			//선급금 신청 상제 정보
			String prepayId					= params.get("prepayId");				//선급ID
			String subContModId				= params.get("subContModId");			//외주계약변경ID
			String prepaySeq				= "1";									//선급금지급차수
			String corpId					= params.get("corpId");					//협력업체ID
			String prepayDate				= "";									//선급일자
			String perpayAmt				= params.get("perpayAmt");				//계약_선급금
			String perpaySupplyAmt			= "";									//계약_선급금공급가
			String subPrepaySupplyTamt		= params.get("subPrepaySupplyTamt");	//계약_선급금공급가_과세
			String subPrepaySupplyNamt		= params.get("subPrepaySupplyNamt");	//계약_선급금공급가_면세
			String subPrepaySurtaxAmt		= params.get("subPrepaySurtaxAmt");		//계약_선급금부가세
			String reqPrepayAmt				= params.get("reqPrepayAmt");			//신청_선급금합계
			//String reqPrepaySupplyAmt		= String.valueOf((Integer.parseInt(params.get("reqPrepaySupplyTamt").replaceAll(",", "")) + Integer.parseInt(params.get("reqPrepaySupplyNamt").replaceAll(",", ""))));	//신청_선급금_공급가
			String reqPrepaySupplyAmt		= String.valueOf((Double.valueOf(params.get("reqPrepaySupplyTamt").replaceAll(",", "")) + Double.valueOf(params.get("reqPrepaySupplyNamt").replaceAll(",", ""))));	//신청_선급금_공급가
			String reqPrepaySupplyTamt		= params.get("reqPrepaySupplyTamt");	//신청_선급금공급가_과세
			String reqPrepaySupplyNamt		= params.get("reqPrepaySupplyNamt");	//신청_선급금공급가_면세
			String reqPrepaySurtaxAmt		= params.get("reqPrepaySurtaxAmt");		//신청_선급금부가세
			String prepayDesc				= "";									//적요
			String conCurCd					= params.get("conCurCd");				//계약통화
			String taxMethodCd				= params.get("chk_info");				//세금계산서발행방법
			String taxDy					= params.get("date1");					//작성일자(증빙일자)
			String taxChargNm				= params.get("userName");				//담당자명
			String taxChargHp				= params.get("hp");						//담당자 핸드폰번호
			String taxChargEmail			= params.get("eMail");					//담당자 이메일
			String perpayReqDiv				= params.get("perpayReqDiv");			//신청부분
			String statusCd					= "01";									//상태(01:작성중, 02:선급신청요청, 21:선급포기요청, 03:선금신청, 04:결재중, 10:선금완료, 11:포기완료, 00:신청대상, 91:반려)
			//if(params.get("statusCd").equals("91")){
				//statusCd = "91";
			//}
			String userId					= storage.getUser().get("ACCT_ID");		//로그인사용자

			String acctGb					= params.get("acct_gb");	
			String guaGb					= params.get("proof_cd");	
			String payGroup					= params.get("pay_group");	
			String paymentTerms				= params.get("payment_terms_id");

			String rateAmt					= params.get("rateAmt");				
			String rateVamt					= params.get("rateVamt");	

			String creBy					= params.get("creBy");
			String updBy					= params.get("updBy");
			String by						= "";
			if("".equals(creBy)){
				by = updBy;
			}else{
				by = creBy;
			}
			
			conn.begin();
			
			persistent_u = new PersistentImpl(super.conn.getConnection());

			  sqlstr_u = " UPDATE SUB1030T      														\n"
		              + " SET    TAX_METHOD_CD    			= ?       , 					            \n"
		              + "        TAX_DY 					= ?       , 					            \n"
		              + "        TAX_CHARG_NM				= ?       , 					            \n"
		              + "        TAX_CHARG_HP				= ?       , 					            \n"
		              + "        TAX_CHARG_EMAIL			= ?       , 					            \n"
					  + "        REQ_PREPAY_SUPPLY_AMT_KRW	= ?       ,					            	\n"
					  + "        REQ_PREPAY_SURTAX_AMT_KRW	= ?       ,					            	\n"
		              + "        REQ_DIV_CD					= ?       , 					            \n"
		              + "        REQ_PREPAY_AMT				= ?       , 					            \n"
		              + "        REQ_PREPAY_SUPPLY_AMT		= ?       , 					            \n"
		              + "        REQ_PREPAY_SUPPLY_TAMT		= ?       , 					            \n"
		              + "        REQ_PREPAY_SUPPLY_NAMT		= ?       , 					            \n"
		              + "        REQ_PREPAY_SURTAX_AMT		= ?       , 					            \n"
		              + "        STATUS_CD					= ?	      , 					            \n"
		              + "        PREPAY_DATE				= TO_DATE(?, 'YYYY-MM-DD'),		            \n"
		              + "        CRE_BY      				= ?       , 					            \n"
		              + "        UPD_BY      				= ?       , 					            \n"
		              + "        RTN_REASON    				= ''      , 					            \n"
		              + "        CRE_DATE    				= sysdate ,									\n"
		              + "        UPD_DATE    				= sysdate									\n"
		              + " WHERE  PREPAY_ID      			= ?        									\n" ;
			  
			persistent_u.setStatement(sqlstr_u);
			
				persistent_u.addParameter(taxMethodCd);
				persistent_u.addParameter(taxDy.replaceAll("-", ""));
				persistent_u.addParameter(taxChargNm);
				persistent_u.addParameter(taxChargHp);
				persistent_u.addParameter(taxChargEmail);
				
				persistent_u.addParameter(rateAmt.replaceAll(",", ""));
				persistent_u.addParameter(rateVamt.replaceAll(",", ""));
				
				persistent_u.addParameter(perpayReqDiv);
				persistent_u.addParameter(reqPrepayAmt.replaceAll(",", ""));
				persistent_u.addParameter(reqPrepaySupplyAmt);
				persistent_u.addParameter(reqPrepaySupplyTamt.replaceAll(",", ""));
				persistent_u.addParameter(reqPrepaySupplyNamt.replaceAll(",", ""));
				persistent_u.addParameter(reqPrepaySurtaxAmt.replaceAll(",", ""));
				persistent_u.addParameter(statusCd);
				persistent_u.addParameter(taxDy);
				persistent_u.addParameter(by);
				persistent_u.addParameter(userId);
				persistent_u.addParameter(prepayId);
				
				WAFLogger.debug(sqlstr_u);
				WAFLogger.debug(persistent_u.getParameters());
				
				persistent_u.execute();
				persistent_u.clearParameters();

				persistent_u1 = new PersistentImpl(super.conn.getConnection());
				sqlstr_u1 = " UPDATE SUB2000T      												\n"
						+ " SET    ACCT_GB    			= ?       , 					        \n"
						+ "        PROOF_CD				= ?       ,					            \n"
						+ "        PAY_GROUP			= ?       ,					            \n"
						+ "        PAYMENT_TERMS		= ?       					            \n"
						+ " WHERE  SUB_CONT_MOD_ID      = ?        								\n"
						;			  
				persistent_u1.setStatement(sqlstr_u1);
				persistent_u1.addParameter(acctGb);
				persistent_u1.addParameter(guaGb);
				persistent_u1.addParameter(payGroup);
				persistent_u1.addParameter(paymentTerms);
				persistent_u1.addParameter(subContModId);
					
				WAFLogger.debug(sqlstr_u1);
				WAFLogger.debug(persistent_u1.getParameters());
					
				persistent_u1.execute();
				persistent_u1.clearParameters();				

				
			return true;
        }
        catch(Exception e) {
            super.doException(e);
            super.storage.setMessage(e.getMessage());
            super.storage.setDetailMessage(e);
            super.conn.setRollback();
            return false;
        }
        finally {
        	super.conn.end();
        }
	}
	
	/**
	 * Persistent 를 이용한 Delete 처리
	 * @param params
	 * @return
	 * * 입력받은 선급금 상세 정보를 삭제합니다.
     * @param connection
     * @param pgmName
     * @param screenName
     * @param systemCode
     * @throws Exception
	 */
	private boolean deleteData(ValueObject params) {
		
		
		Persistent persistent_d = null;
		String sqlstr_d = "";

		try {
		
			//선급금 신청 상제 정보
			String prepayId					= params.get("prepayId");				//선급ID
			String subContModId				= params.get("subContModId");			//외주계약변경ID

			conn.begin();
			
			persistent_d = new PersistentImpl(super.conn.getConnection());
			
			  sqlstr_d = 	" DELETE                   		\n"
		                  + " FROM   PREPAY_OUT 			\n"
		                  + " WHERE  PREPAY_ID = ?        	\n";
		        
			persistent_d.setStatement(sqlstr_d);
			persistent_d.addParameter(prepayId);
			
			WAFLogger.debug(sqlstr_d);
			WAFLogger.debug(persistent_d.getParameters());
			
			persistent_d.execute();
			persistent_d.clearParameters();

			return true;
        }
        catch(Exception e) {
            super.doException(e);
            super.storage.setMessage(e.getMessage());
            super.storage.setDetailMessage(e);
            super.conn.setRollback();
            return false;
        }
        finally {
        	super.conn.end();
        }
	}
	
	/**
	 * Persistent 를 이용한 Save 처리
	 * @param params
	 * @return
	 * * 선급금신처요청을 처리한다.
     * @param connection
     * @param pgmName
     * @param screenName
     * @param systemCode
     * @throws Exception
	 */
	private boolean reqData(ValueObject params) {
		
		Persistent persistent_u = null;
		String sqlstr_u = "";

		try {
		
			//선급금 신청 상제 정보
			String prepayId					= params.get("prepayId");				//선급ID
			String statusCd					= params.get("selectStatusCd");			//상태(02:선급금 신청요청, 21:선급금 포기신청)
			String userId					= storage.getUser().get("ACCT_ID");		//로그인사용자
			
			conn.begin();
			
			persistent_u = new PersistentImpl(super.conn.getConnection());

			sqlstr_u = " UPDATE SUB1030T      														\n"
			          + " SET    STATUS_CD    				= ?       ,								\n"
			          + "        UPD_BY      				= ?       , 				            \n"
			          + "        UPD_DATE    				= sysdate ,								\n"
			          + "        RTN_REASON    				= ''									\n"
			          + " WHERE  PREPAY_ID      			= ?        								\n"
			          ;
			  
			persistent_u.setStatement(sqlstr_u);
			
			persistent_u.addParameter(statusCd);
			persistent_u.addParameter(userId);
			persistent_u.addParameter(prepayId);
			
			WAFLogger.debug(sqlstr_u);
			WAFLogger.debug(persistent_u.getParameters());
			
			persistent_u.execute();
			persistent_u.clearParameters();

			return true;
        }
        catch(Exception e) {
            super.doException(e);
            super.storage.setMessage(e.getMessage());
            super.storage.setDetailMessage(e);
            super.conn.setRollback();
            return false;
        }
        finally {
        	super.conn.end();
        }	
	}
	/**
	 * Persistent 를 이용한 반려 처리
	 * @param params
	 * @return
	 * * 경영정보에서 협력업체에 반려 처리한다.
	 * @param connection
	 * @param pgmName
	 * @param screenName
	 * @param systemCode
	 * @throws Exception
	 */
	private boolean approvalData(ValueObject params) {
		
		Persistent persistent_u = null;
		String sqlstr_u = "";
		
		try {
			
			//외주_기성정보
			String acmpId					= params.get("prepayId");				//기성ID
			String statusCd					= "50";									//선급상태값 : 01:작성중, 02:선급신청요청, 03:선금신청, 04:부분신청, 10:선금완료, 11:선금포기, 00:신청대상, 91:반려
			String userId					= storage.getUser().get("ACCT_ID");		//로그인사용자
			
			conn.begin();
			
			persistent_u = new PersistentImpl(super.conn.getConnection());
			
			sqlstr_u = " UPDATE  SUB1030T   														\n"
					+ " SET    STATUS_CD    				= ?       ,									\n"
					+ "        RTN_REASON      				= ''      , 					            \n"
					+ "        RTN_DATE      				= null      , 					            \n"
					+ "        UPD_BY      				= ?       , 					            \n"
					+ "        UPD_DATE    				= sysdate									\n"
					+ " WHERE  PREPAY_ID      				= ?        									\n" ;
			
			persistent_u.setStatement(sqlstr_u);
			
			persistent_u.addParameter(statusCd);
			persistent_u.addParameter(userId);
			persistent_u.addParameter(acmpId);
			
			WAFLogger.debug(sqlstr_u);
			WAFLogger.debug(persistent_u.getParameters());
			
			persistent_u.execute();
			persistent_u.clearParameters();
			
			return true;
		}
		catch(Exception e) {
			super.doException(e);
			super.storage.setMessage(e.getMessage());
			super.storage.setDetailMessage(e);
			super.conn.setRollback();
			return false;
		}
		finally {
			super.conn.end();
		}	
	}
    // 반려가능상태인지 확인한다.
    public String getRejectionCheck() throws Exception {
    	
        Search search = new SearchImpl();
        RowSet listRowSet = null;
        String sqlstr = "";
        String resultStatus = "";
        ValueObject rowObject = null;
        
        try {

        	sqlstr += " SELECT 			  								  								\n"
					+ "     NVL(B.STATUS_CD,'00') AS STATUS_CD											\n"
        			+ " FROM SUB1030T B																	\n"
        			+ " WHERE 1=1																		\n"
        			;

        	sqlstr += " AND B.PREPAY_ID = '"+ params.get("prepayId") +"'								\n"
        			;
        	
            search.setStatement(sqlstr);
            WAFLogger.debug(sqlstr);
            listRowSet = search.execute();
            rowObject = RowSetUtility.getValueObject(listRowSet.getRow(0));
            resultStatus = rowObject.get("STATUS_CD");
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> getRejectionCheck() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
            return null;
        }
        finally {
            
            return resultStatus;
        }
    }	
    
	private boolean segumData(ValueObject params) {
		
		Persistent persistent_u = null;
		String sqlstr_u = "";

		try {
		
			
			//String taxMethodCd				= params.get("chk_info");				//세금계산서발행방법
			String taxDy					= params.get("date1");					//작성일자(증빙일자)
			String taxChargNm				= params.get("userName");				//담당자명
			String taxChargHp				= params.get("hp");						//담당자 핸드폰번호
			String taxChargEmail			= params.get("eMail");					//담당자 이메일

			//분계유형
			//String acctGb					= params.get("acct_gb");				//분계유형
			//String guaGb					= params.get("proof_cd");				//
			//String payGroup					= params.get("pay_group");				//
			//String paymentTerms				= params.get("payment_terms");			//
			String userId					= storage.getUser().get("ACCT_ID");		//로그인사용자
			
			String pKey				= params.get("pKey");			//
			
			conn.begin();
			
			persistent_u = new PersistentImpl(super.conn.getConnection());
			sqlstr_u = " UPDATE SUB1030T	      														\n"
		              + " SET    --TAX_METHOD_CD    		= ?       , 					            \n"
		              + "        TAX_DY 					= ?       , 					            \n"
		              + "        PREPAY_DATE				= TO_DATE(?, 'YYYY-MM-DD') , 	            \n" //2017.2.9 TAX_DY=PREPAY_DATE
			          + "        TAX_CHARG_NM				= ?       , 					            \n"
		              + "        TAX_CHARG_HP				= ?       , 					            \n"
		              + "        TAX_CHARG_EMAIL			= ?       , 					            \n"
					  + "	     --ACCT_GB      			= ?       , 					            \n"
					  + "        --PAY_GROUP      			= ?       , 					            \n"
					  + "        --GUA_GB      				= ?       , 					            \n"
					  + "        --PAYMENT_TERMS      		= ?       , 					            \n"
					  + "        UPD_BY      				= ?       , 					            \n"
		              + "        UPD_DATE    				= sysdate									\n"
		              + " WHERE  PREPAY_ID      			= ?        									\n" ;
			  
			persistent_u.setStatement(sqlstr_u);
				
			//persistent_u.addParameter(taxMethodCd);
			persistent_u.addParameter(taxDy.replaceAll("-", ""));
			persistent_u.addParameter(taxDy); //2017.2.9 TAX_DY=PREPAY_DATE
			persistent_u.addParameter(taxChargNm);
			persistent_u.addParameter(taxChargHp);
			persistent_u.addParameter(taxChargEmail);
			
			//분계유형
			//persistent_u.addParameter(acctGb);
			//persistent_u.addParameter(payGroup);
			//persistent_u.addParameter(guaGb);
			//persistent_u.addParameter(paymentTerms);
			persistent_u.addParameter(userId);
			
			persistent_u.addParameter(pKey);
				
			WAFLogger.debug(sqlstr_u);
			WAFLogger.debug(persistent_u.getParameters());
				
			persistent_u.execute();
			persistent_u.clearParameters();

			return true;
        }
        catch(Exception e) {
            super.doException(e);
            super.storage.setMessage(e.getMessage());
            super.storage.setDetailMessage(e);
            super.conn.setRollback();
            return false;
        }
        finally {
        	super.conn.end();
        }	
	}
	
    public ValueObject getRageEx(String rateDy, String curGb, String reqPrepaySupplyAmt, String inputVat) {
        Search search = new SearchImpl();
        RowSet listRowSet = null;
        String sqlstr = "";
        ValueObject rowObject = null;
        String resultRateEx = "0";
        
        int segumRate = 0;
        if("INPUT_VAT".equals(inputVat)){
        	segumRate = 100 / 10;
        }
        
        try {

        	sqlstr += " SELECT A.RATE																									\n"
        		    + " 	   , A.RATE * TO_NUMBER("+reqPrepaySupplyAmt+") AS RATE_AMT													\n" 
        		    + " 	   , DECODE('"+segumRate+"',0,0,A.RATE * TO_NUMBER("+reqPrepaySupplyAmt+") / "+segumRate+") AS RATE_VAMT								\n" 
					+ " FROM (SELECT F_GET_EXCHANGE_RATE(TO_DATE("+rateDy+"), '"+curGb+"', 'KRW') AS RATE FROM DUAL) A			\n"
        			;
        	
            search.setStatement(sqlstr);
            WAFLogger.debug(sqlstr);
            listRowSet = search.execute();
            rowObject = RowSetUtility.getValueObject(listRowSet.getRow(0));
            //resultRateEx = rowObject.get("RATE");
            
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> getPrePayment() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
            return null;
        }
        finally {

            if( rowObject == null ) rowObject = new ValueObject();
            return rowObject;
        }
    }

	private boolean guaData(ValueObject params) {
		
		Persistent persistent_u = null;
		String sqlstr_u = "";
		
		try {
			
			//분계유형
			String acctGb					= params.get("acct_gb");				//분계유형
			String guaGb					= params.get("proof_cd");				//
			String payGroup					= params.get("pay_group");				//
			String paymentTerms				= params.get("payment_terms");			//
			String userId					= storage.getUser().get("ACCT_ID");		//로그인사용자
			
			String pKey				= params.get("pKey");			//
			
			conn.begin();
			
			persistent_u = new PersistentImpl(super.conn.getConnection());
			sqlstr_u = " UPDATE SUB2000T	      													\n"
					+ " SET    ACCT_GB      			= ?       , 					            \n"
					+ "        PAY_GROUP      			= ?       , 					            \n"
					+ "        PROOF_CD    				= ?       , 					            \n"
					+ "        PAYMENT_TERMS      		= ?       	 					            \n"
					+ " WHERE  SUB_CONT_MOD_ID 			= ?        									\n"
					;
			
			persistent_u.setStatement(sqlstr_u);
			
			persistent_u.addParameter(acctGb);
			persistent_u.addParameter(payGroup);
			persistent_u.addParameter(guaGb);
			persistent_u.addParameter(paymentTerms);
			persistent_u.addParameter(pKey);
			
			WAFLogger.debug(sqlstr_u);
			WAFLogger.debug(persistent_u.getParameters());
			
			persistent_u.execute();
			persistent_u.clearParameters();
			
			return true;
		}
		catch(Exception e) {
			super.doException(e);
			super.storage.setMessage(e.getMessage());
			super.storage.setDetailMessage(e);
			super.conn.setRollback();
			return false;
		}
		finally {
			super.conn.end();
		}	
	} 

	private boolean rnncData(ValueObject params) {
		
		
		Persistent persistent_u = null;
		String sqlstr_u = "";

		Persistent persistent_u1 = null;
		String sqlstr_u1 = "";

		Persistent persistent_u2 = null;
		String sqlstr_u2 = "";
		
		try {
		
			//선급금 신청 상제 정보
			String prepayId					= params.get("prepayId");				//선급ID
			String subContModId				= params.get("subContModId");			//외주계약변경ID
			String prepaySeq				= "1";									//선급금지급차수
			String corpId					= params.get("corpId");					//협력업체ID
			String prepayDate				= "";									//선급일자
			String perpayAmt				= params.get("perpayAmt");				//계약_선급금
			String perpaySupplyAmt			= "";									//계약_선급금공급가
			String subPrepaySupplyTamt		= params.get("subPrepaySupplyTamt");	//계약_선급금공급가_과세
			String subPrepaySupplyNamt		= params.get("subPrepaySupplyNamt");	//계약_선급금공급가_면세
			String subPrepaySurtaxAmt		= params.get("subPrepaySurtaxAmt");		//계약_선급금부가세
			String reqPrepayAmt				= params.get("reqPrepayAmt");			//신청_선급금합계
			//String reqPrepaySupplyAmt		= String.valueOf((Integer.parseInt(params.get("reqPrepaySupplyTamt").replaceAll(",", "")) + Integer.parseInt(params.get("reqPrepaySupplyNamt").replaceAll(",", ""))));	//신청_선급금_공급가
			String reqPrepaySupplyAmt		= String.valueOf((Double.valueOf(params.get("reqPrepaySupplyTamt").replaceAll(",", "")) + Double.valueOf(params.get("reqPrepaySupplyNamt").replaceAll(",", ""))));	//신청_선급금_공급가
			String reqPrepaySupplyTamt		= params.get("reqPrepaySupplyTamt");	//신청_선급금공급가_과세
			String reqPrepaySupplyNamt		= params.get("reqPrepaySupplyNamt");	//신청_선급금공급가_면세
			String reqPrepaySurtaxAmt		= params.get("reqPrepaySurtaxAmt");		//신청_선급금부가세
			String prepayDesc				= "";									//적요
			String conCurCd					= params.get("conCurCd");				//계약통화
			String taxMethodCd				= params.get("chk_info");				//세금계산서발행방법
			String taxDy					= params.get("date1");					//작성일자(증빙일자)
			String taxChargNm				= params.get("userName");				//담당자명
			String taxChargHp				= params.get("hp");						//담당자 핸드폰번호
			String taxChargEmail			= params.get("eMail");					//담당자 이메일
			String perpayReqDiv				= params.get("perpayReqDiv");			//신청부분
			String statusCd					= "01";									//상태(01:작성중, 02:선급신청요청, 21:선급포기요청, 03:선금신청, 04:결재중, 10:선금완료, 11:포기완료, 00:신청대상, 91:반려)
			//if(params.get("statusCd").equals("91")){
				//statusCd = "91";
			//}
			String userId					= storage.getUser().get("ACCT_ID");		//로그인사용자

			String acctGb					= params.get("acct_gb");	
			String guaGb					= params.get("proof_cd");	
			String payGroup					= params.get("pay_group");	
			String paymentTerms				= params.get("payment_terms_id");

			String rateAmt					= params.get("rateAmt");				
			String rateVamt					= params.get("rateVamt");	

			String creBy					= params.get("creBy");
			String updBy					= params.get("updBy");
			String by						= "";
			if("".equals(creBy)){
				by = updBy;
			}else{
				by = creBy;
			}
			
			conn.begin();
			
			persistent_u = new PersistentImpl(super.conn.getConnection());

			  sqlstr_u = " UPDATE SUB1030T      														\n"
		              + " SET    TAX_METHOD_CD    			= ?       , 					            \n"
		              + "        TAX_DY 					= ?       , 					            \n"
		              + "        TAX_CHARG_NM				= ?       , 					            \n"
		              + "        TAX_CHARG_HP				= ?       , 					            \n"
		              + "        TAX_CHARG_EMAIL			= ?       , 					            \n"
					  + "        REQ_PREPAY_SUPPLY_AMT_KRW	= ?       ,					            	\n"
					  + "        REQ_PREPAY_SURTAX_AMT_KRW	= ?       ,					            	\n"
		              + "        REQ_DIV_CD					= ?       , 					            \n"
		              + "        REQ_PREPAY_AMT				= ?       , 					            \n"
		              + "        REQ_PREPAY_SUPPLY_AMT		= ?       , 					            \n"
		              + "        REQ_PREPAY_SUPPLY_TAMT		= ?       , 					            \n"
		              + "        REQ_PREPAY_SUPPLY_NAMT		= ?       , 					            \n"
		              + "        REQ_PREPAY_SURTAX_AMT		= ?       , 					            \n"
		              + "        STATUS_CD					= '11'	  , 					            \n"
		              + "        PREPAY_DATE				= TO_DATE(?, 'YYYY-MM-DD'),		            \n"
		              + "        CRE_BY      				= ?       , 					            \n"
		              + "        UPD_BY      				= ?       , 					            \n"
		              + "        RTN_REASON    				= ''      , 					            \n"
		              + "        CRE_DATE    				= sysdate ,									\n"
		              + "        UPD_DATE    				= sysdate									\n"
		              + " WHERE  PREPAY_ID      			= ?        									\n" ;
			  
			persistent_u.setStatement(sqlstr_u);
			
				persistent_u.addParameter(taxMethodCd);
				persistent_u.addParameter(taxDy.replaceAll("-", ""));
				persistent_u.addParameter(taxChargNm);
				persistent_u.addParameter(taxChargHp);
				persistent_u.addParameter(taxChargEmail);
				
				persistent_u.addParameter(rateAmt.replaceAll(",", ""));
				persistent_u.addParameter(rateVamt.replaceAll(",", ""));
				
				persistent_u.addParameter(perpayReqDiv);
				persistent_u.addParameter(reqPrepayAmt.replaceAll(",", ""));
				persistent_u.addParameter(reqPrepaySupplyAmt);
				persistent_u.addParameter(reqPrepaySupplyTamt.replaceAll(",", ""));
				persistent_u.addParameter(reqPrepaySupplyNamt.replaceAll(",", ""));
				persistent_u.addParameter(reqPrepaySurtaxAmt.replaceAll(",", ""));
				persistent_u.addParameter(taxDy);
				persistent_u.addParameter(by);
				persistent_u.addParameter(userId);
				persistent_u.addParameter(prepayId);
				
				WAFLogger.debug(sqlstr_u);
				WAFLogger.debug(persistent_u.getParameters());
				
				persistent_u.execute();
				persistent_u.clearParameters();

				persistent_u1 = new PersistentImpl(super.conn.getConnection());
				sqlstr_u1 = " UPDATE SUB2000T      												\n"
						+ " SET    ACCT_GB    			= ?       , 					        \n"
						+ "        PROOF_CD				= ?       ,					            \n"
						+ "        PAY_GROUP			= ?       ,					            \n"
						+ "        PAYMENT_TERMS		= ?       					            \n"
						+ " WHERE  SUB_CONT_MOD_ID      = ?        								\n"
						;			  
				persistent_u1.setStatement(sqlstr_u1);
				persistent_u1.addParameter(acctGb);
				persistent_u1.addParameter(guaGb);
				persistent_u1.addParameter(payGroup);
				persistent_u1.addParameter(paymentTerms);
				persistent_u1.addParameter(subContModId);
					
				WAFLogger.debug(sqlstr_u1);
				WAFLogger.debug(persistent_u1.getParameters());
					
				persistent_u1.execute();
				persistent_u1.clearParameters();				

			return true;
		}
		catch(Exception e) {
			super.doException(e);
			super.storage.setMessage(e.getMessage());
			super.storage.setDetailMessage(e);
			super.conn.setRollback();
			return false;
		}
		finally {
			super.conn.end();
		}	
	} 
}
