<?php
	include '../../inc/inc.koneksi.php';
	include '../../inc/fungsi_tanggal.php';
		
	$norek	= $_GET['noakun'];
	$tglawal = $_GET['tglawal'];
    $tglakhir= $_GET['tglakhir'];
	
	$query2 = mysql_query("SELECT * FROM akun_bp WHERE noakun='$norek'");
	$r2	    = mysql_fetch_assoc($query2);
	$naakun=$r2[naakun];
	$kode	=$r2[kode];
	if($kode=='D')
		$saldo=$r2[saldo]+$r2[mutasi_db]-$r2[mutasi_kd];
	else 
		$saldo=$r2[saldo]+$r2[mutasi_kd]-$r2[mutasi_db];
	
	$query1= mysql_query("SELECT nobp,sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE tanggal>='$tglawal' AND nobp='$norek' AND batal='' GROUP BY nobp ");
	$jdebet=0;
	$jkredit=0;
	while($r1=mysql_fetch_array($query1)){
		$jdebet=$r1[jdebet];
		$jkredit=$r1[jkredit];
	}
	if($kode=='D')
		$sdawal=$saldo-$jdebet+$jkredit;
	else
		$sdawal=$saldo-$jkredit+$jdebet;
	$sdakhir =$sdawal;
		
	$content = "<b align='center'><li>LAPORAN BUKU PEMBANTU</li>	
		<li align='center'>Periode  ".jin_date_str($tglawal)." S/D ".jin_date_str($tglakhir)."</li>		
		<br>
		<table>
			<tr>				
				<td></td>
				<td>Akun</td>
				<td>: $norek -</td>
				<td>$naakun</td>				
				<td></td>
				<td>Saldo Awal	:</td>
				<td>Rp ".number_format($sdawal,2)."</td>
			</tr>
		</table>
	
		<table width='700' border='1' style='border-collapse:collapse'>
			<tr>				
				<th>Tanggal</th>
				<th>Nomor Bukti</th>
				<th>Kode Trans</th>
				<th>Keterangan</th>
				<th>Debet</th>
				<th>Kredit</th>
				<th>Saldo</th>
			</tr>";
		
		$jdebet=0;
		$jkredit=0;		
			
		$sql		= "SELECT * FROM rekapjur WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND nobp='$norek' AND batal='' ORDER BY tanggal ";
		$query	= mysql_query($sql);
		while($r=mysql_fetch_array($query)){
			if($kode=='D')
				$sdakhir=$sdakhir+$r[debet]-$r[kredit];
			else
				$sdakhir=$sdakhir+$r[kredit]-$r[debet];
			
			$content .= "<tr>					
					<td>".jin_date_str($r[tanggal])."</td>
					<td>$r[no_bukti]</td>
					<td align='center'>$r[kode_tr]</td>
					<td>$r[keterangan]</td>
					<td align='right'>".number_format($r[debet],2)."</td>
					<td align='right'>".number_format($r[kredit],2)."</td>
					<td align='right'>".number_format($sdakhir,2)."</td>
				</tr>";			
				$jdebet=$jdebet+$r[debet];
				$jkredit=$jkredit+$r[kredit];								
			}
		
			$content .= "<tr>				
				<td></td>
				<td></td>
				<td></td>
				<td align='right'>Jumlah </td>
				<td align='right'>".number_format($jdebet,2)."</td>
				<td align='right'>".number_format($jkredit,2)."</td>
			</tr>		
		</table>";
				
		header("Content-type: application/x-msdownload");
		header("Content-Disposition: attachment; filename=bp_$norek.xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		echo $content;
?>
