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

$table	="piutang";

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

$sql	= mysql_query("SELECT * FROM interface");
$r 		= mysql_fetch_assoc($sql);
$piut	= $r[piutang];
$labdit	= $r[laba_dit];
$nobp	= $piut.$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 tanggal='$tanggal',nopol='$nopol',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= '$piut'");
	$r 		= mysql_fetch_assoc($sql);
	$saldo	= $r[saldo]-$jmlawl+$jumlah;
	
	$input	= "UPDATE akun_bb SET saldo='$saldo',lastupdate=now() WHERE noakun= '$piut'";
	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_pl,nama,nofak,tanggal,nopol,jumlah,tanggal_jt) VALUES('$kode','$nama','$nofak','$tanggal','$nopol','$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= '$piut'");
	$r 		= mysql_fetch_assoc($sql);
	$saldo	= $r[saldo]+$jumlah;
	
	$input	= "UPDATE akun_bb SET saldo='$saldo',lastupdate=now() WHERE noakun= '$piut'";
	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";
}
?>