package kr.co.udapsoft.ebid.buyer.bid.outside.biz;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;

import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.config.WAFConfig;
import kr.co.hsnc.common.sql.RowSet;
import kr.co.hsnc.common.sql.RowSetImpl;
import kr.co.hsnc.common.sql.WAFConnectionImpl;
import kr.co.hsnc.common.sql.WAFSQLException;
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.util.ValueObject;
import oracle.toplink.internal.ox.StrBuffer;

public class InterfaceOutERPiU{
	
	/**
	 * 외주 발주의뢰 진행 상태 업데이트 (DEMANDED_ID)
	 * @param demandedId
	 * @param statusCd
	 * @throws Exception
	 */
	public static void updateStatusPMM(String demandedId, String statusCd) throws Exception {
		
		WAFConnectionImpl erpConn = new WAFConnectionImpl();
		erpConn.setDataSource("jdbc/erpiu");
		erpConn.begin();

		try{
			StringBuffer sqlstr = new StringBuffer();
			Persistent pstmt = new PersistentImpl(erpConn.getConnection());
    		sqlstr.append("UPDATE	WC_Z_ETEC_PMM_DEMANDED_REQUEST	\n");
    		sqlstr.append("SET		DEMANDED_STATUS = '4'			\n");
    		sqlstr.append("			,EBID_STATUS_CD = ?				\n");
            sqlstr.append("WHERE	DEMANDED_ID = ?					\n");
	       	pstmt.setStatement(sqlstr.toString());
	       	pstmt.addParameter(statusCd);
	       	pstmt.addParameter(demandedId);
			pstmt.execute();
			pstmt.clearParameters();
			
		}catch (WAFSQLException e){
			WAFLogger.error("InterfaceOutERPiU --> updateStatusPMM() : " + e.getMessage());
			WAFLogger.error(e);
			erpConn.setRollback();
			throw e;
        }finally{
        	erpConn.end();
        }
		
	}
	
	/**
	 * 외주 발주의뢰 반려 상태 업데이트
	 * @param demandedId
	 * @param statusCd
	 * @throws Exception
	 */
	public static void updateRejectPMM(String demandedId, String statusCd, String rtnReason, String userName) throws Exception {
		
		WAFConnectionImpl erpConn = new WAFConnectionImpl();
		erpConn.setDataSource("jdbc/erpiu");
		erpConn.begin();
		
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String toDay = sdf.format(date);
		
		try{
			StringBuffer sqlstr = new StringBuffer();
			Persistent pstmt = new PersistentImpl(erpConn.getConnection());
			sqlstr.append("UPDATE	WC_Z_ETEC_PMM_DEMANDED_REQUEST	\n");
			sqlstr.append("SET		DEMANDED_STATUS = '5'			\n");
			sqlstr.append("			,EBID_STATUS_CD = ?				\n");
			sqlstr.append("			,RTN_REASON = ?					\n");
			sqlstr.append("			,RTN_DATE = ?					\n");
			sqlstr.append("			,RTN_EMPNM = ?					\n");
			sqlstr.append("WHERE	DEMANDED_ID = ?					\n");
			pstmt.setStatement(sqlstr.toString());
			pstmt.addParameter(statusCd);
			pstmt.addParameter(rtnReason);
			pstmt.addParameter(toDay);
			pstmt.addParameter(userName);
			pstmt.addParameter(demandedId);
			pstmt.execute();
			pstmt.clearParameters();
			
		}catch (WAFSQLException e){
			WAFLogger.error("InterfaceOutERPiU --> updateRejectPMM() : " + e.getMessage());
			WAFLogger.error(e);
			erpConn.setRollback();
			throw e;
		}finally{
			erpConn.end();
		}
		
	}
	
	/**
	 * DEMANDED_ID 가져오기
	 * @param bidNo
	 * @param bidDeg
	 * @return
	 * @throws Exception
	 */
	public static String getDemandedId(String bidNo, String bidDeg) throws Exception {
		
		String demandedId = null;
		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        StrBuffer sqlstr = new StrBuffer();
        
        try{
        	sqlstr.append("SELECT	DEMANDED_ID		\n");
        	sqlstr.append("FROM		BID_MASTER		\n");
        	sqlstr.append("WHERE	BID_NO = ?		\n");
        	sqlstr.append("AND		BID_DEG = ?		\n");
        	search.setStatement(sqlstr.toString());
        	search.addParameter(bidNo);
        	search.addParameter(bidDeg);
        	rowSet = search.execute();
        	
        	if (rowSet.size() > 0) {
        		demandedId = rowSet.getRow(0).get("DEMANDED_ID");
        	}
        	
        	return demandedId;
        	
        }catch (WAFSQLException e) {
        	WAFLogger.error("InterfaceOutERPiU --> getDemandedId() : " + e.getMessage());
			WAFLogger.error(e);
            throw e;
        }
	}
	
