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

$table		="stock_do";

$kode		=$_POST[kode];
$tanggal	=$_POST[tanggal];
$nobukti	=$_POST[nobukti];
$jenis		=$_POST[jenis];
$jmlliter	=$_POST[jmlliter];
$hgbeli	=$_POST[hgbeli];
$jumlah	=$_POST[jumlah];
$user		=$_SESSION[namauser];

$sql	= mysql_query("SELECT * FROM interface");
$r = mysql_fetch_assoc($sql);
$dobbm	= $r[do_bbm];
$labadit	= $r[laba_dit];

if($jenis=='Premium')
	$bpdo=$dobbm.'0001';
else if($jenis=='Bio Solar')
	$bpdo=$dobbm.'0002';
else if($jenis=='Pertamax')
	$bpdo=$dobbm.'0003';
else if($jenis=='Pertamax Plus')
	$bpdo=$dobbm.'0004';
else if($jenis=='Pertalite')
	$bpdo=$dobbm.'0005';
else if($jenis=='Pertamina  Dex')
	$bpdo=$dobbm.'0006';
else if($jenis=='Dexlite')
	$bpdo=$dobbm.'0007';
else
	$bpdo=$dobbm.'0008';

$sql = mysql_query("SELECT * FROM $table WHERE kode_do='$kode' ORDER BY kode_do ");
$row= mysql_num_rows($sql);

if ($row>0){
	$r=mysql_fetch_assoc($sql);
	$jml_lama=$r[jumlah];
	
	$input = "UPDATE $table SET tanggal='$tanggal',nobukti='$nobukti',jenis='$jenis',jml_liter='$jmlliter',hg_beli='$hgbeli',jumlah='$jumlah',lastupdate=now() WHERE kode_do='$kode' ";
	mysql_query($input);
	
	$sql = mysql_query("SELECT * FROM akun_bb WHERE noakun='$dobbm' ");
	$r=mysql_fetch_assoc($sql);
	$saldo=$r[saldo]-$jml_lama+$jumlah;
	
	mysql_query("UPDATE akun_bb SET saldo='$saldo' WHERE noakun='$dobbm' ");
	
	$sql = mysql_query("SELECT * FROM akun_bp WHERE noakun='$bpdo' ");
	$r=mysql_fetch_assoc($sql);
	$saldo=$r[saldo]-$jml_lama+$jumlah;
	
	mysql_query("UPDATE akun_bp SET saldo='$saldo' WHERE noakun='$bpdo' ");
	
	$sql = mysql_query("SELECT * FROM akun_bb WHERE noakun='$labadit' ");
	$r=mysql_fetch_assoc($sql);
	$saldo=$r[saldo]-$jml_lama+$jumlah;
	
	mysql_query("UPDATE akun_bb SET saldo='$saldo' WHERE noakun='$labadit' ");
}
else{
	$input = "INSERT INTO $table (kode_do,tanggal,nobukti,jenis,jml_liter,hg_beli,jumlah) VALUES('$kode','$tanggal','$nobukti','$jenis','$jmlliter','$hgbeli','$jumlah')";
	mysql_query($input);
	
	$sql = mysql_query("SELECT * FROM akun_bb WHERE noakun='$dobbm' ");
	$r=mysql_fetch_assoc($sql);
	$saldo=$r[saldo]+$jumlah;
	
	mysql_query("UPDATE akun_bb SET saldo='$saldo' WHERE noakun='$dobbm' ");
	
	$sql = mysql_query("SELECT * FROM akun_bp WHERE noakun='$bpdo' ");
	$r=mysql_fetch_assoc($sql);
	$saldo=$r[saldo]+$jumlah;
	
	mysql_query("UPDATE akun_bp SET saldo='$saldo' WHERE noakun='$bpdo' ");
	
	$sql = mysql_query("SELECT * FROM akun_bb WHERE noakun='$labadit' ");
	$r=mysql_fetch_assoc($sql);
	$saldo=$r[saldo]+$jumlah;
	
	mysql_query("UPDATE akun_bb SET saldo='$saldo' WHERE noakun='$labadit' ");
}
?>