/**
 * @(#)file    SalStampBiz.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 SalStampBiz extends Biz {

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

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

	/**
	 * 영업_인지세 조회
	 * @param Params
	 * @return
	 */
	private RowSet getSalStampList(ValueObject param) {

		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        
        StrBuffer sqlstr = new StrBuffer();
        
        try{
			sqlstr.append("   SELECT                                                                                        \n");//
			sqlstr.append("           STAMP_DUTY_ID                                                                         \n");//인지세ID
			sqlstr.append("          ,F_GET_CODE_NAME('STAMP_DUTY_TYPE', STAMP_DUTY_TYPE) AS STAMP_DUTY_TYPE                \n");//인지세구분
			sqlstr.append("          ,F_GET_CODE_NAME('STAMP_DUTY_ISSUE_GB', STAMP_DUTY_ISSUE_GB) AS STAMP_DUTY_ISSUE_GB    \n");//발행주체구분
			sqlstr.append("          ,CORP_REG_NO                                                                           \n");//사업자등록번호
			sqlstr.append("          ,CORP_NAME                                                                             \n");//업체명
			sqlstr.append("          ,CONT_NO                                                                               \n");//계약번호
			sqlstr.append("          ,MOD_NO                                                                                \n");//계약차수       
			sqlstr.append("          ,STAMP_DUTY_NO                                                                         \n");//고유식별번호
			sqlstr.append("          ,CONT_NAME                                                                             \n");//계약명
			sqlstr.append("          ,F_GET_D_CURRENCY(CONT_AMT, F_GET_CURRENCY_DIGIT(CURRENCY_GB)) AS CONT_AMT             \n");//계약금액
			sqlstr.append("          ,CURRENCY_GB                                                                 		    \n");//통화구분
			sqlstr.append("          ,F_GET_D_CURRENCY(STAMP_DUTY_TAX,0) AS STAMP_DUTY_TAX                                  \n");//인지세액
			sqlstr.append("          ,TO_CHAR(TO_DATE(STAMP_DUTY_DATE), 'YYYY-MM-DD') AS STAMP_DUTY_DATE                 	\n");//발행일자
			sqlstr.append("          ,REMARK                                                                                \n");//비고
			sqlstr.append("   FROM STAMP_DUTY																			    \n");
			sqlstr.append("   WHERE  STAMP_DUTY_TYPE = '50'                                               	 		    	\n");
			//sqlstr.append("   WHERE  1=1                                               	 								    \n");
        	// 발주처                                                                                                       
        	if(!param.get("p_corp_name").isEmpty())                                                                         
        		sqlstr.append("   AND CORP_NAME LIKE '%"+param.get("p_corp_name")+"%'									    \n");
        	//발행일자
        	if(!param.get("DutyFromDt").isEmpty())		
        		sqlstr.append("   AND TO_CHAR(TO_DATE(STAMP_DUTY_DATE), 'YYYY-MM-DD') >= '"+param.get("DutyFromDt")+"'	    \n");
        	if(!param.get("DutyToDt").isEmpty())                                                                            
        		sqlstr.append("   AND TO_CHAR(TO_DATE(STAMP_DUTY_DATE), 'YYYY-MM-DD') <= '"+param.get("DutyToDt")+"'	    \n");
        	//계약번호                                                                                                      
        	if(!param.get("p_cont_no").isEmpty())                                                                           
        		sqlstr.append("   AND CONT_NO LIKE '%"+param.get("p_cont_no")+"%'										    \n");
        	//계약명                                                                                                        
        	if(!param.get("p_cont_name").isEmpty())                                                                         
        		sqlstr.append("   AND CONT_NAME LIKE '%"+param.get("p_cont_name")+"%'									    \n");
        	sqlstr.append("   	ORDER BY STAMP_DUTY_ISSUE_GB,CORP_NAME,CONT_NO ASC, 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;
	}

}