	/**
	 * ERP거래처 등록
	 * @param connection
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	public static boolean insertErpSupp(Connection connection, ValueObject vo) throws Exception {
		
		Persistent persistent = new PersistentImpl(connection);
		StrBuffer sqlstr = new StrBuffer();
		try {

			sqlstr.append(" INSERT  INTO MA_PARTNER (															\n");
			sqlstr.append("         CD_PARTNER	,CD_COMPANY	,LN_PARTNER		,USE_YN			,SN_PARTNER			\n");
			sqlstr.append("         ,EN_PARTNER	,TP_PARTNER	,FG_PARTNER		,NM_CEO			,NO_COMPANY			\n");
			sqlstr.append("         ,TP_JOB		,CLS_JOB	,CLS_PARTNER	,CD_NATION		,E_MAIL				\n");
			sqlstr.append("         ,NO_TEL		,NO_TEL1	,NO_FAX1		,CD_EMP_PARTNER	,NO_HPEMP_PARTNER	\n");
			sqlstr.append("         ,ID_INSERT	,DTS_INSERT														\n");
			sqlstr.append(" ) VALUES (																			\n");
			sqlstr.append("         ?			,?			,?				,?				,?					\n");
			sqlstr.append("         ,?			,?			,?				,?				,?					\n");
			sqlstr.append("         ,?			,?			,?				,?				,?					\n");
			sqlstr.append("         ,?			,?			,?				,?				,?					\n");
			sqlstr.append("         ,?			,?																\n");
			sqlstr.append(" )																					\n");
			
			persistent.setStatement(sqlstr.toString());
			persistent.addParameter(vo.get("CD_PARTNER",null));
			persistent.addParameter(vo.get("CD_COMPANY",null));
			persistent.addParameter(vo.get("LN_PARTNER",null));
			persistent.addParameter(vo.get("USE_YN",null));
			persistent.addParameter(vo.get("SN_PARTNER",null));
			persistent.addParameter(vo.get("EN_PARTNER",null));
			persistent.addParameter(vo.get("TP_PARTNER",null));
			persistent.addParameter(vo.get("FG_PARTNER",null));
			persistent.addParameter(vo.get("NM_CEO",null));
			persistent.addParameter(vo.get("NO_COMPANY",null));
			persistent.addParameter(vo.get("TP_JOB",null));
			persistent.addParameter(vo.get("CLS_JOB",null));
			persistent.addParameter(vo.get("CLS_PARTNER",null));
			persistent.addParameter(vo.get("CD_NATION",null));
			persistent.addParameter(vo.get("E_MAIL",null));
			persistent.addParameter(vo.get("NO_TEL",null));
			persistent.addParameter(vo.get("NO_TEL1",null));
			persistent.addParameter(vo.get("NO_FAX1",null));
			persistent.addParameter(vo.get("CD_EMP_PARTNER",null));
			persistent.addParameter(vo.get("NO_HPEMP_PARTNER",null));
			persistent.addParameter(vo.get("ID_INSERT",null));
			persistent.addParameter(vo.get("DTS_INSERT",null));
			persistent.execute();
			
			return true;
		
		}catch(Exception e) {
			WAFLogger.error("InterfaceOutERPiU --> insertErpSupp() : " + e.getMessage());
			WAFLogger.error("SQL : " + sqlstr);
			WAFLogger.error(e);
			//WAFLogger.debug("Exception : " + e.getMessage());
			throw e;
		}
	}

	/**
	 * 외주계약 MASTER 정보 등록
	 * @param connection
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	public static boolean insertErpContMst(Connection connection, ValueObject vo) throws Exception {
		
		Persistent persistent = new PersistentImpl(connection);
		StrBuffer sqlstr = new StrBuffer();
		try {
			sqlstr.append(" INSERT  INTO WC_O_CONTRACT_H (														\n");
			sqlstr.append("         CD_COMPANY	,CD_SITE		,NO_CONT		,SQ_CONT		,NM_CONT		\n");
			sqlstr.append("         ,DT_CONT	,CD_CM00201		,CD_CM00202		,CD_CM00212		,CD_CM00213		\n");
			sqlstr.append("         ,CD_CM00208	,DT_FM_CONT		,DT_TO_CONT		,CD_COOP		,NM_COOP		\n");
			sqlstr.append("         ,TP_COMPANY	,NO_COMPANY		,DRAT_TAX		,FRAT_TAX		,AMT_ORD		\n");
			sqlstr.append("         ,AMT_ACT	,AMT_CONT		,SAMT_CONT		,VAMT_CONT		,FAMT_CONT		\n");
			sqlstr.append("         ,NO_CORD	,SQ_ESTM		,NM_CONT_ML		,DT_DELV		,LOC_DELV		\n");
			sqlstr.append("         ,CURR_SOUR	,UNT_CURR		,AMT_CURR		,VAMT_CURR		,FAMT_CURR		\n");
			sqlstr.append("         ,CD_PROOF	,CD_CM00206		,SUB_CONT_ID	,ID_INSERT		,DTS_INSERT	,CD_COST_TRADE 	\n");
			sqlstr.append(" ) VALUES (																			\n");
			sqlstr.append("         ?			,?				,?				,?				,?				\n");
			sqlstr.append("         ,?			,?				,?				,?				,?				\n");
			sqlstr.append("         ,?			,?				,?				,?				,?				\n");
			sqlstr.append("         ,?			,?				,?				,?				,?				\n");
			sqlstr.append("         ,?			,?				,?				,?				,?				\n");
			sqlstr.append("         ,?			,?				,?				,?				,?				\n");
			sqlstr.append("         ,?			,?				,?				,?				,?				\n");
			sqlstr.append("         ,?			,?				,?				,?				,?	, ?			\n");
			sqlstr.append(" )																					\n");
			
			persistent.setStatement(sqlstr.toString());
			persistent.addParameter(vo.get("CD_COMPANY",null));
			persistent.addParameter(vo.get("DEPT_CD",null));
			persistent.addParameter(vo.get("ERP_NO_CONT",null));
			persistent.addParameter(vo.get("ERP_SQ_CONT",null));
			persistent.addParameter(vo.get("NM_CONT",null));
			persistent.addParameter(vo.get("DT_CONT",null));
			persistent.addParameter(vo.get("CD_CM00201",null));
			persistent.addParameter(vo.get("CD_CM00202",null));
			persistent.addParameter(vo.get("CD_CM00212",null));
			persistent.addParameter(vo.get("CD_CM00213",null));
			persistent.addParameter(vo.get("CD_CM00208",null));
			persistent.addParameter(vo.get("DT_FM_CONT",null));
			persistent.addParameter(vo.get("DT_TO_CONT",null));
			persistent.addParameter(vo.get("CD_COOP",null));
			persistent.addParameter(vo.get("NM_COOP",null));
			persistent.addParameter(vo.get("TP_COMPANY",null));
			persistent.addParameter(vo.get("NO_COMPANY",null));
			persistent.addParameter(vo.get("DRAT_TAX",null));
			persistent.addParameter(vo.get("FRAT_TAX",null));
			persistent.addParameter(vo.get("AMT_ORD",null));
			persistent.addParameter(vo.get("AMT_ACT",null));
			persistent.addParameter(vo.get("AMT_CONT",null));
			persistent.addParameter(vo.get("SAMT_CONT",null));
			persistent.addParameter(vo.get("VAMT_CONT",null));
			persistent.addParameter(vo.get("FAMT_CONT",null));
			persistent.addParameter(vo.get("NO_CORD",null));
			persistent.addParameter(vo.get("SQ_ESTM",null));
			persistent.addParameter(vo.get("NM_CONT_ML",null));
			persistent.addParameter(vo.get("DT_DELV",null));
			persistent.addParameter(vo.get("LOC_DELV",null));
			persistent.addParameter(vo.get("CURR_SOUR",null));
			persistent.addParameter(vo.get("UNT_CURR",null));
			persistent.addParameter(vo.get("AMT_CURR",null));
			persistent.addParameter(vo.get("VAMT_CURR",null));
			persistent.addParameter(vo.get("FAMT_CURR",null));
			persistent.addParameter(vo.get("CD_PROOF",null));
			persistent.addParameter(vo.get("CD_CM00206",null));
			persistent.addParameter(vo.get("SUB_CONT_ID",null));
			persistent.addParameter(vo.get("ID_INSERT",null));
			persistent.addParameter(vo.get("DTS_INSERT",null));
			persistent.addParameter(vo.get("CD_COST_TRADE",null));
			persistent.execute();
			
			return true;
		
		}catch(Exception e) {
			WAFLogger.error("InterfaceOutERPiU --> insertErpContMst() : " + e.getMessage());
			WAFLogger.error("SQL : " + sqlstr);
			WAFLogger.error(e);
			//WAFLogger.debug("Exception : " + e.getMessage());
			throw e;
		}
	}

	/**
	 * 외주계약 DETAIL 정보 등록
	 * @param connection
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	public static boolean insertErpContDet(Connection connection, ValueObject vo) throws Exception {
		
		Persistent persistent = new PersistentImpl(connection);
		StrBuffer sqlstr = new StrBuffer();
		try {
			
			sqlstr.append(" INSERT  INTO WC_O_CONTRACT_D (													\n");
			sqlstr.append("         CD_COMPANY	,CD_SITE	,NO_CONT		,SQ_CONT		,SEQ_CONT		\n");
			sqlstr.append("         ,CD_CM00208	,CD_CM00201	,CD_CM00202		,CD_CM00212		,SQ_BUDGET		\n");
			sqlstr.append("         ,CD_CSTR	,DL_CSTR	,NM_CSTR		,CD_SIZE		,SIZE_CSTR		\n");
			sqlstr.append("         ,UNIT_CSTR	,QTY_ORD	,UNT_ORD		,AMT_ORD		,QTY_ACT		\n");
			sqlstr.append("         ,UNT_ACT	,AMT_ACT	,QTY_TRNS_ACT	,UNT_TRNS_ACT	,AMT_TRNS_ACT	\n");
			sqlstr.append("         ,QTY_CONT	,UNT_CONT	,AMT_CONT		,UNT_CURR		,AMT_CURR		\n");
			sqlstr.append("         ,ID_INSERT	,DTS_INSERT													\n");
			sqlstr.append(" ) VALUES (																		\n");
			sqlstr.append("         ?			,?			,?				,?				,?				\n");
			sqlstr.append("         ,?			,?			,?				,?				,?				\n");
			sqlstr.append("         ,?			,?			,?				,?				,?				\n");
			sqlstr.append("         ,?			,?			,?				,?				,?				\n");
			sqlstr.append("         ,?			,?			,?				,?				,?				\n");
			sqlstr.append("         ,?			,?			,?				,?				,?				\n");
			sqlstr.append("         ,?			,?															\n");
			sqlstr.append(" )																				\n");
			
			persistent.setStatement(sqlstr.toString());
			persistent.addParameter(vo.get("CD_COMPANY",null));
			persistent.addParameter(vo.get("CD_SITE",null));
			persistent.addParameter(vo.get("NO_CONT",null));
			persistent.addParameter(vo.get("SQ_CONT",null));
			persistent.addParameter(vo.get("SEQ_CONT",null));
			persistent.addParameter(vo.get("CD_CM00208",null));
			persistent.addParameter(vo.get("CD_CM00201",null));
			persistent.addParameter(vo.get("CD_CM00202",null));
			persistent.addParameter(vo.get("CD_CM00212",null));
			persistent.addParameter(vo.get("SQ_BUDGET",null));
			persistent.addParameter(vo.get("CD_CSTR",null));
			persistent.addParameter(vo.get("DL_CSTR",null));
			persistent.addParameter(vo.get("NM_CSTR",null));
			persistent.addParameter(vo.get("CD_SIZE",null));
			persistent.addParameter(vo.get("SIZE_CSTR",null));
			persistent.addParameter(vo.get("UNIT_CSTR",null));
			persistent.addParameter(vo.get("QTY_ORD",null));
			persistent.addParameter(vo.get("UNT_ORD",null));
			persistent.addParameter(vo.get("AMT_ORD",null));
			persistent.addParameter(vo.get("QTY_ACT",null));
			persistent.addParameter(vo.get("UNT_ACT",null));
			persistent.addParameter(vo.get("AMT_ACT",null));
			persistent.addParameter(vo.get("QTY_TRNS_ACT",null));
			persistent.addParameter(vo.get("UNT_TRNS_ACT",null));
			persistent.addParameter(vo.get("AMT_TRNS_ACT",null));
			persistent.addParameter(vo.get("QTY_CONT",null));
			persistent.addParameter(vo.get("UNT_CONT",null));
			persistent.addParameter(vo.get("AMT_CONT",null));
			persistent.addParameter(vo.get("UNT_CURR",null));
			persistent.addParameter(vo.get("AMT_CURR",null));
			persistent.addParameter(vo.get("ID_INSERT",null));
			persistent.addParameter(vo.get("DTS_INSERT",null));
			persistent.execute();
			
			return true;
		
		}catch(Exception e) {
			WAFLogger.error("InterfaceOutERPiU --> insertErpContDet() : " + e.getMessage());
			WAFLogger.error("SQL : " + sqlstr);
			WAFLogger.error(e);
			throw e;
		}
	}
	
	//2019.05.16 완료된계약 payment수정 
    public static void updateErpContPayment(Connection connection, ValueObject vo) throws Exception {
		
		Persistent persistent = new PersistentImpl(connection);
		StrBuffer sqlstr = new StrBuffer();
		try {
			sqlstr.append("UPDATE	WC_O_CONTRACT_H 				                                \n");
			sqlstr.append("SET		CD_CM00206 = ?					                                \n");
			sqlstr.append("WHERE	SUB_CONT_ID	= ?	         	                                    \n"); 
			
			persistent.setStatement(sqlstr.toString());
			persistent.addParameter(vo.get("PAYMENT_TERMS",null)); 
			persistent.addParameter(vo.get("SUB_CONT_ID",null)); 
			persistent.execute();
			
		}catch(Exception e) {
			WAFLogger.error("InterfacePurERPiU --> updateErpContPayment() : " + e.getMessage());
			WAFLogger.error("SQL : " + sqlstr);
			WAFLogger.error(e);
			throw e;
		}
	}
	
    
	/**
	 * ERP 거래처 CD_PARTNER 채번
	 * @return
	 * @throws Exception
	 */
	public static String getCdPartner() throws Exception {
		
		Search search = new SearchImpl();
		search.setDSName("jdbc/erpiu");
		RowSet rowSet = new RowSetImpl();
		StrBuffer sqlstr = new StrBuffer();
		String rtnStr = "";
		try{
			sqlstr.append("EXEC UP_FI_Z_ETEC_PARTNERSEQ '"+WAFConfig.get("waf.erp.cdCompany")+"'\n");
			
			search.setStatement(sqlstr.toString());
			rowSet = search.execute();
			if( rowSet != null && rowSet.size() > 0 ){
				rtnStr = rowSet.getRow(0).get(0);
			}
			
			return rtnStr;
			
		}catch(WAFSQLException se) {
			WAFLogger.error("InterfaceOutERPiU --> getCdPartner() : " + se.getMessage());
			WAFLogger.error("SQL : " + sqlstr);
			WAFLogger.error(se);
			throw se;
		}
	}

