<%--
  - Author(s)  : Hyung min Son
  - Date       : 2014.07.25
  - Description: 결격 사유 팝업
  --%>
  
<%@page import="monfox.toolkit.snmp.agent.modules.SnmpV2Mib.SysOREntry"%>
<%@ 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.ui.*" %>
<%@ page import="com.udapsoft.waf.common.*" %>
<%@ page import="kr.co.udapsoft.common.*" %>
<%@ page import="com.udapsoft.waf.common.util.StringUtils" %>
<%@ page import="com.udapsoft.waf.common.ui.HTMLMaker" %>
<%@ page import="com.udapsoft.waf.system.HandlerStorage" %>
<%@ page import="kr.co.udapsoft.ebid.buyer.common.file.FileManager"%>
<%@ 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();
    ValueObject Order = storage.getValueObject("Order");
    
	String ROW =   (String) storage.get("ROW");
	String VENDOR_CD =   (String) storage.get("VENDOR_CD");
	String BID_NO =   (String) storage.get("BID_NO");
	String BID_DEG =   (String) storage.get("BID_DEG");
	String pop_win_id =   (String) storage.get("pop_win_id");
	String FILE_GB =   (String) storage.get("FILE_GB");
	String DEMANDED_ID =   (String) storage.get("DEMANDED_ID");
	String VENDOR_NO =   (String) storage.get("VENDOR_NO");
	String ESTI_TOT_AMT =   (String) storage.get("ESTI_TOT_AMT");
	String OBC_EMP_YN=   (String) storage.get("OBC_EMP_YN");
    String OBC_PENSION_YN=   (String) storage.get("OBC_PENSION_YN");
    String OBC_HEALTH_YN=   (String) storage.get("OBC_HEALTH_YN");
    String OBC_ELDER_YN=   (String) storage.get("OBC_ELDER_YN");
    String OBC_WORK_YN=   (String) storage.get("OBC_WORK_YN");
    String OBC_RESIGN_YN=   (String) storage.get("OBC_RESIGN_YN");
    String OBC_SAFE_YN=   (String) storage.get("OBC_SAFE_YN");
    String OBC_ENV_YN=   (String) storage.get("OBC_ENV_YN");
    String OBC_SITE_YN=   (String) storage.get("OBC_SITE_YN");
    String OBC_DUTY_YN=   (String) storage.get("OBC_DUTY_YN");
    String OBC_WARR_YN=   (String) storage.get("OBC_WARR_YN");
    String OBC_DUTY_RATE=   (String) storage.get("OBC_DUTY_RATE");
    String PointCount=   (String) storage.get("PointCount");
    ValueObject file = storage.getValueObject(storage.get("FILE_GB"));
