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

$table			="faktur_pb";
$kode			=$_POST[kode];
$kodesup	=$_POST[kodesup];
$total			=$_POST[total];
$potong		=$_POST[potong];
$bayar		=$_POST[bayar];
$kurang 		=$_POST[kurang];

$sql	= mysql_query("SELECT * FROM interface");
$r 				= mysql_fetch_assoc($sql);
$kas 			= $r[kas];
$persed 	= $r[persed];
$utdg 			= $r[hutang_dg];
$potbeli		= $r[pot_beli];
$labapb 		= $r[laba_pb];
$labrug 		= $r[laba_rugi];
$noutdg 		= $utdg.$kodesup;

$sql	= mysql_query("SELECT * FROM $table WHERE kode_pb='$kode' AND batal='' ORDER BY kode_pb");
$row= mysql_num_rows($sql);
if($row>0){
	while ($r=mysql_fetch_array($sql)){
		$kodest=$r[kode_st];
		$junit=$r[jml_unit];
	
		mysql_query("UPDATE stock SET jml_stok=jml_stok-$junit WHERE kode_st='$kodest' ORDER BY kode_st ");
	}
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$total WHERE noakun='$persed'");

	if(!$potong==0){
		mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$potong WHERE noakun='$potbeli'");
		mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$potong WHERE noakun='$labapb'");
		mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$potong WHERE noakun='$labrug'");
	}

	if(!$bayar==0) mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$bayar WHERE noakun='$kas'");

	if(!$kurang==0){
		mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$kurang WHERE noakun='$utdg'");
		mysql_query("UPDATE akun_bp SET mutasi_kd=mutasi_kd-$kurang WHERE noakun='$noutdg'");
		mysql_query("DELETE FROM hutang WHERE kode_sp='$kodesup' AND nofak='$nomorfk' ");
	}
	
	mysql_query("DELETE FROM $table WHERE kode_pb='$kode' ORDER BY kode_pb");
	mysql_query("DELETE FROM rekap_mbr WHERE kode_tr='$kode'");
	mysql_query("DELETE FROM rekapjur WHERE kode_tr='$kode'");
	
	echo "Faktur pembelian kode $kode telah dibatalkan.";
}
?>