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

<?php
include '../../inc/inc.koneksi.php';
include '../../inc/fungsi_tanggal.php';
		
$tanggal= $_GET[tgl];
$level	= $_GET[level];
$cari	= $_GET[cari];
$thn = (int) substr($tanggal, 0, 4);
$bln = (int) substr($tanggal, 5, 2);
$tgl = (int) substr($tanggal, 8, 2);
$kab = $thn;

while($kab>4){
	$kab = $kab-4;
}
$tgl=$tgl+1;

if($bln==1){
	if($tgl==32){
		$tgl=1;
		$bln=$bln+1;
	}
}else if($bln==2){
	if($kab>0){
		if($tgl==29){
			$tgl=1;
			$bln=$bln+1;
		}
	}else{
		if($tgl==30){
			$tgl=1;
			$bln=$bln+1;
		}
	}
}else if($bln==3){
	if($tgl==32){
		$tgl=1;
		$bln=$bln+1;
	}
}else if($bln==4){
	if($tgl==31){
		$tgl=1;
		$bln=$bln+1;
	}
}else if($bln==5){
	if($tgl==32){
		$tgl=1;
		$bln=$bln+1;
	}
}else if($bln==6){
	if($tgl==31){
		$tgl=1;
		$bln=$bln+1;
	}
}else if($bln==7){
	if($tgl==32){
		$tgl=1;
		$bln=$bln+1;
	}
}else if($bln==8){
	if($tgl==32){
		$tgl=1;
		$bln=$bln+1;
	}
}else if($bln==9){
	if($tgl==31){
		$tgl=1;
		$bln=$bln+1;
	}
}else if($bln==10){
	if($tgl==32){
		$tgl=1;
		$bln=$bln+1;
	}
}else if($bln==11){
	if($tgl==31){
		$tgl=1;
		$bln=$bln+1;
	}
}else{
	if($tgl==32){
		$tgl=1;
		$bln=1;
		$thn=$thn+1;
	}
}
$tgl=sprintf("%02s",$tgl);
$bln=sprintf("%02s",$bln);
$tglaw=$thn.'-'.$bln.'-'.$tgl;

$query1=mysql_query("SELECT max(tanggal) as tg_akhir FROM rekapjur ORDER BY tanggal");
$r1=mysql_fetch_array($query1);
$tglak=$r1[tg_akhir];

$query2	= mysql_query("SELECT * FROM company");
	$r2			= mysql_fetch_assoc($query2);
	$kop1 		= $r2[nama1];
	$kop2 		= $r2[nama2];
	$kop3 		= $r2[nama3];
	
	echo "<table width='100%'>
		<tr>
			<td style='font:arial 11px'><b>$kop1</b></td>
		</tr>
		<tr>
			<td style='font:arial 11px'>$kop2</td>
		</tr>
		<tr>
			<td style='font:arial 11px'>$kop3</td>
		</tr>
	</table>
		
	<table width='100%'>
		<tr>
			<td align='center' style='font:arial 14px'><b>NERACA SALDO</b></td>
		</tr>
		<tr>
			<td align='center' style='font:arial 14px'><b>PER TANGGAL ".jin_date_str($tanggal)."</b></td>
		</tr>
	</table>
	<br>
	
	<table width='700' border='1' style='border-collapse:collapse;font:12px arial'>
	<tr>			
			<th>Kode</th>
			<th>Nama Akun</th>
			<th>Debet</th>
			<th>Kredit</th>
		</tr>";
		
		if (!empty($cari)){
			$query1= "SELECT * FROM gol_akun WHERE (nagol LIKE '%$cari%' or nogol LIKE '%$cari%') ORDER BY nogol";
		}
		else{
			$query1= "SELECT * FROM gol_akun ORDER BY nogol";
		}
		$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 BETWEEN '$tglaw' AND '$tglak' AND nosub='$norek' AND batal='' GROUP BY nosub");
				}else{
					$norek=$r[noakun];
					$sql1=mysql_query("SELECT sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE tanggal BETWEEN '$tglaw' AND '$tglak' AND noakun='$norek' AND batal='' 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>$r[noakun]</td>
					<td>$r[naakun]</td>
					<td align='right'>".number_format($debet,0)."</td>
					<td align='right'>".number_format($kredit,0)."</td>
				</tr>";							
			}
			echo "<tr>				
				<td></td>
				<td>Jumlah</td>
				<td align='right'>".number_format($sdebet,0)."</td>
				<td align='right'>".number_format($skredit,0)."</td>
				</tr>";
				$tdebet=$tdebet+$sdebet;
				$tkredit=$tkredit+$skredit;
		}
		echo "<tr>			
		<tr>
			<td></td>
			<td>Total</td>
			<td align='right'>".number_format($tdebet,0)."</td>
			<td align='right'>".number_format($tkredit,0)."</td>
		</tr>
	</table>";
?>
<script type="text/javascript">	
	history.back();
</script>