<?php 
session_start();
if(empty($_SESSION['id'])):
header('Location:../index.php');
endif;
include('../dist/includes/dbcon.php');

date_default_timezone_set('Asia/Manila');
	$myid = $_SESSION['id'];	
	$sid = $_POST['transsn'];
	$adminpasscode = $_POST['adminpasscode'];
	$id = $_POST['remitid'];
	date_default_timezone_set("Asia/Manila"); 
	$date = date("Y-m-d");
	$date1 = date("Y-m-d H:i:s");
	$query2=mysqli_query($con,"select * from user where user_id='6' ")or die(mysqli_error($con));
    $row2=mysqli_fetch_array($query2);	
	$gamevoid=$row2['void'];
	
	$query3=mysqli_query($con,"select * from user where user_id='9'")or die(mysqli_error($con));
    $row3=mysqli_fetch_array($query3);	
	$gamevoid2=$row3['void'];
	
	
	$query1=mysqli_query($con,"select * from sales where sales_id='$sid'")or die(mysqli_error($con));
    $row1=mysqli_fetch_array($query1);	
	$mypayment=$row1['partial1']; 
	$totalko=$row1['total']; 
	$status=$row1['status']; 
	$cid=$row1['cust_id'];
	$diffko=$totalko - $mypayment;
	
if($adminpasscode == $gamevoid){

	 $name="today";
	 $nameko="partial";
if($status == $name){
	

mysqli_query($con,"UPDATE customer SET balance=balance-'$diffko' where cust_id='$cid'") or die(mysqli_error($con)); 

$result=mysqli_query($con,"DELETE FROM sales WHERE sales_id ='$sid'")
	or die(mysqli_error());
$result=mysqli_query($con,"DELETE FROM payment WHERE sales_id ='$sid'")
	or die(mysqli_error());
	$result=mysqli_query($con,"DELETE FROM sales_details WHERE sales_id ='$sid'")
	or die(mysqli_error());
		$name="WALKIN";
		$remarks="Delete payment of $mypayment  trans ID $sid for $name";  
	
		mysqli_query($con,"INSERT INTO history_log(user_id,action,date) VALUES('$id','$remarks','$date')")or die(mysqli_error($con));
		echo "<script type='text/javascript'>alert('Successfully added payment!');</script>";
		echo "<script>document.location='salestoday.php?cid=$cid'</script>";
		$nameko="partial";
	}else if($status == $nameko){
		mysqli_query($con,"UPDATE customer SET balance=balance+'$mypayment' where cust_id='$cid'") or die(mysqli_error($con)); 
		$result=mysqli_query($con,"DELETE FROM sales WHERE sales_id ='$sid'")
	or die(mysqli_error());
$result=mysqli_query($con,"DELETE FROM payment WHERE sales_id ='$sid'")
	or die(mysqli_error());
	$result=mysqli_query($con,"DELETE FROM sales_details WHERE sales_id ='$sid'")
	or die(mysqli_error());
		$name="WALKIN";
		$remarks="Delete payment of $mypayment  trans ID $sid for $name";  
	
		mysqli_query($con,"INSERT INTO history_log(user_id,action,date) VALUES('$id','$remarks','$date')")or die(mysqli_error($con));
		echo "<script type='text/javascript'>alert('Successfully added payment!');</script>";
		echo "<script>document.location='salestoday.php?cid=$cid'</script>";
	}else{
		echo "<script type='text/javascript'>alert('YOU CANNOT DELETE QUOTATION TRANS!');</script>";
		echo "<script>document.location='salestoday.php?cid=$cid'</script>";
	}
		
		
		
		
		
		
		
		
		
	}else if($adminpasscode == $gamevoid2){

	 $name="today";
	 $nameko="partial";
if($status == $name){
	

mysqli_query($con,"UPDATE customer SET balance=balance-'$diffko' where cust_id='$cid'") or die(mysqli_error($con)); 

$result=mysqli_query($con,"DELETE FROM sales WHERE sales_id ='$sid'")
	or die(mysqli_error());
$result=mysqli_query($con,"DELETE FROM payment WHERE sales_id ='$sid'")
	or die(mysqli_error());
	$result=mysqli_query($con,"DELETE FROM sales_details WHERE sales_id ='$sid'")
	or die(mysqli_error());
		$name="WALKIN";
		$remarks="Delete payment of $mypayment  trans ID $sid for $name";  
	
		mysqli_query($con,"INSERT INTO history_log(user_id,action,date) VALUES('$id','$remarks','$date')")or die(mysqli_error($con));
		echo "<script type='text/javascript'>alert('Successfully added payment!');</script>";
		echo "<script>document.location='salestoday.php?cid=$cid'</script>";
		
	}else if($status == $nameko){
		mysqli_query($con,"UPDATE customer SET balance=balance+'$mypayment' where cust_id='$cid'") or die(mysqli_error($con)); 
		$result=mysqli_query($con,"DELETE FROM sales WHERE sales_id ='$sid'")
	or die(mysqli_error());
$result=mysqli_query($con,"DELETE FROM payment WHERE sales_id ='$sid'")
	or die(mysqli_error());
	$result=mysqli_query($con,"DELETE FROM sales_details WHERE sales_id ='$sid'")
	or die(mysqli_error());
		$name="WALKIN";
		$remarks="Delete payment of $mypayment  trans ID $sid for $name";  
	
		mysqli_query($con,"INSERT INTO history_log(user_id,action,date) VALUES('$id','$remarks','$date')")or die(mysqli_error($con));
		echo "<script type='text/javascript'>alert('Successfully added payment!');</script>";
		echo "<script>document.location='salestoday.php?cid=$cid'</script>";
	}else{
		echo "<script type='text/javascript'>alert('YOU CANNOT DELETE QUOTATION TRANS!');</script>";
		echo "<script>document.location='salestoday.php?cid=$cid'</script>";
	}
	

	
	}	else{	
	
echo "<script type='text/javascript'>alert('ADMIN VOID CODE ERROR!');</script>";
		echo "<script>document.location='salestoday.php?cid=$cid'</script>";
	}		
				
    						
   
	
?>