<%--
  - FileName   : PayItemPopup.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 + "/payment/outside/PayItemPopup_sheet.screen"; 
        	Modular.view.IBSheet7.doSearch(sheet1, url, jQuery("#form1"), 30, "");
		});		
		jQuery("#btnSave").click(function() {
			for (var i = 2; i <= sheet1.RowCount()+1; i ++){  
				if ( sheet1.GetCellValue(i, "RE_TAMT") < 0  ) {
	            	alert("입력한 금회 기성 과세액이 계약 과세금 보다 큽니다.");
	            	return;
	            }    
				if ( sheet1.GetCellValue(i, "RE_NAMT") < 0  ) {
	            	alert("입력한 금회 기성 면세액이 계약 면세금 보다 큽니다.");
	            	return;
	            }  
	        }  
			
			if (confirm("저장 하시겠습니까?")){
				var f = document.form1;
				$("#fevent").val("DATA_SAVE");
				var url = Modular.model.PageContext.CONTEXT_ROOT + "PayItemPopup_form.screen";
				var result = Modular.view.IBSheet7.doSave(sheet1, url, $("#form1"), false);
			}	
		});	 
		jQuery("#btnSearch").click(); 
	}
    
    function gridInit(sheetName){//CalcLogic:"|CUR_TAMT|*|RATE|"   CalcLogic:"|SUPPLY_TAMT|-|BEF_TAMT|-|CUR_TAMT|",  CalcLogic:"|SUPPLY_NAMT|-|BEF_NAMT|-|CUR_TAMT|*|RATE|",
		var init_sheet = {};
		init_sheet.Cfg = {SearchMode:2, Page:5000, MergeSheet:msPrevColumnMerge + msHeaderOnly}; 
		init_sheet.Cols = [
		                    {Header:'Status',	        Type:"Status", 	SaveName:"STATUS",			Width:10, 	Align:"Center"	,	Hidden:"true"}, 
			       			{Header:'공종코드',		    Type:"Text",	SaveName:"CD_CSTR",		    Width:80, 	Align:"Left", 	Edit:"false",	Hidden:"true"},
			       			{Header:'내역코드',		    Type:"Text",	SaveName:"DL_CSTR",		    Width:100, 	Align:"Left", 	Edit:"false",	Hidden:"true"},
			    			{Header:'내역명|내역명',      Type:"Text",  	SaveName:"ITEM_NAME",		Width:200, 	Align:"Left", 	Edit:"false"},
			    			{Header:'전회|과세',		    Type:"AutoSum",	SaveName:"BEF_TAMT",		Width:80, 	Align:"Right", 	Edit:"false", PointCount:"<%=params.get("digit")%>"},
			       			{Header:'전회|면세',		    Type:"AutoSum",	SaveName:"BEF_NAMT",		Width:80, 	Align:"Right", 	Edit:"false", PointCount:"<%=params.get("digit")%>"},
			       			{Header:'금회|과세',		    Type:"AutoSum",	SaveName:"CUR_TAMT",		Width:80, 	Align:"Right", 	Edit:"True", PointCount:"<%=params.get("digit")%>"},
			       			{Header:'금회|면세',		    Type:"AutoSum",	SaveName:"CUR_NAMT",		Width:80, 	Align:"Right",  Edit:"True", PointCount:"<%=params.get("digit")%>"},
			       			{Header:'잔여|과세',		    Type:"AutoSum",	SaveName:"RE_TAMT",		    Width:80, 	Align:"Right", 	Edit:"false", PointCount:"<%=params.get("digit")%>"},
			       			{Header:'잔여|면세',		    Type:"AutoSum",	SaveName:"RE_NAMT",		    Width:80, 	Align:"Right", 	Edit:"false", PointCount:"<%=params.get("digit")%>"},
			       			{Header:'ACMP_ID',	        Type:"Text", 	SaveName:"ACMP_ID", 	    Width:50, 	Align:"Center",	Hidden:"true"},
			       			{Header:'SUB_CONT_ID',	    Type:"Text", 	SaveName:"SUB_CONT_ID", 	Width:50, 	Align:"Center",	Hidden:"true"},
			       			{Header:'SUPPLY_TAMT',	    Type:"Text", 	SaveName:"SUPPLY_TAMT",	    Width:50, 	Align:"Center",	Hidden:"true"},
			       			{Header:'SUPPLY_NAMT',	    Type:"Text", 	SaveName:"SUPPLY_NAMT",	    Width:50, 	Align:"Center",	Hidden:"true"},
			       			{Header:'RATE',	            Type:"Text", 	SaveName:"RATE",          	Width:50, 	Align:"Center",	Hidden:"true"},
			       			{Header:'MATERIAL_CD',	    Type:"Text", 	SaveName:"MATERIAL_CD", 	Width:50, 	Align:"Center",	Hidden:"true"},
			       			{Header:'CD_SITE',   	    Type:"Text", 	SaveName:"CD_SITE", 	    Width:50, 	Align:"Center",	Hidden:"true"}
		];
		
		init_sheet.HeaderMode = {Sort:1};
		IBS_InitSheet(sheetName, init_sheet);
		sheetName.SetDataAutoTrim(0);
		setGridSize();
	}
 
 	function setGridSize() {
		sheet1.FitColWidth();
	}

 	function doResult(resultObj) {
		try {
			if( resultObj.getRequestEvent() == "DATA_SAVE" ) {
				if( resultObj.getSuccess() ) {
					
				}
				else {
					dlgErrorMsg(resultObj);
				}
				document.form1.isSubmit = false;
			}
		}
		catch(errorObject) {
	        showErrorDlg("doResult()", errorObject);
		}
	}
 	
    function sheet1_OnSearchEnd(code, message) {
	    if(code == 0) {
	    	for (var i = 2; i <= sheet1.RowCount()+1; i ++){  
	    		sheet1.SetCellValue(i, "RE_TAMT", sheet1.GetCellValue(i, "SUPPLY_TAMT")- sheet1.GetCellValue(i, "BEF_TAMT")- sheet1.GetCellValue(i, "CUR_TAMT"));  
				sheet1.SetCellValue(i, "RE_NAMT", sheet1.GetCellValue(i, "SUPPLY_NAMT")- sheet1.GetCellValue(i, "BEF_NAMT")- sheet1.GetCellValue(i, "CUR_NAMT"));  
				
				sheet1.SetCellEditable(i, 7, 1);   //2023.07.21 면세금 입력 권한 제한 해제 
				
				//면세만 금액이 있을경우 수정할수 있게 오픈 
				/* if (sheet1.GetCellValue(i, "SUPPLY_TAMT") > 0 ) {
					sheet1.SetCellEditable(i, 7, 0);   //과세금액이 있으면 수정금지 
				} */
				
				//외주 담당자는 모든경우 면세수정. 변경계약, 준공기성 처리시 비율이 안맞는 경우 발생함.  
				<%-- <% if (user.get("RIGHT_NAME").equals("ADMIN NEW") || user.get("RIGHT_NAME").equals("OUTSIDE")) { %>
					sheet1.SetCellEditable(i, 7, 1); 
				<%}%> --%>
				
	        }   
	    	setGridSize();	    	
		} else {
			//조회 에러 수행
			alert(message);
		}
	}

	function sheet1_OnSaveEnd(code, msg)
	{
		var row = 0;
		var rown = 0 ;
		for (var i = 2; i <= sheet1.RowCount()+1; i ++){  
           	row = row + sheet1.GetCellValue(i, "CUR_TAMT") ;
           	rown = rown + sheet1.GetCellValue(i, "CUR_NAMT") ;    
        }    
		parent.RtnClose(row, rown);
	}

	/*IBSheet Change Event */
	function sheet1_OnChange(Row, Col, Value, OldValue, RaiseFlag) { 
		if (Col == 6){  //과세 
			/* 잔여면세금 로직 추가 20230703 */
			var amt = Value * sheet1.GetCellValue(Row, "RATE");	//금회 면세 자동등록(과세금 * 자동면세금비율)
			var re_namt = sheet1.GetCellValue(Row, "RE_NAMT");	//잔여 면세
			var cal_amt = Math.abs(re_namt - amt);				//잔여 면세금 - 금회 면세금(자동등록)
			
			if(re_namt == 0)		amt = re_namt;				//잔여 면세금이 없으면 0원 자동등록
			else if(cal_amt < 1)	amt = re_namt;				//면세금 계산 차이가 1보다 작으면 잔여 면세금으로 자동등록(이 2조건 외에는 amt(자동면세금비율 금액)로 면세금 자동등록)
			
			sheet1.SetCellValue(Row, "CUR_NAMT", amt);  	
			sheet1.SetCellValue(Row, "RE_TAMT", sheet1.GetCellValue(Row, "SUPPLY_TAMT")- sheet1.GetCellValue(Row, "BEF_TAMT") - Value);  
			sheet1.SetCellValue(Row, "RE_NAMT", sheet1.GetCellValue(Row, "SUPPLY_NAMT")- sheet1.GetCellValue(Row, "BEF_NAMT") - amt);  
	    }
		else if (Col == 7){  //면세 
			sheet1.SetCellValue(Row, "RE_NAMT", sheet1.GetCellValue(Row, "SUPPLY_NAMT")- sheet1.GetCellValue(Row, "BEF_NAMT") - Value);  
	    }
		/*else if (Col == 7 && sheet1.GetCellValue(Row, "SUPPLY_TAMT")== 0){  //면세
            var supp_amt = sheet1.GetCellValue(Row, "SUPPLY_NAMT") - sheet1.GetCellValue(Row, "BEF_NAMT") ;
			sheet1.SetCellValue(Row, "CUR_TAMT", 0);  
			sheet1.SetCellValue(Row, "RE_TAMT", 0);
			sheet1.SetCellValue(Row, "RE_NAMT",supp_amt - Value );
		}*/ 
	}
    
    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); 
	} 

</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="ACMP_ID" value="<%=params.get("ACMP_ID")%>" /> 
	<INPUT	type="hidden" name="SUB_CONT_ID" value="<%=params.get("SUB_CONT_ID")%>" /> 
	<INPUT	type="hidden" name="pop_win_id" value="<%=params.get("pop_win_id")%>" />
	<INPUT	type="hidden" name="EDIT_YN" value="<%=params.get("EDIT_YN")%>" />
	<INPUT type="hidden" name="digit" value="<%=params.get("digit") %>">
	<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:none;"><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>