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

$kode		=$_POST[kode];
$tanggal	=$_POST[tanggal];
$gudang 	=$_POST[gudang];
$user		=$_SESSION[namauser];

$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' ORDER BY kode_op");
$totsls=0;
while($r=mysql_fetch_array($sql)){
	$kodest = $r[kode_st];
	$jmlsls = $r[jml_sls];
	$hgbeli = $r[hg_beli];
	$totsls = $totsls+$r[nilai_sls];
	
	$sql1 = mysql_query("SELECT * FROM stock WHERE kode_st='$kodest' ORDER BY kode_st");
	while($r1=mysql_fetch_array($sql1)){
		if($gudang=='1'){
			$jstok = $r1[jml_gd1]+$jmlsls;
			mysql_query("UPDATE stock SET jml_gd1='$jstok' WHERE kode_st='$kodest' ");
		}else if($gudang=='2'){
			$jstok = $r1[jml_gd2]+$jmlsls;
			mysql_query("UPDATE stock SET jml_gd2='$jstok' WHERE kode_st='$kodest' ");
		}else if($gudang=='3'){
			$jstok = $r1[jml_gd3]+$jmlsls;
			mysql_query("UPDATE stock SET jml_gd3='$jstok' WHERE kode_st='$kodest' ");
		}else{
			$jstok = $r1[jml_gd4]+$jmlsls;
			mysql_query("UPDATE stock SET jml_gd4='$jstok' WHERE kode_st='$kodest' ");
		}
	}
	
	$sql2 = mysql_query("SELECT * FROM rekap_mbr ");	
	$query2 = mysql_query("INSERT INTO rekap_mbr (kode_tr,jurnal,tanggal,no_bukti,kode_st,keterangan,gudang,masuk,harga,user) VALUES('$kode','SO','$tanggal','$kode','$kodest','Stok opname','$gudang','$jmlsls','$hgbeli','$user')");
	mysql_query($query2);
} 
$totsls=$totsls*-1;

 // --->rugi stok opname debet
$sql = mysql_query("SELECT noakun,mutasi_db FROM akun_bb WHERE noakun='$rugisls' ");
$r = mysql_fetch_assoc($sql);	
$debet = $r[mutasi_db]+$totsls;
		
$input	= "UPDATE akun_bb SET mutasi_db='$debet' WHERE noakun='$rugisls'";
mysql_query($input);

mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,debet,keterangan,user) VALUES('$kode','SO','$tanggal','$kode','$rugisls','$nosubsls','$totsls','Selesih stok opname','$user')");

// --->persediaan kredit
$sql = mysql_query("SELECT noakun,mutasi_kd FROM akun_bb WHERE noakun='$persed' ");
$r = mysql_fetch_assoc($sql);	
$kredit= $r[mutasi_kd]+$totsls;
		
$input	= "UPDATE akun_bb SET mutasi_kd='$kredit' WHERE noakun='$persed'";
mysql_query($input);

mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,kredit,keterangan,user) VALUES('$kode','SO','$tanggal','$kode','$persed','$nosubps','$totsls','Selisih stok opname','$user')");

// --->laba periode berjalan debet
$sql = mysql_query("SELECT noakun,mutasi_db FROM akun_bb WHERE noakun='$labapb' ");
$r = mysql_fetch_assoc($sql);
$debet = $r[mutasi_db]+$totsls;
		
$input	= "UPDATE akun_bb SET mutasi_db='$debet' WHERE noakun='$labapb'";
mysql_query($input);

mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,debet,keterangan,user) VALUES('$kode','SO','$tanggal','$kode','$labapb','$nosublpb','$totsls','Selisih stok opname','$user')");

// --->laba rugi kredit
$sql = mysql_query("SELECT noakun,mutasi_kd FROM akun_bb WHERE noakun='$labrug' ");
$r = mysql_fetch_assoc($sql);
$kredit = $r[mutasi_kd]+$totsls;
		
$input = "UPDATE akun_bb SET mutasi_kd='$kredit' WHERE noakun='$labrug'";
mysql_query($input);

mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,kredit,keterangan,user) VALUES('$kode','SO','$tanggal','$kode','$labrug','$nosublr','$totsls','Selisih stok opname','$user')");

mysql_query("SELECT * FROM st_opname WHERE kode_op='$kode' ORDER BY kode_op");

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