/**
 * @(#)file    PurStampBiz.java
 * @(#)author  김예진
 * @(#)version 1.0
 * @(#)date    2015-01-08
 * @(#)since   JDK 1.4.2
 */
package kr.co.udapsoft.ebid.buyer.cont.stamptax.biz;

import oracle.toplink.internal.ox.StrBuffer;
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.WAFSQLException;
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.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

   
public class PurStampBiz extends Biz {

	public PurStampBiz(HandlerStorage storage) {
		super(storage);
	}

	public void doBiz() throws Exception {
		
		if(storage.compareEvent("")){
			
		}else if(storage.compareEvent("SHEET_SELECT")){
			params.set("sheet1", getPurStampList(params));
			
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
			
		}
	}

	/**
	 * 구매_인지세 조회
	 * @param Params
	 * @return
	 */
	private RowSet getPurStampList(ValueObject param) {

		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        
        StrBuffer sqlstr = new StrBuffer();
        
        try{
        	sqlstr.append("    SELECT  																										 \n");
			sqlstr.append("            A.PUR_CONT_MOD_ID                                                                                     \n");
			sqlstr.append("           ,A.DEPT_CD	                                                                                         \n");
			sqlstr.append("           ,A.DEPT_NAME                                                                                           \n");
			sqlstr.append("           ,A.ORD_DOC_NO                                                                                          \n");
			sqlstr.append("           ,A.MOD_NO                                                                                              \n");
			sqlstr.append("           ,A.CST_DOC_NAME                                                                                        \n");
			sqlstr.append("           ,B.VENDCD                                                                                              \n");
			sqlstr.append("           ,B.MEMBER_NAME                                                                                         \n");
			sqlstr.append("           ,F_GET_D_CURRENCY(A.ORD_AMT, F_GET_CURRENCY_DIGIT(A.CURRENCY_GB)) AS ORD_AMT                           \n");
			sqlstr.append("           ,A.CURRENCY_GB                                                                                         \n");
			sqlstr.append("           ,C.STAMP_DUTY_ID                                                                                       \n");
			sqlstr.append("           ,C.STAMP_DUTY_ISSUE_GB                                                                                 \n");
			sqlstr.append("           ,C.STAMP_DUTY_NO                                                                                       \n");
			sqlstr.append("           ,C.STAMP_DUTY_TAX                                                                                      \n");
			sqlstr.append("           ,C.STAMP_DUTY_DATE                                                                                     \n");
			sqlstr.append("           ,C.REMARK                                                                                              \n");
			sqlstr.append("           ,(SELECT DECODE(MAX(AA.FILE_ID), NULL, '0', '1') FROM PUB0510T AA, PUB0500T BB                  \n");
			sqlstr.append("                   WHERE AA.FILE_ID = BB.FILE_ID AND FILE_GB = 'PUR_STAMP_DUTY'                                   \n"); 
			sqlstr.append("                   AND ID = C.STAMP_DUTY_ID) AS FILE_YN                                                           \n"); 
			sqlstr.append("           ,(SELECT MAX(AA.FILE_ID) FROM PUB0510T AA, PUB0500T BB                                                 \n");
			sqlstr.append("                   WHERE AA.FILE_ID = BB.FILE_ID AND FILE_GB = 'PUR_STAMP_DUTY'                                   \n"); 
			sqlstr.append("                   AND ID = C.STAMP_DUTY_ID) AS FILE_ID                                                           \n"); 
			sqlstr.append("    FROM PUR2000T A,                                                                                              \n");
			sqlstr.append("         CONT_PUR_SUPP B,                                                                                         \n");     
			sqlstr.append("         STAMP_DUTY C                                                                                             \n");
			sqlstr.append("    WHERE A.PUR_CONT_MOD_ID  =  B.PUR_CONT_MOD_ID                                                                 \n");
			sqlstr.append("    AND   A.PUR_CONT_MOD_ID = C.CONT_MOD_ID                                                                       \n");
			sqlstr.append("    AND   A.STATUS_CD = '50'                                                                                      \n");
			sqlstr.append("    AND   B.SIGN_SEQ = 2                                                                                          \n");
			sqlstr.append("    AND   C.STAMP_DUTY_TYPE(+) = '10'  																			 \n");
        	// 현장
        	if(!param.get("p_dept_cd").isEmpty()) {
        		sqlstr.append(" AND A.DEPT_CD = ? 																		\n");
    			search.addParameter(params.get("p_dept_cd"));
        	}
        	//계약명
        	if(!param.get("p_cst_doc_name").isEmpty())
        		sqlstr.append("   AND A.CST_DOC_NAME LIKE '%"+param.get("p_cst_doc_name")+"%'							\n");
        	//업체명
        	if(!param.get("p_member_name").isEmpty())
        		sqlstr.append("   AND A.MEMBER_NAME LIKE '%'|| '"+param.get("p_member_name")+"' ||'%'					\n");
        	//발행일자
        	if(!param.get("DutyFromDt").isEmpty())		
        		sqlstr.append("   AND TO_CHAR(TO_DATE(C.STAMP_DUTY_DATE), 'YYYY-MM-DD') >= '"+param.get("DutyFromDt")+"'\n");
        	if(!param.get("DutyToDt").isEmpty())
        		sqlstr.append("   AND TO_CHAR(TO_DATE(C.STAMP_DUTY_DATE), 'YYYY-MM-DD') <= '"+param.get("DutyToDt")+"'	\n");
        	//상태
        	if(!param.get("p_stamp_status").isEmpty())
        		sqlstr.append("   AND C.STAMP_DUTY_ISSUE_GB IN ("+param.get("p_stamp_status")+")						\n");

        	sqlstr.append("   	ORDER BY A.DEPT_NAME,A.ORD_DOC_NO ASC, A.MOD_NO DESC									\n");
        	search.setStatement(sqlstr.toString());
            rowSet = search.execute();
        }
        catch (WAFSQLException se) {
            super.doException(se);
            this.storage.setSuccess(false);  
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
            WAFLogger.error("### WAFSQLException : " + se.getMessage());
        }
        finally {    
            if(rowSet == null){
            	rowSet = new RowSetImpl();
            }
        }
        return rowSet;
	}

}