<?php
	session_start();
	if($_SESSION['did'])
	{
		$userid=$_SESSION['did'];
		$fdate=$_SESSION['fd'];
	    $tdate=$_SESSION['td'];
		$sum=$_SESSION['sm'];
	}
	else
	{
		header('location:../login.php');
	}
?>
<?php
	include'connection.php';
	date_default_timezone_get("asia/kolkata");
	$date=date("Y-m-d");
	$time=date("h:i:s A");
	$oldbalq="select * from wallet where Userid='$userid' order by Id desc";
	$resultoldbal=mysqli_query($con,$oldbalq);
	$dataoldbal=mysqli_fetch_array($resultoldbal);
	$oldbal=$dataoldbal['Balance'];
	$newbal=$oldbal+$sum;
	
	$insertq="insert into wallet(Userid,Openingbalance,Credit,Remarks,Balance,Date,Time,Transactiontype)
	values('$userid','$oldbal','$sum','Add Referal Commision For $fdate to $tdate','$newbal','$date','$time','Referal_Commission')";
	if(mysqli_query($con,$insertq))
	{
		$updatebal="update distributer set Balance='$newbal' where Userid='$userid'";
		mysqli_query($con,$updatebal);
		$updateq="update recharge set Referal='1' where ReferalId='$userid' and Status='SUCCESS' and Rechargedate>='$fdate' and Rechargedate<='$tdate'";
		mysqli_query($con,$updateq);
		header('location:getReferalIncome.php');
	}
	

?>