<%--
  - FileName   : ContItemPopup.jsp
  - Author(s)  : mklee
  - Date       : 2018.10.23
  - Description: ITEM 내역
  --%>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="kr.co.hsnc.common.sql.RowSet" %>
<%@ page import="kr.co.hsnc.common.util.ValueObject" %>
<%@ page import="com.udapsoft.waf.system.*" %>
<%@ page import="com.udapsoft.waf.common.*" %>
<%@ page import="kr.co.udapsoft.common.*" %>
<%@ page import="com.udapsoft.waf.common.util.*" %>
<%@ page import="kr.co.hsnc.common.util.*"%>
<%@ page import="kr.co.hsnc.common.util.Formatter"%>
<%@ page import="com.udapsoft.waf.common.ui.HTMLMaker" %>
<%@ page import="com.udapsoft.waf.system.HandlerStorage" %>
<%@page import="java.net.URLDecoder"%>

<%@ include file="/jsp/work/taglib.jsp" %>

<jsp:useBean id="ctx" class="com.udapsoft.waf.system.context.SessionContext" scope="session" />
<%
    ctx.init(request, response);

    HandlerStorage storage = ctx.getHandlerStorage();
    ValueObject user       = ctx.getUser();
    ValueObject params     = storage.getParams();
    String event           = storage.getEvent(); 
     
%>

