<style type="text/css">
#info {
	font-size:12px;
	font-weight:bold;
	color:#F00;
}
</style>

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

$tanggal= $_GET[tanggal];
$level	= $_GET[level];
$idnrc	= 1;
$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];

// hapus data tampungan neraca
mysql_query("DELETE FROM neraca ");

// merekam data tampungan neraca
// rekam aktiva lancar
mysql_query("INSERT INTO neraca (id_nrc,ket_aktiva) VALUES('$idnrc','AKTIVA LANCAR :')");
$idnrc++;

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='11' 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='11' AND level='2' GROUP BY noakun";
}
$query = mysql_query($sql);

$ket_total='                       Jumlah';
$saktiv=0;
while($r=mysql_fetch_array($query)){
	$aktiv=$r[jsaldo]+$r[mtsdb]-$r[mtskd];		
	$naakun='    '.$r[naakun];
	$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);
	$mutasi=$r1[jdebet]-$r1[jkredit];
	$aktiv=$aktiv-$mutasi;
	$saktiv=$saktiv+$aktiv;

	mysql_query("INSERT INTO neraca (id_nrc,ket_aktiva,rp_aktiva,jml_aktiva) VALUES('$idnrc','$naakun','Rp','$aktiv')");
	$idnrc++;
}
mysql_query("INSERT INTO neraca (id_nrc,ket_aktiva,rp_aktiva,jml_aktiva) VALUES('$idnrc','$ket_total','Rp','$saktiv')");
$idnrc++;
mysql_query("INSERT INTO neraca (id_nrc,ket_aktiva,rp_aktiva,jml_aktiva) VALUES('$idnrc','','','')");
$idnrc++;

// rekam investasi jangka panjang
mysql_query("INSERT INTO neraca (id_nrc,ket_aktiva) VALUES('$idnrc','INVESTASI JANGKA PANJANG :')");
$idnrc++;

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='12' 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='12' AND level='2' ORDER BY noakun";
}
$query = mysql_query($sql);

$saktiv=0;
while($r=mysql_fetch_array($query)){	
	$aktiv=$r[jsaldo]+$r[mtsdb]-$r[mtskd];	
	$naakun='    '.$r[naakun];
	$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);
	$mutasi=$r1[jdebet]-$r1[jkredit];
	$aktiv=$aktiv-$mutasi;
	$saktiv=$saktiv+$aktiv;

	mysql_query("INSERT INTO neraca (id_nrc,ket_aktiva,rp_aktiva,jml_aktiva) VALUES('$idnrc','$naakun','Rp','$aktiv')");
	$idnrc++;
}
mysql_query("INSERT INTO neraca (id_nrc,ket_aktiva,rp_aktiva,jml_aktiva) VALUES('$idnrc','$ket_total','Rp','$saktiv')");
$idnrc++;
mysql_query("INSERT INTO neraca (id_nrc,ket_aktiva,rp_aktiva,jml_aktiva) VALUES('$idnrc','','','')");
$idnrc++;

// rekam aktiva tetap
mysql_query("INSERT INTO neraca (id_nrc,ket_aktiva) VALUES('$idnrc','AKTIVA TETAP :')");
$idnrc++;

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='13' 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='13' AND level='2' GROUP BY noakun";
}
$query = mysql_query($sql);

$saktiv=0;
while($r=mysql_fetch_array($query)){	
	$aktiv=$r[jsaldo]+$r[mtsdb]-$r[mtskd];	
	$naakun='    '.$r[naakun];
	$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);
	$mutasi=$r1[jdebet]-$r1[jkredit];
	$aktiv=$aktiv-$mutasi;
	$saktiv=$saktiv+$aktiv;

	mysql_query("INSERT INTO neraca (id_nrc,ket_aktiva,rp_aktiva,jml_aktiva) VALUES('$idnrc','$naakun','Rp','$aktiv')");
	$idnrc++;
}
mysql_query("INSERT INTO neraca (id_nrc,ket_aktiva,rp_aktiva,jml_aktiva) VALUES('$idnrc','$ket_total','Rp','$saktiv')");
$idnrc++;
mysql_query("INSERT INTO neraca (id_nrc,ket_aktiva,rp_aktiva,jml_aktiva) VALUES('$idnrc','','','')");
$idnrc++;

// rekam aktiva lain
mysql_query("INSERT INTO neraca (id_nrc,ket_aktiva) VALUES('$idnrc','AKTIVA LAIN :')");
$idnrc++;

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='14' 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='14' AND level='2' GROUP BY noakun";
}
$query = mysql_query($sql);

$saktiv=0;
while($r=mysql_fetch_array($query)){	
	$aktiv=$r[jsaldo]+$r[mtsdb]-$r[mtskd];	
	$naakun='    '.$r[naakun];
	$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);
	$mutasi=$r1[jdebet]-$r1[jkredit];
	$aktiv=$aktiv-$mutasi;
	$saktiv=$saktiv+$aktiv;

	mysql_query("INSERT INTO neraca (id_nrc,ket_aktiva,rp_aktiva,jml_aktiva) VALUES('$idnrc','$naakun','Rp','$aktiv')");
	$idnrc++;
}
mysql_query("INSERT INTO neraca (id_nrc,ket_aktiva,rp_aktiva,jml_aktiva) VALUES('$idnrc','$ket_total','Rp','$saktiv')");

// merekam data passiva
// rekam hutang lancar

$idnrc=1;
$sql = "SELECT * FROM neraca WHERE id_nrc='$idnrc' ";
mysql_query($sql);
mysql_query("UPDATE neraca SET ket_passiva='HUTANG LANCAR :' WHERE id_nrc='$idnrc'");
$idnrc++;

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='21' 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='21' AND level='2' GROUP BY noakun";
}
$query = mysql_query($sql);

