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

$table	="retur_pj";
$kode		=$_POST[kode];
$kodeub	=$_POST[kodeub];
$kodeplg=$_POST[kodeplg];
$nomorfk=$_POST[nomorfk];
$total		=$_POST[total];
$tothpp	=$_POST[tothpp];
$caraby	=$_POST[caraby];

$sql	= mysql_query("SELECT * FROM interface");
$r 			= mysql_fetch_assoc($sql);
$kas 		= $r[kas];
$piut 		= $r[piutang];
$persed	= $r[persed];
$labapb 	= $r[laba_pb];
$retjual	= $r[retur_jual];
$nohpp 	= $r[hpp];
$labrug 	= $r[laba_rugi];
$nobppi 	= $piut.$kodeplg;

$sql 	= mysql_query("SELECT * FROM $table WHERE kode_rj='$kode' AND batal='' ORDER BY kode_rj");
$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 ");
	}
	$laba=$total-$tothpp;

	// --->retur penjualan debet dikurangi
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$total WHERE noakun='$retjual'");

	// --->persediaan debet dikurangi
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$tothpp WHERE noakun='$persed'");

	// --->kas / piutang kredit dikurangi
	if($caraby=='Tunai'){
		mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$total WHERE noakun='$kas'");
	}
	else{	
		mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$total WHERE noakun='$piut'");	
		mysql_query("UPDATE akun_bp SET mutasi_kd=mutasi_kd-$total WHERE noakun='$nobppi'");
		mysql_query("UPDATE piutang SET jumlah=jumlah+$total WHERE kode_pl='$kodeplg' AND nofak='$nomorfk'");
	}

	// --->hpp kredit dikurangi
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$tothpp WHERE noakun='$nohpp'");

	// --->laba periode berjalan debet dikurangi	
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$laba WHERE noakun='$labapb'");

	// --->laba rugi kredit dikurangi		
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$laba WHERE noakun='$labrug'");

	mysql_query("UPDATE retur_pj SET batal='BATAL' WHERE kode_rj='$kode' ");
	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 penjualan kode $kode telah dibatalkan.";
}
?>