%>

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

    function _onReady(){
    	gridInit(sheet1);
		autoResize();
		var info = [{StdCol:"DEPTH1" , SumCols:"7|8|9|10|11",CaptionText:"[소    계]",CaptionCol:3}];
		sheet1.ShowSubSum (info);
		sheet1.SetSumValue(3,"총  공  사  비");
		doSearchSheet();
		var vendorname = parent.sheet1.GetCellValue(Number("<%=ROW%>"),"VENDOR_NAME");
		$("vendorname").after(vendorname);
		$("vendorno").after(vendorNo("<%=VENDOR_NO%>"));
		
		$("#btn_Save").click(function(){
			if(!sheet1.IsDataModified()) {
				alert("저장할 내역이 없습니다.");
			}else{
				$("#fevent").val("SAVE");
				var url = Modular.model.PageContext.CONTEXT_ROOT + "/bid/outside/BidOfflineAmtPop_sheet.screen";
				var result = Modular.view.IBSheet7.doSave(sheet1, url, $("#form1"), false);
				
			}
		});
	}

	function doSearchfrom(){
		$("#fevent").val("VENDOR_INFO_SELECT");
		var f = document.form1;
		submitXmlRequest("/bid/outside/BidOfflineAmtPop_from.screen", "VENDOR_INFO_SELECT", f);
	}
    
	
	function doSearchSheet(){
		$("#fevent").val("VENDOR_SHEET_SELECT");
		var url = Modular.model.PageContext.CONTEXT_ROOT + "/bid/outside/BidOfflineAmtPop_sheet.screen";
		Modular.view.IBSheet7.doSearch(sheet1, url, jQuery("#form1"), 30, "");
	}
    
    function gridInit(sheetName){
		var init_sheet = {};
		init_sheet.Cfg = {SearchMode:0 ,  MergeSheet:msHeaderOnly+msPrevColumnMerge};
		init_sheet.Cols = [
			       			{Header:'Del',					Type:"DelCheck",		SaveName:"DEL_CHECK", 				Width:0, 			Align:"Center",		Hidden:"True"},
			    			{Header:'Status',				Type:"Status", 		    SaveName:"STATUS", 					Width:0, 			Align:"Center"		,		Hidden:"True"},
			    			{Header:'구분',					Type:"Text", 			SaveName:"DEPTH1", 					Width:40, 			Align:"Center",		Edit:false},
			    			{Header:'구분',					Type:"Text", 			SaveName:"ITEM_NAME", 				Width:140, 			Align:"left",	 		Edit:false},
			    			{Header:'No.',					Type:"Seq", 			SaveName:"NO",						Width:0, 			Align:"Center",		Hidden:"True"},
			    			{Header:'MATERIAL_CD',	        Type:"Text", 			SaveName:"MATERIAL_CD", 			Width:0, 			Align:"Center",		Hidden:"True"},
			    			{Header:'PARENT_LEVEL',	        Type:"Text", 			SaveName:"PARENT_LEVEL", 			Width:0, 			Align:"left",			Hidden:"True"},
			    			{Header:'수량/규격',			    Type:"Text", 			SaveName:"ITEMS", 					Width:0, 			Align:"Center", 	Hidden:"True"},
			    			{Header:'재료비',					Type:"AutoSum", 		SaveName:"MAT_COST", 				Width:60, 			Align:"right",		PointCount:<%=PointCount%>},
			    			{Header:'노무비',					Type:"AutoSum", 		SaveName:"LAB_COST", 				Width:60, 			Align:"right",		PointCount:<%=PointCount%>},
			    			{Header:'경비',					Type:"AutoSum", 		SaveName:"UNIT_COST", 				Width:60, 			Align:"right",		PointCount:<%=PointCount%>},
			    			{Header:'합계',					Type:"AutoSum", 		SaveName:"COST_SUM",				Width:60, 			Align:"right",	CalcLogic:"|MAT_COST|+|LAB_COST|+|UNIT_COST|",	PointCount:<%=PointCount%>},
			    			{Header:'산출근거',				Type:"Text",	 		SaveName:"BASIS_CALCU", 			Widtd:280, 			Align:"left",	 Edit:false},
			    			{Header:'비고',					Type:"Text", 			SaveName:"NOTI", 					Width:100, 			Align:"left"},
			    			{Header:'수량',					Type:"Text", 			SaveName:"QTY", 					Width:0, 			Align:"left", 	Hidden:"True"},
			    			{Header:'합계',					Type:"Text", 			SaveName:"ROW_SUM_AMT", 		    Width:0, 			Align:"left", 	Hidden:"True"}
		];

		init_sheet.HeaderMode = {Sort:1};
		IBS_InitSheet(sheetName, init_sheet);
		sheetName.SetDataAutoTrim(0);
		setGridSize();
	}
    
    function sheet1_OnSaveEnd(code, msg)
	{
		var event = $("#fevent").val();
		if(event == "SAVE"){
			if(code>= "0"){
				alert("저장되었습니다.");
				var sum_amt = sheet1.GetCellValue(21,"UNIT_COST");
				returnValue = new Array("<%=ROW%>",sum_amt);
		     	parent.PopClose("<%=pop_win_id%>",returnValue);
			}else{
				alert("에러가 발생하였습니다.");
			}
		}
	}
 
 	function setGridSize() {
		//sheet1.FitColWidth();
	}
 
	/**
	 *	IBSheet Event: Search End(IBSheet 데이터 조회 CallBack)
	 */
	function sheet1_OnSearchEnd(code, message) {
	    if(code == 0) {
	    	var row = sheet1.RowCount();
	    	sheet1.RowDelete(row);
	    	sheet1.RowDelete(row-2);
	    	sheet1.RowDelete(row-4);
	    	sheet1.SetMergeCell(row-3,2,1,2);//타이틀 머지
	    	sheet1.SetMergeCell(row-4,2,1,2);
	    	sheet1.SetMergeCell(row-5,2,1,2); 
    		
	    	row = 0;
	    	for (var i = 2; i <= sheet1.RowCount()+1; i ++){  
	           	if (sheet1.GetCellValue(i, "DEPTH1") =="간접비"){
	           		row = i;
	           		break;
	           	}
	        }    
	    	
	    	var ingise_yn = "N";
	    	for (var i = 2; i <= sheet1.RowCount()+1; i ++){  
	    		if (sheet1.GetCellValue(i, "ITEM_NAME") =="인지세"){
	           		ingise_yn = "Y";
	           	}
	        }
	    	
    		//여기
    		rowEditYN(row,"<%=OBC_EMP_YN%>");
    		rowEditYN(row+1,"<%=OBC_PENSION_YN%>");
    		rowEditYN(row+2,"<%=OBC_HEALTH_YN%>");
    		rowEditYN(row+3,"<%=OBC_ELDER_YN%>");
    		rowEditYN(row+4,"<%=OBC_WORK_YN%>");
    		rowEditYN(row+5,"<%=OBC_RESIGN_YN%>");
    		rowEditYN(row+6,"<%=OBC_SAFE_YN%>");
    		rowEditYN(row+7,"<%=OBC_ENV_YN%>");
    		rowEditYN(row+8,"<%=OBC_SITE_YN%>");
    		rowEditYN(row+9,"<%=OBC_DUTY_YN%>");
    		rowEditYN(row+10,"<%=OBC_WARR_YN%>");//건설기계 수수료   
    		
    		if(ingise_yn=="Y"){
    			rowEditYN(row+11,"Y");//인지세
    			rowEditYN(row+13,"Y");//일반관리비  
	    		rowEditYN(row+14,"Y");//이윤  
	    		rowEditYN(row+15,"Y");//단수정리
    		}else{
    			rowEditYN(row+12,"Y");//일반관리비  
	    		rowEditYN(row+13,"Y");//이윤  
	    		rowEditYN(row+14,"Y");//단수정리
    		}
    		
    		var GrowCount = sheet1.LastRow();
    		for(var	i=1; i < GrowCount+1; i++)
			{
    			if(sheet1.GetCellEditable(i, "UNIT_COST") == "1"){
    				if (row > i){
    					sheet1.SetCellBackColor(i, "MAT_COST", "#ccff99");		
    					sheet1.SetCellBackColor(i, "LAB_COST", "#ccff99");		
    				}
    				sheet1.SetCellBackColor(i, "UNIT_COST", "#ccff99");		
    			}
    			if(sheet1.GetCellEditable(i, "NOTI") == "1"){
    				sheet1.SetCellBackColor(i, "NOTI", "#ccff99");		
    			}
			}
    		setTotAmt();//견적 총금액 표시
    		sheet1.FitColWidth();
		} else {
			//조회 에러 수행
			alert(message);
		}
	}
	 function rowEditYN(Row,YN){
		 if(YN != "Y"){
		 	sheet1.SetCellValue(Row,"NOTI","견적제외");
		 	sheet1.SetRowEditable(Row, false);
		 }else{
			 sheet1.SetCellEditable(Row,"MAT_COST",false);
			 sheet1.SetCellEditable(Row,"LAB_COST",false);
		 }
	 }
	 
	 function setTotAmt(){
		 var GrowCount = sheet1.LastRow();
			var amt = sheet1.GetCellValue(GrowCount,"COST_SUM");
			$("#ESTI_TOT_AMT").val(amt);
			if(amt != ""){
				$("#TOT_AMT").val(amt);
				$("#TOT_AMT").val($("#TOT_AMT").val().split(/(?=(?:\d{3})+(?:\.|$))/g).join(','));
			}
	 }
	
	function sheet1_OnChange(Row, Col, Value) {
		setTotAmt();//견적 총금액 표시
		var i = 0;//간접비 시작위치 확인 
    	for (var a = 2; a <= sheet1.RowCount()+1; a ++){  
           	if (sheet1.GetCellValue(a, "DEPTH1") =="간접비"){
           		i = a;
           		break;
           	}
        }  
    	 
    	var ingise_yn = "N";
    	for (var b = 2; b <= sheet1.RowCount()+1; b ++){  
    		if (sheet1.GetCellValue(b, "ITEM_NAME") =="인지세"){
           		ingise_yn = "Y";
           	}
        }
    	
		if(Col == 9 || Row == i){ //고용보험료
			if(sheet1.GetCellValue(i-1,"LAB_COST") > 0 && sheet1.GetCellValue(i,"UNIT_COST") > 0){
				var data = sheet1.GetCellValue(i,"UNIT_COST") / sheet1.GetCellValue(i-1,"LAB_COST") * 100;
				sheet1.SetCellValue(i,"BASIS_CALCU","노무비 * ("+data.toFixed(3)+")%");
			}
		}		 
		 
		if(Row == i+2 || Row == i+3){//노인장기보험료 
			if(sheet1.GetCellValue(i+2,"UNIT_COST") > 0 && sheet1.GetCellValue(i+3,"UNIT_COST") > 0){
				var data = sheet1.GetCellValue(i+3,"UNIT_COST") / sheet1.GetCellValue(i+2,"UNIT_COST") * 100;
				sheet1.SetCellValue(i+3,"BASIS_CALCU","건강 보험료 * ("+data.toFixed(3)+")%");
			}
		}
		if(Col == 9 || Row == i+4){//산재보험료 
			if(sheet1.GetCellValue(i-1,"LAB_COST") > 0 && sheet1.GetCellValue(i+4,"UNIT_COST") > 0){
				var data = sheet1.GetCellValue(i+4,"UNIT_COST") / sheet1.GetCellValue(i-1,"LAB_COST") * 100;
				sheet1.SetCellValue(i+4,"BASIS_CALCU","노무비 * ("+data.toFixed(3)+")%");
			}
		}
		if(Col == 9 || Row == i+5){//퇴직공제부금 
			if(sheet1.GetCellValue(i-1,"LAB_COST") > 0 && sheet1.GetCellValue(i+5,"UNIT_COST") > 0){
				var data = sheet1.GetCellValue(i+5,"UNIT_COST") / sheet1.GetCellValue(i-1,"LAB_COST") * 100;
				sheet1.SetCellValue(i+5,"BASIS_CALCU","노무비 * ("+data.toFixed(3)+")%");
			}
		}
		if(Row == i-1 || Row == i+6){//안전관리비 
			if(sheet1.GetCellValue(i-1,"COST_SUM") > 0 && sheet1.GetCellValue(i+6,"UNIT_COST") > 0){
				var data = sheet1.GetCellValue(i+6,"UNIT_COST") / sheet1.GetCellValue(i-1,"COST_SUM") * 100;
				sheet1.SetCellValue(i+6,"BASIS_CALCU","노무비 * ("+data.toFixed(3)+")%");
			}
		}
		if(Col == 8 || Row == i+7){//환경관리비  
			if(sheet1.GetCellValue(i-1,"MAT_COST") > 0 && sheet1.GetCellValue(i+7,"UNIT_COST") > 0){
				var data = sheet1.GetCellValue(i+7,"UNIT_COST") / sheet1.GetCellValue(i-1,"MAT_COST") * 100;
				sheet1.SetCellValue(i+7,"BASIS_CALCU","재료비 * ("+data.toFixed(3)+")%");
			}
		}
		if(Row == i-1 || Row == i+8){//현장관리비 
			if(sheet1.GetCellValue(i-1,"COST_SUM") > 0 && sheet1.GetCellValue(i+8,"UNIT_COST") > 0){
				var data = sheet1.GetCellValue(i+8,"UNIT_COST") / sheet1.GetCellValue(i-1,"COST_SUM") * 100;
				sheet1.SetCellValue(i+8,"BASIS_CALCU","직접비 * ("+data.toFixed(3)+")%");
			}
		}
		if(Col == 8){//매입부가세
			var data = Number("<%=OBC_DUTY_RATE%>");
			var dataYN = "<%= OBC_DUTY_YN%>";
			if(sheet1.GetCellValue(i-1,"MAT_COST") > 0 && data > 0){
				if(dataYN == "Y"){
					sheet1.SetCellValue(i+9,"BASIS_CALCU","재료비 * 매입세율 ("+data+")% * 10%");
					var data2 = ((data/100) * sheet1.GetCellValue(i-1,"MAT_COST")) *0.1; 
					sheet1.SetCellValue(i+9,"UNIT_COST",Math.floor(data2));
				}
			}
		}
		if(ingise_yn=="Y"){
			/** 금액 변동있을경우 무조건 변경 **/
			if(Col == 9 || Row == i+11){//인지세 
				sheet1.SetCellValue(i+11,"BASIS_CALCU","원사업자 부담분 (50%)");
			}
			if(sheet1.GetCellValue(i+13,"UNIT_COST") > 0){  //일반관리비 
				var sum = sheet1.GetCellValue(i-1,"SUM_COST") + sheet1.GetCellValue(i+12,"UNIT_COST");
				var data = sheet1.GetCellValue(i+13,"UNIT_COST") / sum * 100;
				sheet1.SetCellValue(i+13,"BASIS_CALCU","순공사비 * ("+data.toFixed(3)+")%");
			}
			if(sheet1.GetCellValue(i+14,"UNIT_COST") > 0){ //이윤 
				var sum = sheet1.GetCellValue(i-1,"SUM_COST") + sheet1.GetCellValue(i+12,"UNIT_COST");
				var data = sheet1.GetCellValue(i+14,"UNIT_COST") / sum * 100;
				sheet1.SetCellValue(i+14,"BASIS_CALCU","순공사비 * ("+data.toFixed(3)+")%");
			}
		}else{
			/** 금액 변동있을경우 무조건 변경 **/
			if(sheet1.GetCellValue(i+12,"UNIT_COST") > 0){  //일반관리비 
				var sum = sheet1.GetCellValue(i-1,"SUM_COST") + sheet1.GetCellValue(i+11,"UNIT_COST");
				var data = sheet1.GetCellValue(i+12,"UNIT_COST") / sum * 100;
				sheet1.SetCellValue(i+12,"BASIS_CALCU","순공사비 * ("+data.toFixed(3)+")%");
			}
			if(sheet1.GetCellValue(i+13,"UNIT_COST") > 0){ //이윤 
				var sum = sheet1.GetCellValue(i-1,"SUM_COST") + sheet1.GetCellValue(i+11,"UNIT_COST");
				var data = sheet1.GetCellValue(i+13,"UNIT_COST") / sum * 100;
				sheet1.SetCellValue(i+13,"BASIS_CALCU","순공사비 * ("+data.toFixed(3)+")%");
			}
		} 	
	}
    
    function sheet1_OnSmartResize(Width, Height) { 
		//sheet1.FitColWidth();
		autoResize();
	}
    
    
	 function numberFormat(num) {
		 var pattern = /(-?[0-9]+)([0-9]{3})/;
		 while(pattern.test(num)) {
			 num = num.replace(pattern,"$1,$2");
		 }
		 return num;
	 }
	 
	function doResult(Objresult){
		
	}
	function autoResize(){
		sheet1.SetSheetHeight(560);
		sheet1.SetSheetWidth(600); 
	}
	


	/**
	 * 첨부파일 예약 함수
	 */
	function fileDown(file_id){
			var url="/common/file/FileDownloadServlet";
	 		url += "?file_id=" + file_id;
	 		
	 		window.open(url, "_fileDown", "toolbar=no, width=400, height=100, top=500, left=400, toolbar=no,directories=no,status=no,scrollbars=yes,resize=no,menubar=no,target=new")
	}
	
	//첨부파일   
	function doAttachFile(obj,No) {
		var file_div = "bid/outside/"+"<%=BID_NO%>"+"/";
		 if(obj==''){
			alert("After you have saved, you can attach a file.");
		}
		else if(obj != ''){ 
			viewFileListNew('<%=FILE_GB%>', obj.value, document.form1.p_AttachFile, "Y", file_div);
		} 
	}
	
	function doAttachFileDiv(obj) {
		 if(obj==''){
			alert("저장을 한 후 파일을 첨부할 수 있습니다.");
		}
		else if(obj != ''){ 
			viewFileListNew('<%=FILE_GB%>', obj.value, document.form1.p_AttachFile, "Y", file_div);
		} 
	}
	
	
	function vendorNo(str){
		return str.substring(0,3)+"-"+str.substring(3,5)+"-"+str.substring(5,10);
	}
	
	
    document.onkeydown 		= closePopupWindow;
    //document.body.onload = _onReady;
