	<?php
	if($_POST['bulan']== $_POST['bulanakh']){
		$ketperiode= monthFull($_POST['bulan']);
	}else{
		$ketperiode= monthFull($_POST['bulan'])." sd ".monthFull($_POST['bulanakh']);
	}
	
	ini_set("max_execution_time", 0);
	header("Content-Disposition: attachment; filename=\"Buku Besar Periode  ". $ketperiode." ".$_POST['tahun'].".xls\"");
	header("Content-type: application/vnd.ms-excel");  
	?>
	<h3 align="center">Buku Besar <?php //echo $rekening;?></h3>
	<h4 align="center">Periode <?php echo $ketperiode;?> Tahun <?php echo $_POST['tahun'];?></h4>
	<table border="1">
		<thead>
			<tr>
				<th>#</th>
				<th>Tahun</th>
				<th>Kode Rekening</th>
				<th>Nama Rekening</th>
				<th>Type</th>
				<th>Balance</th>
				<th>Saldo Awal</th>
				<th>Debit</th>
				<th>Kredit</th>
				<th>Saldo Akhir</th>					
			</tr>
		</thead>
		
		<?php $no=1;$saldo=0;$rekening=array('4','5','6');												
			foreach($result as $dtk){ 
				
				if(isset($debitz[$dtk->kd_rek])){
					$debitxz=$debitz[$dtk->kd_rek];
				}else{
					$debitxz=0;
				}
				
				if(isset($kreditz[$dtk->kd_rek])){
					$kreditxz=$kreditz[$dtk->kd_rek];
				}else{
					$kreditxz=0;
				}
				
				if(isset($debitrealz[trim($dtk->kd_rek)])){
					$debitrealxz=$debitrealz[trim($dtk->kd_rek)];
				}else{
					$debitrealxz=0;
				}
				
				if(isset($kreditrealz[trim($dtk->kd_rek)])){
					$kreditrealxz=$kreditrealz[trim($dtk->kd_rek)];
				}else{
					$kreditrealxz=0;
				}
			
				if(!in_array(substr($dtk->kd_rek,0,1),$rekening)){	
					if(isset($balancez[trim($dtk->kd_rek)]) AND $balancez[trim($dtk->kd_rek)]=='D'){
						$saldoawl=$saldoz[trim($dtk->kd_rek)]+$debitxz - $kreditxz;
					}else if(isset($balancez[trim($dtk->kd_rek)]) AND $balancez[trim($dtk->kd_rek)]=='K'){
						$saldoawl=$saldoz[trim($dtk->kd_rek)]-$debitxz + $kreditxz;
					}else{
						$saldoawl=0;
					}
				}else{
					$saldoawl=0;
				}	
				
				if($dtk->balance=='D'){
					//$saldoakh=$saldoawl+$dtk->debit - $dtk->kredit;
					$saldoakh=$saldoawl+$debitrealxz - $kreditrealxz;
				}else{
					//$saldoakh=$saldoawl-$dtk->debit + $dtk->kredit;
					$saldoakh=$saldoawl-$debitrealxz + $kreditrealxz;
				}
		?>
			<tr>
				<td><?php echo $no;?></td>
				<td><?php echo $dtk->thn;?></td>
				<td><?php echo $dtk->kd_rek;?></a></td>
				<td><?php echo $dtk->nm_rek;?></td>
				<td><?php echo $dtk->tipe;?></td>
				<td><?php echo $dtk->balance;?></td>
				<td align="right"><?php echo number_format($saldoawl,0,",",",");?></td>
				<td align="right" class=""><?php echo number_format($debitrealxz,0,",",",");?></td>
				<td align="right" class=""><?php echo number_format($kreditrealxz,0,",",",");?></td>
				<td align="right" class=""><?php echo number_format($saldoakh,0,",",",");?></td>	

			</tr>
		<?php $no++; } ?>
			
	</table>