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

$table ="retur_pb";

date_default_timezone_set('Asia/Jakarta');

$kode	=$_POST['kode'];
$tanggal=$_POST['tanggal'];
$kodesup=$_POST['kodesup'];
$namasup=$_POST['namasup'];
$nomorfk=$_POST['nomorfk'];
$total	=$_POST['total'];
$caraby	=$_POST['caraby'];
$user	=$_SESSION['namauser'];
$jam	=date('H:i:s');

$sql= mysql_query("SELECT * FROM interface");
$r 	= mysql_fetch_assoc($sql);
$kas 		= $r['kas'];
$persed 	= $r['persed'];
$utdg 		= $r['hutang_dg'];
$rugiretur 	= $r['rugi_retur'];
$labapb 	= $r['laba_pb'];
$labrug 	= $r['laba_rugi'];
$nosubps 	= substr($persed,0,3);
$nosubut 	= substr($utdg,0,3);
$noutdg 	= $utdg.$kodesup;
$nosubks	= substr($kas,0,3);
$nosubrr	= substr($rugiretur,0,3);
$nosublpb	= substr($labapb,0,3);
$nosublr 	= substr($labrug,0,3);

$sql = mysql_query("SELECT * FROM $table WHERE kode_rb='$kode' AND posting='' ORDER BY no_urut");
$row = mysql_num_rows($sql);

if($row>0){
	$trugi=0;
	$tasal=0;
	$total=0;

	while($r=mysql_fetch_array($sql)){
		$nourut		=$r['no_urut'];
		$kodest 	=$r['kode_st'];
		$jmlretur	=$r['jml_retur'];
		$hgbeli  	=$r['hg_beli'];
		$ket		=$r['ket'];
		$rugi		=$r['jumlah_asal']-$r['jumlah'];
		$trugi		=$trugi+$rugi;
		$tasal		=$tasal+$r['jumlah_asal'];
		$total		=$total+$r['jumlah'];
	
		mysql_query("UPDATE stock SET jml_stokb=jml_stokb-$jmlretur WHERE kode_st='$kodest' AND kode_cab='01'");
		mysql_query("INSERT INTO rekap_mbr (kode_tr,jurnal,tanggal,no_bukti,no_urut,kode_st,gudang,keterangan,masuk,keluar,harga,user,jam,batal) VALUES('$kode','RPB','$tanggal','$kode','$nourut','$kodest','Gudang','Retur $ket','0','$jmlretur','$hgbeli','$user','$jam','')");
	}

	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,kode_cab,no_urut,noakun,nosub,nobp,nofak,debet,kredit,keterangan,user,jam) VALUES
		('$kode','RPB','$tanggal','$kode','01','$nourut','$kas','$nosubks','','',$total','0','Retur faktur $nomorfk $namasup','$user','$jam')");
	}
	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 kode_sp='$kodesup' AND nofak='$nomorfk'");
	
		mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,kode_cab,no_urut,noakun,nosub,nobp,nofak,debet,kredit,keterangan,user,jam) VALUES
		('$kode','RPB','$tanggal','$kode','01','$nourut','$utdg','$nosubut','$noutdg','','$total','0','Retur faktur $nomorfk $namasup','$user','$jam')");
	}
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd+$tasal WHERE noakun='$persed'");
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,kode_cab,no_urut,noakun,nosub,nobp,nofak,debet,kredit,keterangan,user,jam) VALUES
	('$kode','RPB','$tanggal','$kode','01','$nourut','$persed','$nosubps','','','0','$tasal','Retur faktur $nomorfk $namasup','$user','$jam')");

	if(!($trugi==0)){
		mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db+$trugi WHERE noakun='$rugiretur'");
		mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,kode_cab,no_urut,noakun,nosub,nobp,nofak,debet,kredit,keterangan,user,jam) VALUES
		('$kode','RPB','$tanggal','$kode','01','$nourut','$rugiretur','$nosubrr','','',$trugi','0','Retur faktur $nomorfk $namasup','$user','$jam')");

		mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db+$trugi WHERE noakun='$labapb'");
		mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,kode_cab,no_urut,noakun,nosub,nobp,nofak,debet,kredit,keterangan,user,jam) VALUES
		('$kode','RPB','$tanggal','$kode','01','$nourut','$labapb','$nosublpb','','','$trugi','0','Retur faktur $nomorfk $namasup','$user','$jam')");
	
		mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd+$trugi WHERE noakun='$labrug'");
		mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,kode_cab,no_urut,noakun,nosub,nobp,nofak,debet,kredit,keterangan,user,jam) VALUES
		('$kode','RPB','$tanggal','$kode','01','$nourut','$labrug','$nosublr','','','0','$trugi','Retur faktur $nomorfk $namasup','$user','$jam')");
	}

	mysql_query("UPDATE $table SET posting='P' WHERE kode_rb='$kode'");

	echo "Data retur pembelian nomor $kode sukses disimpan.";
}
?>