<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<link rel="stylesheet" href="function/dataTables/media/css/demo_page.css">
	<link rel="stylesheet" href="function/dataTables/media/css/demo_table_jui.css">
	<link rel="stylesheet" href="function/dataTables/media/themes/smoothness/jquery-ui-1.8.4.custom.css">
	<script type="text/javascript" src="function/dataTables/media/js/jquery.dataTables.js"></script>
	<script type="text/javascript" language="javascript">
		$(document).ready(function() {
			$("#fromdate").datepicker({
				dateFormat: "yy-mm-dd",
				changeMonth: true,
				changeYear: true,
			});
		});
		$(document).ready(function() {
			$("#todate").datepicker({
				dateFormat: "yy-mm-dd",
				changeMonth: true,
				changeYear: true,
			});
		});
		$(document).ready(function() {
			var oTable = $('#myTables').dataTable({
				"sDom": 'R<"H"lfr>t<"F"ip>',
				"bJQueryUI": true,
				"sPaginationType": "full_numbers",
				"iDisplayLength": 10
			});
		});

		function setTrColor(obj, color) {
			obj.style.backgroundColor = color;
			obj.onmouseout = function() {
				obj.style.backgroundColor = '';
			}
		}

		function search_click() {
			form.action = '?page=Data-Penyusutan&Act=CARI';
			form.method = 'post';
			form.submit();
			return true;
		}

		function add_click() {
			form.action = '?page=Proses-Penyusutan&Act=ADD';
			form.method = 'post';
			form.submit();
			return true;
		}

		function auto_click() {
			form.action = '?page=Proses-Penyusutan&Act=AUT';
			form.method = 'post';
			form.submit();
			return true;
		}

		function susut_click() {
			form.action = '?page=Proses-Penyusutan&Act=PAY_ADDS';
			form.method = 'post';
			form.submit();
			return true;
		}

		function detail_click(py_id) {
			form.action = '?page=Proses-Penyusutan&Act=DETAIL&py_id=' + py_id;
			form.method = 'post';
			form.submit();
			return true;
		}

		function hapus_click() {
			var a = confirm("Apakah anda yakin akan menghapus data?");
			if (a == true) {
				form.action = '?page=Proses-Penyusutan&Act=DEL_DET';
				form.method = 'post';
				form.submit();
				return true;
			}
		}

		function payment_click(py_id) {
			form.action = '?page=Proses-Penyusutan&Act=PAY_ADD&py_id=' + py_id;
			form.method = 'post';
			form.submit();
			return true;
		}

		function excel_click() {
			form.action = './transaksi/akuntansi/export_penyusutan.php';
			form.method = 'post';
			form.submit();
			return true;
		}
		function excel_detail_click() {
			form.action = './transaksi/akuntansi/export_penyusutan_detail.php';
			form.method = 'post';
			form.submit();
			return true;
		}
		/*function pdf_click(){
			form.action ='./transaksi/akuntansi/pdf_buku_besar_hutang.php';
			form.method = 'post';
			form.submit();			
			return true;
		}*/
	</script>
	<?php
	$py_tipe	 	= isset($_REQUEST['py_tipe']) ? $_REQUEST['py_tipe'] : '';
	$pyd_tanggal	= isset($_REQUEST['pyd_tanggal']) ? $_REQUEST['pyd_tanggal'] : $date;
	if (isset($_REQUEST['Act']) == "CARI") {
		if ($py_tipe == "0") {
			$search    = "";
		} else {
			$search    = "id='$py_tipe' AND";
		}
	} else {
		$search    = "";
	}
	?>
</head>