</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="ROW" value="<%=ROW%>">
<INPUT type="hidden" name="VENDOR_CD" value="<%=VENDOR_CD%>">
<INPUT type="hidden" name="DEMANDED_ID" value="<%=DEMANDED_ID%>">
<INPUT type="hidden" name="BID_NO" value="<%=BID_NO%>">
<INPUT type="hidden" name="BID_DEG" value="<%=BID_DEG%>">
<INPUT type="hidden" name="FILE_GB" value="<%=FILE_GB%>">
<INPUT type="hidden" name="VENDOR_NO" value="<%=VENDOR_NO%>">
<INPUT type="hidden" name="VENDOR_NAME" value="">
<INPUT type="hidden" name="ESTI_TOT_AMT" value="<%=ESTI_TOT_AMT%>">
<TABLE  width="100%">
  <TR height="2"><TD colspan="2"></TD></TR>
  <TR height="25">
    <TD>
		<table  id="t1" width="100%" border="0" cellspacing="0" cellpadding="0" class="ftit_table">
				<colgroup>
					<col width="*" />
					<col width="150" />
				</colgroup>
			<tr>
			   	<th>
			   </th>
			   <td align="right">
					<span class="btnSearch center" id=btn_Save><a href="javascript:void(0);">저장</a></span>
			   </td>
		   	</tr>
		   	</table>
