<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : SettleConfirmForm.html
 * @desc    : 정산확정관리 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.10.26   gagamel     최초 작성
 *******************************************************************************
 -->
	<div id="main">
		<!-- 메인타이틀 영역 -->
		<div class="main-title">
		</div>
		<!-- //메인타이틀 영역 -->
		
		<!-- 메뉴 설명 -->
		<div class="infoBox menu-desc">
		</div>
		<!-- //메뉴 설명 -->
		
		<!-- 검색조건 영역 -->
		<div class="panelStyle">
			<form id="searchForm" name="searchForm" action="#" th:action="@{'/settle/confirm/list'}" onsubmit="$('#btnSearch').trigger('click'); return false;">
				<table class="frmStyle" aria-describedby="검색조건">
					<colgroup>
						<col style="width:10%;"/>
						<col style="width:25%;"/>
						<col style="width:10%;"/>
						<col/>
					</colgroup>
					<tr>
						<th>정산기준월<i class="required" title="필수" aria-hidden="true"></i></th>
						<td>
							<input type="text" class="schMonth w60" name="settleYm" id="settleYm" maxlength="7" required="required"/>
							<button type="button" class="btn icn schBtn" data-id="settleYm"><i class="fa fa-calendar" aria-hidden="true"></i></button>
						</td>
						<th>공급업체</th>
						<td>
							<select name="supplyCompCd">
								<option value="">[전체]</option>
								<option th:if="${supplyCompList}" th:each="oneData, status : ${supplyCompList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
					</tr>
				</table>
				
				<ul class="panelBar">
					<li class="center">
						<button type="button" class="btn btn-base btn-lg" id="btnSearch">조회</button>
						<button type="button" class="btn btn-gray btn-lg" onclick="$('#searchForm')[0].reset();">초기화</button>
					</li>
				</ul>
			</form>
		</div>
		<!-- 검색조건 영역 -->

		<!-- 리스트 영역 -->
		<div class="panelStyle">
			<!-- 버튼 배치 영역 -->
			<ul class="panelBar">
				<li class="left">
					<span class="infoTxt cBlue">* 항목 설명</span>
					<!-- 아이콘 툴팁 -->
					<div class="iconTooltip marL10">
						<i class="fa fa-info" aria-hidden="true"></i>
						<span class="left" style="width: 600px;">
							<strong>1.계산서발행금액</strong>: ROUND(수수료 - (자사쿠폰분담액 + 자사다다익선분담액), 0) (< 0 인 경우 0으로 표기)<br/>
							<strong>2.공급가액</strong>: INT(계산서발행금액 / 1.1)<br/>
							<strong>3.세액</strong>: 계산서발행금액 - 공급가액<br/>
							<strong>4.지급금액</strong>: 계산서발행금액 + 배송비 - 기타차감<br/>
							<strong>5.총지급금액</strong>: 지급금액 - 미수금<br/>
							<strong>6.상태</strong>: 전자계약여부 = 'N' then [보류] else 지급금액 < 0 then [미수금] else [정상]
						</span>
					</div>
					<!-- //아이콘 툴팁 -->
				</li>
				<li class="right">
					<button type="button" class="btn btn-default btn-lg" onclick="cfnDownloadSampleFile('SF025');">기타차감양식다운로드</button>
					<button type="button" class="btn btn-gray btn-lg" id="btnUploadEtcDeduct">기타차감 엑셀업로드</button>
					<button type="button" class="btn btn-info btn-lg" id="btnSaveReceivableAmt">미수금저장</button>
					<button type="button" class="btn btn-warning btn-lg" id="btnDownloadTaxBillExcel">세금계산서용 엑셀다운로드</button>
					<a href="javascript:void(0);" id="taxBillExcel" style="display: none;">세금계산서엑셀</a>
					<button type="button" class="btn btn-default btn-lg" id="btnExcel">엑셀다운로드</button>
				</li>
			</ul>
			<!-- //버튼 배치 영역 -->
			
			<div id="gridList" style="width: 100%; height: 570px" class="ag-theme-balham"></div>
		</div>
		<!-- //리스트 영역 -->
	</div>

