
<?php
  include 'connect.php';
define('YBASE',true);
session_start();

if (isset($_SESSION['yuser'])) {
			include_once 'config/log.php';
	} else {
	header('location:login.php');
	}
$jabat=$_SESSION['jabat'];  
$slm=$_SESSION['sales'];  

  
  
$nettof=mysqli_query($con,"select netto,kdslm,custno from somobileheader where nobukti='$_GET[id]';");

  

$netto=mysqli_fetch_array($nettof);

$kdslm=$netto['kdslm'];						
$custno=$netto['custno'];						

$saldof=mysqli_query($con,"select (netto+debet-kredit-bayar) as sales from fakturjualsalesheader where custno='$custno' and kdslm='$kdslm' and tgl<=curdate();");
$saldor=mysqli_query($con,"select (netto+debet-kredit-bayar) as retur from returjualsalesheader where custno='$custno' and kdslm='$kdslm' and tgl<=curdate();");
$saldolph=mysqli_query($con,"select (a.nilaibayar+a.nilaigiro) as bayar from tagihandetail  a
						left join tagihanheader b on a.nobukti=b.nobukti
						where a.custno='$custno' and b.tgltagih=curdate() and b.kdslm='$kdslm';");

$saldofd=mysqli_fetch_array($saldof);						
$saldord=mysqli_fetch_array($saldor);						
$saldolphd=mysqli_fetch_array($saldolph);						
$saldo1=$saldofd['sales']-$saldord['retur'];
$saldo=$saldo1-$saldolphd['bayar'];
if ($saldo==null) $saldo=0;  
$sql = mysqli_query($con,"UPDATE somobileheader set stat='1' where nobukti='$_GET[id]' and stat<>'1' ;");
  
    //perintah untuk melakukan update
    //melakukan update data berdasarkan ID

if ($jabat=='1') {	
	$sql = mysqli_query($con,"UPDATE somobileheader set stat='1' where nobukti='$_GET[id]' and stat='0' and netto<=10000000 and $saldo<=50000;");
	$sql = mysqli_query($con,"UPDATE somobileheader set stat='2' where nobukti='$_GET[id]' and stat='0' and $saldo>50000;");
	$sql = mysqli_query($con,"UPDATE somobileheader set stat='2' where nobukti='$_GET[id]' and stat='0' and netto>10000000;");
?>	
	<meta http-equiv="refresh" content="0; url=listkanvas.php" >
<?php 	
}
if ($jabat=='2') {
	$sql = mysqli_query($con,"UPDATE somobileheader set stat='1' where nobukti='$_GET[id]' and stat<>'1' and netto<=20000000 and $saldo<=10000000;");
	$sql = mysqli_query($con,"UPDATE somobileheader set stat='3' where nobukti='$_GET[id]' and stat<>'1' and $saldo>10000000;");
	$sql = mysqli_query($con,"UPDATE somobileheader set stat='3' where nobukti='$_GET[id]' and stat<>'1' and netto>20000000;"); 	
?>
<meta http-equiv="refresh" content="0; url=listkanvas.php?id=<?php echo $kdslm;?>" >
<?php }
	
if ($jabat=='5') {
	$sql = mysqli_query($con,"UPDATE somobileheader set stat='1' where nobukti='$_GET[id]' and stat<>'1' and netto<=30000000 and $saldo<=30000000;");
	$sql = mysqli_query($con,"UPDATE somobileheader set stat='4' where nobukti='$_GET[id]' and stat<>'1' and $saldo>30000000;");
	$sql = mysqli_query($con,"UPDATE somobileheader set stat='4' where nobukti='$_GET[id]' and stat<>'1' and netto>30000000;"); 	
?>
<meta http-equiv="refresh" content="0; url=listkanvas.php?id=<?php echo $kdslm;?>" >
<?php }

if ($jabat=='6') {
	$sql = mysqli_query($con,"UPDATE somobileheader set stat='1' where nobukti='$_GET[id]' and stat<>'1' ;");
?>
<meta http-equiv="refresh" content="0; url=listkanvas.php?id=<?php echo $kdslm;?>" >
<?php }		
?>