/**
 *
 * @(#)author  [ok]
 * @(#)version 1.0
 * @(#)date    2014. 07. 24
 * @(#)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.purchase.biz;


import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.sql.RowSet;
import kr.co.hsnc.common.sql.RowSetImpl;
import kr.co.hsnc.common.sql.search.Search;
import kr.co.hsnc.common.sql.search.SearchImpl;
import kr.co.hsnc.common.util.ValueObject;

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

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

	public void doBiz() throws Exception {

		if(storage.compareEvent("")){
			
			WAFLogger.debug("전자조달 선급금현황 리스트 페이지 INIT");
			
			//검색 조건에 ComboBox데이터 설정 [상태]
			RowSet rowSet = getPerpayStatus_searchCombo();
			storage.addRowSet("statusComboList", rowSet);
			
			//시트의 ComboBox데이터 설정 [상태]
			ValueObject vo = getPerpayStatus_sheetCombo();
			storage.addObject("selectText", vo.getObject("DETAILCD"));
			storage.addObject("selectValue", vo.getObject("DETAILNM"));
			
			//시트의 ComboBox데이터 설정 [신청구분]
			vo = getPerpayReqDiv_sheetCombo();
			storage.addObject("selectText1", vo.getObject("DETAILCD"));
			storage.addObject("selectValue1", vo.getObject("DETAILNM"));
			
			//시트의 ComboBox데이터 설정 [세금계산서 발행방법]
			vo = getTaxMethodCd_sheetCombo();
			storage.addObject("selectText2", vo.getObject("DETAILCD"));
			storage.addObject("selectValue2", vo.getObject("DETAILNM"));
			
	
			storage.setSuccess(true);
		
		}else if(storage.compareEvent("GET_DATA")){
			params.set("sheet1", getPrePaymentList(params));
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
		}
		
	}

    /**
     * 선급금 신청 상태Combox 데이터를 조회 한다.
	 * @param 
	 * @param 
	 * @return ValueObject
	 */
	public RowSet getPerpayStatus_searchCombo(){

        Search search = new SearchImpl();
        RowSet listRowSet = null;
        String sqlstr = "";
        
        try {
            sqlstr += " SELECT DETAILCD, DETAILNM             		\n"
                    + " FROM EBID_COM911T      						\n"
                    + " WHERE BASECD = 'PREPAY_STATUS_CD'      		\n"
                    + " AND DETAILCD not IN('10', '11')      		\n"
		            + " ORDER BY DETAILCD      						\n";
             
            search.setStatement(sqlstr);
            listRowSet = search.execute();
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{

			if( listRowSet == null ){
            	listRowSet = new RowSetImpl();
            }
            
			return listRowSet;
		}
	}
	
    /**
     * 선급금 신청 상태를 조회 한다.
	 * @param 
	 * @param 
	 * @return ValueObject
	 */
	public ValueObject getPerpayStatus_sheetCombo(){

		ValueObject vo = new ValueObject();
		try{
			RowSet rowSet = getPerpayStatus_searchCombo();
			
			InitCombo initCombo  = new InitCombo();
			initCombo.makeComboData(rowSet,"DETAILCD", "DETAILNM");
		
			vo.set("DETAILCD", initCombo.getCode());
			vo.set("DETAILNM", initCombo.getCodeName());
		    
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if( vo == null ){
				vo = new ValueObject();
            }
			return vo;
		}
		
	}

	
    /**
     * 선급금 신청 신청구분를 조회 한다.
	 * @param 
	 * @param 
	 * @return ValueObject
	 */
	public RowSet getPerpayReqDiv(){

        Search search = new SearchImpl();
        RowSet listRowSet = null;
        String sqlstr = "";
        
        try {
            sqlstr += " SELECT DETAILCD, DETAILNM             	\n"
                    + " FROM EBID_COM911T      					\n" 
                    + " WHERE BASECD = 'REG_DIV_CD'     		\n";
             
            search.setStatement(sqlstr);
            listRowSet = search.execute();
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{

			if( listRowSet == null ){
            	listRowSet = new RowSetImpl();
            }
            
			return listRowSet;
		}
	}
	
    /**
     * 세금계산서 발행방법 
	 * @param 
	 * @param 
	 * @return ValueObject
	 */
	public RowSet getTaxMethodCd(){

        Search search = new SearchImpl();
        RowSet listRowSet = null;
        String sqlstr = "";
        
        try {
            sqlstr	+= 	" SELECT DETAILCD, DETAILNM         \n"
                    + 	"   FROM EBID_COM911T      			\n"
                    + 	"  WHERE BASECD = 'TAX_METHOD_CD'	\n";
             
            search.setStatement(sqlstr);
            listRowSet = search.execute();
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{

			if( listRowSet == null ){
            	listRowSet = new RowSetImpl();
            }
            
			return listRowSet;
		}
	}
	
	/**
     * 세금계산서 발행방법 Combo
	 * @param 
	 * @param 
	 * @return ValueObject
	 */
	public ValueObject getTaxMethodCd_sheetCombo(){

		ValueObject vo = new ValueObject();
		//String RIGHT_ID = "";
		//String RIGHT_NAME = "";
		try{
			RowSet rowSet = getTaxMethodCd();
			
			InitCombo initCombo  = new InitCombo();
			initCombo.makeComboData(rowSet,"DETAILCD", "DETAILNM");
		
			vo.set("DETAILCD", initCombo.getCode());
			vo.set("DETAILNM", initCombo.getCodeName());
		    
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if( vo == null ){
				vo = new ValueObject();
            }
			return vo;
		}
	}
	
    /**
     * 선급금 신청구분를 조회 한다.
	 * @param 
	 * @param 
	 * @return ValueObject
	 */
	public ValueObject getPerpayReqDiv_sheetCombo(){

		ValueObject vo = new ValueObject();
		//String RIGHT_ID = "";
		//String RIGHT_NAME = "";
		try{
			RowSet rowSet = getPerpayReqDiv();
			
			InitCombo initCombo  = new InitCombo();
			initCombo.makeComboData(rowSet,"DETAILCD", "DETAILNM");
		
			vo.set("DETAILCD", initCombo.getCode());
			vo.set("DETAILNM", initCombo.getCodeName());
		    
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if( vo == null ){
				vo = new ValueObject();
            }
			return vo;
		}
	}
	
    /**
     *@param ValueObject
     *@return RowSet
     *기   능 : 선급금 신청 데이터 조회
     *@author OK
     *@version 2014. 6. 23.
     *@since JDK1.7
     */
    public RowSet getPrePaymentList(ValueObject Params) {
        Search search = new SearchImpl();
        RowSet listRowSet = null;
        String sqlstr = "";
        
        try {
        	sqlstr	+=	" SELECT 																													\n"
        			+ 	"     	 A.PUR_CONT_MOD_ID AS SUB_CONT_MOD_ID               -- 발주계약변경ID       										\n"
        			+ 	"      	 ,A.DEPT_NAME  AS DEPT_NAME	                   -- 계약명	  														\n"
        			+ 	"     	 ,A.CST_DOC_NAME  AS BUILD_NAME	                   -- 계약명	  													\n"
        			+ 	"        ,A.ORD_DOC_NO AS CON_DOC_NO	                   -- 발주계약번호													\n"
        			+ 	"        ,B.PREPAY_NO AS MOD_NO		                	   -- 선급차수														\n"
        			+ 	"        ,B.PREPAY_ID AS SUB_CONT_ID	                   -- 선급내역ID 													\n"
        			+ 	"        ,TO_CHAR(A.ORD_START_DATE, 'YYYY-MM-DD') || ' ~ ' || TO_CHAR(A.ORD_END_DATE, 'YYYY-MM-DD') AS START_END_WORK_DATE	\n"
        			+ 	"     	 ,A.ORD_SUPPLY_AMT AS CON_AMT	                		   -- 발주계약금액											\n"
        			+ 	"     	 ,B.ORG_SUPPLY_AMT  AS ORG_AMT  --선급금액	   -- 선급공급가 		  										\n"
        			+ 	"     	 ,B.PREPAY_SUPPLY_AMT  AS PREPAY_AMT  --선급금액	   -- 선급공급가 		  										\n"
        			+ 	"     	 ,NVL(B.REQ_PREPAY_SUPPLY_AMT, 0) AS REQ_PREPAY_AMT --신청 선급금 공급가액											\n"
        			+ 	"     	 ,B.TAX_DY  AS SIGN_DT  --신청일		              		  							\n"
        			+ 	"      	 ,NVL(B.PREPAY_PROC_GB, '00') AS STATUS_CD          --신청구분     		  											\n"
        			+ 	"     	 ,NVL(B.REQ_DIV_CD, '1') AS REQ_DIV_CD   		   --상태															\n"
        			+ 	"     	 ,B.CORP_KOR																										\n"
        			+ 	"		 ,B.TAX_METHOD_CD	--	세금계산서_발행방법																			\n"
        			+ 	"		 ,B.CHG_DIV_CD	--	신규변경구분																		\n"
        			+ 	"   FROM PUR2000T A, PUR1030T B				                			  													\n"
        			+ 	"  WHERE A.PUR_CONT_MOD_ID = B.PUR_CONT_MOD_ID																				\n"
        			+ 	"    AND A.CON_CLS_YN = '9'																									\n"
        			+ 	"    AND B.PREPAY_AMT > 0																									\n"	
        	        +   "    AND B.PREPAY_PROC_GB NOT IN('10', '11')																			    \n";
           
        	if(!"".equals(params.get("searchDeptCd"))){
        		sqlstr += " AND A.DEPT_CD = '"+Params.get("searchDeptCd")+"'																		\n";
        	}
        	if(!"".equals(params.get("searchBuildName"))){
        		sqlstr += " AND UPPER(A.CST_DOC_NAME) LIKE UPPER('%" + params.get("searchBuildName") + "%')															\n";
        	}
        	if(!"".equals(params.get("searchStatusCd"))){
        		sqlstr += " AND B.PREPAY_PROC_GB = '"+Params.get("searchStatusCd")+"'																					\n";
        	}
        	if(!"".equals(params.get("searchSignDt_from"))){
        		sqlstr += " AND B.TAX_DY >= '"+Params.get("searchSignDt_from").replaceAll("-", "")+"'															\n";
        	}
        	if(!"".equals(params.get("searchSignDt_to"))){
        		sqlstr += " AND B.TAX_DY <= '"+Params.get("searchSignDt_to").replaceAll("-", "")+"'																\n";
        	}
        	if(!"".equals(params.get("searchCorp"))){
        		sqlstr += " AND UPPER(B.CORP_KOR) LIKE UPPER('%" + params.get("searchCorp") + "%')																\n";
        	}
            sqlstr += " ORDER BY A.PUR_CONT_ID DESC																													\n" ; 
            search.setStatement(sqlstr);
            WAFLogger.debug(sqlstr);
            listRowSet = search.execute();
           
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> getPrePaymentList() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
            return null;
        }
        finally {

            if( listRowSet == null ) listRowSet = new RowSetImpl();
            return listRowSet;
        }
    }	
}
