<%--
  - FileName   : ContBudget.jsp
  - Author(s)  : lee mi kyoung
  - Date       : 2017.1.25
  -
  - Description: 구매외주 예산등록 
  --%>
  


<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="com.udapsoft.waf.system.HandlerStorage" %>
<%@ page import="com.udapsoft.waf.common.ui.*" %>
<%@ page import="kr.co.hsnc.common.util.*"%>
<%@ page import="kr.co.hsnc.common.sql.*" %>
<%@ page import = "blowfishj.*"%>   
<%@ page import = "java.text.SimpleDateFormat"%>    
<%@ page import="com.udapsoft.waf.common.util.DateUtil"%>  
<%@ page import="kr.co.udapsoft.common.code.CommCodeManager" %>                                                                                                                                                                                                        
<%@ page import = "java.util.Date"%>                                                                                                                                                                                                                         
<%@ page import = "java.util.*"%>  
<%@ 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 params = storage.getParams();
    ValueObject user = storage.getUser();
    String event = storage.getEvent();
    
%>

<script type="text/javascript" src="/js/creditEvaluationPop.js"></script>
<script language="javascript">
	/*예약함수*/
	var sheet1 = new ibsheetObject();
	var sheet1Row;
	var sheet1PageNo = 1;
	var sheet2 = new ibsheetObject();
	var sheet2Row;
	var sheet2PageNo = 1;
	var pageLastRowIndex = 1;
	var validationResult = false;
	var filterShow = false;
	
	
	$(document).on("keyup", "input:text", function(e) {
	    if($(this).attr("numberOnly") == "true")
        {
	        $(this).val( $(this).val().replace(/[^0-9]/gi,"") );
        }
	    
	    if (e.keyCode == 13)
        {
	        $.searchList();
        }
	    
	});
	
	
	$.gridInit = function(sheetName)
	{
	 
		var init_sheet = {};
		init_sheet.Cfg = {SearchMode:2, MergeSheet:msHeaderOnly, Page:100,ChildPage:10};
		if(sheetName.id == "sheet1") {
			init_sheet.Cols = [
	                        {Header:"레벨",	 		     	Type:"Text", 			TreeCol:1,				LevelSaveName:"GROUP_SEQ",	 	SaveName:"GROUP_SEQ"	,	Width:50},
			       			{Header:'Del',					Type:"DelCheck",		SaveName:"DEL_CHECK", 				Width:50, 		Align:"Center" },
			    			{Header:'Status',				Type:"Status", 			SaveName:"STATUS", 					Width:0, 		Align:"Center", 	Edit:"true", Hidden:"True"},
			    			{Header:'ORD',				    Type:"Text", 			SaveName:"ORD", 			        Width:120, 		Align:"Left", 	    Edit:"true", Hidden:"True"},
			    			{Header:'DEPT_CD',				Type:"Text", 			SaveName:"DEPT_CD", 			    Width:120, 		Align:"Left", 	    Edit:"true", Hidden:"True"},
			    			{Header:'PAY_ITEM',			    Type:"Text", 	    	SaveName:"PAY_ITEM", 			    Width:40, 		Align:"Left", 		Edit:"true", Hidden:"True"},
			    			{Header:'구분',   			    Type:"Combo", 		    SaveName:"KIND_GB",			        Width:40, 		Align:"Left", 		Edit:"false",ComboText:"구매|외주", ComboCode:"P|O"},
			    			{Header:'PO NO',			    Type:"Text", 		    SaveName:"MR_NO",			        Width:60, 		Align:"Left", 		Edit:"true"},
			    			{Header:'품목',       			Type:"Combo", 		    SaveName:"PAY_ITEM_SEQ",			Width:120, 		Align:"Left", 		Edit:"true",ComboText:"", ComboCode:""},
			    			{Header:'MR_ID',				Type:"Text", 			SaveName:"MR_ID",  			        Width:120, 		Align:"Left", 	    Edit:"true", Hidden:"True"},
			    			{Header:'ITEM_NAME', 	        Type:"Text", 		    SaveName:"ITEM_NAME",			    Width:60, 		Align:"Left", 		Edit:"true", Hidden:"True"},
			    			{Header:'실행예산',			    Type:"Int",	            SaveName:"EXEAMT",			        Width:60, 		Align:"Right", 		Edit:"false",Format:"#,##0"}, 
			    			{Header:'집행금액',			    Type:"Int", 		    SaveName:"CONAMT",			        Width:60, 		Align:"Right", 		Edit:"true", Format:"#,##0"}, 
			    			{Header:'계약체결일',			    Type:"Text", 		    SaveName:"CONDATE",			        Width:60, 		Align:"Center", 	Edit:"true"},
			    			{Header:'C/O',			        Type:"Int", 		    SaveName:"COAMT",			        Width:60, 		Align:"Right", 		Edit:"false", Format:"#,##0"},
			    			{Header:'예실대비',			    Type:"Float", 		    SaveName:"RATE",			        Width:60, 		Align:"Right", 		Edit:"false", Format:"#,##0.00"}, 
			    			{Header:'절감액',			        Type:"Int", 		    SaveName:"SAVEAMT",			        Width:60, 		Align:"Right", 		Edit:"false", Format:"#,##0"},
			    			{Header:'예실대비(C/O)',			Type:"Float", 		    SaveName:"CORATE",			        Width:60, 		Align:"Right", 		Edit:"false", Format:"#,##0.00"},
			    			{Header:'절감액(C/O)',			Type:"Int", 		    SaveName:"COSAVEAMT",			    Width:60, 		Align:"Right", 		Edit:"false", Format:"#,##0"}, 
			    			{Header:'통화',			        Type:"Text", 		    SaveName:"CURRENCY_GB",			    Width:60, 		Align:"Center", 	Edit:"true"},
			    			{Header:'업체',			        Type:"Text", 		    SaveName:"VENDOR_NM",			    Width:60, 		Align:"Left", 		Edit:"true"},
			    			{Header:'계약명',			        Type:"Text", 		    SaveName:"TITLE",       		    Width:60, 		Align:"Left", 		Edit:"true"},
			    			{Header:'REMARK1',			    Type:"Text", 		    SaveName:"REMARK1",			        Width:60, 		Align:"Left", 		Edit:"true", Hidden:"True"},
			    			{Header:'수정일자',				Type:"Text", 		    SaveName:"UPD_DATE", 			    Width:60, 		Align:"Center", 	Edit:"false"}
			    			
			];
		}else{
			init_sheet.Cols = [
				       			{Header:'Del',			        Type:"DelCheck",		SaveName:"DEL_CHECK", 				Width:40, 		Align:"Center",		Hidden:"False",ColMerge:"False"},
				    			{Header:'Status',		        Type:"Status", 		    SaveName:"STATUS", 				    Width:0, 		Align:"Center", 	Edit:"false", Hidden:"True"},
				    			{Header:'DEPT_CD',				Type:"Text", 			SaveName:"DEPT_CD", 			    Width:120, 		Align:"Left", 	    Edit:"true", Hidden:"True"},
				    			{Header:'SEQ',			        Type:"Text", 		    SaveName:"SEQ",		  	            Width:60, 		Align:"Left", 		Edit:"true", Hidden:"True"},
				    			{Header:'PO NO',			    Type:"Text", 		    SaveName:"MR_NO",			        Width:60, 		Align:"Left", 		Edit:"true"},
				    			{Header:'품목',       			Type:"Combo", 		    SaveName:"PAY_ITEM_SEQ",	        Width:120, 		Align:"Left", 		Edit:"true",ComboText:"", ComboCode:""},
				    			{Header:'실행예산',			    Type:"AutoSum", 		SaveName:"EXEAMT",			        Width:60, 		Align:"Right", 		Edit:"false", Format:"#,##0"}, 
				    			{Header:'집행금액',			    Type:"AutoSum", 		SaveName:"CONAMT",			        Width:60, 		Align:"Right", 		Edit:"false", Format:"#,##0"}, 
				    			{Header:'C/O',			        Type:"AutoSum", 		SaveName:"COAMT",			        Width:60, 		Align:"Right", 		Edit:"true", Format:"#,##0"},
				    			{Header:'C/O포함예산',		    Type:"AutoSum", 		SaveName:"RATE",		            Width:60, 		Align:"Right", 		Edit:"false", CalcLogic:"|EXEAMT|+|COAMT|"}, 
				    			{Header:'예실대비(C/O)',			Type:"AutoAvg", 		SaveName:"CORATE",		            Width:60, 		Align:"Right", 		Edit:"false", CalcLogic:"|CONAMT| / (|EXEAMT|+|COAMT|) * 100", Format:"#,##0.00"	},
				    			{Header:'상태',			        Type:"Combo", 		    SaveName:"STATUS_CD",			    Width:60, 		Align:"Center",		Edit:"true", ComboText:"승인|미승인", ComboCode:"1|0"},
				    			{Header:'비고',			        Type:"Text", 		    SaveName:"REMARK1",			        Width:100, 		Align:"Left", 		Edit:"true" }
				    			];
		}
		init_sheet.HeaderMode = {Sort:1};
		IBS_InitSheet(sheetName, init_sheet);
		sheetName.SetDataAutoTrim(0); 

		sheet1.SetCountPosition(0);
		sheet1.FitColWidth();
		setGridSize();	 		 
	}
	
	
	$(function(){
		
		$("#btn_search").click(function(){
			if ($("#p_dept_cd").val() == "") {
				alert("현장을 선택하십시요.");
				return ;
			}
		    $.searchList();
		});
		
		$("#btn_add").click(function(){
			var f = document.form1;
			if ($("#p_dept_cd").val() == "") {
				alert("현장을 선택하십시요.");
				return ;
			}
			sheet1.DataAutoTrim = false;
		
			if (sheet1.LastRow() == 0){
				var rowIdx = sheet1.DataInsert(0);
				sheet1.SetRowEditable(rowIdx, true);
			}
			else{
				var rowIdx = sheet1.DataCopy();
				
				sheet1.SetCellValue(rowIdx, "ITEM_NAME", '');
				sheet1.SetCellValue(rowIdx, "UPD_DATE", '');
				sheet1.SetRowEditable(rowIdx, true);
			}
			
		});
		
		
		$("#btn_save").click(function(){
			var rowcnt = sheet1.RowCount();
	 
    	    for(var r=1; r < rowcnt+1; r++)
			{
    	    	if(sheet1.GetCellValue(r,"DEL_CHECK") == 0 && sheet1.GetCellValue(r,"CONAMT") == "0" && sheet1.GetCellValue(r,"VENDOR_NM") != "" && (sheet1.GetCellValue(r,"ORD") == "2" || sheet1.GetCellValue(r,"ORD") == "6")  ){
					alert(sheet1.GetCellValue(r,"VENDOR_NM") + " 집행금액이 0입니다." );
					return ;
				}
			}
			jQuery("#fevent").val("BUDGET_SAVE");
			var url = Modular.model.PageContext.CONTEXT_ROOT + "/cont/purchase/ContBudgetMR_sheet.screen";
			var result = Modular.view.IBSheet7.doSave(sheet1, url, jQuery("#form1"), false);		
		});
		
		$("#btn_excel").click(function(){
	        var params = {FileName:"예산현황.xls",SheetName:"예산현황",Merge:1,SheetDesign:1, HiddenColumn:1};
	        sheet1.Down2Excel (params);
	    });
		
		$("#btn_file").click(function(){
			var f = document.form1;
			var v_dept = $("#p_dept_cd").val();
			if (v_dept == "") {
				alert("현장을 선택하십시요.");
				return ;
			}
			else if(v_dept != ''){
				var upLoadFlg = "Y";
				viewFileListNew1('CONT_BCS_'+v_dept, v_dept, upLoadFlg, "cont/bcs/"+v_dept) ; 
			} 
	    });
		
		$("#btn_AddCo").click(function(){
			var f = document.form1;
			if ($("#p_dept_cd").val() == "") {
				alert("현장을 선택하십시요.");
				return ;
			}
			sheet2.DataAutoTrim = false;		
			
			var rowIdx = sheet2.DataInsert(0);
				
			var data_lc = document.form1.GET_ITEM_CO.value;
	    	var data_lc_value = document.form1.GET_ITEM_NAME_CO.value;
	    	var info = {Header:'PAY_ITEM_SEQ',  Type:"Combo", SaveName:"PAY_ITEM_SEQ", Width:100,  Align:"Left", Edit:"true" , ComboText:data_lc, ComboCode:data_lc_value, ColMerge:"False"};               
	    	sheet2.InitCellProperty(rowIdx, "PAY_ITEM_SEQ", info); 
			
		});
		
		$("#btn_SaveCo").click(function(){
			jQuery("#fevent").val("BUDGET_SAVE_CO");
			var url = Modular.model.PageContext.CONTEXT_ROOT + "/cont/purchase/ContBudgetMR_sheet.screen";
			var result = Modular.view.IBSheet7.doSave(sheet2, url, jQuery("#form1"), false);		
		});
		
		$.searchList = function()
		{
			var f= document.form1;
			$("#fevent").val("GET_ITEM"); 
			submitXmlRequest("/cont/purchase/ContBudgetMR_form.screen", "GET_ITEM", f, "ROWSET");
		} 
		 
	});
	
	
	$(document).ready(function(){
		
		<%if(user.get("right_name").equals("OUTSIDE")){%>
			$("#P_KIND_GB").val("O");
		<%}else if(user.get("right_name").equals("PURCHASE")){%>
			$("#P_KIND_GB").val("P");
		<%}%>
	
	    $.gridInit(sheet1);
	    $.gridInit(sheet2);
	    
		autoResize(); 
	});
	 
	function setGridSize() { 
		sheet1.FitColWidth();
		sheet2.FitColWidth();
	}	 
	
	function autoResize(){ 		 
		var t1_height = document.getElementById("search-table").scrollHeight + document.getElementById("title").scrollHeight + 350;
		var iframeHeight= document.body.scrollHeight - t1_height;   
		var iframeWidth= document.body.scrollWidth;   
		
		sheet1.SetSheetHeight(iframeHeight); 	
		
		sheet2.SetSheetWidth(sheet1.GetSheetWidth()); 	
		 
	}		
	
	function sheet1_OnSmartResize(Width, Height) { 
		sheet1.FitColWidth();
		autoResize();
	}
	
	function sheet2_OnSmartResize(Width, Height) { 
		sheet2.FitColWidth();
		autoResize();
	}
	
	function doResult(resultObj) {
		try {
			if ( resultObj.getRequestEvent() == "GET_ITEM" ) {
				var rowSet	= resultObj.getRowSet();
				var getData = "";
				var getDataValue = "";
				var getDataCo = "";
				var getDataValueCo = "";
				for ( var i = 0; i < rowSet.getRowSize(); i++ ){
					getData      = getData + rowSet.getColValue( i, 1 ) + "|";
					getDataValue = getDataValue + rowSet.getColValue( i, 0 ) + "|";
					if (rowSet.getColValue( i, 2 ) != 0){
						getDataCo      = getDataCo + rowSet.getColValue( i, 1 ) + "|";
						getDataValueCo = getDataValueCo + rowSet.getColValue( i, 2 ) + "|";
					}
				}
		 
				$("#GET_ITEM").val(getData);
				$("#GET_ITEM_NAME").val(getDataValue);
				
				$("#GET_ITEM_CO").val(getDataCo);
				$("#GET_ITEM_NAME_CO").val(getDataValueCo);
				document.form1.isSubmit = false;
				
				jQuery("#fevent").val("BUDGET_CONTROL");
				var url = Modular.model.PageContext.CONTEXT_ROOT + "/cont/purchase/ContBudgetMR_sheet.screen";
				Modular.view.IBSheet7.doSearch(sheet1, url, jQuery("#form1"), 10, "");
			} 
		}
		catch(errorObject) {
			showErrorDlg("doResult()", errorObject);
		}
	}
	function sheet1_OnSearchEnd(code, msg) {
		if(code == 0){
    		var data_lc = document.form1.GET_ITEM.value;
    		var data_lc_value = document.form1.GET_ITEM_NAME.value;
    		var info = {Header:'ITEM_NAME',  Type:"Combo", SaveName:"ITEM_NAME", Width:100,  Align:"Left", Edit:"true" , ComboText:data_lc, ComboCode:data_lc_value, ColMerge:"False"};               
    		
    		var rowcnt = sheet1.RowCount();
        	if(rowcnt > 0){
    			for(var r=1; r < rowcnt+1; r++)
    			{
    				if(sheet1.GetCellValue(r,"MR_ID") == "0" && sheet1.GetCellValue(r,"ORD") != "2"   ){
    					sheet1.SetRowBackColor(r,"#DBE5F1");
    					sheet1.SetRowEditable(r, false);
    					
    					if (sheet1.GetCellValue(r,"ORD") == "1" ){
    						sheet1.SetRowBackColor(r,"#FFFFFF");
    						sheet1.SetRangeFontBold(r, 0, r, 22, 1);   						
    					}else if ( sheet1.GetCellValue(r,"ORD") == "5"){
    						sheet1.SetRangeFontBold(r, 0, r, 22, 1);   		
    					}
    				}
    				else{
    					if (sheet1.GetCellValue(r,"MR_ID") == "0"){
    						sheet1.SetRowEditable(r, false);
    					}
    					sheet1.InitCellProperty(r, "PAY_ITEM_SEQ", info); 
    				}    				
    			}
    		}
			sheet1.ShowTreeLevel(0, 1);
			
			jQuery("#fevent").val("BUDGET_CONTROL_CO");
			var url = Modular.model.PageContext.CONTEXT_ROOT + "/cont/purchase/ContBudgetMR_sheet.screen";
			Modular.view.IBSheet7.doSearch(sheet2, url, jQuery("#form1"), 10, "");
		}else{
			alert(message);
		}
	}
	
	function sheet2_OnSearchEnd(code, msg) {
		if(code == 0){
					
			var data_lc = document.form1.GET_ITEM_CO.value;
    		var data_lc_value = document.form1.GET_ITEM_NAME_CO.value;
    		var info = {Header:'PAY_ITEM_SEQ',  Type:"Combo", SaveName:"PAY_ITEM_SEQ", Width:100,  Align:"Left", Edit:"true" , ComboText:data_lc, ComboCode:data_lc_value, ColMerge:"False"};               
    		
    		var rowcnt = sheet2.RowCount();
        	if(rowcnt > 0){
    			for(var r=1; r < rowcnt+1; r++)
    			{
    				sheet2.InitCellProperty(r, "PAY_ITEM_SEQ", info); 
    			}
    		}
		}else{
			alert(message);
		}
	}
	
	function sheet1_OnSaveEnd(code, msg) {
	    if(code >= 0) {
	    	alert("저장되었습니다.");
	    	$.searchList();
	    } else {
	    	alert("저장 실패");
	    }	
	}
	
	function sheet2_OnSaveEnd(code, msg) {
	    if(code >= 0) {
	    	alert("저장되었습니다.");
	    	$.searchList();
	    } else {
	    	alert("저장 실패");
	    }	
	}
	
	function viewFileListNew1(fileGB, id, isModify, isDrectory, iframeID, user_filename) {
		var win_id = "_viewFileList";
		var link = "/common/file/FileListNew.screen";
		    link += "?pop_win_id="+win_id;
		    link += "&extFunction=viewFileListClose1";
		    link += "&file_gb=" + fileGB;
		    link += "&id=" + id;
		    
		    if( iframeID != null )
		    	link += "&iframeID=" + iframeID;
		    
			if( isModify != null )
				link += "&is_modify=" + isModify;
			if( isDrectory != null )
				link += "&sys_gb=" + isDrectory;	    
			if( user_filename != null )
				link += "&user_filename=" + user_filename;	 
		
		fLayerPop(win_id, link, 750,430, '', '', 'NO', '200');
	}
	
	document.body.onresize = addEvent( document.body.onresize, "autoResize()"); 