	/**
	 * 외주 선급금 등록
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	public static boolean insertPrepayInfo(ValueObject vo, RowSet signUserInfo) throws Exception {
		WAFConnectionImpl erpConn = new WAFConnectionImpl();
		
		try{
			erpConn.setDataSource("jdbc/erpiu");
			erpConn.begin();
			Persistent pstmt = new PersistentImpl(erpConn.getConnection());
        	StrBuffer sqlstr = new StrBuffer();
        	
        	sqlstr.append(" INSERT	INTO WC_O_PREPAY (													\n");
        	sqlstr.append(" 		CD_COMPANY		,CD_SITE	,CD_CM00215	,NO_CONT	,SQ_PREPAY		\n");
		    sqlstr.append("     	,SQ_CONT		,DT_PREPAY	,AMT_PREPAY	,VAT_PREPAY	,DT_CONFIRM_S	\n");
		    sqlstr.append("     	,NM_CONFIRM_S	,DT_CONFIRM	,NM_CONFIRM	,ID_INSERT	,DTS_INSERT		\n");
		    sqlstr.append("     	,FAMT_PREPAY	,CURR_SOUR	,UNT_CURR	,AMT_CURR	,VAMT_CURR		\n");
		    sqlstr.append("     	,FAMT_CURR		,YN_BAN		,CD_EXCH	,RT_EXCH	,DT_EXCH		\n");
		    sqlstr.append("     	,YN_ISS			,CD_COST_TRADE										\n");
        	sqlstr.append(" ) VALUES (																	\n");
        	sqlstr.append("     	?				,?			,?			,?			,?				\n");
        	sqlstr.append("     	,?				,?			,?			,?			,?				\n");
        	sqlstr.append("     	,?				,?			,?			,?			,?				\n");
        	sqlstr.append("     	,?				,?			,?			,?			,?				\n");
        	sqlstr.append("     	,?				,?			,?			,?			,?				\n");
        	sqlstr.append("     	,?				,?													\n");
		    sqlstr.append(" )																			\n");
	       	pstmt.setStatement(sqlstr.toString());
	       	pstmt.addParameter(vo.get("CD_COMPANY",null));   
	       	pstmt.addParameter(vo.get("CD_SITE",null));      
	       	pstmt.addParameter(vo.get("CD_CM00215",null));   
	       	pstmt.addParameter(vo.get("NO_CONT",null));      
	       	pstmt.addParameter(vo.get("SQ_PREPAY",null));    
	       	pstmt.addParameter(vo.get("SQ_CONT",null));      
	       	pstmt.addParameter(vo.get("DT_PREPAY",null));    
	       	pstmt.addParameter(vo.get("AMT_PREPAY",null));   
	       	pstmt.addParameter(vo.get("VAT_PREPAY",null));   
	       	pstmt.addParameter(vo.get("REQ_DATE",null)); 
	       	pstmt.addParameter(vo.get("USER_NAME",null)); 
	       	pstmt.addParameter(vo.get("APPLY_DATE",null));   
	       	pstmt.addParameter(vo.get("USER_NAME",null));   
	       	pstmt.addParameter(vo.get("USER_SABUN",null));    
	       	pstmt.addParameter(vo.get("DTS_INSERT",null));   
	       	pstmt.addParameter(vo.get("FAMT_PREPAY",null));  
	       	pstmt.addParameter(vo.get("CURR_SOUR",null));
	       	pstmt.addParameter(vo.get("UNT_CURR",null));     
	       	pstmt.addParameter(vo.get("AMT_CURR",null));     
	       	pstmt.addParameter(vo.get("VAMT_CURR",null));     
	       	pstmt.addParameter(vo.get("FAMT_CURR",null));     
	       	pstmt.addParameter(vo.get("YN_BAN",null));     
	       	pstmt.addParameter(vo.get("CD_EXCH",null));     
	       	pstmt.addParameter(vo.get("RT_EXCH",null));     
	       	pstmt.addParameter(vo.get("DT_EXCH",null));     
	       	pstmt.addParameter(vo.get("YN_ISS",null));
	       	pstmt.addParameter(vo.get("CD_COST_TRADE",null)); //2019.07.29 ERP 자동분개를 위한 계정코드 추가
			pstmt.execute();
			
			/*
			 * ERPIU 보조정보 Insert
			 */
			insertPrepaySubInfo(erpConn.getConnection(), vo);
			
