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

$kode		=$_POST[kode];
$tanggal	=$_POST[tanggal];
$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' AND posting='' ORDER BY no_urut");
$row 	= mysql_num_rows($sql);
if($row>0){
	$totsls=0;
	$nourut=0;
	while($r=mysql_fetch_array($sql)){
	    $nourut     = $nourut+1;
		$kodest 	= $r[kode_st];
		$jmlsls 	= $r[jml_sls];
		$hgbeli 	= $r[hg_beli];
		$totsls 	= $totsls+$r[nilai_sls];
	
		mysql_query("UPDATE stock SET jml_stok=jml_stok+$jmlsls 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','SO','$tanggal','$kode','$nourut','$kodest','Stok opname','$jmlsls','$hgbeli','$user')");	
	} 
	$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,noakun,nosub,debet,keterangan,user) VALUES('$kode','SO','$tanggal','$kode','$rugisls','$nosubsls','$totsls','Selisih stok opname','$user')");

	// --->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,noakun,nosub,kredit,keterangan,user) VALUES('$kode','SO','$tanggal','$kode','$persed','$nosubps','$totsls','Selisih stok opname','$user')");

	// --->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,noakun,nosub,debet,keterangan,user) VALUES('$kode','SO','$tanggal','$kode','$labapb','$nosublpb','$totsls','Selisih stok opname','$user')");

	// --->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,noakun,nosub,kredit,keterangan,user) VALUES('$kode','SO','$tanggal','$kode','$labrug','$nosublr','$totsls','Selisih stok opname','$user')");
	
	mysql_query("UPDATE st_opname SET posting='P' WHERE kode_op='$kode' ");
}
?>
