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

$kode	=$_POST[kode];
$tanggal=$_POST[tanggal];
$kodesp =$_POST[kodesp];
$namasp	=$_POST[namasp];
$user 	=$_POST[user];
$tunit	=$_POST[tunit];
$total	=$_POST[total];

$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];
$nosubps	= substr($persed,0,3);
$nosublpb	= substr($labapb,0,3);
$nosubjl 	= substr($jual,0,3);
$nosubhpp   = substr($nohpp,0,3);
$nosublr	= substr($labrug,0,3);

$sql = mysql_query("SELECT * FROM faktur_sj WHERE kode_pj='$kode' AND posting='' ORDER BY no_urut");
$row= mysql_num_rows($sql);
if($row>0){
	$thpp=0;
	$nourut=0;
	while($r=mysql_fetch_assoc($sql)){
	    $nourut++;
		$jmlunit= $r[jml_unit];
		$jumlah	= $r[jumlah];
		$kodepl	= $r[kode_pl];
		$namapl	= $r[nama_pl];
		$nobank	= $r[nobank];
		$nosubbk= substr($nobank,0,3);
		$thpp	= $thpp+$r[hpp];
		
		mysql_query("UPDATE stock SET jml_stok=jml_stok-$jmlunit WHERE kode_st='$kodelpg'");
		mysql_query("INSERT INTO rekap_mbr (kode_tr,jurnal,tanggal,no_bukti,no_urut,kode_st,keterangan,keluar,user) VALUES('$kode','FKT','$tanggal','$kode','$nourut','$kodelpg','Penjualan LPG $namapl','$jmlunit','$user')");
		
		mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db+$jumlah WHERE noakun='$nobank' ");
		mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,debet,keterangan,user) VALUES('$kode','FKT','$tanggal','$kode','$nobank','$nosubbk','$jumlah','Penjualan $jmlunit tabung LPG $namapl','$user')");
	}
	$laba=$total-$thpp;
	
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db+$thpp WHERE noakun='$nohpp'");
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,debet,keterangan,user) VALUES('$kode','FKT','$tanggal','$kode','$nohpp','$nosubhpp','$thpp','Penjualan $tunit tabung LPG SJ. $kodesj','$user')");
	
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd+$total WHERE noakun='$jual'");
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,kredit,keterangan,user) VALUES('$kode','FKT','$tanggal','$kode','$jual','$nosubjl','$total','Penjualan $tunit tabung LPG SJ. $kodesj','$user')");

	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd+$thpp WHERE noakun='$persed'");
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,kredit,keterangan,user) VALUES('$kode','FKT','$tanggal','$kode','$persed','$nosubps','$thpp','Penjualan $tunit tabung LPG SJ. $kodesj','$user')");
	
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd+$laba WHERE noakun='$labapb'");
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,kredit,keterangan,user) VALUES('$kode','FKT','$tanggal','$kode','$labapb','$nosublpb','$laba','Penjualan $tunit tabung LPG SJ. $kodesj','$user')");
	
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db+$laba WHERE noakun='$labrug'");
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,debet,keterangan,user) VALUES('$kode','FKT','$tanggal','$kode','$labrug','$nosublr','$laba','Penjualan $tunit tabung LPG SJ. $kodesj','$user')");
	
	mysql_query("UPDATE faktur_sj SET posting='P' WHERE kode_pj='$kode'");

	echo "Data sukses disimpan";
}
?>