<?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);

$persed   = $r['persed'];
$rugipsb  = $r['rugi_psb'];
$labapb   = $r['laba_pb'];
$labrug	  = $r['laba_rugi'];
$nosubps  = substr($persed,0,3);
$nosubpsb = substr($rugipsb,0,3);
$nosublpb = substr($labapb,0,3);
$nosublr  = substr($labrug,0,3);

$sql = mysql_query("SELECT * FROM jurkrsb WHERE kode_kb='$kode' AND posting='' ORDER BY kode_kb");
$row = mysql_num_rows($sql);
if($row>0){
	$totnil=0;
	while($r=mysql_fetch_array($sql)){
		$nourut = $r['no_urut'];
		$kodest = $r['kode_st'];
		$jmlunit= $r['jml_unit'];
		$hgpokok= $r['hg_pokok'];
		$ket	= $r['keterangan'];
		$totnil = $totnil+$r['nilai_kr'];
	
		$sql1 = mysql_query("SELECT * FROM stock WHERE kode_st='$kodest' ORDER BY kode_st");
	
		$r1=mysql_fetch_assoc($sql1);
		$jstok= $r1['jml_stok']-$jmlunit;
		
		mysql_query("UPDATE stock SET jml_stok='$jstok' WHERE kode_st='$kodest' ");
		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','KRB','$tanggal','$kode','$nourut','$kodest','','$ket','0','$jmlunit','$hgpokok','$user','','')");
	}

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

	mysql_query("UPDATE akun_bb SET mutasi_db='$debet' WHERE noakun='$rugipsb'");
	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','KRB','$tanggal','$kode','','0','$rugipsb','$nosubsls','','','$totnil','0','Kerusakan barang $ket','$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']+$totnil;

	mysql_query("UPDATE akun_bb SET mutasi_kd='$kredit' 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','KRB','$tanggal','$kode','','0','$persed','$nosubps','','','0','$totnil','Kerusakan barang $ket','$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']+$totnil;
		
	mysql_query("UPDATE akun_bb SET mutasi_db='$debet' 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','KRB','$tanggal','$kode','','0','$labapb','$nosublpb','','','$totnil','0','Kerusakan barang $ket','$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']+$totnil;
		
	mysql_query("UPDATE akun_bb SET mutasi_kd='$kredit' 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','KRB','$tanggal','$kode','','0','$labrug','$nosublr','','','0','$totnil','Kerusakan barang $ket','$user','')");
	mysql_query("SELECT * FROM jurkrsb WHERE kode_kb='$kode' ORDER BY kode_kb");

	mysql_query("UPDATE jurkrsb SET posting='P' WHERE kode_kb='$kode'");
}
?>