<?php
    ob_start();
	session_start();
	if($_SESSION['distributer'])
	{
		$userid    = $_SESSION['distributer'];
		$fdate     = $_SESSION['fd'];
	    $ldate     = $_SESSION['ld'];
		$commision = $_SESSION['sm'];
		$IP = $_SERVER['REMOTE_ADDR'];
		
		if($commision<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';
	include'../functions.php';
	
	$oldBal=getBal($userid);
	$newBal=$oldBal+$commision;
	    
	$updateq="UPDATE comm_wallet SET Redeem='YES' WHERE Userid='$userid' AND Date>='$fdate' AND Date<='$ldate'";
	if(mysqli_query($con,$updateq))
	{   
	    $insertq="INSERT INTO wallet(Date,Time,Userid,OpBal,Txn,ClBal,TxnType,TxnWith,Remarks)
	    values('$date','$time','$userid','$oldBal','$commision','$newBal','Commission_Redeemed','$IP','Add Commision For $fdate to $ldate')";
	    if(mysqli_query($con,$insertq))
	    {
	        $newInsertId=mysqli_insert_id($con);
	        $updateTxnId="UPDATE comm_wallet SET Redeem='$newInsertId' WHERE Userid='$userid' AND Date>='$fdate' AND Date<='$ldate'";
	        if(mysqli_query($con,$updateTxnId))
	        {
		        $respMsg="SUCCESS";
		    }
		    else
		    {
		        $respMsg="FAILED";
		    }
	    }
	    else
	    {
	        $respMsg="FAILED";
	        //$respMsg=$insertq;
	    }
	}
	else
	{
	    $respMsg="FAILED";
	    //$respMsg=$updateq;
	}
	header('location:redeem_commission.php?respMsg='.$respMsg);
	
    ob_end_flush();
?>