			for ( int i = 0 ; i < signUserInfo.size() ; i++ ) {
	        	ValueObject signUser = new ValueObject();
	    		signUser.set("DOC_ID", 				vo.get("DOC_ID"));
	    		signUser.set("APRMEMBERSN", 		""+(i+1));
	    		signUser.set("APRMEMBERJOBTITLE", 	signUserInfo.getRow(i).get("APRMEMBERJOBTITLE"));
	    		signUser.set("APRMEMBERID", 		signUserInfo.getRow(i).get("SABUN"));
	    		signUser.set("APRMEMBERNAME", 		signUserInfo.getRow(i).get("APRMEMBERNAME"));
	        	signUser.set("PROCESSDATE", 		signUserInfo.getRow(i).get("PROCESSDATE"));
	        	insertApprovalLine(erpConn.getConnection(), signUser);
	        }
			
			return true;
        }catch (Exception e){
			WAFLogger.error("InterfaceOutERPiU --> insertErpIu() : " + e.getMessage());
			WAFLogger.error(e);
			erpConn.setRollback();
			return false;
        }finally{
        	erpConn.end();
        }
	}
	
	/**
	 * 외주선급금 보조정보 등록
	 * @param connection
	 * @param vo
	 * @throws Exception
	 */
	public static void insertPrepaySubInfo(Connection connection, ValueObject vo) throws Exception {
		
		try{
			Persistent pstmt = new PersistentImpl(connection);
			StrBuffer sqlstr = new StrBuffer();
			
			sqlstr.append(" INSERT	INTO WC_Z_ETEC_O_PREPAY_SUB (										\n");
			sqlstr.append(" 		CD_COMPANY		,CD_SITE	,CD_CM00215	,NO_CONT	,SQ_PREPAY		\n");
			sqlstr.append("     	,PREPAY_ID		,DOC_ID												\n");
			sqlstr.append(" ) VALUES (																	\n");
			sqlstr.append("     	?				,?			,?			,?			,?				\n");
			sqlstr.append("     	,?				,?													\n");
			sqlstr.append(" )																			\n");
			pstmt.setStatement(sqlstr.toString());
			pstmt.addParameter(vo.get("CD_COMPANY",null));   
			pstmt.addParameter(vo.get("CD_SITE",null));      
			pstmt.addParameter(vo.get("CD_CM00215",null));   
			pstmt.addParameter(vo.get("NO_CONT",null));      
			pstmt.addParameter(vo.get("SQ_PREPAY",null));    
			pstmt.addParameter(vo.get("PREPAY_ID",null));      
			pstmt.addParameter(vo.get("DOC_ID",null));    
			pstmt.execute();
			
		}catch (Exception e){
			WAFLogger.error("InterfaceOutERPiU --> insertErpIuSub() : " + e.getMessage());
			WAFLogger.error(e);
			throw e;
		}
	}

	/**
	 * 결재라인 등록
	 * @param connection
	 * @param vo
	 * @throws Exception
	 */
	public static void insertApprovalLine(Connection connection, ValueObject vo) throws Exception {
		
		try{
			Persistent pstmt = new PersistentImpl(connection);
			StrBuffer sqlstr = new StrBuffer();
			
			sqlstr.append(" INSERT	INTO TBENDAPRLINEINFO_EBID (														\n");
			sqlstr.append(" 		DOC_ID			,APRMEMBERSN	,APRMEMBERJOBTITLE	,APRMEMBERID	,APRMEMBERNAME	\n");
			sqlstr.append("     	,PROCESSDATE																		\n");
			sqlstr.append(" ) VALUES (																					\n");
			sqlstr.append("     	?				,?				,?					,?				,?				\n");
			sqlstr.append("     	,?																					\n");
			sqlstr.append(" )																							\n");
			pstmt.setStatement(sqlstr.toString());
			pstmt.addParameter(vo.get("DOC_ID",null));   
			pstmt.addParameter(vo.get("APRMEMBERSN",null));      
			pstmt.addParameter(vo.get("APRMEMBERJOBTITLE",null));   
			pstmt.addParameter(vo.get("APRMEMBERID",null));      
			pstmt.addParameter(vo.get("APRMEMBERNAME",null));    
			pstmt.addParameter(vo.get("PROCESSDATE",null));
			pstmt.execute();
			
		}catch (Exception e){
			WAFLogger.error("InterfaceOutERPiU --> insertApprovalLine() : " + e.getMessage());
			WAFLogger.error(e);
			throw e;
		}
	}

	/**
	 * 외주 자금청구 등록
	 * @param connection
	 * @param mstVo
	 * @param builtVo
	 * @throws Exception
	 */
	public static void insertErpBuilt(Connection connection, ValueObject mstVo, ValueObject builtVo) throws Exception {
		Persistent persistent = new PersistentImpl(connection);
		StrBuffer sqlstr = new StrBuffer();
		try {
			sqlstr.append("INSERT	INTO WC_S_BUILT_SQ_D (											\n");
			sqlstr.append("			CD_COMPANY	,CD_SITE	,SQ_BUILT	,NO_BUILT	,CD_CM00008		\n");
			sqlstr.append("			,DT_FM_FUND	,DT_TO_FUND	,TAG_CLOSE	,SEQ_BUILT	,YM_PAY			\n");
			sqlstr.append("			,DTS_INSERT														\n");
			sqlstr.append(" ) VALUES (																\n");
			sqlstr.append("			?			,?			,?			,?			,?				\n");
			sqlstr.append("			,?			,?			,?			,?			,?				\n");
			sqlstr.append("			,?																\n");
			sqlstr.append("	)																		\n");
			
			persistent.setStatement(sqlstr.toString());
			persistent.addParameter(mstVo.get("CD_COMPANY",null));
			persistent.addParameter(mstVo.get("CD_SITE",null));
			persistent.addParameter(builtVo.get("SQ_BUILT",null));
			persistent.addParameter(builtVo.get("NO_BUILT",null));
			persistent.addParameter("003");
			persistent.addParameter(builtVo.get("DT_FM_FUND",null));
			persistent.addParameter(builtVo.get("DT_TO_FUND",null));
			persistent.addParameter("N");
			persistent.addParameter(builtVo.get("SEQ_BUILT",null));
			persistent.addParameter(builtVo.get("YM_PAY",null));
			persistent.addParameter(builtVo.get("DTS_INSERT",null));
			persistent.execute();
			
		}catch(Exception e) {
			WAFLogger.error("InterfaceOutERPiU --> insertErpBuilt() : " + e.getMessage());
			WAFLogger.error("SQL : " + sqlstr);
			WAFLogger.error(e);
			throw e;
		}
	}
	
	/**
	 * 외주기성 MASTER 등록
	 * @param connection
	 * @param vo
	 * @throws Exception
	 */
	public static void insertErpSUB1040T(Connection connection, ValueObject vo) throws Exception {
        Persistent persistent = new PersistentImpl(connection);
        StrBuffer sqlstr = new StrBuffer();
        try {
        	sqlstr.append("INSERT	INTO WC_O_BUILT_H (		\n");
        	sqlstr.append("			CD_COMPANY				,CD_SITE				,NO_CONT				,SQ_BUILT				,NO_BUILT				\n");
        	sqlstr.append("			,SEQ_BUILT				,SQ_CONT				,YM_BUILT				,NM_CONT				,CD_CM00201				\n");
        	sqlstr.append("			,CD_CM00213				,CD_COOP				,TP_COMPANY				,NO_COMPANY				,NM_COOP				\n");
        	sqlstr.append("			,AMT_CONT				,SAMT_CONT				,VAMT_CONT				,FAMT_CONT				,AMT_BUILT_P			\n");
        	sqlstr.append("			,SAMT_BUILT_P			,VAMT_BUILT_P			,FAMT_BUILT_P			,AMT_BUILT_S			,SAMT_BUILT_S			\n");
        	sqlstr.append("			,VAMT_BUILT_S			,FAMT_BUILT_S			,AMT_D_PREPAY_P			,VAMT_D_PREPAY_P		,FAMT_D_PREPAY_P		\n");
        	sqlstr.append("			,AMT_D_PREPAY_S			,VAMT_D_PREPAY_S		,FAMT_D_PREPAY_S		,DT_REQUEST_S			,ID_INSERT				\n");
        	sqlstr.append("			,DTS_INSERT				,TAG_COPY_S				,CD_CM00252				,DESC_BUILT				,CURR_SOUR				\n");
        	sqlstr.append("			,UNT_CURR				,AMT_CURR_P				,VAMT_CURR_P			,FAMT_CURR_P			,AMT_CURR_S				\n");
        	sqlstr.append(" 		,VAMT_CURR_S			,FAMT_CURR_S			,AMT_D_PREPAY_CURR_P	,VAMT_D_PREPAY_CURR_P	,FAMT_D_PREPAY_CURR_P	\n");
        	sqlstr.append(" 		,AMT_D_PREPAY_CURR_S	,VAMT_D_PREPAY_CURR_S	,FAMT_D_PREPAY_CURR_S	,CD_EXCH				,RT_EXCH				\n");
        	sqlstr.append(" 		,DT_EXCH				,SEQ_BUILT_ETEC			,CD_COST_TRADE															\n");
        	sqlstr.append(" ) VALUES (																														\n");
        	sqlstr.append(" 		?						,?						,?						,?						,?						\n");
        	sqlstr.append(" 		,?						,?						,?						,?						,?						\n");
        	sqlstr.append(" 		,?						,?						,?						,?						,?						\n");
        	sqlstr.append(" 		,?						,?						,?						,?						,?						\n");
        	sqlstr.append(" 		,?						,?						,?						,?						,?						\n");
        	sqlstr.append(" 		,?						,?						,?						,?						,?						\n");
        	sqlstr.append(" 		,?						,?						,?						,?						,?						\n");
        	sqlstr.append(" 		,?						,?						,?						,?						,?						\n");
        	sqlstr.append(" 		,?						,?						,?						,?						,?						\n");
        	sqlstr.append(" 		,?						,?						,?						,?						,?						\n");
        	sqlstr.append(" 		,?						,?						,?						,?						,?						\n");
        	sqlstr.append(" 		,?						,?						,?																		\n");
        	sqlstr.append(" )																																\n");
        	
            persistent.setStatement(sqlstr.toString());
            persistent.addParameter(vo.get("CD_COMPANY",null));
            persistent.addParameter(vo.get("CD_SITE",null));
            persistent.addParameter(vo.get("NO_CONT",null));
            persistent.addParameter(vo.get("SQ_BUILT",null));
            persistent.addParameter(vo.get("NO_BUILT",null));
            persistent.addParameter(vo.get("SEQ_BUILT",null));
            persistent.addParameter(vo.get("SQ_CONT",null));
            persistent.addParameter(vo.get("YM_BUILT",null));
            persistent.addParameter(vo.get("NM_CONT",null));
            persistent.addParameter(vo.get("CD_CM00201",null));
            persistent.addParameter(vo.get("CD_CM00213",null));
            persistent.addParameter(vo.get("CD_COOP",null));
            persistent.addParameter(vo.get("TP_COMPANY",null));
            persistent.addParameter(vo.get("NO_COMPANY",null));
            persistent.addParameter(vo.get("NM_COOP",null));
            persistent.addParameter(vo.get("AMT_CONT",null));
            persistent.addParameter(vo.get("SAMT_CONT",null));
            persistent.addParameter(vo.get("VAMT_CONT",null));
            persistent.addParameter(vo.get("FAMT_CONT",null));
            persistent.addParameter(vo.get("AMT_BUILT_P",null));
            persistent.addParameter(vo.get("SAMT_BUILT_P",null));
            persistent.addParameter(vo.get("VAMT_BUILT_P",null));
            persistent.addParameter(vo.get("FAMT_BUILT_P",null));
            persistent.addParameter(vo.get("AMT_BUILT_S",null));
            persistent.addParameter(vo.get("SAMT_BUILT_S",null));
            persistent.addParameter(vo.get("VAMT_BUILT_S",null));
            persistent.addParameter(vo.get("FAMT_BUILT_S",null));
            persistent.addParameter(vo.get("AMT_D_PREPAY_P",null));
            persistent.addParameter(vo.get("VAMT_D_PREPAY_P",null));
            persistent.addParameter(vo.get("FAMT_D_PREPAY_P",null));
            persistent.addParameter(vo.get("AMT_D_PREPAY_S",null));
            persistent.addParameter(vo.get("VAMT_D_PREPAY_S",null));
            persistent.addParameter(vo.get("FAMT_D_PREPAY_S",null));
            persistent.addParameter(vo.get("DT_REQUEST_S",null));
            persistent.addParameter(vo.get("ID_INSERT",null));
            persistent.addParameter(vo.get("DTS_INSERT",null));
            persistent.addParameter(vo.get("TAG_COPY_S",null));
            persistent.addParameter(vo.get("CD_CM00252",null));
            persistent.addParameter(vo.get("DESC_BUILT",null));
            persistent.addParameter(vo.get("CURR_SOUR",null));
            persistent.addParameter(vo.get("UNT_CURR",null));
            persistent.addParameter(vo.get("AMT_CURR_P",null));
            persistent.addParameter(vo.get("VAMT_CURR_P",null));
            persistent.addParameter(vo.get("FAMT_CURR_P",null));
            persistent.addParameter(vo.get("AMT_CURR_S",null));
            persistent.addParameter(vo.get("VAMT_CURR_S",null));
            persistent.addParameter(vo.get("FAMT_CURR_S",null));
            persistent.addParameter(vo.get("AMT_D_PREPAY_CURR_P",null));
            persistent.addParameter(vo.get("VAMT_D_PREPAY_CURR_P",null));
            persistent.addParameter(vo.get("FAMT_D_PREPAY_CURR_P",null));
            persistent.addParameter(vo.get("AMT_D_PREPAY_CURR_S",null));
            persistent.addParameter(vo.get("VAMT_D_PREPAY_CURR_S",null));
            persistent.addParameter(vo.get("FAMT_D_PREPAY_CURR_S",null));
            persistent.addParameter(vo.get("CD_EXCH",null));
            persistent.addParameter(vo.get("RT_EXCH",null));
            persistent.addParameter(vo.get("DT_EXCH",null));
            persistent.addParameter(vo.get("SEQ_BUILT_ETEC",null));  //2019.04.02 자금청구차수 추가 
            persistent.addParameter(vo.get("CD_COST_TRADE",null));   //2019.07.29 ERP 자동분개를 위한 계정코드 추가
            
            persistent.execute();
        
        }catch(Exception e) {
            WAFLogger.error("InterfaceOutERPiU --> insertErpSUB1040T() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(e);
            throw e;
        }
    }
	
	/**
	 * 외주기성 MASTER 보조정보 등록
	 * @param connection
	 * @param vo
	 * @throws Exception
	 */
	public static void insertErpSUB1040TSub(Connection connection, ValueObject vo) throws Exception {
		Persistent persistent = new PersistentImpl(connection);
		StrBuffer sqlstr = new StrBuffer();
		try {
			sqlstr.append(" INSERT	INTO WC_Z_ETEC_O_BUILT_H_SUB (	\n");
			sqlstr.append("			CD_COMPANY						\n");
			sqlstr.append("			,CD_SITE						\n");
			sqlstr.append("			,NO_CONT						\n");
			sqlstr.append("			,SQ_BUILT						\n");
			sqlstr.append("			,SEQ_BUILT						\n");
			sqlstr.append("			,ACMP_ID						\n");
			sqlstr.append("			,YN_ESCROW						\n");	//2022.04.28 ERP 에스크로계좌유무 추가
			sqlstr.append(" ) VALUES (								\n");
			sqlstr.append(" 		?								\n");
			sqlstr.append(" 		,?								\n");
			sqlstr.append(" 		,?								\n");
			sqlstr.append(" 		,?								\n");
			sqlstr.append(" 		,?								\n");
			sqlstr.append(" 		,?								\n");
			sqlstr.append(" 		,?								\n");
			sqlstr.append(" )										\n");
			
			persistent.setStatement(sqlstr.toString());
			persistent.addParameter(vo.get("CD_COMPANY",null));
			persistent.addParameter(vo.get("CD_SITE",null));
			persistent.addParameter(vo.get("NO_CONT",null));
			persistent.addParameter(vo.get("SQ_BUILT",null));
			persistent.addParameter(vo.get("SEQ_BUILT",null));
			persistent.addParameter(vo.get("ACMP_ID",null));
			persistent.addParameter(vo.get("LABOR_USE_YN",null));
			persistent.execute();
		
		}catch(Exception e) {
			WAFLogger.error("InterfaceOutERPiU --> insertErpSUB1040TSub() : " + e.getMessage());
			WAFLogger.error("SQL : " + sqlstr);
			WAFLogger.error(e);
			throw e;
		}
	}

	/**
	 * 외주기성 DETAIL 등록
	 * @param connection
	 * @param mstVo
	 * @param detVo
	 * @throws Exception
	 */
	public static void insertErpSUB1041T(Connection connection, ValueObject mstVo, ValueObject detVo) throws Exception {
		Persistent persistent = new PersistentImpl(connection);
		StrBuffer sqlstr = new StrBuffer();
		try {
			sqlstr.append(" INSERT	INTO WC_O_BUILT_D (																\n");
			sqlstr.append(" 		CD_COMPANY		,CD_SITE		,NO_CONT		,SQ_BUILT		,SEQ_BUILT		\n");
			sqlstr.append(" 		,NO_BUILT		,SQ_CONT		,CD_CM00201		,CD_CM00208		,CD_CSTR		\n");
			sqlstr.append(" 		,DL_CSTR		,NM_CSTR		,CD_SIZE		,SIZE_CSTR		,UNIT_CSTR		\n");
			sqlstr.append(" 		,QTY_CONT		,UNT_CONT		,AMT_CONT		,QTY_BUILT_P	,AMT_BUILT_P	\n");
			sqlstr.append(" 		,QTY_BUILT_S	,AMT_BUILT_S	,QTY_BUILT		,ID_INSERT		,DTS_INSERT		\n");
			sqlstr.append(" 		,ID_UPDATE		,DTS_UPDATE		,CD_CM00006		,AMT_CURR_S						\n");
			sqlstr.append(" ) VALUES (																				\n");
			sqlstr.append(" 		?				,?				,?				,?				,?				\n");
			sqlstr.append(" 		,?				,?				,?				,?				,?				\n");
			sqlstr.append(" 		,?				,?				,?				,?				,?				\n");
			sqlstr.append(" 		,?				,?				,?				,?				,?				\n");
			sqlstr.append(" 		,?				,?				,?				,?				,?				\n");
			sqlstr.append(" 		,?				,?				,?				,?								\n");
			sqlstr.append(" )																						\n");
			
			persistent.setStatement(sqlstr.toString());
			persistent.addParameter(mstVo.get("CD_COMPANY",null));
			persistent.addParameter(mstVo.get("CD_SITE",null));
			persistent.addParameter(mstVo.get("NO_CONT",null));
			persistent.addParameter(mstVo.get("SQ_BUILT",null));
			persistent.addParameter(mstVo.get("SEQ_BUILT",null));
			persistent.addParameter(detVo.get("NO_BUILT",null));
			persistent.addParameter(detVo.get("SQ_CONT",null));
			persistent.addParameter(detVo.get("CD_CM00201",null));
			persistent.addParameter(detVo.get("CD_CM00208",null));
			persistent.addParameter(detVo.get("CD_CSTR",null));
			persistent.addParameter(detVo.get("DL_CSTR",null));
			persistent.addParameter(detVo.get("NM_CSTR",null));
			persistent.addParameter(detVo.get("CD_SIZE",null));
			persistent.addParameter(detVo.get("SIZE_CSTR",null));
			persistent.addParameter(detVo.get("UNIT_CSTR",null));
			persistent.addParameter(detVo.get("QTY_CONT",null));
			persistent.addParameter(detVo.get("UNT_CONT",null));
			persistent.addParameter(detVo.get("AMT_CONT",null));
			persistent.addParameter(detVo.get("QTY_BUILT_P",null));
			persistent.addParameter(detVo.get("AMT_BUILT_P",null));
			persistent.addParameter(detVo.get("QTY_BUILT_S",null));
			persistent.addParameter(detVo.get("AMT_BUILT_S",null));
			persistent.addParameter(detVo.get("QTY_BUILT",null));
			persistent.addParameter(mstVo.get("ID_INSERT",null));
			persistent.addParameter(mstVo.get("DTS_INSERT",null));
			persistent.addParameter(mstVo.get("ID_UPDATE",null));
			persistent.addParameter(mstVo.get("DTS_UPDATE",null));
			persistent.addParameter(detVo.get("CD_CM00006",null));
			persistent.addParameter(detVo.get("AMT_CURR_S",null));
			persistent.execute();
		
		}catch(Exception e) {
			WAFLogger.error("InterfaceOutERPiU --> insertErpSUB1041T() : " + e.getMessage());
			WAFLogger.error("SQL : " + sqlstr);
			WAFLogger.error(e);
			throw e;
		}
	}
	
	public static void updateChangeContReject(Connection connection, String subContReqId, String rtnReason, String userSabun) throws Exception {
		
		Persistent persistent = new PersistentImpl(connection);
		StrBuffer sqlstr = new StrBuffer();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String toDay = sdf.format(date);
		
		try{
			sqlstr.append("UPDATE	WC_Z_ETEC_SUB3000T	\n");
			sqlstr.append("SET		STATUS_CD = '90'	\n"); 
			sqlstr.append("    		,RETURN_REASON  = ?	\n");
			sqlstr.append("    		,RETURN_EMPNO  = ?	\n");
			sqlstr.append("    		,RETURN_DT  = ?		\n");
			sqlstr.append("WHERE SUB_CONT_REQ_ID = ?	\n");
			
			persistent.setStatement(sqlstr.toString());
			persistent.addParameter(rtnReason);
			persistent.addParameter(userSabun);
			persistent.addParameter(toDay);
			persistent.addParameter(subContReqId);
			persistent.execute();
			persistent.clearParameters();
			
		}catch (WAFSQLException e){
			WAFLogger.error("InterfaceOutERPiU --> updateERPIU() : " + e.getMessage());
			WAFLogger.error("SQL : " + sqlstr);
			WAFLogger.error(e);
			throw e;
		}
	}

	/**
	 * 타절계약 사업자번호 변경
	 * @return
	 * @throws Exception
	 */
	public static void execCoopChg(Connection connection, ValueObject vo) throws Exception {		
		CallableStatement cs 	= null;
		String pssql			= "";
		
		try{
			pssql = "{call UP_WC_Z_ETEC_COOP_CHG(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}";
			cs = connection.prepareCall(pssql);
			cs.setString(1, "01"); // 01.외주, 02.자재
			cs.setString(2, vo.get("CD_COMPANY",null)); // 회사코드
			cs.setString(3, vo.get("DEPT_CD",null)); // 프로젝트코드
			cs.setString(4, vo.get("ERP_NO_CONT",null)); // 품의번호(외주)
			cs.setString(5, vo.get("ERP_SQ_CONT",null)); // 품의순번(외주)
			cs.setString(6, ""); // 품의구분(구매)
			cs.setString(7, ""); // 품의년월(구매)
			cs.setString(8, ""); // 품의번호(구매)
			cs.setString(9, vo.get("ORG_ERP_CD_PARTNER",null)); // 변경전 사업자번호
			cs.setString(10, vo.get("ERP_CD_PARTNER",null)); // 변경후 사업자번호
			cs.registerOutParameter(11, java.sql.Types.NVARCHAR);
			cs.registerOutParameter(12, java.sql.Types.NVARCHAR);
			cs.execute();

            if("N".equals(cs.getString(11))){
            	throw new Exception(cs.getString(12));
            }
		}catch(WAFSQLException se) {
			WAFLogger.error("InterfaceOutERPiU --> execCoopChg() : " + se.getMessage());
			WAFLogger.error("SQL : " + pssql);
			WAFLogger.error(se);
			throw se;
		}		
	}

	
}
