<script type="text/javascript">
$(document).ready(function() {
    $(function() {
        $("#theList tr:even").addClass("stripe1");
        $("#theList tr:odd").addClass("stripe2");

        $("#theList tr").hover(
            function() {
                $(this).toggleClass("highlight");
            },
            function() {
                $(this).toggleClass("highlight");
            }
        );
    });	
});

	function pilih(ID){
		var noakun  = ID;
		var tglawal = $("#txt_tglawal").val();
		var tglakhir= $("#txt_tglakhir").val();
						
		$.ajax({
			type	: "POST",
			url		: "modul/akun_bp/cari.php",
			data	: "noakun="+noakun,
			dataType: "json",
			success	: function(data){
				$("#t_noakun").val(noakun);
				$("#t_naakun").val(data.naakun);
				$("#t_kode").val(data.kode);
				$("#pencarian_bp").hide();
				$("#tampil_data_bp").hide();
				$("#info_bp").show();
				$("#tampil_detail_bp").show();
				$("#tampil_detail_bp").load("modul/akun_bp/tampil_detail_bp.php?noakun="+noakun+"&tglawal="+tglawal+"&tglakhir="+tglakhir);
			}
		});
	}
</script>

<?php
include '../../inc/inc.koneksi.php';

$tglaw 	= $_GET['tgl_awal'];
$tglak 	= $_GET['tgl_akhir'];
$cari	= $_GET['cari'];

echo "<table id='theList' width='100%'>
		<tr>
			<th>No.</th>
			<th>Kode</th>
			<th>Nama Akun</th>
			<th>Debet</th>
			<th>Kredit</th>
			<th>View</th>
		</tr>";
		if(!empty($cari))
			$sql1= "SELECT nobb FROM akun_bp WHERE (naakun LIKE '%$cari%' or noakun LIKE '%$cari%') GROUP BY nobb";
		else
			$sql1= "SELECT * FROM akun_bp GROUP BY nobb";
		$data1 = mysql_query($sql1);
		
		$jml=1;
		$tdebet=0;
		$tkredit=0;
		while($r1=mysql_fetch_array($data1)){
			$nobb=$r1['nobb'];
			
			$sql2	= mysql_query("SELECT * FROM akun_bb WHERE noakun='$nobb' ");
			$r2		= mysql_num_rows($sql2);
			$nabb = $r2['naakun'];

			echo "<tr>
				<td></td>
				<td><b>$nobb</b></td>			
				<td><b>$nabb</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 tanggal,nobp,sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE tanggal>='$tglaw' AND nobp='$norek' GROUP BY nobp");
				
				$rdebet=0;
				$rkredit=0;
				while($r1=mysql_fetch_array($query1)){
					$rdebet=$rdebet+$r1['jdebet'];
					$rkredit=$rkredit+$r1['jkredit'];
				}
				
				if($kode=='D'){
					$mutasi=$rdebet-$rkredit;
					$debet=$debet-$mutasi;
					$sdebet=$sdebet+$debet;
					$kredit='';
					
				}else{	
					$mutasi=$rkredit-$rdebet;
					$kredit=$kredit-$mutasi;
					$skredit=$skredit+$kredit;
					$debet='';
				}
				
				echo "<tr>
					<td>$no</td>
					<td>$r[noakun]</td>
					<td>$r[naakun]</td>					
					<td align='right'>".number_format($debet,0)."</td>
					<td align='right'>".number_format($kredit,0)."</td>
					<td align='center'>
						<a href='javascript:void(0)' onClick=\"pilih('$r[noakun]')\">
							<img src='images/view.png' border='0' id='lihat' title='Lihat Buku Besar' width='18' height='18'>
						</a>
					</td>
				</tr>";			
				$no++;				
			}
			echo "<tr>
				<td></td>
				<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>
		<tr>
			<td></td>
			<td></td>
			<td>Total</td>
			<td align='right'>".number_format($tdebet,0)."</td>
			<td align='right'>".number_format($tkredit,0)."</td>
		</tr>
	</table>";
?>