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

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

$tanggal= $_GET['tanggal'];
$level	= $_GET['level'];
$idnrc	= 1;

// 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>'$tanggal' AND nosub='$norek' GROUP BY nosub");
	}else{
		$norek=$r['noakun'];
		$sql1=mysql_query("SELECT sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE tanggal>'$tanggal' AND noakun='$norek' 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' 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>'$tanggal' AND nosub='$norek' GROUP BY nosub");
	}else{
		$norek=$r['noakun'];
		$sql1=mysql_query("SELECT sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE tanggal>'$tanggal' AND noakun='$norek' 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>'$tanggal' AND nosub='$norek' GROUP BY nosub");
	}else{
		$norek=$r['noakun'];
		$sql1=mysql_query("SELECT sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE tanggal>'$tanggal' AND noakun='$norek' 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>'$tanggal' AND nosub='$norek' GROUP BY nosub");
	}else{
		$norek=$r['noakun'];
		$sql1=mysql_query("SELECT sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE tanggal>'$tanggal' AND noakun='$norek' 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>'$tanggal' AND nosub='$norek' GROUP BY nosub");
	}else{
		$norek=$r['noakun'];
		$sql1=mysql_query("SELECT sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE tanggal>'$tanggal' AND noakun='$norek' 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>'$tanggal' AND nosub='$norek' GROUP BY nosub");
	}else{
		$norek=$r['noakun'];
		$sql1=mysql_query("SELECT sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE tanggal>'$tanggal' AND noakun='$norek' 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>'$tanggal' AND nosub='$norek' GROUP BY nosub");
	}else{
		$norek=$r['noakun'];
		$sql1=mysql_query("SELECT sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE tanggal>'$tanggal' AND noakun='$norek' 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;	

$sql2= mysql_query("SELECT * FROM company");
while($r2=mysql_fetch_array($sql2)){
	$kop1 = $r2['nama1'];
	$kop2 = $r2['nama2'];
	$kop3 = $r2['alamat'];
}

echo "<table width='100%' style='font:12px arial'><b>
		<tr>
			<td>$kop1</td>
		</tr>
		<tr>
			<td>$kop2</td>
		</tr>
		<tr>
			<td>$kop3</td>
		</tr>
	</table>
		
	<table width='100%'><b>
		<tr>
			<td align='center' style='font:14px arial'><b>LAPORAN NERACA PERUSAHAAN</b></td>
		</tr>
		<tr>
			<td align='center' style='font:14px arial'><b>PER ".tgl_indo($tanggal)."</b></td>
		</tr>
	</b></table>
		
	<table width='100%' style='border:solid;font:13px arial'>
		<tr>
			<td> </td>
			<td></td>
			<td></td>
			<td></td>
			<td style='border-right:double 2px'></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
		</tr>";
		
		$query = "SELECT * FROM neraca ";		
		$sql	= mysql_query($query);
		$taktiv=0;
		$tpassiv=0;
		
		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></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'></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>";
?>
<script type="text/javascript">	
	history.back();
</script>