<?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'];
	
	$query2="insert into wallet(Userid,Txn,Debit,Date,Time,Transactiontype,Paynrechid,Operator,Mobile,Amount)
	values('$userid','-$debitamount','$debitamount','$date','$time','Reverse_Duplicate_Refund','$id','$oper','$mobile','$amount')";
	
	if(mysqli_query($con,$query2))
	{
	    $refundewalletid=mysqli_insert_id($con);
	    $queryNewBal="select * from wallet where Userid='$userid' and Id>=234084";
	    $resultNewBal=mysqli_query($con,$queryNewBal);
	    $newbal=0;
	    while($dataNewBal=mysqli_fetch_array($resultNewBal))
	    {
	        $Retbal= $dataNewBal['Txn'];
	        $newbal=$newbal+$Retbal;
	    }
	    $OpeningBal=$newbal+$debitamount;
	    $queryW="update wallet set Openingbalance='$OpeningBal', Balance='$newbal' where Id='$refundewalletid'";
		mysqli_query($con,$queryW);
		
		$query9="update retailer set Balance='$newbal' where Userid='$userid'";
		mysqli_query($con,$query9);
		
		$query4="update recharge set Creditewalletid='$refundewalletid', Balance='$newbal' where Id='$id' ";
		mysqli_query($con, $query4);
		
		$quUpdateRefund="update wallet set ActiveRefund='1' where Paynrechid='$id' and Transactiontype='Recharge_Refund'";
		mysqli_query($con,$quUpdateRefund);
		header('location:Duplicate_Refund.php');
	}

?>