<script th:inline="javascript">
/*<![CDATA[*/
// 	let supplyCompList = gagajf.convertToArray([[${supplyCompList}]]);
// 	let distributionGbList = gagajf.convertToArray([[${distributionGbList}]]);
// 	let bankList = gagajf.convertToArray([[${bankList}]]);
	
	let columnDefs = [
// 		{width: 40, minWidth: 40, cellClass: 'text-center', headerCheckboxSelection: true, checkboxSelection: true, filter: false},
		{headerName: "CRUD", field: "crud", width: 75, hide: true},
// 		{
// 			headerName: "정산확정", field: "confirmYn", width: 100, cellClass: 'text-center',
// 			cellRenderer: function(params) {
// 				if (params.value == 'Y') {
// 					return '<button type="button" class="btn btn-success btn-sm">확정</button>';
// 				} else {
// 					return '<button type="button" class="btn btn-danger btn-sm">해제</button>';
// 				}
// 			}
// 		},
		{ headerName: "정산연월", field: "usacYm", width: 80, cellClass: 'text-center', hide: true },
		{ headerName: "공급업체코드", field: "supplyCompCd", width: 100, cellClass: 'text-center', hide: true },
		{ headerName: "공급업체", field: "supplyCompNm", width: 200 },
		{ headerName: "전자계약여부", field: "econtractYn", width: 100, cellClass: 'text-center' },
		{ headerName: "사업자등록번호", field: "bizNo", width: 120, cellClass: 'text-center' },
		{ headerName: "정산주기", field: "settleDay", width: 100, cellClass: 'text-center', hide:true },
		{ headerName: "정산주기", field: "settleDayNm", width: 100, cellClass: 'text-center' },
		{
			headerName: "판매수량", field: "sellQty", width: 100, cellClass: 'text-center',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "상품총액", field: "realSellAmt", width: 100, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "수수료", field: "sellFeeAmt", width: 100, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "쿠폰할인금액", field: "cpnDcAmt", width: 100, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "자사쿠폰분담액", field: "selfCpnDcAmt", width: 120, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "입점쿠폰분담액", field: "supplyCompCpnDcAmt", width: 120, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "다다익선할인금액", field: "tmtbDcAmt", width: 120, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "자사다다익선분담액", field: "selfTmtbDcAmt", width: 150, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "입점다다익선분담액", field: "supplyCompTmtbDcAmt", width: 150, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "계산서발행금액", field: "billAmt", width: 120, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "공급가액", field: "supplyAmt", width: 100, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "세액", field: "taxAmt", width: 100, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "배송비", field: "delvFee", width: 100, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "기타차감", field: "etcDeductAmt", width: 100, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle: { color: '#B50808', 'background-color': '#FF9AA2' }
		},
		{
			headerName: "지급금액", field: "giveAmt", width: 100, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "미수금", field: "receivableAmt", width: 100, cellClass: 'text-right',
			editable: true, cellEditor: 'numericCellEditor', cellEditorParams: { maxlength: 12, validType: 'integer', required: true },
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(Number(params.value)); },
			cellStyle: { color: '#fffff', 'background-color': '#aaaaff' }
		},
		{
			headerName: "총지급금액", field: "totGiveAmt", width: 100, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "상태", field: "settleStat", width: 100, cellClass: 'text-center'/* ,
			cellRenderer: function (params) {
				// 전자계약이 안 되어 있으면 "보류"
				// 전자계약은 되어 있으나 지급금액 < 0 이면 "미수금" 그 외는 "정상"
				if (params.data.econtractYn == 'N') {
					return '보류';
				}
				
				if (params.data.giveAmt < 0) {
					return '미수금';
				}
				
				return '정상';
			} */
		},
		{ headerName: "은행코드", field: "bankCd", width: 100, cellClass: 'text-center', hide: true },
		{ headerName: "은행", field: "bankNm", width: 100, cellClass: 'text-center' },
		{ headerName: "계좌번호", field: "accountNo", width: 150, cellClass: 'text-center' },
		{ headerName: "예금주명", field: "depositorNm", width: 100, cellClass: 'text-center' },
		{ headerName: "계산서이메일", field: "billEmail", width: 100, cellClass: 'text-center' }
	];

	let gridOptions = gagaAgGrid.getGridOptions(columnDefs);

	gridOptions.stopEditingWhenGridLosesFocus = true;
	
	// 검색
	$('#btnSearch').on('click', function() {
		// 입력 값 체크
		if (!gagajf.validation('#searchForm'))
			return false;
		
		gagaAgGrid.fetch($('#searchForm').prop('action'), gridOptions, '#searchForm');
	});
	
	// 기타차감엑셀업로드
	$('#btnUploadEtcDeduct').on('click', function() {
		cfnExcelUploadPopup('uploadEtcDeduct', 'uploadEtcDeduct');
	});
	
	var uploadEtcDeduct = function(result) {
		var data = { procJob : result.procJob, excelFileNm : result.excelFileNm };
		var jsonData = JSON.stringify(data);
		gagajf.ajaxJsonSubmit('/settle/etc/deduct/amt/upload', jsonData, function() {
			$('#btnSearch').trigger('click');
		});
	}
	
	// 미수금 저장
	$('#btnSaveReceivableAmt').on('click', function() {
		var changeData = gagaAgGrid.getChangedData(gridOptions);
		
		if (changeData.length == 0) {
			mcxDialog.alert('변경된 행이 없습니다.');
			return;
		}
		
		mcxDialog.confirm("미수금을 저장하시겠습니까?", {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function() {
				var jsonData = JSON.stringify(changeData);
				gagajf.ajaxJsonSubmit('/settle/receievable/amt/list/save', jsonData, function() {
					$('#btnSearch').trigger('click');
				});
			}
		});
	});
	
	// 세금계산서용 엑셀다운로드
	$('#btnDownloadTaxBillExcel').on('click', function() {
		// 입력 값 체크
		if (!gagajf.validation('#searchForm'))
			return false;
		
		mcxDialog.confirm($('#settleYm').val() + "월 세금계산서용 엑셀을 다운로드 하시겠습니까?", {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function() {
				var params =  $('#searchForm').serialize();
				$('#taxBillExcel').attr({ href : '/settle/tax/bill/excel/download?' + params}).get(0).click();
			}
		});
	});
	
	// 엑셀다운로드
	$('#btnExcel').on('click', function() {
		if (gridOptions.api.getDisplayedRowCount() <= 0) {
			mcxDialog.alert("조회된 데이터가 없습니다. 조회 후 다운로드 하세요.");
			return false;
		}
		
		gagaAgGrid.exportToExcel('정산확정 목록', gridOptions);
	});
	
	$(document).ready(function() {
		$('#settleYm').val((new Date()).before(0, 1, 0).format("YYYY-MM"));
		
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);
	});
/*]]>*/
</script>

</html>
