<?php
include "../../inc/inc.koneksi.php";
session_start();

$table	="retur_pj";
$kode			=$_POST[kode];
$tanggal		=$_POST[tanggal];
$kodeplg	=$_POST[kodeplg];
$namaplg	=$_POST[namaplg];
$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];
$piut 			= $r[piutang];
$persed 	= $r[persed];
$labapb 		= $r[laba_pb];
$retjual		= $r[retur_jual];
$nohpp 		= $r[hpp];
$labrug 		= $r[laba_rugi];
$nosubks	= substr($kas,0,3);
$nosubpi	= substr($piut,0,3);
$nobppi 		= $piut.$kodeplg;
$nosubps	= substr($persed,0,3);
$nosublpb 	= substr($labapb,0,3);
$nosubrj  	= substr($retjual,0,3);
$nosubhpp = substr($nohpp,0,3);
$nosublr  	= substr($labrug,0,3);

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

if($row>0){
	$tothpp=0;
	while($r=mysql_fetch_array($sql)){
		$tanggal	=$r[tanggal];
		$nourut 	=$r[no_urut];
		$kodest 	=$r[kode_st];
		$jmlretur	=$r[jml_retur];
		$hgjual  	=$r[hg_jual];	
		$ket		=$r[ket];	
		$tothpp	=$tothpp+$r[hpp];
	
		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,no_urut,kode_st,keterangan,masuk,harga,user) VALUES('$kode','RPJ','$tanggal','$kode','$nourut','$kodest','Retur $ket','$jmlretur','$hgjual','$user')");
	}
	$laba=$total-$tothpp;

	// --->retur penjualan debet	
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db+$total WHERE noakun='$retjual'");
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,debet,keterangan,user) VALUES('$kode','RPJ','$tanggal','$kode','$retjual','$nosubrj','$total','Retur nota $nomorfk $namaplg','$user')");

	// --->persediaan debet
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db+$tothpp WHERE noakun='$persed'");
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,debet,keterangan,user) VALUES('$kode','RPJ','$tanggal','$kode','$persed','$nosubps','$tothpp','Retur nota $nomorfk $namaplg','$user')");

	// --->kas / piutang kredit
	if($caraby=='Tunai'){
		mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd+$total WHERE noakun='$kas'");
		mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,kredit,keterangan,user) VALUES('$kode','RPJ','$tanggal','$kode','$kas','$nosubks','$total','Retur nota $nomorfk $namaplg','$user')");
	}
	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'");
		mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,nobp,kredit,keterangan,user) VALUES('$kode','RPJ','$tanggal','$kode','$piut','$nosubpi','$nobppi','$total','Retur nota $nomorfk $namaplg','$user')");
	}

	// --->hpp kredit	
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd+$tothpp WHERE noakun='$nohpp'");
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,kredit,keterangan,user) VALUES('$kode','RPJ','$tanggal','$kode','$nohpp','$nosubhpp','$tothpp','Retur nota $nomorfk $namaplg','$user')");

	// --->laba periode berjalan debet	
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db+$laba WHERE noakun='$labapb'");
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,debet,keterangan,user) VALUES('$kode','RPJ','$tanggal','$kode','$labapb','$nosublpb','$laba','Retur nota $nomorfk $namaplg','$user')");

	// --->laba rugi kredit	
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd+$laba WHERE noakun='$labrug'");
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,kredit,keterangan,user) VALUES('$kode','RPJ','$tanggal','$kode','$labrug','$nosublr','$laba','Retur nota $nomorfk $namaplg','$user')");
	mysql_query("UPDATE $table SET posting='P' WHERE kode_rj='$kode'");

	echo "Data sukses disimpan.";
}
?>