<body>
	<form action="#" method="post" name="form">
		<table width="100%" border="0" cellspacing="1" cellpadding="1">
			<tr>
				<td colspan="10" align="center">
					<h1><b>DATA PENYUSUTAN</b></h1>
				</td>
			</tr>
			<tr>
				<td colspan="8">
					<img src="images/btn_add.png" height="30" width="30" title="Tambah" border="0" style="cursor:pointer" onclick="add_click();" />
					<img src="images/btn_interface.png" height="30" width="30" title="Otomatis" border="0" style="cursor:pointer" onclick="auto_click();" />
				</td>
			</tr>
			<tr>
				<td colspan="10">Tipe Penyusutan :
					<select name="py_tipe">
						<option value="0" selected="selected">SEMUA</option>
						<?php
						$dataSql = mysql_query("SELECT id, pyt_tipe FROM penyusutan_tipe WHERE pyt_status=10 ORDER BY id ASC");
						while ($dataRow = mysql_fetch_array($dataSql)) {
							if ($dataRow['id'] == $py_tipe) {
								$cek = " selected";
							} else {
								$cek = "";
							}
							echo "<option value='$dataRow[id]' $cek>$dataRow[pyt_tipe]</option>";
						}
						$sqlData = "";
						?>
					</select>
					<input type="text" id="todate" name="pyd_tanggal" size="9" value="<?=$pyd_tanggal;?>" style="border-collapse:inheri" readonly="true">
					<input type="button" name="cari" value="CARI" style="cursor:pointer" onclick="search_click();">
					<input type="button" name="susut" value="SUSUT" style="cursor:pointer" onclick="susut_click();">
					<input type="button" name="hapus" value="HAPUS" style="cursor:pointer" onclick="hapus_click();">
					<input type="button" name="export_h" value="EXCEL-SUMMARY" style="cursor:pointer" onClick="excel_click();" />
					<input type="button" name="export_d" value="EXCEL-DETAIL" style="cursor:pointer" onClick="excel_detail_click();" />
					<!--<input type="button" name="pdf" value="Export to PDF" style="cursor:pointer" onClick="pdf_click();"/>-->
				</td>
			</tr>
		</table>
		<div class="scroll">
			<table id="myTables" class="display" cellpadding="0" cellspacing="0">
				<tr height="40px;">
					<th>No</th>
					<th>Tanggal</th>
					<th>Nama Barang</th>
					<th>Satuan</th>
					<th>Jumlah</th>
					<th>Umur</th>
					<th>Nilai<br>Perolehan</th>
					<th>Tarif</th>
					<th>Nilai<br><?=$pyd_tanggal;?></th>
					<th>Total<br>Disusutkan</th>
					<th>Sisa</th>
					<th>Nilai Susut<br>/bulan</th>
					<th>Action</th>
				</tr>
				<?php
				$gtnilai = 0;
				$gtpay = 0;
				$gtsisa = 0;
				$gtda= 0;
				$qh = mysql_query("SELECT id, pyt_tipe FROM penyusutan_tipe WHERE $search pyt_status=10 ORDER BY id ASC");
				while ($dh = mysql_fetch_array($qh)) {
				?>
					<tr bgcolor="#FFFFCC">
						<td colspan="13">Tipe Penyusutan :<?= $dh['pyt_tipe']; ?></td>
					</tr>
					<?php
					$no = 1;
					$tnilai = 0;
					$tpay = 0;
					$tsisa = 0;
					$tda= 0;
					$vdate ="";
					$qd = mysql_query("SELECT * FROM penyusutan WHERE py_status<>100 AND py_tipe='$dh[id]'");
					while ($dd = mysql_fetch_array($qd)) {
						$da = mysql_fetch_array(mysql_query("SELECT pyd_nilai FROM penyusutan_detail WHERE pyd_hid='$dd[id]' AND pyd_status=10 AND pyd_tanggal='$pyd_tanggal'"));
						$py = mysql_fetch_array(mysql_query("SELECT SUM(pyd_nilai) susut FROM penyusutan_detail WHERE pyd_hid='$dd[id]' AND pyd_status=10"));
						$sisa   = $dd['py_nilai'] - $py['susut'];
						#if ($sisa > 0) {
							$tnilai = $tnilai + $dd['py_nilai'];
							$tpay   = $tpay + $py['susut'];
							$tsisa  = $tsisa + $sisa;
							$tda    = $tda + $da['pyd_nilai'];
							if($dd['py_nilai']>0 && $dd['py_jumlah']>0 && $dd['py_umur']>0){
								$nsusut = (($dd['py_nilai'] / $dd['py_jumlah']) / ($dd['py_umur']));
							}else{
								$nsusut = 0;
							}
						#validasi taggal
						$yeara	= substr($dd['py_tanggal'],0,4);
						$montha = substr($dd['py_tanggal'],5,2);
						$daysa  = substr($dd['py_tanggal'],8,2);
						$tgla   = "$yeara$montha$daysa";						
						$yearp	= substr($pyd_tanggal,0,4);
						$monthp = substr($pyd_tanggal,5,2);
						$daysp  = substr($pyd_tanggal,8,2);
						$tglp   = "$yearp$monthp$daysp";
						if($tglp >= $tgla){
							$vdate = "OK";
						}else{
							$vdate = "NG";
						}
						#echo $vdate;
					?>
							<tr onmouseover="setTrColor(this, '#FF9900');">
								<td align="center"><?= $no; ?>.</td>
								<td align="center"><?= $dd['py_tanggal']; ?></td>
								<td align="left"><?= $dd['py_nmbarang']; ?></td>
								<td align="center"><?= $dd['py_stbarang']; ?></td>
								<td align="right"><?= number_format($dd['py_jumlah'], 0, '.', ','); ?></td>
								<td align="right"><?= number_format($dd['py_umur'], 0, '.', ','); ?>&nbsp;bulan</td>
								<td align="right"><?= number_format($dd['py_nilai'], 2, '.', ','); ?></td>
								<td align="right"><?= number_format($dd['py_tarif'], 2, '.', ','); ?>&nbsp;%</td>
								<td align="right"><?= number_format($da['pyd_nilai'], 2, '.', ','); ?></td>
								<td align="right"><?= number_format($py['susut'], 2, '.', ','); ?></td>
								<td align="right"><?= number_format($sisa, 2, '.', ','); ?></td>
								<td align="right"><?= number_format($nsusut, 2, '.', ','); ?></td>
								<td align="center">
									<input type="hidden" name="pilih2[<?= $dd['id']; ?>]" value="<?= $dd['id']; ?>" />
									<input type="hidden" name="pyd_nilai2[<?= $dd['id']; ?>]" value="<?= $nsusut; ?>" />
									<input type="hidden" name="pyd_nmbarang2[<?= $dd['id']; ?>]" value="<?= $dd['py_nmbarang']; ?>" />
									<input type="hidden" name="vdate2[<?= $dd['id']; ?>]" value="<?= $vdate; ?>" />
									<img src="images/btn_edit.png" width="20" height="20" border="0" title="Edit" style="cursor:pointer" onclick="detail_click('<?= $dd['id']; ?>');" />
									<img src="images/btn_delete.png" width="20" height="20" border="0" title="Hapus" style="cursor:pointer" onclick="delete_click('<?= $dd['id']; ?>','<?= $dd['py_nmbarang']; ?>');" />
								</td>
							</tr>
					<?php $no++;
						#}
					}  ?>
					<tr bgcolor="#CCFFCC">
						<td>&nbsp;</td>
						<td colspan="5"><b>Total</b></td>
						<td align="right"><b><?= number_format($tnilai, 2, '.', ','); ?></b></td>
						<td>&nbsp;</td>
						<td align="right"><b><?= number_format($tda, 2, '.', ','); ?></b></td>
						<td align="right"><b><?= number_format($tpay, 2, '.', ','); ?></b></td>
						<td align="right"><b><?= number_format($tsisa, 2, '.', ','); ?></b></td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
					</tr>
				<?php
					$gtnilai = $gtnilai + $tnilai;
					$gtpay  = $gtpay + $tpay;
					$gtsisa = $gtsisa + $tsisa;
					$gtda   = $gtda + $tda;
				}  ?>
				<tr bgcolor="#CCFFFF">
					<td>&nbsp;</td>
					<td colspan="5"><b>Grand Total</b></td>
					<td align="right"><b><?= number_format($gtnilai, 2, '.', ','); ?></b></td>
					<td>&nbsp;</td>
					<td align="right"><b><?= number_format($gtda, 2, '.', ','); ?></b></td>
					<td align="right"><b><?= number_format($gtpay, 2, '.', ','); ?></b></td>
					<td align="right"><b><?= number_format($gtsisa, 2, '.', ','); ?></b></td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
			</table>
		</div>
	</form>
</body>

</html>