/**
 * @(#)file    StockDeliveryWillPriceBiz.java
 * @(#)author  gyeongmo an
 * @(#)date    2014.7.15
 * @(#)Description: 가입고 단가등록
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 * All rights reserved.
 */
package kr.co.udapsoft.ebid.buyer.warehouse.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 com.udapsoft.modular.web.ibsheet7.IBSheet7Factory;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

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

    public void doBiz() throws Exception {
    	if(storage.compareEvent("GET_STOCKDELIVERYWILLPRICE")){
    		params.set("sheet1", getStockDeliveryWillPrice());
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
        } 
    }
    
    /**
     * 가입고 단가등록 조회
     * @author Gyeong mo An
     * @since 2014.03.19
     * @return
     * @throws Exception
     */
    public RowSet getStockDeliveryWillPrice() throws Exception {
        Search search 	  = new SearchImpl();
        RowSet listRowSet = new RowSetImpl();
        StringBuffer sql  = new StringBuffer();
        //2018.11.27 가입고단가 미사용 
        try {
        	sql.append("	SELECT                                                                                                                                  		\n");
        	sql.append("	        A.DEPT_CD                           -- 현장코드                                                                                		\n");
        	sql.append("	       , E.DEPT_NM                           -- 현장코드                                                                                		\n");
        	sql.append("	       , A.VENDOR_CD	\n");
        	sql.append("	       , GET_CORP_NM(A.VENDOR_CD) VENDOR_NM	\n");
        	sql.append("	       , B.PUR_CONT_ID							--계약번호	\n");
        	sql.append("	       , C.CST_DOC_NAME                      -- 계약명                                                                                  		\n");
        	sql.append("	       , B.MATERIAL_CD                       -- 자재코드                                                                                		\n");
        	sql.append("	       , D.MATERIAL_NAME                     --자재명                                                                                   		\n");
        	sql.append("	       , D.MATERIAL_SPEC                     --규격                                                                                     		\n");
        	sql.append("	       , F_GET_UNIT_KIND_NAME(B.MATERIAL_CD) UNIT_KIND_NAME	\n");
        	sql.append("	       , SUM(B.STOCK_QTY) AS STOCK_QTY       -- 입고수량                                                                                		\n");
        	sql.append("	       , MIN(B.STOCK_PRI) STOCK_PRI                         -- 단가                                                                                    		\n");
        	sql.append("	       , SUM(B.STOCK_QTY) * MIN(B.STOCK_PRI) STOCK_AMT                         -- 금액                                                                                    		\n");
        	sql.append("	FROM 2018.11.27 가입고단가 미사용 , PUR1000T C, PMM_COMM_MATERIAL_MASTER@CCSIF D, ETEC_ERPIF.ACZ10100 E                                                              		\n");
        	sql.append("	WHERE A.STOCK_ID = B.STOCK_ID                                                                                                           		\n");
        	sql.append("	AND B.PUR_CONT_ID = C.PUR_CONT_ID                                                                                                               		\n");
        	sql.append("	AND B.MATERIAL_CD = D.MATERIAL_CD                                                                                                       		\n");
        	sql.append("	AND A.DEPT_CD = E.DEPT_CD                                                                                                       		\n");
        	sql.append("	AND B.STOCK_QTY > 0	\n");
        	sql.append("	AND (D.PRICE_FLG = 'Y' OR B.STOCK_PRI = 0)	\n");
        	sql.append("	AND A.END_FLAG <> '9'	\n");
        	
        	// 현장
        	if(!"".equals(params.get("dept_cd"))) {
        		sql.append("		AND     A.DEPT_CD = ?                                                              						\n");
    			search.addParameter(params.get("dept_cd"));
        	}
        	// 입고년월
        	if ( !"".equals(params.get("dateFrom")) ) {
        		sql.append("	and SUBSTR(A.STOCK_DATE, 1, 6) = '" + params.get("dateFrom").replaceAll("-", "") + "'	\n");
        	}
        	sql.append("	GROUP by A.DEPT_CD, E.DEPT_NM, A.VENDOR_CD, B.PUR_CONT_ID, C.CST_DOC_NAME, B.MATERIAL_CD, D.MATERIAL_NAME, D.MATERIAL_SPEC		\n");
        	sql.append("	ORDER by A.DEPT_CD, B.PUR_CONT_ID DESC, A.VENDOR_CD, B.MATERIAL_CD                                                                                               		\n");

        	search.setStatement(sql.toString());
            listRowSet = search.execute();
            WAFLogger.debug(sql.toString());
        } catch(Exception e) {
            super.doException(e);
            this.storage.setSuccess(false);
            this.storage.setMessage(e.getMessage());
            storage.setDetailMessage(e);
        } 
        return listRowSet;
    }
}


