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

$tahun	=$_POST[tahun];
$thnnow =(int) $tahun+1;

$sql    = mysql_query("SELECT * FROM interface");
$r      = mysql_fetch_assoc($sql);
$labadit= $r[laba_dit];
$labapb = $r[laba_pb];
$labrug = $r[laba_rugi];
$prive  = $r[prive];

$query  = mysql_query("SELECT noakun,sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE year(tanggal)>$tahun AND batal='' GROUP BY noakun");

while($data=mysql_fetch_array($query)){
	$noakun = $data[noakun];
	$jdebet = $data[jdebet];
	$jkredit= $data[jkredit];
	
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$jdebet,mutasi_kd=mutasi_kd-$jkredit WHERE noakun='$noakun'");
}
echo "Hitung saldo Buku Besar setelah tahun $tahun ...................selesai. <br>";

$query = mysql_query("SELECT nobp,sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE !(nobp='') AND !(nobp='-') AND year(tanggal)>$tahun AND batal='' GROUP BY nobp");

while($data=mysql_fetch_array($query)){
	$nobp   = $data[nobp];
	$jdebet = $data[jdebet];
	$jkredit= $data[jkredit];
	
	mysql_query("UPDATE akun_bp SET mutasi_db=mutasi_db-$jdebet,mutasi_kd=mutasi_kd-$jkredit WHERE noakun='$nobp'");
}
echo "Hitung saldo akhir Buku Pembantu tahun $tahun ..................selesai. <br>";

$sql    = mysql_query("SELECT * FROM akun_bb WHERE noakun='$labapb' ORDER BY noakun");
$r      = mysql_fetch_assoc($sql);
$labath = $r[mutasi_kd]-$r[mutasi_db];

$sql    = mysql_query("SELECT * FROM akun_bb WHERE noakun='$prive' ORDER BY noakun");
$r      = mysql_fetch_assoc($sql);
$privth = $r[mutasi_db]-$r[mutasi_kd];
$labath = $labath+$privth;

mysql_query("UPDATE akun_bb SET saldo_tl=saldo WHERE noakun='$labadit'");
mysql_query("UPDATE akun_bb SET saldo=saldo+$labath WHERE noakun='$labadit'");
mysql_query("UPDATE akun_bb SET mutasi_db=0,mutasi_kd=0,saldo_tl=$labath WHERE noakun='$labapb'");
mysql_query("UPDATE akun_bb SET mutasi_db=0,mutasi_kd=0,saldo_tl=$labath WHERE noakun='$labrug'");
mysql_query("UPDATE akun_bb SET mutasi_db=0,mutasi_kd=0,saldo_tl=$privth WHERE noakun='$prive'");

echo "Penyesuaian saldo Laba Periode Berjalan dan Laba Ditahan .......selesai. <br>";

$query  = mysql_query("SELECT * FROM akun_bb WHERE !(noakun='$labapb') AND level='2' ORDER BY noakun");
while($data=mysql_fetch_array($query)){
	$noakun = $data[noakun];
	$kode   = $data[kode];
	$nokel  = substr($noakun,0,1);
	
	if($kode=='D')
	    $saldo=$data[saldo]+$data[mutasi_db]-$data[mutasi_kd];
	else
	    $saldo=$data[saldo]+$data[mutasi_kd]-$data[mutasi_db];
	
	if($nokel<'4')
	    mysql_query("UPDATE akun_bb SET saldo=$saldo,mutasi_db=0,mutasi_kd=0,saldo_tl=$saldo WHERE noakun='$noakun'");
	else
	    mysql_query("UPDATE akun_bb SET mutasi_db=0,mutasi_kd=0,saldo_tl=$saldo WHERE noakun='$noakun'");
}
mysql_query("UPDATE akun_bb SET tahun=$thnnow WHERE 1");

echo "Penyesuaian saldo akhir Buku Besar tahun $tahun ................selesai. <br>";

$query  = mysql_query("SELECT * FROM akun_bp WHERE 1 ORDER BY noakun");
while($data=mysql_fetch_array($query)){
	$noakun = $data[noakun];
	$kode   = $data[kode];
	$nokel  = substr($noakun,0,1);
	
	if($kode=='D')
	    $saldo=$data[saldo]+$data[mutasi_db]-$data[mutasi_kd];
	else
	    $saldo=$data[saldo]+$data[mutasi_kd]-$data[mutasi_db];
	
	if($nokel<'4')
	    mysql_query("UPDATE akun_bp SET saldo=$saldo,mutasi_db=0,mutasi_kd=0,saldo_tl=$saldo WHERE noakun='$noakun'");
	else
	    mysql_query("UPDATE akun_bp SET mutasi_db=0,mutasi_kd=0,saldo_tl=$saldo WHERE noakun='$noakun'");
}
mysql_query("UPDATE akun_bp SET tahun=$thnnow WHERE 1 ");

echo "Penyesuaian saldo akhir Buku Pembantu tahun $tahun .............selesai. <br>";

$query  = mysql_query("SELECT noakun,sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE year(tanggal)>$tahun AND batal='' GROUP BY noakun");

while($data=mysql_fetch_array($query)){
	$noakun = $data[noakun];
	$jdebet = $data[jdebet];
	$jkredit= $data[jkredit];
	
	mysql_query("UPDATE akun_bb SET mutasi_db=$jdebet,mutasi_kd=$jkredit WHERE noakun='$noakun'");
}
echo "Penyesuaian kembali saldo Buku Besar setelah tahun $tahun.......selesai. <br>";

$query  = mysql_query("SELECT nobp,sum(debet) as jdebet,sum(kredit) as jkredit FROM rekapjur WHERE !(nobp='') AND !(nobp='-') AND year(tanggal)>$tahun AND batal='' GROUP BY nobp");

while($data=mysql_fetch_array($query)){
	$nobp   = $data[nobp];
	$jdebet = $data[jdebet];
	$jkredit= $data[jkredit];
	
	mysql_query("UPDATE akun_bp SET mutasi_db=$jdebet,mutasi_kd=$jkredit WHERE noakun='$nobp'");
}
echo "Penyesuaian kembali saldo Buku Pembantu setelah tahun $tahun..selesai. <br>";

mysql_query("DELETE FROM jurbk WHERE year(tanggal)='$tahun' OR year(tanggal)<$tahun");
mysql_query("DELETE FROM jurbm WHERE year(tanggal)='$tahun' OR year(tanggal)<$tahun");
mysql_query("DELETE FROM jurkk WHERE year(tanggal)='$tahun' OR year(tanggal)<$tahun");
mysql_query("DELETE FROM jurkm WHERE year(tanggal)='$tahun' OR year(tanggal)<$tahun");
mysql_query("DELETE FROM jurnalum WHERE year(tanggal)='$tahun' OR year(tanggal)<$tahun");
mysql_query("DELETE FROM rekapjur WHERE year(tanggal)='$tahun' OR year(tanggal)<$tahun");
mysql_query("DELETE FROM retur_pb WHERE year(tanggal)='$tahun' OR year(tanggal)<$tahun");

echo "Penghapusan seluruh transaksi tahun $tahun dan sebelumnya ......selesai. <br><br>";
echo "Proses Tutup Buku tahun $tahun selesai .................................";
?>