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

$kode	=$_POST[kode];

$sql	= mysql_query("SELECT * FROM interface");
$r		= mysql_fetch_assoc($sql);

$persed	= $r[persed];
$labapb	= $r[laba_pb];
$jual 	= $r[penjualan];
$nohpp 	= $r[hpp];
$labrug = $r[laba_rugi];
$kodelpg= $r[kode_lpg];

$sql = mysql_query("SELECT * FROM faktur_sj WHERE kode_pj='$kode' AND batal='' ORDER BY no_urut");
$row= mysql_num_rows($sql);
if($row>0){
	$thpp=0;
	$total=0;
	while($r=mysql_fetch_assoc($sql)){
		$jmlunit= $r[jml_unit];
		$jumlah	= $r[jumlah];
		$nobank	= $r[nobank];
		$thpp	= $thpp+$r[hpp];
		$total	= $total+$r[jumlah];
		
		mysql_query("UPDATE stock SET jml_stok=jml_stok+$jmlunit WHERE kode_st='$kodelpg'");
		mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$jumlah WHERE noakun='$nobank' ");
	}
	$laba=$total-$thpp;
	
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$thpp WHERE noakun='$nohpp' ");
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$total WHERE noakun='$jual' ");
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$thpp WHERE noakun='$persed'");
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$laba WHERE noakun='$labapb'");
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$laba WHERE noakun='$labrug'");
	
	mysql_query("DELETE FROM rekap_mbr WHERE kode_tr='$kode'");
	mysql_query("DELETE FROM rekapjur WHERE kode_tr='$kode'");
	
	mysql_query("UPDATE faktur_sj SET batal='BATAL' WHERE kode_pj='$kode'");

	echo "Data sukses disimpan";
}
?>