/**
 *
 * @(#)author  [ok]
 * @(#)version 1.0
 * @(#)date    2014. 06. 18
 * @(#)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.supplier.portal.payment.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 SuppPayRequestListBiz extends Biz {
	
	public SuppPayRequestListBiz(HandlerStorage storage) {
		super(storage);
	}

	public void doBiz() throws Exception {

		//System.out.println("전자조달.협력업체포탈.대금.구매_대금신청현황 프로세스처리.[SuppPayRequestListBiz]");
		
		if(storage.compareEvent("")) {

			//System.out.println("[SuppPayRequestListBiz] INIT");

			//검색 조건에 ComboBox데이터 설정 [상태]
			RowSet rowSet = getPurchaseStatus_searchCombo();
			storage.addRowSet("statusComboList", rowSet);
			
			//시트의 ComboBox데이터 설정 [상태]
			ValueObject vo = getPurchaseStatus_sheetCombo();
			storage.addObject("selectText", vo.getObject("DETAILCD"));
			storage.addObject("selectValue", vo.getObject("DETAILNM"));
			
			storage.setSuccess(true);
			
		}else if(storage.compareEvent("GET_LIST")){
			
			//System.out.println("[SuppPayRequestListBiz] GET_LIST");
			
			params.set("sheet1", getList(params));
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
		}
		
	}
	
    /**
     * 대금관리 상태Combox 데이터를 조회 한다.
	 * @param 
	 * @param 
	 * @return ValueObject
	 */
	public RowSet getPurchaseStatus_searchCombo(){

        Search search = new SearchImpl();
        RowSet listRowSet = null;
        String sqlstr = "";
        
        try {
            sqlstr += " SELECT DETAILCD, DETAILNM             		\n"
                    + " FROM EBID_COM911T      						\n"
                    + " WHERE BASECD = 'PURCHASE_STATUS_CD'   		\n"
		            + " ORDER BY SORT_NO ASC  						\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 getPurchaseStatus_sheetCombo(){

		ValueObject vo = new ValueObject();
		try{
			RowSet rowSet = getPurchaseStatus_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 ValueObject
     *@return RowSet
     *기   능 : 대금 리스트 조회
     *@author OK
     *@version 2014. 7. 23.
     *@since JDK1.7
     */
    public RowSet getList(ValueObject Params) {
        Search search = new SearchImpl();
        RowSet listRowSet = null;
        String sqlstr = "";
        
        try {
        	sqlstr += " SELECT A.*					 																	 	\n"
        			+ " FROM (					  																			\n"
        			+ " 		SELECT A.DEPT_CD                        AS DEPT_CD,             --현장코드						  	\n"
        			+ "     			A.DEPT_NAME            			AS DEPT_NAME,          	--현장명                			  	\n"
        			+ "     			A.CST_DOC_NAME     	   			AS CST_DOC_NAME,   	  	--계약명	           				  	\n"
        			+ "     			A.CORP_ID      	   				AS CORP_ID ,   	  		--협력업체 ID							\n"
        			+ "     			(SELECT CORP_KOR FROM SUPP_INFO WHERE CORP_ID = A.CORP_ID) CORP_KOR, 	--협력업체명			\n"
        			+ "     			A.ORD_START_DATE 	   			AS ORD_START_DATE,				           			  		\n"
        			+ "     			A.ORD_END_DATE         			AS ORD_END_DATE,				              			  	\n"
        			+ "     			TO_CHAR(A.ORD_START_DATE, 'yyyy-mm-dd') ||' ~ '||TO_CHAR(A.ORD_END_DATE, 'yyyy-mm-dd') AS ORD_START_END_DATE,  	\n"
        			+ "     			B.PAY_DIV              			AS PAY_DIV,				                				  	\n"
        			+ "     			DECODE(B.PAY_DIV,'1','납품정산','계획분할') PAY_DIV_NM, 		--납품설치구분명			  				\n"
        			+ "     			B.DEPOSIT_CD           			AS DEPOSIT_CD,      --지급구분	               			  	\n"
        			+ "     			F_GET_CODE_NAME('DEPOSIT_CD', B.DEPOSIT_CD) AS DEPOSIT_NM,        			 			  	\n"
        			+ "     			TO_CHAR(TO_DATE(B.TAX_DY,'YYYYMMDD'), 'yyyy-mm-dd') AS REQ_DATE,		--신청서작성일자        				  	\n"
        			+ "     			B.DEPOSIT_SEQ          			AS DEPOSIT_SEQ,     --지급차수      	          			  	\n"
        			+ "     			A.CURRENCY_GB          			AS CURRENCY_GB,                							  	\n"
        			+ "     			A.ORD_SUPPLY_AMT      			AS ORD_AMT,                			  						\n"
        			+ "     			B.CUR_ACMP_APPLYAMT   		 	AS CUR_REQ_AMT,     			--금회청구_공급가액     	  	\n"
        			+ "     			B.CUR_BEF_ACMP_SUPPLYAMT     	AS CUR_BEF_ACMP_AMT,			--누계청구_공급가액			\n"
        			+ "     			B.REMAINDER_ACMP_SUPPLYAMT   	AS REMAINDER_ACMP_AMT, 			--잔액청구_공급가액  			\n"
        			+ "     			TO_CHAR(B.CUR_ACMP_PERCENT, 'FM990.00') AS CUR_ACMP_PERCENT,   	--금회기성요율				\n"
        			+ "     			B.REQ_STATUS           			AS  REQ_STATUS,                			  					\n"
        			+ "     			A.PUR_CONT_ID          			AS  PUR_CONT_ID,                			  				\n"
        			+ "     			B.PAYREQ_ID                     AS  PAYREQ_ID,												\n"
        			+ "     			B.PUR_CONT_MOD_ID AS PUR_CONT_MOD_ID														\n"
        			+ " 		FROM PUR1000T A, PUR1100T B						   			                						\n"
        			+ " 		WHERE A.PUR_CONT_ID = B.PUR_CONT_ID                       											\n"
        			+ " 		AND B.PAY_DIV = '1'					                       											\n"
        			+ " 		UNION ALL							                       											\n"
        			+ " 		SELECT A.DEPT_CD                        AS DEPT_CD,             --현장코드						  	\n"
        			+ "     			A.DEPT_NAME            			AS DEPT_NAME,          	--현장명                			  	\n"
        			+ "     			A.CST_DOC_NAME     	   			AS CST_DOC_NAME,   	  	--계약명	           				  	\n"
        			+ "     			A.CORP_ID      	   				AS CORP_ID ,   	  		--협력업체 ID							\n"
        			+ "     			(SELECT CORP_KOR FROM SUPP_INFO WHERE CORP_ID = A.CORP_ID) CORP_KOR, 	--협력업체명			\n"
        			+ "     			A.ORD_START_DATE 	   			AS ORD_START_DATE,				           			  		\n"
        			+ "     			A.ORD_END_DATE         			AS ORD_END_DATE,				              			  	\n"
        			+ "     			TO_CHAR(A.ORD_START_DATE, 'yyyy-mm-dd') ||' ~ '||TO_CHAR(A.ORD_END_DATE, 'yyyy-mm-dd') AS ORD_START_END_DATE,  	\n"
        			+ "     			B.PAY_DIV              			AS PAY_DIV,				                				  	\n"
        			+ "     			DECODE(B.PAY_DIV,'1','납품정산','계획분할') PAY_DIV_NM, 		--납품설치구분명			  				\n"
        			+ "     			B.DEPOSIT_CD           			AS DEPOSIT_CD,      --지급구분	               			  	\n"
        			+ "     			F_GET_CODE_NAME('DEPOSIT_CD', B.DEPOSIT_CD) AS DEPOSIT_NM,        			 			  	\n"
        			+ "     			TO_CHAR(TO_DATE(B.TAX_DY,'YYYYMMDD'), 'yyyy-mm-dd') AS REQ_DATE,		--신청서작성일자        				  	\n"
        			+ "     			B.DEPOSIT_SEQ          			AS DEPOSIT_SEQ,     --지급차수      	          			  	\n"
        			+ "     			A.CURRENCY_GB          			AS CURRENCY_GB,                							  	\n"
        			+ "     			A.ORD_SUPPLY_AMT      			AS ORD_AMT,                			  						\n"
        			+ "     			B.CUR_ACMP_APPLYAMT   		 	AS CUR_REQ_AMT,     			--금회청구_공급가액     	  	\n"
        			+ "     			B.CUR_BEF_ACMP_SUPPLYAMT     	AS CUR_BEF_ACMP_AMT,			--누계청구_공급가액			\n"
        			+ "     			B.REMAINDER_ACMP_SUPPLYAMT   	AS REMAINDER_ACMP_AMT, 			--잔액청구_공급가액  			\n"
        			+ "     			TO_CHAR(B.CUR_ACMP_PERCENT, 'FM990.00') AS CUR_ACMP_PERCENT,   	--금회기성요율				\n"
        			+ "     			B.REQ_STATUS           			AS  REQ_STATUS,                			  					\n"
        			+ "     			A.PUR_CONT_ID          			AS  PUR_CONT_ID,                			  				\n"
        			+ "     			B.PAYREQ_ID                     AS  PAYREQ_ID,												\n"
        			+ "     			B.PUR_CONT_MOD_ID AS PUR_CONT_MOD_ID														\n"
        			+ " 		FROM PUR1000T A, PUR1100T B						   			                						\n"
        			+ " 		WHERE A.PUR_CONT_ID = B.PUR_CONT_ID                       											\n"
        			+ " 		AND B.PAY_DIV = '2'					                       											\n"        			
        			+ " 		AND B.REQ_STATUS NOT IN ('00','01','05','98')              											\n"        			
        			+ " 	) A												              											\n"        			
        			+ " 	, CONT_PUR_SUPP C1								              											\n"        			
        			+ " WHERE 1=1											              											\n"        			
        			+ " AND C1.SIGN_SEQ = 2                       																	\n"
        			+ " AND A.PUR_CONT_MOD_ID = C1.PUR_CONT_MOD_ID                      											\n"
        			;
        	if(!"".equals(params.get("searchBuildName"))){
        		sqlstr += " AND UPPER(A.CST_DOC_NAME) LIKE UPPER('%" + params.get("searchBuildName") + "%')							\n";
        	}
        	if(!"".equals(params.get("searchDateStart"))){
        		sqlstr += " AND A.REQ_DATE BETWEEN '"+Params.get("searchDateStart")+"'	AND '"+Params.get("searchDateEnd")+"'		\n";
        	}
        	if(!"".equals(params.get("searchStatusCd"))){
        		sqlstr += " AND A.REQ_STATUS = '"+Params.get("searchStatusCd")+"'													\n";
    		}            
        	if(!"".equals(params.get("userCorpId"))){
        		sqlstr += " AND C1.CORP_ID  = '"+Params.get("userCorpId")+"'														\n";
        	}        	
            sqlstr += " ORDER BY A.DEPT_CD, A.PUR_CONT_ID, A.DEPOSIT_CD, A.DEPOSIT_SEQ	  											\n" ; 
            search.setStatement(sqlstr);
            //System.out.println(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;
        }
    }
}