</script>


<modular:search id="form1">
	<modular:key id="fevent" />
</modular:search>
<form id="form1" name="form1" method="post" modular:type="search">
<input type="hidden" name="fevent" id="fevent" value="">
<input type="hidden" name="materialgroup" id="materialgroup" value=""/>
<input type="hidden" name="work_sheet" value="">
<input type="hidden" name="GET_ITEM" value="">
<input type="hidden" name="GET_ITEM_NAME" value="">
<input type="hidden" name="GET_ITEM_CO" value="">
<input type="hidden" name="GET_ITEM_NAME_CO" value="">

<div align="left" id="title">
	<ul>
    	<li>Budget Control Sheet</li> 
   	</ul>
</div> 
<!-- 검색조건 시작 -->
<table id="search-table" class="search-form-table">
	<colgroup>
		<col width="100" />
		<col width="400" />
		<col width="100" />
		<col width="*" />
		<col width="300" />
	</colgroup>
  <tr height="25"> 
  	<th>현장</th>
   	<td>
  		<%=InputText.getSiteCodeNew("p_dept_cd", "p_dept_nm", "", user, params.get("p_dept_cd"), params.get("p_dept_nm"))%>
	</td>
	<th>구분</th>
   	<td>
  		<%=CommCodeManager.getSelectCodeList("JOB_DIV","P_KIND_GB","height=18;width:80px;","<option value=''>--전체--</option>",params.get("P_KIND_GB")) %>
	</td>
  	<td class="button" >
		<span class="btnSearch center" id="btn_search"><a href="javascript:void(0);">검색</a></span>
		<span class="btnSearch center" id="btn_add"><a href="javascript:void(0);">추가</a></span>
		<span class="btnSearch center" id="btn_save"><a href="javascript:void(0);">저장</a></span>
		<span class="btnSearch center"><a href="javascript:void(0);" id="btn_excel">엑셀</a></span>
		<span class="btnSearch center" id="btn_file"><a href="javascript:void(0);" >첨부파일</a></span>
	</td>	
  </tr>   
</table>
</form>
<img src="/img/com/blank.gif" height="5px" />
   <div align="right" id="bugase">
    	<ul>
        단위(원)
    	</ul>
  	</div>
<div id="sheet1_div" style="padding-top:5px; height: auto; min-height: 315px; overflow: auto;">
	<script>createIBSheet('sheet1','100%', '300px');</script>
</div>

 
<div class="application-info magT15">
	<div class="application-sub-title">
		<span class="title">C/O합산시 추정 절감율</span>
	</div>
	<div style="text-align:right;">
		   <span class="btnSearch center"><a href="javascript:void(0);" id="btn_AddCo">추가</a></span>  
		   <span class="btnSearch center"><a href="javascript:void(0);" id="btn_SaveCo">저장</a></span> 		   
	</div>
</div>
<div id="sheet2_div" style="padding-top:5px; height: auto; min-height: 200px; overflow: auto;">
	<script>createIBSheet('sheet2','100%', '200px');</script>
</div>




