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

$kodeaw	=$_POST['kodeaw'];
$kodeak	=$_POST['kodeak'];

$sql= mysql_query("SELECT * FROM interface");
$r	= mysql_fetch_assoc($sql);
$utang  	= $r['hutang_dg'];
$labapb 	= $r['laba_pb'];
$labrug 	= $r['laba_rugi'];
$potbeli	= $r['pot_beli'];

$sql = mysql_query("SELECT * FROM jurbk WHERE kode_bk BETWEEN '$kodeaw' AND '$kodeak' AND posting='P' ORDER BY kode_bk");

while($r=mysql_fetch_array($sql)){
	$kodebk 	= $r['kode_bk'];
	$tanggal	= $r['tanggal'];
	$nokas 		= $r['nokas'];
	$nourut  	= $r['no_urut'];
	$noakun 	= $r['noakun'];
	$nobp 		= $r['nobp'];
	$nofak		= $r['nofak'];
	$kodecab 	= $r['kode_cab'];
	$jumlah		= $r['jumlah'];
	$diskon		= $r['diskon'];
	$ket 		= $r['keterangan'];
	$user 		= $r['user'];	
	$kelakun	= (int) substr($noakun,0,1);
	
	// --->kredit kas bertambah
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$jumlah WHERE noakun='$nokas'");
		
	// --->jika kode akun buku besar adalah hutang dagang, kurangi saldo hutang
	if($noakun==$utang){
		$kodesp = substr($nobp,5,4);
		$sql1 = mysql_query("SELECT kode_sp,nofak,jumlah FROM hutang WHERE kode_sp='$kodesp' AND nofak='$nofak'");
		$r1 = mysql_fetch_assoc($sql1);
		$saldo = $r1['jumlah']+$jumlah+$diskon;
				
		mysql_query("UPDATE hutang SET jumlah='$saldo' WHERE kode_sp='$kodesp' AND nofak='$nofak'");
	}
	
	// --->debet noakun bertambah
	$sql1 = mysql_query("SELECT noakun,mutasi_db FROM akun_bb WHERE noakun='$noakun' ");
	$r1 = mysql_fetch_assoc($sql1);	
	$debet = $r1['mutasi_db']-$jumlah-$diskon;
	
	mysql_query("UPDATE akun_bb SET mutasi_db='$debet' WHERE noakun='$noakun'");
		
	if(!($nobp=='')){
		// --->Jika akun buku pembantu tidak kosong, debet bertambah
		$sql1 = mysql_query("SELECT noakun,mutasi_db FROM akun_bp WHERE noakun='$nobp' ");
		$r1 = mysql_fetch_assoc($sql1);
		$debet = $r1['mutasi_db']-$jumlah-$diskon;

		mysql_query("UPDATE akun_bp SET mutasi_db='$debet' WHERE noakun='$nobp'");
	}
	
	if($kelakun>3){
		// --->laba periode berjalan debet
		mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$jumlah WHERE noakun='$labapb'");
		mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$jumlah WHERE noakun='$labrug'");
	}
	
	if(!($diskon==0)){		
		mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$diskon WHERE noakun='$potbeli'");		
		mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$diskon WHERE noakun='$labapb'");		
		mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$diskon WHERE noakun='$labrug'");
	}
	mysql_query("DELETE FROM rekapjur WHERE kode_tr='$kodebk'");
	
	mysql_query("UPDATE jurbk SET posting='' WHERE kode_bk='$kodebk'");
}
echo "Posting jurnal pengeluaran bank nomor $kodeaw sampai dengan $kodeak telah dibatalkan.";
?>