<?php
	include'connection.php';
	$id=$_GET['id'];
	$Qty=$_GET['Qty'];
	$DebitQty=$Qty-1;
	date_default_timezone_set("asia/Kolkata");
	$date= Date("Y-m-d");
	
	$query="select * from recharge where Id=$id";
	$result=mysqli_query($con,$query);
	$data=mysqli_fetch_array($result);
	
	$userid=$data['Userid'];
	
	$amount=$data['Debitamount'];
	$debitamount=$amount*$DebitQty;
	$ewalletid=$data['Ewalletid'];
	
	$query1="select * from wallet where Userid=$userid order by Id desc";
	$result1=mysqli_query($con,$query1);
	$data1=mysqli_fetch_array($result1);
	$oldbal=$data1['Balance'];
	$newbal=$oldbal-$debitamount;
	
	$query2="insert into wallet(Userid,Openingbalance,Debit,Balance,Date,Time,Transactiontype,Paynrechid,Operator,Mobile,Amount)
	values('$userid','$oldbal','$debitamount','$newbal','$date','$time','Reverse_Duplicate_Refund','$id','$oper','$mobile','$amount')";
	
	if(mysqli_query($con,$query2))
	{
		$query3="select * from wallet where Userid='$userid' order by Id desc";
		$result3=mysqli_query($con,$query3);
		$data3=mysqli_fetch_array($result3);
		$cewalletid=$data3['Id'];
		
		$query4="update recharge set Creditewalletid='$cewalletid', Balance='$newbal' where Id='$id' ";
		mysqli_query($con, $query4);
		$query12="update retailer set Balance='$newbal' where Userid='$userid'";
		mysqli_query($con,$query12);
		
		$quUpdateRefund="update wallet set ActiveRefund='1' where Paynrechid='$id' and Transactiontype='Recharge_Refund'";
		mysqli_query($con,$quUpdateRefund);
		header('location:Duplicate_Refund.php');
	}

?>