<?php include(mvc::$cfg['PATH_TPL'] . 'public/header.php'); ?>
<style>
	body {
		color: #000
	}

	.ui-th-div {
		color: #676a6c
	}

	.table>tbody>tr>td,
	.table>tbody>tr>th,
	.table>tfoot>tr>td,
	.table>tfoot>tr>th,
	.table>thead>tr>td,
	.table>thead>tr>th {
		padding: 5px 5px 5px 5px;
		height: 30px;
	}

	.form-control {
		height: 25px
	}

	.form-control,
	.single-line {
		padding: 0 0 0 5px;
		font-size: 12px
	}
</style>
<div class="list_pager">
	<div id="list_buttons"></div>
	<table id="grid_wages_report"></table>
	<div id="jqGridPager"></div>
</div>
<script type="text/javascript" defer>
	var cycle = "<?php echo $_REQUEST['cycle'] ?>";
	var grid = null;
	var form_title = '工资统计';
	var url = HOST + 'wages/report';
	var cycleSelectList = $('<select id="cycleList"></select>');
	$(document).ready(function() {
		qg.ajax.post(HOST + 'report/getCycle', {}, function(data) {
			$.each(data, function(i, row) {
				if (i == 0 && cycle == '') cycle = row.v_cycle;
				if (cycle == row.v_cycle)
					cycleSelectList.append(new Option(row.v_cycle, row.v_cycle, true, true));
				else
					cycleSelectList.append(new Option(row.v_cycle, row.v_cycle));
				cycleSelectList.on('change', function(e) {
					qg.win.go(qg.url.self().base().add({
						cycle: $(this).val()
					}).val());
				});
			});
			createGrid();
			createTools();
		});
	});
	/**方法************************/
	var gridReload = function() { //供弹窗调用
		grid.reload();
	};
	var createGrid = function() {
		var colModel = [{
				label: '项目',
				name: 'title',
				width: 260,
				sortable: false
			},
			{
				label: '金额',
				name: 'money',
				width: 150,
				sortable: false,
				align: 'right',
				formatter: viewMoney,
				unformat: unMoney,
				cellattr: moneyAttr
			},
		];
		grid = new qgGrid('grid_wages_report', {
			url: qg.url.set(url).add({
				cycle: cycle
			}).val(),
			autowidth: true,
			rowNum: 100,
			searchOperators: false,
			colModel: colModel,
			pager: false,
			gridComplete: function() {
				;
			},
		});
	};

	var btn1 = $('<button id="btn1" class="btn btn-sm btn-white" style="margin-left:4px"><i class="fa fa-credit-card"></i>&nbsp;应付薪酬凭证</button>');
	btn1.on("click", function(event) {
		createeVoucher(1);
	});
	var btn2 = $('<button id="btn1" class="btn btn-sm btn-white" style="margin-left:4px"><i class="fa fa-credit-card"></i>&nbsp;代扣社保、公积金、个税凭证</button>');
	btn2.on("click", function(event) {
		createeVoucher(2);
	});
	var btn3 = $('<button id="btn1" class="btn btn-sm btn-white" style="margin-left:4px"><i class="fa fa-credit-card"></i>&nbsp;工资发放凭证</button>');
	btn3.on("click", function(event) {
		createeVoucher(3);
	});
	var btn4 = $('<button id="btn1" class="btn btn-sm btn-white" style="margin-left:4px"><i class="fa fa-credit-card"></i>&nbsp;应付公积金凭证</button>');
	btn4.on("click", function(event) {
		createeVoucher(4);
	});
	var btn5 = $('<button id="btn1" class="btn btn-sm btn-white" style="margin-left:4px"><i class="fa fa-credit-card"></i>&nbsp;应付保险凭证</button>');
	btn5.on("click", function(event) {
		createeVoucher(5);
	});
	var customHtml = $('<span></span>');
	$(customHtml).append(cycleSelectList);
	$(customHtml).append(btn1);
	$(customHtml).append(btn2);
	$(customHtml).append(btn3);
	$(customHtml).append(btn4);
	$(customHtml).append(btn5);

	var createTools = function() {
		qgui.tableButton({
			dom: '#list_buttons',
			grid: grid,
			title: form_title,
			buttons: [{
				btn: 'refresh'
			}, ],
			customHtml: customHtml,
		});
	};
	var viewMoney = function(cellValue, options, rowObject, action) {
		if (cellValue == '0' || cellValue == '0.00' || !cellValue)
			cellValue = '';
		else
			cellValue = qg.num.money.format(cellValue);
		return cellValue;
	};
	var unMoney = function(cellValue, options, rowObject) {
		return qg.num.money.unformat(cellValue);
	};
	var moneyAttr = function(rowId, val, rawObject, cm, rdata) {
		if (val < 0) {
			return "style='color:red'";
		}
	}
	var createeVoucher = function(type) {
		var month = cycle.substring(4).trim('0') * 1 - 1;
		if (month < 1) {
			month = 12;
		}
		var summary = month + '月';
		qg.ajax.get(HOST + 'wages/reportData', {
			"cycle": cycle
		}, function(data) {
			if (type == 1) {
				var tpl = [];
				var item;
				$.each(data.keyData.depa_yfgz, function(depa, row) {
					var subjectText = getDepaSubjectText(row.title, 1);
					item = {
						"v_id": 0,
						"subjectText": subjectText,
						"v_j": row.money,
						"v_d": "0",
						"depText": row.title,
						"clientText": "",
						"v_summary": summary + '工资'
					};
					tpl.push(item);
				});
				item = {
					"v_id": 0,
					"subjectText": "应付薪酬_应付工资",
					"v_j": "0",
					"v_d": data.keyData.yfgz_total,
					"depText": "",
					"clientText": "",
					"v_summary": summary + '工资'
				};
				tpl.push(item);
				qg.local.set('voucher_tpl', tpl);
				qgui.page.open('应付薪酬凭证', HOST + 'voucher/form.html?fromtpl=1&type=' + type);
			} else if (type == 2) {
				console.log(data);
				var tpl = [];
				var item;
				item = {
					"v_id": 0,
					"subjectText": "应付薪酬_应付工资",
					"v_j": data.keyData.w_5x1jgs,
					"v_d": "0",
					"depText": "",
					"clientText": "",
					"v_summary": summary + "代扣社保、公积金、个税"
				};
				tpl.push(item);

				item = {
					"v_id": 0,
					"subjectText": "应付薪酬_社会保险",
					"v_j": "0",
					"v_d": data.keyData.w_2211_02,
					"depText": "",
					"clientText": "",
					"v_summary": summary + "代扣社保、公积金、个税"
				};
				tpl.push(item);

				item = {
					"v_id": 0,
					"subjectText": "应付薪酬_住房公积金",
					"v_j": "0",
					"v_d": data.keyData.w_2211_04,
					"depText": "",
					"clientText": "",
					"v_summary": summary + "代扣社保、公积金、个税"
				};
				tpl.push(item);

				item = {
					"v_id": 0,
					"subjectText": "应交税费_个人所得税",
					"v_j": "0",
					"v_d": data.keyData.w_2221_01,
					"depText": "",
					"clientText": "",
					"v_summary": summary + "代扣社保、公积金、个税"
				};
				tpl.push(item);

				qg.local.set('voucher_tpl', tpl);
				qgui.page.open('代扣社保、公积金、个税凭证', HOST + 'voucher/form.html?fromtpl=1&type=' + type);
			} else if (type == 3) {
				var tpl = [];
				var item;
				item = {
					"v_id": 0,
					"subjectText": "应付薪酬_应付工资",
					"v_j": data.keyData.sfgz_total,
					"v_d": "0",
					"depText": "",
					"clientText": "",
					"v_summary": summary + '工资发放'
				};
				tpl.push(item);
				$.each(data.keyData.zch_sfgz, function(depa, row) {
					item = {
						"v_id": 0,
						"subjectText": row.title,
						"v_j": "0",
						"v_d": row.money,
						"depText": "",
						"clientText": "",
						"v_summary": summary + '工资发放'
					};
					tpl.push(item);
				});
				qg.local.set('voucher_tpl', tpl);
				qgui.page.open('工资发放凭证', HOST + 'voucher/form.html?fromtpl=1&type=' + type);
			} else if (type == 4) {
				var tpl = [];
				var item;
				$.each(data.keyData.depa_gjjgsbf, function(depa, row) {
					var subjectText = getDepaSubjectText(row.title, 1);
					item = {
						"v_id": 0,
						"subjectText": subjectText,
						"v_j": row.money,
						"v_d": "0",
						"depText": row.title,
						"clientText": "",
						"v_summary": summary + '公积金'
					};
					tpl.push(item);
				});
				item = {
					"v_id": 0,
					"subjectText": "应付薪酬_住房公积金",
					"v_j": "0",
					"v_d": data.keyData.gjjgsbf_total,
					"depText": "",
					"clientText": "",
					"v_summary": summary + '公积金'
				};
				tpl.push(item);
				qg.local.set('voucher_tpl', tpl);
				qgui.page.open('应付公积金凭证', HOST + 'voucher/form.html?fromtpl=1&type=' + type);
			} else if (type == 5) {
				var tpl = [];
				var item;
				$.each(data.keyData.depa_bxgsbf, function(depa, row) {
					var subjectText = getDepaSubjectText(row.title, 1);
					item = {
						"v_id": 0,
						"subjectText": subjectText,
						"v_j": row.money,
						"v_d": "0",
						"depText": row.title,
						"clientText": "",
						"v_summary": summary + '保险'
					};
					tpl.push(item);
				});
				item = {
					"v_id": 0,
					"subjectText": "应付薪酬_社会保险",
					"v_j": "0",
					"v_d": data.keyData.bxgsbf_total,
					"depText": "",
					"clientText": "",
					"v_summary": summary + '保险'
				};
				tpl.push(item);
				qg.local.set('voucher_tpl', tpl);
				qgui.page.open('应付保险凭证', HOST + 'voucher/form.html?fromtpl=1&type=' + type);
			}
		});
	};
</script>
<?php include(mvc::$cfg['PATH_TPL'] . 'public/footer.php'); ?>