<?php 
include "../config.php";
$customer = $_POST['customer'];
$billamount= $_POST['total'];
$oldbillamount=$_POST['oldtotal'];
$oldbilltype=$_POST['oldbilltype'];
//$table=$_POST['table'];
//echo $customer;
//echo $oldbillamount;

if ($customer != '')
{
$sql = "select * from customerdetails where code = '$customer' && status !='deleted'";
//echo $sql;
$conn=mysqli_query($connect,$sql);
while($row=mysqli_fetch_array($conn))
{
    $invoicelimit = $row['invoicelimit'];
    
    $openingbalance = $row['openingbalance'];
     $year = date("Y");
     $month = date ("m");
    //$val = "select ifnull((sum(totalamount)),0)as price from second_transaction where customer = '$customer' && status !='deleted' && date between '$year-$month-01' and '$year-$month-31'";
	$val = "select ifnull((sum(totalamount)),0)as price from second_transaction where customer = '$customer' && status !='deleted' ";
   
    $con = mysqli_query($connect,$val);
    while($chk = mysqli_fetch_array($con))
        {
            //$valest = "select ifnull((sum(totalamount)),0)as price from second_transaction_est where customer = '$customer' && status !='deleted' && date between '$year-$month-01' and '$year-$month-31'";
			
			$valest = "select ifnull((sum(totalamount)),0)as price from second_transaction_est where customer = '$customer' && status !='deleted'  ";
   
    $conest = mysqli_query($connect,$valest);
    while($chkest = mysqli_fetch_array($conest))
        {

            $invoicelimit = $row['invoicelimit'];
                    $pricegst =$chk['price'];
                    $priceest = $chkest['price'];
              $priceval = $pricegst ;
              
			  
			  if($oldbillamount!="" && $oldbilltype == "Gst" )
			  {
				   
                $nettotal = $invoicelimit -($priceval+$billamount-$oldbillamount)-$openingbalance;
			  }
			  if($oldbillamount!="" && $oldbilltype == "Est" )
			  {
				   $nettotal = $invoicelimit -($priceval+$billamount)-$openingbalance;
			  }
			  //echo $nettotal;
			  
              if($invoicelimit != 0 )
              {
             
              if($nettotal < 0)
              {
                  $billvalue = 'Est';
              }
              else
              {
                  $billvalue = 'Gst';
              }
            }
            else
            {
                $billvalue = 'Gst';
            }
        }
    }
   
}
//echo $invoicelimit;
//echo $priceval;
//echo $nettotal;
echo $billvalue;
//echo $billamount;
}
else
{
 echo $creval;
}

?>