<SCRIPT language="JavaScript">
	var sheet1;
	var sheet1Row;
	var sheet1PageNo = 1;
	var pageLastRowIndex = 1;
	var validationResult = false;
	var filterShow = false;

	
    function _onReady(){
    	if(jQuery("#edit_yn").val() == "Y"){		 
			jQuery("#btnSave").show();
		}else{
			jQuery("#btnSave").hide();
		}
    	
		gridInit(sheet1);

		//autoResize();
		
		jQuery("#btnSearch").click(function() {
        	jQuery("#fevent").val("SHEET_SELECT");
        	var url = Modular.model.PageContext.CONTEXT_ROOT + "/cont/outside/ContItemPopup_sheet.screen";
        	Modular.view.IBSheet7.doSearch(sheet1, url, jQuery("#form1"), 30, "");
		});		
		jQuery("#btnSave").click(function() {
			for (var i = 2; i <= sheet1.RowCount(); i ++ ){
				if (Number(sheet1.GetCellValue(i, "SUPPLY_AMT"))  != Number(sheet1.GetCellValue(i, "SUPPLY_TAMT")) + Number(sheet1.GetCellValue(i, "SUPPLY_NAMT"))   ){
					alert("과세,면세 합계가 공급액과 일치하지 않습니다.");
					alert(Number(sheet1.GetCellValue(i, "SUPPLY_AMT")));
					alert(Number(sheet1.GetCellValue(i, "SUPPLY_AMT"))+ Number(sheet1.GetCellValue(i, "SUPPLY_NAMT")) );
					
					return;
				}
				if (Number(sheet1.GetCellValue(i, "SUPPLY_TAMT"))  < Number(sheet1.GetCellValue(i, "FUND_TAMT"))  ){
					alert("기성과세누계보다 과세금액이 적습니다. ");  //변경계약일때 
					return;
				}
				if (Number(sheet1.GetCellValue(i, "SUPPLY_NAMT"))  < Number(sheet1.GetCellValue(i, "FUND_NAMT"))  ){
					alert("기성면세누계보다 면세금액이 적습니다. ");  //변경계약일때 
					return;
				}
			}			 
			
			 if (confirm("저장 하시겠습니까?")){
				var f = document.form1;
				$("#fevent").val("DATA_SAVE");
				var url = Modular.model.PageContext.CONTEXT_ROOT + "ContItemPopup_form.screen";
				var result = Modular.view.IBSheet7.doSave(sheet1, url, $("#form1"), false);
			}	
		});	 
		jQuery("#btnSearch").click();
	}
    
    function gridInit(sheetName){
		var init_sheet = {};
		init_sheet.Cfg = {SearchMode:2, Page:5000, MergeSheet:msPrevColumnMerge + msHeaderOnly}; 
	
		if ( "<%=params.get("MOD_NO")%>" == '0') {//원계약 
			init_sheet.Cols = [
	                            {Header:'Status',		Type:"Status",  SaveName:"STATUS", 			Width:0, 	Align:"Center",	Hidden:"True"},
				       			{Header:'공종코드|공종코드',		Type:"Text",	SaveName:"CD_CSTR",		    Width:80, 	Align:"Left", 	Edit:"false"},
				       			{Header:'내역코드|내역코드',		Type:"Text",	SaveName:"DL_CSTR",		    Width:100, 	Align:"Left", 	Edit:"false"},
				    			{Header:'내역명|내역명',		    Type:"Text",	SaveName:"ITEM_NAME",		Width:200, 	Align:"Left", 	Edit:"false"},
				    			{Header:'현재|공급액',		    Type:"AutoSum",	SaveName:"SUPPLY_AMT",		Width:80, 	Align:"Right", 	Edit:"True",PointCount:"<%=params.get("digit")%>"}, 
				    			{Header:'현재|과세',		    Type:"AutoSum",	SaveName:"SUPPLY_TAMT",		Width:80, 	Align:"Right", 	Edit:"True",PointCount:"<%=params.get("digit")%>"},
				       			{Header:'현재|면세',		    Type:"AutoSum",	SaveName:"SUPPLY_NAMT",		Width:80, 	Align:"Right", 	Edit:"false",PointCount:"<%=params.get("digit")%>"},
				       			{Header:'MATERIAL_CD',	Type:"Text", 	SaveName:"MATERIAL_CD", 	Width:50, 	Align:"Center",	Hidden:"true"},
				       			{Header:'과세비율',		Type:"Text",	SaveName:"SUPP_RATE",	    Width:80, 	Align:"Right", 	Hidden:"true" } 
			];
		}
		else {  //변경계약 
			init_sheet.Cols = [
	                            {Header:'Status',		 Type:"Status",     SaveName:"STATUS", 			Width:0, 	Align:"Center",	Hidden:"True"},
				       			{Header:'공종코드|공종코드',Type:"Text",	    SaveName:"CD_CSTR",		    Width:80, 	Align:"Left", 	Edit:"false"},
				       			{Header:'내역코드|내역코드',Type:"Text",	    SaveName:"DL_CSTR",		    Width:100, 	Align:"Left", 	Edit:"false"},
				    			{Header:'내역명|내역명',	 Type:"Text",	    SaveName:"ITEM_NAME",		Width:200, 	Align:"Left", 	Edit:"false"},
				    			{Header:'이전|공급액',	 Type:"AutoSum",	SaveName:"B_SUPPLY_AMT",	Width:80, 	Align:"Right", 	Edit:"false",PointCount:"<%=params.get("digit")%>"}, 
				    			{Header:'이전|과세',		 Type:"AutoSum",	SaveName:"B_SUPPLY_TAMT",	Width:80, 	Align:"Right", 	Edit:"false",PointCount:"<%=params.get("digit")%>"},
				       			{Header:'이전|면세',		 Type:"AutoSum",	SaveName:"B_SUPPLY_NAMT",	Width:80, 	Align:"Right", 	Edit:"false",PointCount:"<%=params.get("digit")%>"},
				       			{Header:'현재|공급액',	 Type:"AutoSum",	SaveName:"SUPPLY_AMT",		Width:80, 	Align:"Right", 	Edit:"True", PointCount:"<%=params.get("digit")%>"}, 
				    			{Header:'현재|과세',		 Type:"AutoSum",	SaveName:"SUPPLY_TAMT",		Width:80, 	Align:"Right", 	Edit:"True",PointCount:"<%=params.get("digit")%>"},
				       			{Header:'현재|면세',		 Type:"AutoSum",	SaveName:"SUPPLY_NAMT",		Width:80, 	Align:"Right", 	Edit:"false",PointCount:"<%=params.get("digit")%>"},
				       			{Header:'기성누계|과세',	 Type:"AutoSum",	SaveName:"FUND_TAMT",		Width:80, 	Align:"Right", 	Edit:"false",PointCount:"<%=params.get("digit")%>"},
				       			{Header:'기성누계|면세',	 Type:"AutoSum",	SaveName:"FUND_NAMT",		Width:80, 	Align:"Right", 	Edit:"false",PointCount:"<%=params.get("digit")%>"},
				       			{Header:'MATERIAL_CD',	 Type:"Text", 	    SaveName:"MATERIAL_CD", 	Width:50, 	Align:"Center",	Hidden:"true"},
				       			{Header:'과세비율',		 Type:"Text",	    SaveName:"SUPP_RATE",	    Width:80, 	Align:"Right", 	Hidden:"true" }  
				    			
			];
		}
		init_sheet.HeaderMode = {Sort:1};
		IBS_InitSheet(sheetName, init_sheet);
		sheetName.SetDataAutoTrim(0);
		setGridSize();
	}
 
 	function setGridSize() {
		sheet1.FitColWidth();
	}

    function sheet1_OnSearchEnd(code, message) {
	    if(code == 0) {  
	    	setGridSize();	    	
		} else {
			//조회 에러 수행
			alert(message);
		}
	}

	function sheet1_OnSaveEnd(code, msg)
	{
		var event = $("#fevent").val();
		if(event == "DATA_SAVE"){
			alert("저장 완료되었습니다."); 			
		}
		
	}

	/**
	 *	IBSheet Change Event
	 */
	 function sheet1_OnChange(Row, Col, Value, OldValue, RaiseFlag) {
		if (Col == 5 || Col == 8) {  //과세 수정
			sheet1.SetCellValue(Row,"SUPPLY_NAMT", sheet1.GetCellValue(Row,"SUPPLY_AMT") - sheet1.GetCellValue(Row,"SUPPLY_TAMT"));
			sheet1.SetCellValue(Row,"SUPPLY_AMT", sheet1.GetCellValue(Row,"SUPPLY_NAMT") + sheet1.GetCellValue(Row,"SUPPLY_TAMT"));
		}else if (Col == 4 || Col == 7){ //공급가액 수정 
			sheet1.SetCellValue(Row,"SUPPLY_TAMT", (sheet1.GetCellValue(Row,"SUPPLY_AMT") * sheet1.GetCellValue(Row,"SUPP_RATE")) );
			sheet1.SetCellValue(Row,"SUPPLY_NAMT", sheet1.GetCellValue(Row,"SUPPLY_AMT") - (sheet1.GetCellValue(Row,"SUPPLY_AMT") * sheet1.GetCellValue(Row,"SUPP_RATE")));		 	
		}
		//}else if(Col == 6){ //면세수정 
		//	sheet1.SetCellValue(Row,"SUPPLY_TAMT", sheet1.GetCellValue(Row,"SUPPLY_AMT") - sheet1.GetCellValue(Row,"SUPPLY_NAMT"));
		//} SUP_RATE = 계약과세/계약공급가*/	    
	}
	 
	 
    function sheet1_OnSmartResize(Width, Height) { 
		sheet1.FitColWidth();
		//autoResize();
	} 
 	function autoResize(){
		var t1_height = document.getElementById("search-table").scrollHeight + document.getElementById("title_pop").scrollHeight + 20;
		var iframeHeight= document.body.scrollHeight - t1_height;   
		var iframeWidth= document.body.scrollWidth;   

		sheet1.SetSheetHeight(iframeHeight); 
	} 
 	
 	function doResult(resultObj) {
		try {
			if( resultObj.getRequestEvent() == "DATA_SAVE" ) {
				if( resultObj.getSuccess() ) {
					alert("저장 완료되었습니다."); 
				}
				else {
					dlgErrorMsg(resultObj);
				}
				document.form1.isSubmit = false;
			}
		}
		catch(errorObject) {
	        showErrorDlg("doResult()", errorObject);
		}
	}

