/**
 * @(#)file    ContPopBiz.java
 * @(#)author  Kim Myung Ho
 * @(#)version 1.0
 * @(#)date    2014-08-11
 * @(#)since   JDK 1.4.2
 */
package kr.co.udapsoft.ebid.buyer.cont.purchase.biz;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

import oracle.toplink.internal.ox.StrBuffer;
import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.config.WAFConfig;
import kr.co.hsnc.common.sql.Row;
import kr.co.hsnc.common.sql.RowSet;
import kr.co.hsnc.common.sql.RowSetImpl;
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.sql.util.RowSetUtility;
import kr.co.hsnc.common.util.ValueObject;
import kr.co.udapsoft.common.code.CommCodeManager;
import kr.co.udapsoft.common.util.Crosscert;
import kr.co.udapsoft.common.util.PDFMaker;
import kr.co.udapsoft.ebid.buyer.common.file.FileManager;
import kr.co.udapsoft.ebid.buyer.sycs.user.biz.UserCommon;

import com.udapsoft.modular.web.ibsheet7.IBSheet7Factory;
import com.udapsoft.modular.web.ibsheet7.util.IBSheet7Utils;
import com.udapsoft.waf.common.ui.InitCombo;
import com.udapsoft.waf.common.util.StringUtils;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

   
public class PayPlanBiz extends Biz {

	public PayPlanBiz(HandlerStorage storage) {
		super(storage);
	}
	