$spassiv=0;
while($r=mysql_fetch_array($query)){	
	$passiv=$r[jsaldo]+$r[mtskd]-$r[mtsdb];	
	$naakun='    '.$r[naakun];
	$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);
	$mutasi=$r1[jkredit]-$r1[jdebet];
	$passiv=$passiv-$mutasi;
	$spassiv=$spassiv+$passiv;

	mysql_query("SELECT * FROM neraca WHERE id_nrc='$idnrc' ");
	mysql_query("UPDATE neraca SET ket_passiva='$naakun',rp_passiva='Rp',jml_passiva='$passiv' WHERE id_nrc= '$idnrc'");
	$idnrc++;
}
mysql_query("SELECT * FROM neraca WHERE id_nrc='$idnrc' ");
mysql_query("UPDATE neraca SET ket_passiva='$ket_total',rp_passiva='Rp',jml_passiva='$spassiv' WHERE id_nrc= '$idnrc'");
$idnrc++;
$idnrc++;

mysql_query("SELECT * FROM neraca WHERE id_nrc='$idnrc' ");
mysql_query("UPDATE neraca SET ket_passiva='HUTANG JANGKA PANJANG :' WHERE id_nrc='$idnrc'");
$idnrc++;

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='22' 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='22' AND level='2' GROUP BY noakun";
}
$query = mysql_query($sql);

$spassiv=0;
while($r=mysql_fetch_array($query)){	
	$passiv=$r[jsaldo]+$r[mtskd]-$r[mtsdb];		
	$naakun='    '.$r[naakun];
	$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);
	$mutasi=$r1[jkredit]-$r1[jdebet];
	$passiv=$passiv-$mutasi;
	$spassiv=$spassiv+$passiv;

	mysql_query("SELECT * FROM neraca WHERE id_nrc='$idnrc' ");
	mysql_query("UPDATE neraca SET ket_passiva='$naakun',rp_passiva='Rp',jml_passiva='$passiv' WHERE id_nrc= '$idnrc'");
	$idnrc++;
}
mysql_query("SELECT * FROM neraca WHERE id_nrc='$idnrc' ");
mysql_query("UPDATE neraca SET ket_passiva='$ket_total',rp_passiva='Rp',jml_passiva='$spassiv' WHERE id_nrc= '$idnrc'");
$idnrc++;
$idnrc++;

mysql_query("SELECT * FROM neraca WHERE id_nrc='$idnrc' ");
mysql_query("UPDATE neraca SET ket_passiva='MODAL :' WHERE id_nrc='$idnrc'");
$idnrc++;

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='31' 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='31' AND level='2' GROUP BY noakun";
}
$query = mysql_query($sql);

$spassiv=0;
while($r=mysql_fetch_array($query)){
	$passiv=$r[jsaldo]+$r[mtskd]-$r[mtsdb];	
	$naakun='    '.$r[naakun];
	$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);
	$mutasi=$r1[jkredit]-$r1[jdebet];
	$passiv=$passiv-$mutasi;
	$spassiv=$spassiv+$passiv;

	mysql_query("SELECT * FROM neraca WHERE id_nrc='$idnrc' ");
	mysql_query("UPDATE neraca SET ket_passiva='$naakun',rp_passiva='Rp',jml_passiva='$passiv' WHERE id_nrc= '$idnrc'");
	$idnrc++;
}
mysql_query("SELECT * FROM neraca WHERE id_nrc='$idnrc' ");
mysql_query("UPDATE neraca SET ket_passiva='$ket_total',rp_passiva='Rp',jml_passiva='$spassiv' WHERE id_nrc= '$idnrc'");

// menampilkan data tampungan
$query = "SELECT * FROM neraca ";		
$sql	= mysql_query($query);
$taktiv=0;
$tpassiv=0;
echo "<table style='border:solid' width='100%' align='left'>
	<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td style='border-right:double 2px'></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
	</tr>";
		
	while($r=mysql_fetch_array($sql)){
		if($r[ket_aktiva]=='                       Jumlah')
			$taktiv=$taktiv+$r[jml_aktiva];
	
		if($r[ket_passiva]=='                       Jumlah')
			$tpassiv=$tpassiv+$r[jml_passiva];
		$jaktiv=$r[jml_aktiva];
		$jpasiv=$r[jml_passiva];
		
		if($jaktiv==0)
			$t_jaktiv='';
		else
			$t_jaktiv=number_format($r[jml_aktiva],2);
		
		if($jpasiv==0)
			$t_jpasiv='';
		else
			$t_jpasiv=number_format($r[jml_passiva],2);
		
		echo "<tr>
			<td></td>
			<td>$r[ket_aktiva]</td>
			<td>$r[rp_aktiva]</td>			
			<td align='right'>$t_jaktiv</td>
			<td style='border-right:double 2px'></td>			
			<td>$r[ket_passiva]</td>
			<td>$r[rp_passiva]</td>
			<td align='right'>$t_jpasiv</td>
			<td></td>
		</tr>";	
	}

echo "
	<tr>
		<td style='border-top:double 2px'></td>
		<td style='border-top:double 2px' width='33.35%'>Jumlah Aktiva</td>
		<td style='border-top:double 2px'>Rp </td>		
		<td style='border-top:double 2px' align='right'>".number_format($taktiv,2)."</td>
		<td style='border-top:double 2px;border-right:double 2px'></td>
		<td style='border-top:double 2px' width='26.65%'>Jumlah Passiva</td>
		<td style='border-top:double 2px'>Rp </td>
		<td style='border-top:double 2px' width='17.5%' align='right'>".number_format($tpassiv,2)."</td>
		<td style='border-top:double 2px'></td>
	</tr>
	</table>
	<hr>";
?>