<?php
include '../../inc/inc.koneksi.php';
include '../../inc/fungsi_tanggal.php';
		
$tanggal= $_GET[tgl];
$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];

if($cari=='')
	$kel='KESELURUHAN';
else
	$kel=$cari;
		
$content = "<b><li>DAFTAR SALDO BUKU PEMBANTU</li>
	<li>PER TANGGAL ".jin_date_str($tgl)."</li></b>
	<li>KELOMPOK : $kel</li></b>
	<br>
	
	<table width='700' border='1' style='border-collapse:collapse'>
	<tr>			
		<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($r_data1=mysql_fetch_array($data1)){
		$nobb=$r_data1[nobb];
			
		$content .= "<tr>				
			<td align='left'><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];
							
			$sql1	= "SELECT sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE tanggal BETWEEN '$tglaw' AND '$tglak' AND nobp='$norek' AND batal='' GROUP BY nobp";
			$query1 = mysql_query($sql);
				
			$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;
			}else{	
				$mutasi=$rkredit-$rdebet;
				$kredit=$kredit-$mutasi;
				$skredit=$skredit+$kredit;
			}
				
			$content .= "<tr>
				<td>$r[noakun]</td>
				<td>$r[naakun]</td>					
				<td align='right'>$debet</td>
				<td align='right'>$kredit</td>
			</tr>";				
			$jml++;
		}
			
		$content .= "<tr>
			<td></td>				
			<td align='right'>Jumlah</td>
			<td align='right'>$sdebet</td>
			<td align='right'>$skredit</td>
		</tr>";
		$tdebet=$tdebet+$sdebet;
		$tkredit=$tkredit+$skredit;
	}
		
	$content .= "<tr>
		<tr>			
			<td></td>
			<td align='right'>Total</td>
			<td align='right'>".number_format($tdebet,0)."</td>
			<td align='right'>".number_format($tkredit,0)."</td>
		</tr>";
	$content .= "</table>";
		
	$content .= "<li>Jumlah Data : $jml</li>";
				
	header("Content-type: application/x-msdownload");
	header("Content-Disposition: attachment; filename=daftar_saldo_bp.xls");
	header("Pragma: no-cache");
	header("Expires: 0");
	echo $content;
?>
