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

$table	="hutang";

$kode	=$_POST[kodesp];
$nama	=$_POST[nama];
$nofak	=$_POST[nofak];
$tanggal=$_POST[tanggal];
$jumlah	=$_POST[jumlah];
$tanggaljt=$_POST[tanggaljt];

$sql	= mysql_query("SELECT * FROM interface");
$r 		= mysql_fetch_assoc($sql);
$utang	= $r[hutang_dg];
$labdit	= $r[laba_dit];
$nobp	= $utang.$kode;

$sql = mysql_query("SELECT * FROM $table WHERE nofak= '$nofak'");
$row = mysql_num_rows($sql);
if ($row>0){
	$r 		= mysql_fetch_assoc($sql);
	$jmlawl	= $r[jumlah];
	
	$input	= "UPDATE $table SET nama='$nama',nofak='$nofak',tanggal='$tanggal',jumlah='$jumlah',tanggal_jt='$tanggaljt',lastupdate=now() WHERE nofak= '$nofak'";
	mysql_query($input);
	
	$sql 	= mysql_query("SELECT * FROM akun_bp WHERE noakun= '$nobp'");
	$r 		= mysql_fetch_assoc($sql);
	$saldo	= $r[saldo]-$jmlawl+$jumlah;
	
	$input	= "UPDATE akun_bp SET saldo='$saldo',lastupdate=now() WHERE noakun= '$nobp'";
	mysql_query($input);
	
	$sql 	= mysql_query("SELECT * FROM akun_bb WHERE noakun= '$utang'");
	$r 		= mysql_fetch_assoc($sql);
	$saldo	= $r[saldo]-$jmlawl+$jumlah;
	
	$input	= "UPDATE akun_bb SET saldo='$saldo',lastupdate=now() WHERE noakun= '$utang'";
	mysql_query($input);
	
	$sql 	= mysql_query("SELECT * FROM akun_bb WHERE noakun= '$labdit'");
	$r 		= mysql_fetch_assoc($sql);
	$saldo	= $r[saldo]+$jmlawl-$jumlah;
	
	$input	= "UPDATE akun_bb SET saldo='$saldo',lastupdate=now() WHERE noakun= '$labdit'";
	mysql_query($input);
	echo "Data Sukses diubah.";
}
else{
	$input = "INSERT INTO $table (kode_sp,nama,nofak,tanggal,jumlah,tanggal_jt) VALUES('$kode','$nama','$nofak','$tanggal','$jumlah','$tanggaljt')";
	mysql_query($input);
	
	$sql 	= mysql_query("SELECT * FROM akun_bp WHERE noakun= '$nobp'");
	$r 		= mysql_fetch_assoc($sql);
	$saldo	= $r[saldo]+$jumlah;
	
	$input	= "UPDATE akun_bp SET saldo='$saldo',lastupdate=now() WHERE noakun= '$nobp'";
	mysql_query($input);
	
	$sql 	= mysql_query("SELECT * FROM akun_bb WHERE noakun= '$utang'");
	$r 		= mysql_fetch_assoc($sql);
	$saldo	= $r[saldo]+$jumlah;
	
	$input	= "UPDATE akun_bb SET saldo='$saldo',lastupdate=now() WHERE noakun= '$utang'";
	mysql_query($input);
	
	$sql 	= mysql_query("SELECT * FROM akun_bb WHERE noakun= '$labdit'");
	$r 		= mysql_fetch_assoc($sql);
	$saldo	= $r[saldo]-$jumlah;
	
	$input	= "UPDATE akun_bb SET saldo='$saldo',lastupdate=now() WHERE noakun= '$labdit'";
	mysql_query($input);
	echo "Data sukses disimpan";
}
?>