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

session_start();

date_default_timezone_set('Asia/Jakarta');

$kode	  =$_POST['kode'];
$tanggal  =$_POST['tanggal'];
$gudang   =$_POST['gudang'];
$user	  =$_POST['user'];
$tjumlah1 =$_POST['tjumlah1'];
$tjumlah2 =$_POST['tjumlah2'];
$tselisih =$_POST['tselisih'];

$user	=$_SESSION['username'];
$tgllap	=date('d-m-Y');
$jam	=date('H:i:s');
$tanggal=jin_date_str($tanggal);

$sql	= mysql_query("SELECT * FROM interface");
$r 		= mysql_fetch_assoc($sql);
$persed	= $r['persed'];
$labapb	= $r['laba_pb'];
$rugipsb= $r['rugi_psb'];
$labrug = $r['laba_rugi'];

$user	=$_SESSION['username'];
$tgllap	=date('d-m-Y');
$jam	=date('H:i:s');
$tanggal=jin_date_str($tanggal);
	
$sql	= mysql_query("SELECT * FROM jurpsb WHERE kode_psb='$kode' AND batal='' ORDER BY no_urut ");
$row	= mysql_num_rows($sql);

if($row>0){
	$namast1='';
	$namast2='';
	while($r=mysql_fetch_array($sql)){
		$nourut 	=$r['no_urut'];
		$kodest1	=$r['kode_st1'];
		$namast1	=$namast1.', '.$r['nama_st1'].' '.$r['jml_unit1'];
		$satuan1	=$r['satuan1'];
		$jmlunit1	=$r['jml_unit1'];
		$hgbeli1	=$r['hg_beli1'];
		$kodest2	=$r['kode_st2'];
		$namast2	=$namast2.', '.$r['nama_st2'].' '.$r['jml_unit2'];
		$hgbeli2 	=$r['hg_beli2'];
		$jmlunit2	=$r['jml_unit2'];
		$jumlah2	=$r['jumlah2'];
		$kodegr1	=substr($kodest1,0,3);
		$kodegr2	=substr($kodest2,0,3);
		
		if(!($kodegr1=='')){
			if($gudang=='Gudang')
			    mysql_query("UPDATE stock SET jml_stokb=jml_stokb+$jmlunit1 WHERE kode_st='$kodest1' AND kode_cab='01'");
			else
				mysql_query("UPDATE stock SET jml_stok=jml_stok+$jmlunit1 WHERE kode_st='$kodest1' AND kode_cab='$gudang'");
		}
		
		if(!($kodegr2=='')){
			if($gudang=='Gudang')
			    mysql_query("UPDATE stock SET jml_stokb=jml_stokb-$jmlunit2 WHERE kode_st='$kodest2' AND kode_cab='01'");
			else
			    mysql_query("UPDATE stock SET jml_stok=jml_stok-$jmlunit2 WHERE kode_st='$kodest2' AND kode_cab='$gudang'");
		}
	}
	
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$tjumlah1 WHERE noakun='$persed'");
	
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$tjumlah2 WHERE noakun='$persed' ");
	
	if(!($tselisih==0)){
		mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$tselisih WHERE noakun='$rugipsb'");
		
		mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$tselisih WHERE noakun='$labapb'");
		
		mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$tselisih WHERE noakun='$labrug' ");		
	}
	mysql_query("DELETE FROM rekapjur WHERE kode_tr='$kode' ");
	mysql_query("DELETE FROM rekap_mbr WHERE kode_tr='$kode' ");
	mysql_query("UPDATE jurpsb SET batal='BATAL' WHERE kode_psb='$kode' ");

	mysql_query("INSERT INTO log_history (user,tanggal,jam,aktivitas,no_bukti,nama_st,keterangan) VALUES('$user','$tgllap','$jam','BATAL PERUBAHAN STATUS BARANG','$kode','$namast1','Tanggal PSB $tanggal, diubah menjadi $namast2')");
	
	echo "Data perubahan status kode $kode telah dibatalkan !";
}
?>