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

$table	="setor_bbm";

$kode		=$_POST[kode];
$tanggal	=$_POST[tanggal];
$shift		=$_POST[shift];
$kodetl 	=$_POST[kodetl];
$jenis  		=$_POST[jenis];
$tanki    	=$_POST[tanki];
$telleraw 	=$_POST[telleraw];
$tellerak 	=$_POST[tellerak];
$jmlkeluar	=$_POST[jmlkeluar];
$jmltera	=$_POST[jmltera];
$jmlbon 	=$_POST[jmlbon];
$jmltunai	=$_POST[jmltunai];
$nilaitunai	=$_POST[nilaitunai];
$hpptunai	=$_POST[hpptunai];
$user		=$_SESSION[namauser];

$sql	= mysql_query("SELECT * FROM interface");
$r = mysql_fetch_assoc($sql);
$kas 		= $r[kas];
$piut 		= $r[piutang];
$persed 	= $r[persed];
$labapb  	= $r[laba_pb];
$jualbbm	= $r[jual_bbm];
$hppbbm	= $r[hpp_bbm];
$labarugi	= $r[laba_rugi];
$prem    	= $r[premium];
$solar   	= $r[solar];
$ptmx    	= $r[pertamax];
$ptmp    	= $r[ptm_plus];
$ptlt    		= $r[pertalite];
$dex     	= $r[ptmn_dex];
$dexlite 	= $r[dexlite];
$solek   	= $r[solar_eko];
$nosubks = substr($kas,0,3);
$nosubpi = substr($piut,0,3);
$nosubps= substr($persed,0,3);
$nosublp	= substr($labapb,0,3);
$nosubjl 	= substr($jualbbm,0,3);
$nosubhp= substr($hppbbm,0,3);
$nosublr 	= substr($labarugi,0,3);

$sql = mysql_query("SELECT * FROM $table WHERE kode_sb='$kode' GROUP BY kode_sb ");
$row = mysql_num_rows($sql);
If($row>0){
	while ($r=mysql_fetch_array($sql)){
		$idbon =$r[id_bon];
	}
	mysql_query("UPDATE $table SET jml_keluar='$jmlkeluar',jml_tera='$jmltera',jml_bon='$jmlbon',jml_tunai='$jmltunai',nilai_tunai='$nilaitunai',hpp_tunai='$hpptunai' WHERE kode_sb='$kode' AND id_bon='$idbon'");
}else{
	$input = "INSERT INTO $table (kode_sb,tanggal,shift,kode_tl,jenis,tanki,petugas,teller_aw,teller_ak,jml_keluar,jml_tera,jml_bon,jml_tunai,nilai_tunai,hpp_tunai,user) VALUES('$kode','$tanggal','$shift','$kodetl','$jenis','$tanki','$petugas','$telleraw','$tellerak','$jmlkeluar','$jmltera','$jmlbon','$jmltunai','$nilaitunai','$hpptunai','$user')";
	mysql_query($input);
}

if($jenis=='Premium')
	$kodest=$prem;
else if($jenis=='Bio Solar')
	$kodest=$solar;
else if($jenis=='Pertamax')
	$kodest=$ptmx;
else if($jenis=='Pertamax Plus')
	$kodest=$ptmp;
else if($jenis=='Pertalite')
	$kodest=$ptlt;
else if($jenis=='Pertamina  Dex')
	$kodest=$dex;
else if($jenis=='Dexlite')
	$kodest=$dexlite;
else
	$kodest=$solek;

$bpjual = $jualbbm.substr($kodest,2,4);
$bphpp = $hppbbm.substr($kodest,2,4);

$sql1 = mysql_query("SELECT * FROM stock WHERE kode_st='$kodest' ORDER BY kode_st");
$r1=mysql_fetch_assoc($sql1);
$stok=$r1[jml_stok]-$jmltunai-$jmlbon;
$hgbeli=$r1[hg_beli];

$input1 = "UPDATE stock SET jml_stok='$stok' WHERE kode_st='$kodest'";
mysql_query($input1);

$input1 = "INSERT INTO rekap_mbr (kode_tr,jurnal,tanggal,no_bukti,kode_st,keterangan,keluar,harga,user) VALUES('$kode','BBM','$tanggal','$kode','$kodest','Penjualan BBM shift $shift teller $kodetl tunai','$jmltunai','$hgbeli','$user')";
mysql_query($input1);
$input1 = "INSERT INTO rekap_mbr (kode_tr,jurnal,tanggal,no_bukti,kode_st,keterangan,keluar,harga,user) VALUES('$kode','BBM','$tanggal','$kode','$kodest','Penjualan BBM shift $shift teller $kodetl kredit','$jmlbon','$hgbeli','$user')";
mysql_query($input1);

$sql = mysql_query("SELECT noakun,mutasi_db FROM akun_bb WHERE noakun='$kas' ");
$r = mysql_fetch_assoc($sql);
$debet = $r[mutasi_db]+$nilaitunai;

$input	= "UPDATE akun_bb SET mutasi_db='$debet' WHERE noakun='$kas'";
mysql_query($input);

mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,debet,keterangan,user) VALUES('$kode','BBM','$tanggal','$kode','$kas','$nosubks','$nilaitunai','Setoran $jenis shift $shift teller $kodetl','$user')");

$nilbon=0;
$hppbon=0;

