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

$table		="retur_pb";

$kode			=$_POST[kode];
$tanggal		=$_POST[tanggal];
$kodesup	=$_POST[kodesup];
$namasup	=$_POST[namasup];
$nomorfk	=$_POST[nomorfk];
$total			=$_POST[total];
$caraby		=$_POST[caraby];
$user			=$_SESSION[namauser];

$sql	= mysql_query("SELECT * FROM interface");
$r 	= mysql_fetch_assoc($sql);
$kas 			= $r[kas];
$persed 	= $r[persed];
$utdg 			= $r[hutang_dg];
$nosubps 	= substr($persed,0,3);
$nosubut 	= substr($utdg,0,3);
$noutdg 		= $utdg.$kodesup;
$nosubks 	= substr($kas,0,3);

$sql = mysql_query("SELECT * FROM $table WHERE kode_rb='$kode' AND posting='' ORDER BY kode_rb");
$row= mysql_num_rows($sql);
if($row>0){
	while ($r=mysql_fetch_array($sql)){
		$tanggal	=$r[tanggal];		
		$kodest 	=$r[kode_st];		
		$jmlretur	=$r[jml_retur];
		$hgbeli  	=$r[hg_beli];	
		$ket		=$r[ket];
	
		mysql_query("UPDATE stock SET jml_stok=jml_stok-$jmlretur WHERE kode_st='$kodest'");
		mysql_query("INSERT INTO rekap_mbr (kode_tr,jurnal,tanggal,no_bukti,kode_st,keterangan,keluar,harga,user) VALUES('$kode','RPB','$tanggal','$kode','$kodest','Retur $ket','$jmlretur','$hgbeli','$user')");
	}

	if($caraby=='Tunai'){
		mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db+$total WHERE noakun='$kas'");	
		mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,debet,keterangan,user) VALUES('$kode','RPB','$tanggal','$kode','$kas','$nosubks','$total','Retur faktur $nomorfk $namasup','$user')");
	}
	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("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,nobp,debet,keterangan,user) VALUES('$kode','RPB','$tanggal','$kode','$utdg','$nosubut','$noutdg','$total','Retur faktur $nomorfk $namasup','$user')");
	}

	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd+$total WHERE noakun='$persed'");
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,kredit,keterangan,user) VALUES('$kode','RPB','$tanggal','$kode','$persed','$nosubps','$total','Retur faktur $nomorfk $namasup','$user')");
	mysql_query("UPDATE $table SET posting='P' WHERE kode_rb='$kode'");

	echo "Data sukses disimpan";
}
?>