<?PHP
 $txnid= $_GET['txnId'];




			session_start();


if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
	header ("Location: ../login.php");
}
		


?>


<?php
require 'config.php';

			 $db_handle = mysqli_connect(DB_SERVER, DB_USER, DB_PASS);
			 $database = $_SESSION['database'];
			$db_found = mysqli_select_db($db_handle, $database);
			


  
if($db_found){
	 

	  $SQL = "SELECT  product_name,s_qty FROM txn_table   where product_id= '$txnid'";

	  
									
									$result = mysqli_query($db_handle, $SQL);

									 $db_found = mysqli_fetch_assoc($result) ;
	   
										$product_nam = $db_found['product_name'];
										
										$quantity = $db_found['s_qty'];
		 
									
	  
	  	$SQll = "SELECT * FROM product_table where product_name = '$product_nam' ";
	  $resull = mysqli_query($db_handle, $SQll);
	   $db_founn = mysqli_fetch_assoc($resull);
	  
	  if($db_founn==false){
		 // echo "it isnt included";
		  

	 
	$SQL = "DELETE FROM txn_table WHERE product_id=$txnid";
   $query_del = mysqli_query($db_handle, $SQL);
		  
		   header ("Location: transaction.php");

	  }
	  
	  else if ($db_founn==true) {
	
		  
		  
		
			  
			  
			  
			  $SQ = "SELECT product_name,  unit_q FROM product_table where product_name = '$product_nam' ";
	  
												$resul = mysqli_query($db_handle, $SQ);
											$db_foun = mysqli_fetch_assoc($resul);
		  										$product_name = $db_foun['product_name'];
			  							$product_qty = $db_foun['unit_q'];
	  
	 echo  $reversed_qty = $quantity + $product_qty;
	   
	   	$db_found = new mysqli(DB_SERVER, DB_USER, DB_PASS, $database );
		
	  $SQl_update = "UPDATE product_table SET unit_q = '$reversed_qty' WHERE product_name = '$product_name' ";
	
		mysqli_query($db_found,$SQl_update);
	  
	   
	$SQL = "DELETE FROM txn_table WHERE product_id=$txnid";
   $query_del = mysqli_query($db_handle, $SQL);
			  
			  
			  	 header ("Location: transaction.php");
			  
		  
		  
		  
	
		  
		 
		  
	  }
		
		
		
	
	
	  
  }
	

else{
	
	echo "database not found";
}

	  

?>






	<html>
		
			<title>Delete_script</title>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->	
		 <link href="css/bootstrap-4.4.1.css" rel="stylesheet">
		
		
		
	<head>



	</head>
	<body>


		

			
	<div class="jumbotron" style="text-align: center">
		
		
		<h3><b><span style="color: #EC0408">Are you sure you want to DELETE this transaction?</b></h3>
		</span> <br> Transaction ID<?php echo $txnid;?>
		
		
		
			<table style="margin: auto;width: 100%" >
					<form action="record_delete.php" method="post">
		
	<tr>
				<td>
				
		 				<input type="submit" value="NO" class="btn btn-danger" style= "width:100%; height: 70px" name = "NO" >
					
				</td>
					
				<td>
					
		 			<a href="delet.php">	<input type="button"  value="YES" class="btn btn-danger" style= "width:100%; height: 70px"  > </a>
				</td>
	
	</tr>
	</form>
			
			</table>
	
		
			</div>
			
	

	</body>
	</html>