	public void doBiz() throws Exception {
        ValueObject user = new ValueObject();
        //WAFLogger.debug("ContPopBiz :  ~" + storage.getUser());

		boolean success = false;
		
		if(storage.compareEvent("")){
			params.set("sheet1", getPayPlanList(params));
			
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
			
		}else if(storage.compareEvent("SHEET_SAVE")){
			String[] status = super.gets("sheet1.STATUS");
			//WAFLogger.debug("HHHHHHHHHHHHHHHHHHHHHHHHHH : " + params.get("PUR_CONT_MOD_ID"));
			delPlanData(conn.getConnection(), params);	//기존 자금계획  삭제			
			
			for (int i = 0; i < status.length; i++) {
				//WAFLogger.debug(IBSheet7Utils.getIBSheet7SqlParams("SHEET1", params, i));
				System.out.println("---->>>" + status[i]);
				if(!"D".equals(status[i])){
					success = savePlanData(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET1", params, i));
				}
			}
						
//			for (int i = 0; i < status.length; i++) {				
//				WAFLogger.debug("params : "+params);
//				if("Y".equals(params.get("PLAN_APPR_YN"))){	//설계평가여부 'Y'인것만 로직 찾아야함
//					savePlanApprData(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET1", params, i));  //설계평가표 등록(설계평가 체크되어 있는 경우)
//				}
//			}
			
			super.storage.setSuccess(success);
            //WAFLogger.debug("SUCCESS : ++++++++++++++++++++++++++++++++++++++++++ " + success);
            if(success)
            	storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));			
		}else if(storage.compareEvent("PAYPLAN_COPY")){  
			params.set("sheet1", getPrePayPlanList(params));
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
		}
	}

	/**
	 * 대금 계획 리스트 조회
	 * @param Params
	 * @return
	 */
	private RowSet getPayPlanList(ValueObject param) {

		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        
        StrBuffer sql = new StrBuffer();
        try{
        	sql.append("    	SELECT 'Y' AS DEL_CHECK		\n");
            sql.append("    	     , A.PUR_CONT_MOD_ID		\n");
            sql.append("    	     , A.PUR_CONT_ID		\n");        	
        	sql.append("    	     , A.PAY_SEQ		\n");
        	sql.append("    	     , A.PUR_CONT_ID		\n");
        	sql.append("    	     , F_GET_D_CURRENCY(A.PAY_SUPPLY_AMT,F_GET_CURRENCY_DIGIT(B.CURRENCY_GB)) AS PAY_SUPPLY_AMT		\n");				//F_GET_D_CURRENCY(B.ESTI_TOT_AMT,F_GET_CURRENCY_DIGIT(A.CURRENCY_GB))
        	sql.append("    	     , ROUND((A.PAY_SUPPLY_AMT / B.ORD_SUPPLY_AMT) * 100,2) AS PAY_SUPPLY_AMT_RATE		\n");
        	sql.append("    	     , A.GUAR_YN		\n");
        	sql.append("    	     , A.WARR_GUBUN		\n");
        	sql.append("    	     , A.VP_YN		\n");
        	sql.append("    	     , A.VP_DESC		\n");
        	sql.append("    	     , A.PAY_DIV_CD		\n");
        	sql.append("    	     , A.PAYREQ_ID		\n");
        	sql.append("    	     , NVL(A.PAY_YN, 'N') AS PAY_YN		\n");
        	sql.append("    	     , A.VP_CHK_EMPNO 		\n");
        	sql.append("    	     , A.VP_CHK_EMPNM 		\n");
        	sql.append("    	     , A.CRE_BY		\n");
        	sql.append("    	     , A.CRE_DATE		\n");
        	sql.append("    	     , A.UPD_BY		\n");
        	sql.append("    	     , A.UPD_DATE 		\n");
        	sql.append("    	     , 'N' AS INFLAG 		\n");
        	sql.append("    	     , NVL(A.PLAN_APPR_YN, 'N') AS PLAN_APPR_YN		\n");
        	sql.append("    	     , A.PLAN_APPR_CHK_EMPNO 		\n");
        	sql.append("    	     , A.PLAN_APPR_CHK_EMPNM 		\n");
        	sql.append("    	     , A.PLAN_APPR_SEND_YN 		\n");        	
        	sql.append("    	     , B.DEPT_CD 		\n");
        	sql.append("    	     , B.ORD_DOC_NO 		\n");
        	sql.append("    	     , (select CORP_ID from CONT_PUR_SUPP WHERE PUR_CONT_MOD_ID = B.PUR_CONT_MOD_ID AND SIGN_SEQ = 2) CORP_ID 		\n");
        	sql.append("    	  FROM CONT_PUR_PAYPLAN A, PUR2000T B 		\n");
            sql.append("    	WHERE A.PUR_CONT_MOD_ID = B.PUR_CONT_MOD_ID		\n");
            sql.append("    		AND A.PUR_CONT_MOD_ID = ?		\n");
            sql.append("  ORDER BY PAY_SEQ    \n");
            
            search.setStatement(sql.toString());
            WAFLogger.debug(sql.toString());
        	search.addParameter( params.get("PUR_CONT_MOD_ID"));
        	
            rowSet = search.execute();
            
            if (rowSet.size()==0){
            	rowSet = getPrePayPlanList(params);
            }
        }
        catch (WAFSQLException se) {
            super.doException(se);
            this.storage.setSuccess(false);  
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
        }
        finally {    
            if(rowSet == null){
            	rowSet = new RowSetImpl();
            }
        }
        //WAFLogger.debug("### rowSet size : " + rowSet.size());
        return rowSet;
	}
	
	/**
	 * 대금 계획이 데이터가 없을때 계약에서  리스트 조회
	 * @param Params
	 * @return
	 */
	private RowSet getPrePayPlanList(ValueObject param) {

		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        
        StrBuffer sql = new StrBuffer();
        try{
        	
        	
        	
        	sql.append("			SELECT  'Y' AS DEL_CHECK                                                                                                \n");
        	sql.append("			     ,  ? AS PUR_CONT_MOD_ID                                                                                      		\n");
        	sql.append("			     ,  PUR_CONT_ID                                                                                                     \n");
        	sql.append("			     ,  PAY_SEQ                                                                                                         \n");
        	sql.append("			     ,  PAY_SUPPLY_AMT                                                                                                  \n");
        	sql.append("			     ,  GUAR_YN                                                                                                         \n");
        	sql.append("			     ,  WARR_GUBUN                                                                                                      \n");
        	sql.append("			     ,  VP_YN                                                                                                           \n");
        	sql.append("			     ,  VP_DESC                                                                                                         \n");
        	sql.append("			     ,  PAY_DIV_CD                                                                                                      \n");
        	sql.append("			     ,  PAY_YN                                                                                                          \n");
        	sql.append("			     ,  PAYREQ_ID                                                                                                       \n");
        	sql.append("			     ,  VP_CHK_EMPNO                                                                                                    \n");
        	sql.append("			     ,  VP_CHK_EMPNM                                                                                                    \n");
        	sql.append("		         ,  PLAN_APPR_YN                                                                                                    \n ");
			sql.append("		         ,  PLAN_APPR_CHK_EMPNO                                                                                             \n ");
			sql.append("		         ,  PLAN_APPR_CHK_EMPNM                                                                                             \n ");
			sql.append("		         ,  PLAN_APPR_SEND_YN                                                                                               \n ");						
        	sql.append("			FROM CONT_PUR_PAYPLAN                                                                                                   \n");
        	sql.append("			WHERE PUR_CONT_MOD_ID = (SELECT PUR_CONT_MOD_ID                                                                         \n");
        	sql.append("			                         FROM PUR2000T                                                                                  \n");
        	sql.append("			                         WHERE (PUR_CONT_ID , MOD_NO) IN ( SELECT PUR_CONT_ID , MOD_NO -1  AS MOD_NO                    \n");
        	sql.append("			                                                           FROM PUR2000T                                                \n");
        	sql.append("			                                                           WHERE PUR_CONT_MOD_ID = ?                              		\n");
        	sql.append("			                                                         )                                                              \n");
        	sql.append("			                         )																								\n");
        	sql.append("           ORDER BY  PAY_SEQ		\n");
        	/*
            sql.append("    	SELECT 'Y' AS DEL_CHECK		\n");
            sql.append("    	           ,PUR_CONT_MOD_ID		\n");
            sql.append("    	           ,PUR_CONT_ID		\n");
            sql.append("    	           ,ROWNUM PAY_SEQ		\n");
            sql.append("    	           ,PAY_SUPPLY_AMT		\n");
            sql.append("    	           ,PAY_SUPPLY_AMT_RATE		\n");
            sql.append("    	           ,GUAR_YN		\n");
            sql.append("    	           ,WARR_GUBUN		\n");
            sql.append("    	           ,PAY_DIV_CD		\n");
            sql.append("    	     	   ,'N' AS PAY_YN		\n");
        	sql.append("    	     	   ,'N' AS INFLAG 		\n");
            sql.append("    	     	   ,'' AS VP_CHK_EMPNO		\n");
        	sql.append("    	     	   ,'' AS VP_CHK_EMPNM 		\n");
            sql.append("    	FROM(		\n");
            sql.append("    	SELECT A.PUR_CONT_MOD_ID		\n");
            sql.append("    	            ,A.PUR_CONT_ID		\n");
            sql.append("    	            ,TRUNC(A.PREPAY_AMT) AS PAY_SUPPLY_AMT		\n");
            sql.append("    	            ,A.PREPAY_AMT_RATE PAY_SUPPLY_AMT_RATE		\n");
            sql.append("    	            ,CASE WHEN (SELECT NVL(MAX(PREPAY_GUAR_AMT),0) FROM PUR2000T WHERE PUR_CONT_MOD_ID = A.PUR_CONT_MOD_ID) > 0 THEN 'Y' ELSE 'N' END GUAR_YN		\n");
            sql.append("    	            ,CASE WHEN (SELECT NVL(MAX(PREPAY_GUAR_AMT),0) FROM PUR2000T WHERE PUR_CONT_MOD_ID = A.PUR_CONT_MOD_ID) > 0 THEN 3 ELSE NULL END WARR_GUBUN		\n");
            sql.append("    	            ,'01' PAY_DIV_CD		\n");
            sql.append("    	FROM PUR2000T A		\n");
            sql.append("    	WHERE PUR_CONT_MOD_ID = ?		\n");
            sql.append("    	AND NVL(PREPAY_AMT,0) > 0		\n");

            sql.append("    	UNION ALL		\n");
            
            sql.append("    	SELECT A.PUR_CONT_MOD_ID		\n");
            sql.append("    	            ,A.PUR_CONT_ID		\n");
            sql.append("    	            ,TRUNC(A.MID_AMT) AS PAY_SUPPLY_AMT		\n");
            sql.append("    	            ,A.MID_AMT_RATE AS PAY_SUPPLY_AMT_RATE		\n");
            sql.append("    	            ,'N' GUAR_YN		\n");
            sql.append("    	            ,NULL WARR_GUBUN		\n");
            sql.append("    	            ,'02' PAY_DIV_CD		\n");
            sql.append("    	FROM PUR2000T A		\n");
            sql.append("    	WHERE PUR_CONT_MOD_ID = ?		\n");
            sql.append("    	AND NVL(MID_AMT,0) > 0		\n");

            sql.append("    	UNION ALL		\n");
            
            sql.append("    	SELECT A.PUR_CONT_MOD_ID		\n");
            sql.append("    	            ,A.PUR_CONT_ID		\n");
            sql.append("    	            ,TRUNC(A.RMDR_AMT) AS PAY_SUPPLY_AMT		\n");
            sql.append("    	            ,A.RMDR_AMT_RATE AS PAY_SUPPLY_AMT_RATE		\n");
            sql.append("    	            ,CASE WHEN (SELECT NVL(MAX(DEF_GUAR_AMT),0) FROM PUR2000T WHERE PUR_CONT_MOD_ID = A.PUR_CONT_MOD_ID) > 0 THEN 'Y' ELSE 'N' END GUAR_YN		\n");
            sql.append("    	            ,CASE WHEN (SELECT NVL(MAX(DEF_GUAR_AMT),0) FROM PUR2000T WHERE PUR_CONT_MOD_ID = A.PUR_CONT_MOD_ID) > 0 THEN 5 ELSE NULL END WARR_GUBUN		\n");
            sql.append("    	            ,'03' PAY_DIV_CD		\n");
            sql.append("    	FROM PUR2000T A		\n");
            sql.append("    	WHERE PUR_CONT_MOD_ID = ?		\n");
            sql.append("    	AND NVL(RMDR_AMT,0) > 0		\n");
            sql.append("    	)		\n");
            sql.append("    ORDER BY  PAY_SEQ		\n");
            search.setStatement(sql.toString());
            WAFLogger.debug(sql.toString());
        	search.addParameter( params.get("PUR_CONT_MOD_ID"));
        	search.addParameter( params.get("PUR_CONT_MOD_ID"));
        	search.addParameter( params.get("PUR_CONT_MOD_ID"));
        	 */
        	search.setStatement(sql.toString());
        	WAFLogger.debug(sql.toString());
        	search.addParameter( params.get("PUR_CONT_MOD_ID"));
        	search.addParameter( params.get("PUR_CONT_MOD_ID"));
            rowSet = search.execute();
        }
        catch (WAFSQLException se) {
            super.doException(se);
            this.storage.setSuccess(false);  
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
        }
        finally {    
            if(rowSet == null){
            	rowSet = new RowSetImpl();
            }
        }
        //WAFLogger.debug("### rowSet size : " + rowSet.size());
        return rowSet;
	}	
	/**
     * 대금계획 삭제
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	public void delPlanData(Connection connection, ValueObject valueObject) throws Exception {

        Persistent persistent = new PersistentImpl(connection);
        StrBuffer sqlstr = new StrBuffer();
        try {
        	sqlstr.append("DELETE FROM  CONT_PUR_PAYPLAN						\n");
        	sqlstr.append("WHERE PUR_CONT_MOD_ID = ?						\n");
            persistent.setStatement(sqlstr.toString());
            persistent.addParameter(valueObject.get("PUR_CONT_MOD_ID"));
            persistent.execute();
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> updUser() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(e);
            throw e;
        }
    }	
	/**
     * 대금계획 추가
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */	
	public boolean savePlanData(Connection connection, ValueObject sqlParams) throws Exception {
		Persistent persistent = new PersistentImpl(connection);
		StringBuffer sql = new StringBuffer();
		
		try {
			sql.append("		INSERT 							\n ");
			sql.append("		  INTO CONT_PUR_PAYPLAN 		\n ");
			sql.append("		       ( 						\n ");
			sql.append("		           PUR_CONT_MOD_ID		\n ");
			sql.append("		         ,PAY_SEQ				\n ");
			sql.append("		         ,PUR_CONT_ID			\n ");
			sql.append("		         ,PAY_SUPPLY_AMT		\n ");
			sql.append("		         ,GUAR_YN				\n ");
			sql.append("		         ,WARR_GUBUN		\n ");
			sql.append("		         ,VP_YN				\n ");
			sql.append("		         ,VP_DESC				\n ");
			sql.append("		         ,PAY_DIV_CD		\n ");
			sql.append("		         ,PAYREQ_ID		\n ");
			sql.append("		         ,PAY_YN		\n ");
			sql.append("		         ,VP_CHK_EMPNO		\n ");
			sql.append("		         ,VP_CHK_EMPNM		\n ");
			sql.append("		         ,CRE_BY		\n ");
			sql.append("		         ,CRE_DATE		\n ");
			sql.append("		         ,UPD_BY		\n ");
			sql.append("		         ,UPD_DATE 		\n ");
			sql.append("		         ,PLAN_APPR_YN				\n ");
			sql.append("		         ,PLAN_APPR_CHK_EMPNO		\n ");
			sql.append("		         ,PLAN_APPR_CHK_EMPNM		\n ");
			sql.append("		         ,PLAN_APPR_SEND_YN		\n ");
			sql.append("		       ) 		\n ");
			sql.append("		       VALUES 		\n ");
			sql.append("		       ( ?		\n ");
			sql.append("		         , (select nvl(max(pay_seq), 0) + 1 from CONT_PUR_PAYPLAN where PUR_CONT_MOD_ID = ?) 		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		         , SYSDATE		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		         , SYSDATE		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		         , ?		\n ");
			sql.append("		       ) 		\n ");

			WAFLogger.debug("!!savePlanData!! : " + sqlParams);
			persistent.setStatement(sql.toString());
			
			int paramIdx = 1;

			persistent.addParameter(params.get("PUR_CONT_MOD_ID"));
			persistent.addParameter(params.get("PUR_CONT_MOD_ID"));
			persistent.addParameter(params.get("PUR_CONT_ID"));
			persistent.addParameter(sqlParams.get("PAY_SUPPLY_AMT"));
			persistent.addParameter(sqlParams.get("GUAR_YN"));
			persistent.addParameter(sqlParams.get("WARR_GUBUN"));
			persistent.addParameter(sqlParams.get("VP_YN"));
			persistent.addParameter(sqlParams.get("VP_DESC"));
			persistent.addParameter(sqlParams.get("PAY_DIV_CD"));
			persistent.addParameter(sqlParams.get("PAYREQ_ID"));
			persistent.addParameter(sqlParams.get("PAY_YN"));
			persistent.addParameter(sqlParams.get("VP_CHK_EMPNO"));
			persistent.addParameter(sqlParams.get("VP_CHK_EMPNM"));
			persistent.addParameter(storage.getUser().getInt("ACCT_ID"));
			persistent.addParameter(storage.getUser().getInt("ACCT_ID"));
			persistent.addParameter(sqlParams.get("PLAN_APPR_YN"));
			persistent.addParameter(sqlParams.get("PLAN_APPR_CHK_EMPNO"));
			persistent.addParameter(sqlParams.get("PLAN_APPR_CHK_EMPNM"));
			persistent.addParameter(sqlParams.get("PLAN_APPR_SEND_YN"));
			
			persistent.execute();
			return true;
		}         
			catch(Exception e) {
			storage.addObject("ibsheet7Data", IBSheet7Utils.exceptionHandle(storage.request, e));
            WAFLogger.error(this.getClass().getName() + " --> updUser() : " + e.getMessage());
            WAFLogger.error("SQL : " + sql);
            WAFLogger.error(e);
            throw e;
        }
	}
	
	/**
     * 설계평가 삭제
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	/*public void delPlanApprData(Connection connection, ValueObject valueObject) throws Exception {

        Persistent persistent = new PersistentImpl(connection);
        StrBuffer sqlstr = new StrBuffer();
        try {
        	sqlstr.append("DELETE FROM  SUPP_APPR						\n");
        	sqlstr.append("WHERE CONT_ID = (SELECT pur_cont_id FROM CONT_PUR_PAYPLAN WHERE PLAN_APPR_YN <> 'Y' AND PUR_CONT_ID = ? AND PAYREQ_ID = ?)						\n");        	
        	sqlstr.append("AND DOC_CD = ?						\n");
            persistent.setStatement(sqlstr.toString());
            persistent.addParameter(params.get("PUR_CONT_ID"));
            persistent.addParameter(valueObject.get("PAYREQ_ID"));            
            persistent.addParameter("06");
            
            WAFLogger.debug(persistent.getParameters());
            WAFLogger.debug(sqlstr.toString());
            persistent.execute();
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> updUser() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(e);
            throw e;
        }
    }*/
	
	/**
     * 설계평가 등록
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	private boolean savePlanApprData(Connection connection, ValueObject sqlParams) throws Exception {
		PreparedStatement pstmt = null;
		
		try {
			StringBuffer sql = new StringBuffer();
			sql.append("MERGE INTO SUPP_APPR A  		           			                \n");
			sql.append("USING DUAL  					                                    \n");
			sql.append("   ON (A.CORP_ID = ? 					                        \n");
			sql.append("  AND A.CONT_ID = ?   					                            \n");
			sql.append("  AND A.PAYREQ_ID = ? )  					                            \n");
			sql.append(" WHEN MATCHED THEN  	 			                                \n");
			sql.append("      UPDATE           					                            \n");
			sql.append("      SET APPR_DY       = to_char(sysdate,'yyyymmdd')   				                        \n");
			sql.append("         ,APPR_BY_ID      = ?   				                        \n");
			sql.append("         ,REGU_STATUS_CD        = '01' 					                        \n");
			sql.append("         ,WRITE_NO   = ?  					                        \n");
			sql.append("         ,WRITE_NM = ?  					                        \n");
			sql.append("         ,UPD_BY     = ?  					                        \n");
			sql.append("         ,UPD_DATE   = SYSDATE  					                        \n");
			sql.append("  WHEN NOT MATCHED THEN  					                        \n");
			sql.append("          INSERT                        \n");
            sql.append("                    (                                                        \n");
            sql.append("                    DEPT_CD         --현장ID                             \n");                            
            sql.append("                   ,CORP_ID         --협력업체ID                             \n");          
            sql.append("                   ,ARR_ID          --평가표 일련번호                              \n");
            sql.append("                   ,DOC_CD          --문서관리코드                                  \n");
            sql.append("                   ,APPR_DY         --평가일                                          \n");
            sql.append("                   ,APPR_BY_ID      --평가자 ID                          \n");
            sql.append("                   ,APPR_BY_NM      --평가자명                                \n");                        
            sql.append("                   ,ORD_DOC_NO      --ORD_DOC_NO                                                        \n");
            sql.append("                   ,CONT_ID         --CONT_ID                                                     \n");
            sql.append("                   ,REGU_STATUS_CD  --상태                                 \n");
            sql.append("                   ,WRITE_NO        --작성자사번                                 \n");
            sql.append("                   ,WRITE_NM        --작성자사번                                 \n");
            sql.append("                   ,PAYREQ_ID       --대금지급 일련번호                              \n");
            sql.append("                   ,CRE_BY          --등록자                                                      \n");
            sql.append("                   ,CRE_DATE        --등록일자                                              \n");
            sql.append("                )VALUES(                                                        \n");
            sql.append("                    ?                                                               \n");    //현장ID              
            sql.append("                   ,?                                                               \n");    //협력업체ID
            sql.append("                   ,(SELECT DECODE(MAX(ARR_ID), '', 1, MAX(ARR_ID)+1) FROM SUPP_APPR WHERE CORP_ID = ? )               \n");//평가표 일련번호
            sql.append("                   ,'06'                                                             \n");     //문서관리코드                   
            sql.append("                   ,to_char(sysdate,'yyyymmdd')                                      \n");     //평가일                      
            sql.append("                   ,?                                                                \n");     //평가자 ID-현장평가자 2016.4.21                 
            sql.append("                   ,?                                                                \n");     //평가자명  -현장평가자 2016.4.21                                                                                          
            sql.append("                   ,?                                                                \n");     //ORD_DOC_NO               
            sql.append("                   ,?                                                                \n");     //CONT_ID                  
            sql.append("                   ,'01'                                                                \n");     //상태                       
            sql.append("                   ,?                                                                \n");     //작성자사번              --본사평가자 2016.4.21      
            sql.append("                   ,?                                                                \n");     //작성자명                 --본사평가자 2016.4.21      
            sql.append("                   ,?                                                                \n");     //대금지급 일련번호
            sql.append("                   ,?                                                                \n");     //등록자                      
            sql.append("                   ,SYSDATE                                                          \n");     //등록일자                     
            sql.append("                )                                                                    \n"); 
			
			pstmt = connection.prepareStatement(sql.toString());
			int paramIdx = 1;
			
			pstmt.setString(paramIdx++, sqlParams.get("CORP_ID"));
			pstmt.setString(paramIdx++, params.get("PUR_CONT_ID"));
			pstmt.setString(paramIdx++, sqlParams.get("PAYREQ_ID"));
			
			pstmt.setString(paramIdx++, storage.getUser().get("ACCT_ID"));
			pstmt.setString(paramIdx++, sqlParams.get("PLAN_APPR_CHK_EMPNO"));
			pstmt.setString(paramIdx++, sqlParams.get("PLAN_APPR_CHK_EMPNM"));
			pstmt.setString(paramIdx++, storage.getUser().get("ACCT_ID"));
 
			pstmt.setString(paramIdx++, sqlParams.get("DEPT_CD"));			
			pstmt.setString(paramIdx++, sqlParams.get("CORP_ID"));
			pstmt.setString(paramIdx++, sqlParams.get("CORP_ID"));
			pstmt.setString(paramIdx++, sqlParams.get("PLAN_APPR_CHK_EMPNO"));
			pstmt.setString(paramIdx++, sqlParams.get("PLAN_APPR_CHK_EMPNM"));			
//			pstmt.setString(paramIdx++, sqlParams.get("ITEM_STD_CD"));			
			pstmt.setString(paramIdx++, sqlParams.get("ORD_DOC_NO"));
			pstmt.setString(paramIdx++, params.get("PUR_CONT_ID"));			
			pstmt.setString(paramIdx++, sqlParams.get("PLAN_APPR_CHK_EMPNO"));
			pstmt.setString(paramIdx++, sqlParams.get("PLAN_APPR_CHK_EMPNM"));
			pstmt.setString(paramIdx++, sqlParams.get("PAYREQ_ID"));
			pstmt.setString(paramIdx++, storage.getUser().get("ACCT_ID"));
			
			//System.out.println(sql.toString());
//			WAFLogger.debug(sqlParams);
//            WAFLogger.debug(sql.toString());
			pstmt.executeUpdate();
			return true;
			
		} catch (Exception e) {
			WAFLogger.error(e);
			System.out.println(e);
			throw new Exception(e);
		} finally {
			try {
				if(pstmt != null) pstmt.close();
			} catch(SQLException se) {
				WAFLogger.error(se);
			}
		}
	}
}