if($jmlbon>0){
	$sql1 = mysql_query("SELECT * FROM $table WHERE kode_sb='$kode' ");
	while ($r1=mysql_fetch_array($sql1)){
		$kodepl=$r1[kode_pl];
		$namapl=$r1[nama_pl];
		$nopol=$r1[nopol];
		$jmlnopol=$r1[jml_nopol];
		$nilainopol=$r1[nilai_nopol];
		$tanggaljt=$r1[tanggal_jt];
		$bppiut=$piut.$kodepl;
		
		$nilbon=$nilbon+$r1[nilai_nopol];
		$hppbon=$hppbon+$r1[hpp_nopol];
		
		$sql = mysql_query("SELECT noakun,mutasi_db FROM akun_bp WHERE noakun='$bppiut' ");
		$r = mysql_fetch_assoc($sql);
		$debet = $r[mutasi_db]+$nilainopol;

		$input = "UPDATE akun_bp SET mutasi_db='$debet' WHERE noakun='$bppiut'";
		mysql_query($input);
		
		$input = "INSERT INTO piutang (kode_pl,nama,nofak,tanggal,nopol,jumlah,tanggal_jt) VALUES('$kodepl','$namapl','$kode','$tanggal','$nopol','$nilainopol','$tanggaljt')";
		mysql_query($input);
		mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,nobp,debet,keterangan,user) VALUES('$kode','BBM','$tanggal','$kode','$piut','$nosubpi','$bppiut','$nilainopol','$jenis kredit shift $shift teller $kodetl $nopol','$user')");
	}
	
	$sql = mysql_query("SELECT noakun,mutasi_db FROM akun_bb WHERE noakun='$piut' ");
	$r = mysql_fetch_assoc($sql);
	$debet = $r[mutasi_db]+$nilbon;

	$input = "UPDATE akun_bb SET mutasi_db='$debet' WHERE noakun='$piut'";
	mysql_query($input);
}

$sql = mysql_query("SELECT noakun,mutasi_kd FROM akun_bb WHERE noakun='$jualbbm' ");
$r = mysql_fetch_assoc($sql);
$kredit = $r[mutasi_kd]+$nilaitunai+$nilbon;
$niljual = $nilaitunai+$nilbon;
$hppjual = $hpptunai+$hppbon;
$input = "UPDATE akun_bb SET mutasi_kd='$kredit' WHERE noakun='$jualbbm'";
mysql_query($input);

$sql = mysql_query("SELECT noakun,mutasi_kd FROM akun_bp WHERE noakun='$bpjual' ");
$r = mysql_fetch_assoc($sql);
$kredit = $r[mutasi_kd]+$nilaitunai+$nilbon;
$input = "UPDATE akun_bp SET mutasi_kd='$kredit' WHERE noakun='$bpjual'";
mysql_query($input);

mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,kredit,keterangan,user) VALUES('$kode','BBM','$tanggal','$kode','$jualbbm','$nosubjl','$niljual','Penjualan $jenis shift $shift teller $kodetl','$user')");

$sql = mysql_query("SELECT noakun,mutasi_db FROM akun_bb WHERE noakun='$hppbbm' ");
$r = mysql_fetch_assoc($sql);
$debet = $r[mutasi_db]+$hppjual;

$sql = mysql_query("SELECT noakun,mutasi_db FROM akun_bp WHERE noakun='$bphpp' ");
$r = mysql_fetch_assoc($sql);
$debet = $r[mutasi_db]+$hppjual;

$input = "UPDATE akun_bp SET mutasi_db='$debet' WHERE noakun='$bphpp'";
mysql_query($input);

mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,debet,keterangan,user) VALUES('$kode','BBM','$tanggal','$kode','$hppbbm','$nosubhp','$hppjual','Penjualan $jenis shift $shift teller $kodetl','$user')");

$sql = mysql_query("SELECT noakun,mutasi_kd FROM akun_bb WHERE noakun='$persed' ");
$r = mysql_fetch_assoc($sql);
$kredit = $r[mutasi_kd]+$hppjual;

$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','BBM','$tanggal','$kode','$persed','$nosubps','$hppjual','Penjualan $jenis shift $shift teller $kodetl','$user')");

$sql = mysql_query("SELECT noakun,mutasi_kd FROM akun_bb WHERE noakun='$labapb' ");
$r = mysql_fetch_assoc($sql);
$laba = $niljual-$hppjual;
$kredit = $r[mutasi_kd]+$laba;

$input = "UPDATE akun_bb SET mutasi_kd='$kredit' WHERE noakun='$labapb'";
mysql_query($input);
		
mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,kredit,keterangan,user) VALUES('$kode','BBM','$tanggal','$kode','$labapb','$nosublp','$laba','Penjualan $jenis shift $shift teller $kodetl','$user')");
mysql_query($input);

$sql = mysql_query("SELECT noakun,mutasi_db FROM akun_bb WHERE noakun='$labarugi' ");
$r = mysql_fetch_assoc($sql);
$debet = $r[mutasi_db]+$laba;

$input = "UPDATE akun_bp SET mutasi_db='$debet' WHERE noakun='$labarugi'";
mysql_query($input);
		
mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,debet,keterangan,user) VALUES('$kode','BBM','$tanggal','$kode','$labarugi','$nosublr','$laba','Penjualan $jenis shift $shift teller $kodetl','$user')");

$input = "UPDATE teller SET angka_tl='$tellerak' WHERE kode_tl='$kodetl'";
mysql_query($input);

$input = "UPDATE setor_bbm SET posting='P' WHERE kode_sb='$kode'";
mysql_query($input);
?>