<%--
  - Author(s)  : Kim Myung Ho
  - Date       : 2014.08.22
  - Description: 계약 구매 대금 계획
--%>
<%@ page contentType="text/html; charset=utf-8" %>
<SCRIPT language="JavaScript">
	var sheet1 = new ibsheetObject();
	
	var pCount = 0;
	var mCount = 0;
	var rCount = 0;
	
	function doSearchPayPlanSheet(){
		$("#fevent").val("");
		var url = Modular.model.PageContext.CONTEXT_ROOT + "/cont/purchase/payPlan_sheet.screen";
		Modular.view.IBSheet7.doSearch(sheet1, url, jQuery("#form1"), 30, "");		
	}

	//Grid 상태값 변경
	function doInsert(gRow) {
    	for(var i=1; i<=sheet1.RowCount(); i++){
    		sheet1.SetCellValue(i,2,i);
    	}
		sheet1.SetColProperty(gRow,1,info);
		var info= {Edit:true}; 
	}   
		
	function gridInit(sheetName){
		var init_sheet = {};
		init_sheet.Cfg = {SearchMode:3, Page:30};
		init_sheet.Cols = [
		       			{Header:'삭제',				Type:"DelCheck",		SaveName:"DEL_CHECK", 				Width:0, 		Align:"Center", HeaderCheck:false},		       			
		    			{Header:'구분',				Type:"Combo", 			SaveName:"PAY_DIV_CD", 				Width:80, 		Align:"Left", 	Edit:false,	ComboText:"중도금|선급금|잔금", ComboCode:"02|01|03"},
		    			{Header:'지급차수',				Type:"Int", 			SaveName:"PAY_SEQ",					Width:60, 		Align:"Center", Edit:false},
		    			{Header:'청구금액',				Type:"AutoSum", 		SaveName:"PAY_SUPPLY_AMT", 			Width:100,		Align:"Right", 	Edit:true},
		    			{Header:'요율(%)',			Type:"Text", 			SaveName:"PAY_SUPPLY_AMT_RATE", 	Width:100,		Align:"Center", Edit:false},
		    			{Header:'대금여부',				Type:"Text", 			SaveName:"PAY_YN", 					Width:100, 		Align:"Center", Edit:false},
		    			{Header:'보증서여부',			Type:"CheckBox", 		SaveName:"GUAR_YN", 				Width:100, 		Align:"Center", Edit:true,	TrueValue:"Y", FalseValue:"N"},		    			
		    			{Header:'Vendor Print 여부',	Type:"CheckBox",	 	SaveName:"VP_YN", 					Width:100, 		Align:"Center", Edit:true,	TrueValue:"Y", FalseValue:"N"},
		    			{Header:'Vendor Print 항목',	Type:"Text", 			SaveName:"VP_DESC", 				Width:200, 		Align:"Left"},
		    			{Header:'지급신청ID_PUR100T',	Type:"Text", 			SaveName:"PAYREQ_ID", 				Width:0, 		Align:"Left",	Hidden:"True"},
		    			{Header:'WARR_GUBUN',		Type:"Text", 			SaveName:"WARR_GUBUN", 				Width:50, 		Align:"Left",	Hidden:"True"},
		    			{Header:'Status',			Type:"Status", 			SaveName:"STATUS", 					Width:50, 		Align:"Center",	Hidden:"True"},
		    			{Header:'INFLAG',			Type:"Text", 			SaveName:"INFLAG", 					Width:50, 		Align:"Center",	Hidden:"True"},
		    			{Header:'Vendor Print 담당자ID',	Type:"Text", 		SaveName:"VP_CHK_EMPNO", 			Width:20, 		Align:"Left",	Hidden:"True"},
		    			{Header:'설계평가 담당자ID',	Type:"Text", 			SaveName:"PLAN_APPR_CHK_EMPNO",		Width:20, 		Align:"Left",	Hidden:"True"},
		    			{Header:'Vendor Print 담당자명',	Type:"Popup", 		SaveName:"VP_CHK_EMPNM", 			Width:100, 		Align:"Left"},		    		
		    			{Header:'설계평가여부',		Type:"CheckBox", 		SaveName:"PLAN_APPR_YN",			Width:100, 		Align:"Center", Edit:true,	TrueValue:"Y", FalseValue:"N"},		    			
		    			{Header:'설계평가 담당자명',	Type:"Popup", 			SaveName:"PLAN_APPR_CHK_EMPNM",		Width:100, 		Align:"Left"},
		    			{Header:'PLAN_APPR_SEND_YN',	Type:"Text", 		SaveName:"PLAN_APPR_SEND_YN",		Width:50, 		Align:"Center",	Hidden:"True"},
		    			{Header:'DEPT_CD',			Type:"Text", 			SaveName:"DEPT_CD",					Width:50, 		Align:"Center",	Hidden:"True"},
		    			{Header:'ORD_DOC_NO',		Type:"Text", 			SaveName:"ORD_DOC_NO",				Width:50, 		Align:"Center",	Hidden:"True"},
		    			{Header:'CORP_ID',			Type:"Text", 			SaveName:"CORP_ID",					Width:50, 		Align:"Center",	Hidden:"True"},
		];
		init_sheet.HeaderMode = {Sort:0};
		IBS_InitSheet(sheetName, init_sheet);
		sheetName.SetDataAutoTrim(0);

		//sheetName.SizeMode(0);
		setGridSize();
	}
	
	/**
	 *	IBSheet Event: Click
	 */
	function sheet1_OnClick(Row, Col, Value, CellX, CellY, CellW, CellH) {

	}
	
	/**
	 *	IBSheet size setting
	 */
	function setGridSize() {
		sheet1.FitColWidth();
	}

	/**
	 *	IBSheet Event: Search End(IBSheet 데이터 조회 CallBack)
	var pCount = 0;
	var mCount = 0;
	var rCount = 0;
	 */
	function sheet1_OnSearchEnd(code, message) {
	    if(code == 0) {
	    	for(var i=1; i<=sheet1.RowCount(); i++){
	    		if(sheet1.GetCellValue(i,1)=="01"){
	    			pCount = pCount + 1;
	    		}else if(sheet1.GetCellValue(i,1)=="02"){
	    			mCount = mCount + 1;
	    		}else if(sheet1.GetCellValue(i,1)=="03"){
	    			rCount = rCount + 1;
	    		}
	    	}
			//소수점 설정
	    	var info = {Type:"Float", PointCount:jQuery('#sosuNum').val()};
   	
	    	for(var i=1;i <= sheet1.RowCount();i++){
	    		if(sheet1.GetCellValue(i,"PAY_YN") == "Y"){
	    			sheet1.SetRowEditable(i,0);
	    		}
	    		//소수점 설정
	    		sheet1.InitCellProperty(i, 3, info);
	    	}	    	
	    	setGridSize();
		} else {
			//조회 에러 수행
			alert(message);
		}
	}
	
	function sheet1_OnSaveEnd(code, msg)
	{
		var event = $("#fevent").val();
		if(event == "SHEET_SAVE"){
			if(code>= "0"){
				if(payCallNum==1){
					alert("계약서저장에 성공하였습니다.\n\n저장된 계약서는 구매_계약현황페이지에서 확인하실수 있습니다.");
					location.href="/cont/purchase/ContIngList.screen";
					//location.reload();
				}else if(payCallNum==2){
					alert("저장 되었습니다.");
 					//goListProc();
					location.reload();
				}else{
					alert("저장 되었습니다.")
					location.reload();
				}
			}
		}else{
			if(code>= "0"){
				alert("저장되었습니다.");
				refresh();
			}else{
				alert("저장실패.");
			}
		}
	}

	function sheet1_OnDblClick(Row, Col, Value, CellX, CellY, CellW, CellH) {
		 if(Row == 0)return;
		 if ((sheet1.ColSaveName(Col) == "PAY_YN")) {
				 if("<%=user.get("RIGHT_NAME")%>" == "ADMIN NEW"){
					 $("#p_pay_seq").val(sheet1.GetCellValue(Row,"PAY_SEQ"));
					 if(sheet1.GetCellValue(Row,"PAY_YN") == "Y"){
						 $("#p_pay_yn").val("N");
					 }
					 else{
						 $("#p_pay_yn").val("Y");
					 }
					 
					 if (confirm("대금여부 값을 변경 하시겠습니까?")){
						 	var f = document.form1;
							jQuery("#fevent").val("TRANS_PAY_YN");
							submitXmlRequest("/cont/purchase/ContResult_form.screen", "TRANS_PAY_YN", f);
						}
				 }
		}
	}
	
	/**
	 *	IBSheet size setting
	 */
	function setGridSize() {
		sheet1.FitColWidth();
	}
	/**
	 *	IBSheet Change Event
	 */
	 function sheet1_OnChange(Row, Col, Value, OldValue, RaiseFlag) { 
		var oldPrepayAmt ="";
		if(Col == 3){//선급금 금액 체크
			oldPrepayAmt = sheet1.GetCellValue(Row,3);
			if(sheet1.GetCellValue(Row,1) == "01"){
				if(parseInt(trimComma(form1.prepay_amt.value)) != parseInt(sheet1.GetCellValue(Row,3))){
					alert("계약서의 금액과 선급금 금액이 일치 하지 않습니다.");
					sheet1.SetCellValue(Row,Col,OldValue,0);
					//return;
				}
			}
			for(var i=1; i<=sheet1.RowCount(); i++){
				sheet1.SetCellValue(i,4, (parseFloat(sheet1.GetCellValue(i,3)) / parseFloat(trimComma(form1.ord_supply_amt.value))*100).toFixed(2));	
			}
		}
	}
	//사용자 추가
	function sheet1_OnPopupClick(Row, Col) {
		var sName = sheet1.ColSaveName(Col);
		
		if(sName=="VP_CHK_EMPNM"){
			if(sheet1.GetCellValue(Row,"VP_YN")=="N"){
				alert("Vendor Print 여부에 체크를 해주시고 담당자를 선택 해 주시기 바랍니다.");
				return;
			}
			if ( sName=="VP_CHK_EMPNM" ) {	// 현장
				clickRow = Row;
				SearchUser('Sheet','','');
			}
		}
		
		if(sName=="PLAN_APPR_CHK_EMPNM"){
			if(sheet1.GetCellValue(Row,"PLAN_APPR_YN")=="N"){
				alert("설계평가 여부에 체크를 해주시고 담당자를 선택 해 주시기 바랍니다.");
				return;
			}
			if ( sName=="PLAN_APPR_CHK_EMPNM" ) {	// 현장
				clickRow = Row;
				SearchUser('Sheet2','','');
			}
		}				
	}
	
	function paySaveChk(){
		var pAmt = 0;
		var mAmt = 0;
		var rAmt = 0;
		var sumAmt = 0;
		
		for(var i=1; i<=sheet1.RowCount(); i++){
    		if(sheet1.GetCellValue(i,1)=="01"){
    			pAmt = pAmt + sheet1.GetCellValue(i,3)
    		}else if(sheet1.GetCellValue(i,1)=="02"){
    			mAmt = mAmt + sheet1.GetCellValue(i,3)
    		}else if(sheet1.GetCellValue(i,1)=="03"){
    			rAmt = rAmt + sheet1.GetCellValue(i,3)
    		}			
    		sheet1.SetCellValue(i, 12, "Y");
		}
		for(var i=1; i<=sheet1.RowCount(); i++){
			if((sheet1.GetCellValue(i,"PAY_DIV_CD")=="01") && (sheet1.GetCellValue(i,"PLAN_APPR_YN")=="Y")){
    			alert("선급금은 설계평가를 하지 않습니다.");
    			sheet1.SetCellValue(i, "PLAN_APPR_YN", "N");
    			sheet1.SetCellValue(i, "PLAN_APPR_CHK_EMPNO", "");
    			sheet1.SetCellValue(i, "PLAN_APPR_CHK_EMPNM", "");     			
    			sheet1.SetCellValue(i, "PLAN_APPR_SEND_YN", "");
    			return false;
    		}
			if((sheet1.GetCellValue(i,"PLAN_APPR_YN")=="N") || (sheet1.GetCellValue(i,"PLAN_APPR_YN")=="")){
    			sheet1.SetCellValue(i, "PLAN_APPR_YN", "N");
    			sheet1.SetCellValue(i, "PLAN_APPR_CHK_EMPNO", "");
    			sheet1.SetCellValue(i, "PLAN_APPR_CHK_EMPNM", "");
    			sheet1.SetCellValue(i, "PLAN_APPR_SEND_YN", "");
    		}			
    		if((sheet1.GetCellValue(i,"PLAN_APPR_YN")=="Y") && (sheet1.GetCellValue(i,"PLAN_APPR_CHK_EMPNO")=="")){
    			alert(i+ "번째  설계평가 담당자를 선택해 주세요.");
    			return false;
    		}
		} 
		for(var i=1; i<=sheet1.RowCount(); i++){
    		if((sheet1.GetCellValue(i,"VP_YN")=="Y") && (sheet1.GetCellValue(i,"VP_CHK_EMPNO")=="")){
    			alert(i+ "번째  Vendor Print 담당자를 선택해 주세요.");
    			return false;
    		}
		}
		//alert(form1.prepay_amt.value + " / " + trimComma(form1.mid_amt.value) + " / " + form1.rmdr_amt.value + " / " + mAmt);
		sumAmt = sheet1.GetSumValue(0 ,"PAY_SUPPLY_AMT");
		if(trimComma(form1.ord_supply_amt.value) != sumAmt){
			alert("계약서의 금액과 합계 금액이 일치 하지 않습니다.");
			return false;
		}
		/*
 		if(trimComma(form1.prepay_amt.value) != pAmt){
			alert("계약서의 금액과 선급금 금액이 일치 하지 않습니다.");
			return false;
		}		
		if(trimComma(form1.mid_amt.value) != mAmt){
			alert("계약서의 금액과 중도금 합계 금액이 일치 하지 않습니다.");
			return false;
		}		
		if(trimComma(form1.rmdr_amt.value) != rAmt){
			alert("계약서의 금액과 잔금 합계 금액이 일치 하지 않습니다.");
			return false;
		} 
		*/
		return true;
	}
	
	function payPlanSave(){		//저장
		jQuery("#fevent").val("SHEET_SAVE");
		var url = Modular.model.PageContext.CONTEXT_ROOT + "/cont/purchase/payPlan_sheet.screen";
		var result = Modular.view.IBSheet7.doSave(sheet1, url, jQuery("#form1"), false);				
	}
	
	function sheet1_OnSearchEnd(code, message) {
	    if(code == 0 && jQuery("#fevent").val() == "PAYPLAN_COPY") {
	    	//payPlanSave();	//대금 계획 저장
			//alert("대금계획저장 클릭 후 적용됩니다.");    	
		} 
	}
	
	function payPlanInit(){			//초기 셋팅
		sheet1.SetSumValue(3,"총 금 액");

		doSearchPayPlanSheet();	
		
		//2016.4.27 이전차수 자료 복사 
		jQuery("#btnAdd0").click(function() {
			if (confirm("이전 차수 대금계획을 가져 오시겠습니까?")){
				jQuery("#fevent").val("PAYPLAN_COPY");
				var url = Modular.model.PageContext.CONTEXT_ROOT + "/cont/purchase/payPlan_sheet.screen";
				Modular.view.IBSheet7.doSearch(sheet1, url, jQuery("#form1"), 30, "");
			}
		});
		jQuery("#btnAdd1").click(function() {
			if(pCount==0){
				var rowIdx = sheet1.DataInsert(0);
				sheet1.SetCellValue(rowIdx,1,"01");
				pCount = pCount + 1;
				doInsert(rowIdx);
			}else{
				alert("선급금은 한번만 입력 하실수 있습니다.")
			}
		});
		jQuery("#btnAdd2").click(function() {
			var rowIdx = sheet1.DataInsert();
			sheet1.SetCellValue(rowIdx,1,"02");
			doInsert(rowIdx);
		});
		jQuery("#btnAdd3").click(function() {
			var rowIdx = sheet1.DataInsert();  //2017.11.20 -1
			sheet1.SetCellValue(rowIdx,1,"03");
			rCount = rCount + 1;
			doInsert(rowIdx);
		});		
	}
	
	function sheet1_OnSearchEnd(code, message) {
	    if(code == 0) {
	    	setGridSize();
	    	for(var i=1;i <= sheet1.RowCount();i++){
	    		if(sheet1.GetCellValue(i,"PAY_YN") == "Y"){
	    			sheet1.SetRowEditable(i,0);
	    		}
	    	}		    	
		} else {
			//조회 에러 수행
			alert(message);
		}
	}
