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

$table	="retur_pb";
$kode			=$_POST[kode];
$kodesup	=$_POST[kodesup];
$total			=$_POST[total];
$caraby		=$_POST[caraby];

$sql	= mysql_query("SELECT * FROM interface");
$r 	= mysql_fetch_assoc($sql);
$kas 		= $r[kas];
$persed	= $r[persed];
$utdg 		= $r[hutang_dg];
$noutdg 	= $utdg.$kodesup;

$sql = mysql_query("SELECT * FROM $table WHERE kode_rb='$kode' AND batal='' ORDER BY kode_rb");
$row= mysql_num_rows($sql);
if($row>0){
	while ($r=mysql_fetch_array($sql)){
		$jmlretur	=$r[jml_retur];
		mysql_query("UPDATE stock SET jml_stok=jml_stok+$jmlretur WHERE kode_st='$kodest'");
	}

	if($caraby=='Tunai'){
		mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$total WHERE noakun='$kas'");	
	}
	else{
		mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$total WHERE noakun='$utdg'");
		mysql_query("UPDATE akun_bp SET mutasi_db=mutasi_db-$total WHERE noakun='$noutdg'");
		mysql_query("UPDATE hutang SET jumlah=jumlah+$total WHERE kodesp='$kodesup' AND nofak='$nomorfk'");
	}

	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$total WHERE noakun='$persed'");
	mysql_query("DELETE FROM $table WHERE kode_rb='$kode' ORDER BY kode_rb");
	mysql_query("DELETE FROM rekap_mbr WHERE kode_tr='$kode' ORDER BY kode_tr");
	mysql_query("DELETE FROM rekapjur WHERE kode_tr='$kode' ");

	echo "Retur pembelian kode $kode telah dibatalkan.";
}
?>