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

<?php
	include '../../inc/inc.koneksi.php';
	include '../../inc/fungsi_tanggal.php';
		
	$tanggal= $_GET['tgl'];
	$cari	= $_GET['cari'];
	
	if($cari=='')
		$kel='KESELURUHAN';
	else
		$kel=$cari;

	$query2 = mysql_query("SELECT * FROM company");
	$r2=mysql_fetch_assoc($query2);
	$kop1 = $r2['nama1'];
	$kop2 = $r2['nama2'];
	$kop3 = $r2['alamat'];
	
	echo "<table width='700' style='font:12px arial'><b>
		<tr>
			<td>$kop1</td>
		</tr>
		<tr>
			<td>$kop2</td>
		</tr>
		<tr>
			<td>$kop3</td>
		</tr>
	</table>
		
	<table width='700'>
		<tr>
			<td align='center' style='font:14px arial'><b>DAFTAR SALDO BUKU PEMBANTU</b></td>
		</tr>
		<tr>
			<td align='center' style='font:14px arial'><b>PER TANGGAL ".jin_date_str($tanggal)."</b></td>
		</tr>
		<tr>
			<td align='center' style='font:14px arial'><b>KELOMPOK : $kel</b></td>
		</tr>
	</table>
	<br>
	
	<table width='700' border='1' style='border-collapse:collapse;font:12px arial'>
		<tr>
			<th>No.</th>
			<th>Kode</th>
			<th>Nama Akun</th>
			<th>Debet</th>
			<th>Kredit</th>
		</tr>";
		
		$query1= "SELECT a.noakun,a.naakun,b.nobb FROM akun_bb as a JOIN akun_bp as b ON b.nobb=a.noakun GROUP BY b.nobb";
		$data1	= mysql_query($query1);
		$jml=1;
		$tdebet=0;
		$tkredit=0;
		while($r1=mysql_fetch_array($data1)){
			$nobb=$r1['nobb'];

			echo "<tr>
				<td></td>
				<td><b>$r1[nobb]</b></td>			
				<td><b>$r1[naakun]</b></td>
			</tr>";
			
			if(!empty($cari)){
				$sql= "SELECT * FROM akun_bp WHERE (naakun LIKE '%$cari%' or noakun LIKE '%$cari%') AND nobb='$nobb' ORDER BY noakun";
			}else{
				$sql= "SELECT * FROM akun_bp WHERE nobb='$nobb' ORDER BY noakun";
			}
			$query = mysql_query($sql);
		
			$no=1;
			$sdebet=0;
			$skredit=0;
			while($r=mysql_fetch_array($query)){
				if($r['kode']=='D'){
					$debet=$r['saldo']+$r['mutasi_db']-$r['mutasi_kd'];
					$kredit='';
				}else{
					$kredit=$r['saldo']+$r['mutasi_kd']-$r['mutasi_db'];
					$debet='';
				}
				$norek=$r['noakun'];
				$kode=$r['kode'];
								
				$query1 = mysql_query("SELECT sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE tanggal>'$tanggal' AND nobp='$norek' GROUP BY nobp");
				
				$r1     = mysql_fetch_assoc($query1);
				$rdebet = $r1['jdebet'];
				$rkredit= $r1['jkredit'];
				
				if($kode=='D'){
					$mutasi=$rdebet-$rkredit;
					$debet=$debet-$mutasi;
					$sdebet=$sdebet+$debet;
				}else{	
					$mutasi=$rkredit-$rdebet;
					$kredit=$kredit-$mutasi;
					$skredit=$skredit+$kredit;
				}
				
				echo "<tr>
					<td>$no</td>
					<td>$r[noakun]</td>
					<td>$r[naakun]</td>					
					<td align='right'>".number_format($debet,2)."</td>
					<td align='right'>".number_format($kredit,2)."</td>
				</tr>";			
				$no++;				
			}
			echo "<tr>
				<td></td>
				<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></tr>			
		<tr>
			<td></td>
			<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>