</SCRIPT>
<INPUT type="hidden" name="p_pay_yn" value="">
<INPUT type="hidden" name="p_pay_seq" value="">

	<table id=copr_list width="100%">
		<colgroup>
			<col width="500" />
			<col width="*" />
		</colgroup>
		<tr>
	   		<th>
	   		<div class="application-info magT20">
				<div class="application-sub-title">
					<span class="title">대금계획</span>
				</div>
			</div>
	   		</th>
	   		<td align="right">
	   		    <% if (!ContInfo.get("MOD_NO").equals("0") && ContInfo.get("PUR1100T_CNT").equals("0") ) { %>  <!-- 2016.4.28 변경계약이고, 변경계약으로 생성된 대금지급이 없을때만 -->
	            <span class="btnSearch center" id="btnAdd0"><a href="javascript:void(0);">대금계획생성</a></span> 
	            <%} %>
	            <span class="btnSearch center" id="btnAdd1"><a href="javascript:void(0);">선급금추가</a></span>
	            <span class="btnSearch center" id="btnAdd2"><a href="javascript:void(0);">중도금추가</a></span>
	            <span class="btnSearch center" id="btnAdd3"><a href="javascript:void(0);">잔금추가</a></span>
	            <%if (isViewWrite=="V"){ %>
	            <span class="btnSearch center" id="btnPaySave"><a href="javascript:void(0);">대금계획저장</a></span>
	            <%} %>
	   		</td>
	  	</tr>
	</table>
	<div id="sheet1_div" style="padding-top:5px;height: auto; min-height: 200px; overflow: auto;">
		<script>createIBSheet('sheet1','100%', '200px');</script>
	</div>
<script>
	/*대금 계획 추가 내용*/
	gridInit(sheet1);

	//대금계획 조회
	payPlanInit();		
</script>