<?php
    ob_start();
	session_start();
	if($_SESSION['did'])
	{
		$userid=$_SESSION['did'];
		$fdate=$_SESSION['fd'];
	    $tdate=$_SESSION['td'];
		$sum=$_SESSION['sm'];
		if($sum<100)
		{
		    echo "<h2 align='center' style='color:red;'>Commission Will Be Redeemed Rs.100 or Above only !</h2>";
		    exit();
		}
	}
	else
	{
		header('location:../login.php');
	}
?>
<?php
	include'connection.php';
	
	$quGetEwalletId="select * from RetBal where Userid='$userid'";
    $reGetEwalletId=mysqli_query($con,$quGetEwalletId);
    $dataGetEwalletId=mysqli_fetch_array($reGetEwalletId);
    $EwalletId=$dataGetEwalletId['Walletid'];
    
	date_default_timezone_set("asia/kolkata");
	$date=date("Y-m-d");
	$time=date("h:i:s A");
	
	$insertq="insert into wallet(Userid,Txn,Credit,Remarks,Date,Time,Transactiontype)
	values('$userid','$sum','$sum','Add Commision For $fdate to $tdate','$date','$time','Commission_Redeemed')";
	if(mysqli_query($con,$insertq))
	{
	    $DisEwalletId=mysqli_insert_id($con);
	    
	    $queryNewBal="select * from wallet where Userid='$userid' and Id>=$EwalletId";
	    $resultNewBal=mysqli_query($con,$queryNewBal);
	    $newbal=0;
	    while($dataNewBal=mysqli_fetch_array($resultNewBal))
	    {
	        $Retbal= $dataNewBal['Txn'];
	        $newbal=$newbal+$Retbal;
	    }
	    
	    $queryW="update wallet set Balance='$newbal' where Id='$DisEwalletId'";
		mysqli_query($con,$queryW);
		
		$updatebal="update distributer set Balance='$newbal' where Userid='$userid'";
		mysqli_query($con,$updatebal);
		
		$updateq="update commwallet set Active='1' where Userid=$userid and Date>='$fdate' and Date<='$tdate'";
		mysqli_query($con,$updateq);
		
		header('location:withdraw_commision.php');
	}
	
    ob_end_flush();
?>