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

date_default_timezone_set('Asia/Jakarta');

$kode	=$_POST['kode'];
$tanggal=$_POST['tanggal'];
$gudang =$_POST['gudang'];
$user	=$_SESSION['namauser'];
$jam	=date('H:i:s');

if($gudang=='Gudang') $kodecab='01';
else $kodecab=$gudang;

$sql= mysql_query("SELECT * FROM interface");
$r  = mysql_fetch_assoc($sql);
$kas    = $r['kas'];
$persed = $r['persed'];
$rugisls= $r['rugi_sls'];
$labapb = $r['laba_pb'];
$labrug = $r['laba_rugi'];
$nosubps = substr($persed,0,3);
$nosubsls= substr($rugisls,0,3);
$nosublpb= substr($labapb,0,3);
$nosublr = substr($labrug,0,3);

$sql = mysql_query("SELECT * FROM st_opname WHERE kode_op='$kode'");
$totsls=0;
$nourut=0;

while($r=mysql_fetch_array($sql)){	
	$gudang	= $r['gudang'];
	$kodest = $r['kode_st'];
	$jmlsls = $r['jml_sls'];
	$hgbeli = $r['hg_beli'];
	$totsls = $totsls+$r['nilai_sls'];
	$nourut = $nourut+1;
	
	if($gudang=='Gudang')
	    $sql1 = mysql_query("SELECT * FROM stock WHERE kode_st='$kodest' AND kode_cab='01'");
	else
	    $sql1 = mysql_query("SELECT * FROM stock WHERE kode_st='$kodest' AND kode_cab='$gudang'");
	$r1 = mysql_fetch_assoc($sql1);
		
	if($gudang=='Gudang')
		mysql_query("UPDATE stock SET jml_stokb=jml_stokb+$jmlsls WHERE kode_st='$kodest' AND kode_cab='01'");
	else
		mysql_query("UPDATE stock SET jml_stok=jml_stok+$jmlsls WHERE kode_st='$kodest' AND kode_cab='$gudang'");
	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','SO','$tanggal','$kode','$nourut','$kodest','$gudang','Stok opname','$jmlsls','0','$hgbeli','$user','$jam','')");
}
$totsls=$totsls*-1;

 // --->rugi stok opname debet
mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db+$totsls WHERE noakun='$rugisls'");
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','SO','$tanggal','$kode','$kodecab','1','$rugisls','$nosubsls','','','$totsls','0','Selesih stok opname','$user','$jam')");

// --->persediaan kredit
mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd+$totsls 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','SO','$tanggal','$kode','$kodecab','1','$persed','$nosubps','','','0','$totsls','Selisih stok opname','$user','$jam')");

// --->laba periode berjalan debet
mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db+$totsls 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','SO','$tanggal','$kode','$kodecab','1','$labapb','$nosublpb','','','$totsls','0','Selisih stok opname','$user','$jam')");

// --->laba rugi kredit
mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd+$totsls 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','SO','$tanggal','$kode','$kodecab','1','$labrug','$nosublr','','','0','$totsls','Selisih stok opname','$user','$jam')");

mysql_query("UPDATE st_opname SET posting='P' WHERE kode_op='$kode'");
?>