<?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'];
$noutdg = $utdg.$kodesup;

$sql = mysql_query("SELECT * FROM $table WHERE kode_pb='$kode' ORDER BY kode_pb");

while ($r=mysql_fetch_array($sql)){
	$kodest=$r['kode_st'];
	$junit=$r['jml_unit'];
	
	$sql1 = mysql_query("SELECT * FROM stock WHERE kode_st='$kodest' ORDER BY kode_st ");
	while ($r1=mysql_fetch_array($sql1)){
		$jstok=$r1['jml_stok'];
	}
	$stok_akhir=$jstok-$junit;
	
	$input1= "UPDATE stock SET jml_stok='$stok_akhir' WHERE kode_st='$kodest' ORDER BY kode_st ";
	mysql_query($input1);
}

$sql = mysql_query("SELECT noakun,mutasi_db FROM akun_bb WHERE noakun='$persed' ");
$r = mysql_fetch_assoc($sql);
$debet = $r['mutasi_db']-$total;

$input = "UPDATE akun_bb SET mutasi_db='$debet' WHERE noakun='$persed'";
mysql_query($input);

if(!$potong==0){
	$sql = mysql_query("SELECT noakun,mutasi_kd FROM akun_bb WHERE noakun='$potbeli' ");
	$r = mysql_fetch_assoc($sql);
	$kredit = $r['mutasi_kd']-$potong;

	$input	= "UPDATE akun_bb SET mutasi_kd='$kredit' WHERE noakun='$potbeli'";
	mysql_query($input);
}

if(!$bayar==0){
	$sql = mysql_query("SELECT noakun,mutasi_kd FROM akun_bb WHERE noakun='$kas' ");
	$r = mysql_fetch_assoc($sql);
	$kredit = $r['mutasi_kd']-$bayar;

	$input	= "UPDATE akun_bb SET mutasi_kd='$kredit' WHERE noakun='$kas'";
	mysql_query($input);
}

if(!$kurang==0){
	$sql = mysql_query("SELECT noakun,mutasi_kd FROM akun_bb WHERE noakun='$utdg' ");
	$r = mysql_fetch_assoc($sql);
	$kredit = $r['mutasi_kd']-$kurang;

	$input	= "UPDATE akun_bb SET mutasi_kd='$kredit' WHERE noakun='$utdg'";
	mysql_query($input);
	
	$sql = mysql_query("SELECT noakun,mutasi_kd FROM akun_bp WHERE noakun='$noutdg' ");
	$r = mysql_fetch_assoc($sql);
	$kredit = $r['mutasi_kd']-$kurang;

	$input = "UPDATE akun_bp SET mutasi_kd='$kredit' WHERE noakun='$noutdg'";
	mysql_query($input);
		
	$input = "DELETE FROM hutang where kode_sp='$kodesup' AND nofak='$nomorfk'";
	mysql_query($input);
}

$input	= "DELETE FROM $table WHERE kode_pb='$kode' ORDER BY kode_pb";
mysql_query($input);

$input2= "DELETE FROM rekap_mbr WHERE kode_tr='$kode'";
mysql_query($input2);

$input2= "DELETE FROM rekapjur WHERE kode_tr='$kode'";
mysql_query($input2);
echo "Faktur pembelian kode $kode telah dibatalkan.";
?>