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

$table	="setor_bbm";

$kode		=$_POST[kode];
$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];
$jmlliter   =$jmltunai+$jmlbon;

$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];

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

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

if($tanki=='1')
	mysql_query("UPDATE stock SET jml_gd1=jml_gd1+$jmlliter WHERE kode_st='$kodest' ");
else if($tanki=='2')
	mysql_query("UPDATE stock SET jml_gd2=jml_gd2+$jmlliter WHERE kode_st='$kodest' ");
else if($tanki=='3')
	mysql_query("UPDATE stock SET jml_gd3=jml_gd3+$jmlliter WHERE kode_st='$kodest' ");

if(!($jmltunai=='0')){
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$nilaitunai WHERE noakun='$kas'");
}

if($jmlbon>0){
	$sql1 = mysql_query("SELECT * FROM $table WHERE kode_sb='$kode' AND batal='' ");
	
	$nilbon=0;
	$hppbon=0;
	while ($r1=mysql_fetch_array($sql1)){
		$kodepl		=$r1[kode_pl];
		$nofak		=$kode.$r1[id_bon];
		$jmlnopol	=$r1[jml_nopol];
		$nilainopol	=$r1[nilai_nopol];		
		$bppiut		=$piut.$kodepl;
		$nilbon		=$nilbon+$r1[nilai_nopol];
		$hppbon		=$hppbon+$r1[hpp_nopol];
		
		mysql_query("UPDATE akun_bp SET mutasi_db=mutasi_db-$nilainopol WHERE noakun='$bppiut'");
		mysql_query("DELETE FROM piutang WHERE kode_pl='$kodepl' AND nofak='$nofak' ");
	}
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$nilbon WHERE noakun='$piut'");
}
$niljual 	= $nilaitunai+$nilbon;
$hppjual	= $hpptunai+$hppbon;
$laba		= $niljual-$hppjual;

mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$niljual WHERE noakun='$jualbbm'");
mysql_query("UPDATE akun_bp SET mutasi_kd=mutasi_kd-$niljual WHERE noakun='$bpjual'");

mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$hppjual WHERE noakun='$hppbbm'");
mysql_query("UPDATE akun_bp SET mutasi_db=mutasi_db-$hppjual WHERE noakun='$bphpp'");

mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$hppjual 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='$labarugi'");
		
mysql_query("UPDATE teller SET angka_tl='$telleraw' WHERE kode_tl='$kodetl'");
mysql_query("DELETE FROM setor_bbm WHERE kode_sb='$kode'");

mysql_query("DELETE FROM rekapjur WHERE kode_tr='$kode'");
mysql_query("DELETE FROM rekap_mbr WHERE kode_tr='$kode'");

echo "Setoran bbm $kode telah dibatalkan.";
?>