</TD>
  </TR>
</TABLE>
<table cellspacing="1" cellpadding="0" width="100%">
		<colgroup>
			<col width="400" />
			<col />
		</colgroup>
<tr height="100%">
<td valign="top">
	<table cellspacing="1" cellpadding="0" class="search_table">
		<colgroup>
			<col width="110" />
			<col />
		</colgroup>
	<tr height="25">
	<td class="insert_table_title">사업자번호</td>
	<td class="search_table_data"><vendorno></vendorno></td>
	</tr>
	<tr height="25">
	<td class="insert_table_title">업체명</td>
	<td class="search_table_data"><vendorname></vendorname></td>
	</tr>
	<tr height="25">
	<td class="insert_table_title">견적 총 금액</td>
	<td class="search_table_data"><%= InputText.get("TOT_AMT", "", 40, 300, "class=input_blank,desc=견적합계금액,readOnly=white") %></td>
	</tr>
	<tr height="100">
	<td class="insert_table_title">첨부파일</td>
	<td class="search_table_data">
	<TEXTAREA name="p_AttachFile" id="p_AttachFile" style="width:85%;height:80px" rows="1" class="input" readOnly><%=FileManager.getFileListForTextarea(FILE_GB, "") %></TEXTAREA>
    <IMG id='search_spot_button' onClick="doAttachFile(document.form1.DEMANDED_ID);" src='/img/buyer/button/bt_change.gif' border='0'  align="top" align='absmiddle' alt='[Attach File]' style='cursor:pointer;'>
	
	</td>
	</tr>
	</table>
</td>
<td >
<div id="sheet1_div" style="padding-left:5px;height: auto; min-height: 300px; overflow: auto;" >
		<script>createIBSheet('sheet1','100%', '100%');</script>
	</div></td>
</td>
</tr>
</table>

</form>

