<?php
	include'connection.php';
	$id=$_GET['id'];
	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'];
	$debitamount=$data['Debitamount'];
	$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,Txn,Debit,Date,Time,Transactiontype,Paynrechid,Operator,Mobile,Amount,Status)
	values('$userid','-$debitamount','$debitamount','$date','$time','Recharge-Resuccess','$id','$oper','$mobile','$amount','SUCCESS')";
	
	if(mysqli_query($con,$query2))
	{
	        $cewalletid=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;
	        }
	        $Openingbalance=$newbal+$debitamount;
		
	        $queryW="update wallet set Openingbalance='$Openingbalance', Balance='$newbal' where Id='$cewalletid'";
			mysqli_query($con,$queryW);
		$query4="update recharge set Creditewalletid='$cewalletid', Balance='$newbal', Status='SUCCESS' where Id='$id' ";
		mysqli_query($con, $query4);
		$query12="update retailer set Balance='$newbal' where Userid='$userid'";
		mysqli_query($con,$query12);
		header('location:admin_recharge_history.php');
	}

?>