</SCRIPT>

<modular:search id="form1">
	<modular:key id="fevent" />
</modular:search>

<FORM id="form1" name="form1" method="get" modular:type="search">
	<INPUT type="hidden" name="fevent" id="fevent" value="">
	<INPUT	type="hidden" name="SUB_CONT_MOD_ID" value="<%=params.get("SUB_CONT_MOD_ID")%>" /> 
	<INPUT	type="hidden" name="MOD_NO" value="<%=params.get("MOD_NO")%>" /> 
	<INPUT	type="hidden" name="pop_win_id" value="<%=params.get("pop_win_id")%>" />
	<INPUT type="hidden" name="digit" value="<%=params.get("digit") %>">
	<INPUT type="hidden" name="edit_yn" value="<%=params.get("edit_yn") %>">
	<table id="search-table" class="search-form-table">
		<colgroup>
			<col width="*" />
		</colgroup>
		<tr>
			<td class="button" style="text-align:right; padding-right:10px;">
			    <span class="btnSearch center" id="btnSave" style="display:;"><a href="javascript:void(0);">저장</a></span>	 
				<span class="btnSearch center" id="btnSearch" style="display:;"><a href="javascript:void(0);">검색</a></span>	 
			</td>
		</tr>
	</table>
	<!-- 검색조건 종료 -->
	<div id="sheet1_div" style="padding-top:5px; height: auto; min-height: 450px; overflow: auto;">
		<script>createIBSheet('sheet1','100%', '450px');</script>
	</div>
	
</form>