<script type="text/javascript">	
	window.print();
</script>

<?php
	include '../../inc/inc.koneksi.php';
	include '../../inc/fungsi_tanggal.php';
		
	$tgl	= $_GET['tgl'];
	$level	= $_GET['level'];

	$query2 = mysql_query("SELECT * FROM company");
    $r2=mysql_fetch_assoc($query2);
	$kop1 = $r2['nama1'];
	$kop2 = $r2['nama2'];
	$kop3 = $r2['alamat'];
	
	echo "<table width='100%' style='font:12px arial'><b>
		<tr>
			<td>$kop1</td>
		</tr>
		<tr>
			<td>$kop2</td>
		</tr>
		<tr>
			<td>$kop3</td>
		</tr>
	</table>
	
	<table width='100%'>
		<tr>
			<td align='center' style='font:14px arial'><b>NERACA SALDO</b></td>
		</tr>
		<tr>
			<td align='center' style='font:14px arial'>PER TANGGAL ".jin_date_str($tgl)."</b></td>
		</tr>
	</table>
	<br>
	
	<table width='100%' border='1' style='border-collapse:collapse;font:12px arial'>
		<tr>			
			<th>Kode</th>
			<th>Nama Akun</th>
			<th>Debet</th>
			<th>Kredit</th>
		</tr>";
		
		$data1	= mysql_query("SELECT * FROM gol_akun ORDER BY nogol");
		$jml=1;
		$tdebet=0;
		$tkredit=0;
		while($r1=mysql_fetch_array($data1)){
			$nogol=$r1['nogol'];
			
			echo "<tr>				
				<td><b>$r1[nogol]</b></td>			
				<td><b>$r1[nagol]</b></td>
			</tr>";
		
			$data1	= mysql_query($query1);
		    $jml=1;
		    $tdebet=0;
		    $tkredit=0;
		    while($r1=mysql_fetch_array($data1)){
			    $nogol=$r1['nogol'];

    			echo "<tr>				
	    			<td><b>$r1[nogol]</b></td>			
		    		<td><b>$r1[nagol]</b></td>
			    </tr>";
			
			    if($level=='1')
				    $sql= "SELECT nogol,noakun,naakun,nosub,kode,sum(saldo) as jsaldo,sum(mutasi_db) as mtsdb,sum(mutasi_kd) as mtskd FROM akun_bb WHERE nogol='$nogol' GROUP BY nosub";
			    else
				    $sql= "SELECT nogol,noakun,naakun,nosub,kode,sum(saldo) as jsaldo,sum(mutasi_db) as mtsdb,sum(mutasi_kd) as mtskd FROM akun_bb WHERE nogol='$nogol' AND level='2' GROUP BY noakun";
			    $query = mysql_query($sql);
					
    			$sdebet=0;
	    		$skredit=0;
		    	while($r=mysql_fetch_array($query)){
			    	if($r['kode']=='D'){
				    	$debet=$r['jsaldo']+$r['mtsdb']-$r['mtskd'];
					    $kredit='';
    				}else{
	    				$kredit=$r['jsaldo']+$r['mtskd']-$r['mtsdb'];
		    			$debet='';
			    	}
				    $kode=$r['kode'];
						
    				if($level=='1'){
	    				$norek=$r['nosub'];
		    			$sql1=mysql_query("SELECT sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE tanggal>'$tanggal' AND nosub='$norek' GROUP BY nosub");
				    }else{
					    $norek=$r['noakun'];
					    $sql1=mysql_query("SELECT sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE tanggal>'$tanggal' AND noakun='$norek' GROUP BY noakun");
				    }
				    $r1=mysql_fetch_assoc($sql1);
			
				    if($kode=='D'){					
					    $mutasi=$r1['jdebet']-$r1['jkredit'];
    					$debet=$debet-$mutasi;
	    				$sdebet=$sdebet+$debet;
		    		}else{
			    		$mutasi=$r1['jkredit']-$r1['jdebet'];
				    	$kredit=$kredit-$mutasi;
    					$skredit=$skredit+$kredit;
	    			}
				
		    		echo "<tr>			
			    		<td>$norek</td>
				    	<td>$r[naakun]</td>
					    <td align='right'>".number_format($debet,2)."</td>
    					<td align='right'>".number_format($kredit,2)."</td>
	    			</tr>";							
		    	}
			    echo "<tr>				
    				<td></td>
	    			<td>Jumlah</td>
		    		<td align='right'>".number_format($sdebet,2)."</td>
			    	<td align='right'>".number_format($skredit,2)."</td>
    			</tr>";
				$tdebet=$tdebet+$sdebet;
				$tkredit=$tkredit+$skredit;
		    }
	    }
	    echo "<tr>
    		<td></td>
	    	<td>Total</td>
		   	<td align='right'>".number_format($tdebet,2)."</td>
		    <td align='right'>".number_format($tkredit,2)."</td>
		</tr>
	</table>";
?>
<script type="text/javascript">	
	history.back();
</script>