<?php
    ob_start();
	session_start();
	if($_SESSION['mid'])
	{
		$userid=$_SESSION['mid'];
		$fdate=$_SESSION['fd'];
	    $ldate=$_SESSION['ld'];
		$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';
	include'../Retailers/functions.php';
	
	date_default_timezone_set("asia/kolkata");
	$date=date("Y-m-d");
	$time=date("h:i:s A");
	
	$oldBal=getBal($userid);
	$newBal=$oldBal+$sum;
	    
	$updateq="UPDATE commwallet SET Active='1' WHERE Userid=$userid AND Date>='$fdate' AND Date<='$ldate'";
	if(mysqli_query($con,$updateq))
	{   
	    $insertq="INSERT INTO wallet (Userid,OpBal,Txn,ClBal,Remarks,Date,Time,TxnType)
	    VALUES ('$userid','$oldBal','$sum','$newBal','Add Commision For $fdate to $ldate','$date','$time','Commission_Redeemed')";
	    if(mysqli_query($con,$insertq))
	    {
	        $updatebal="UPDATE masterdis SET Balance='$newBal' WHERE Userid='$userid'";
		    if(mysqli_query($con,$updatebal))
		    {
		        $respMsg="SUCCESS";
		    }
		    else
		    {
		        $respMsg="FAILED";
		    }
	    }
	    else
	    {
	        $respMsg="FAILED";
	    }
	}
	else
	{
	    $respMsg="FAILED";
	}
	header('location:redeem_commission.php?respMsg='.$respMsg);
	
    ob_end_flush();
?>