<?php
        // Reward Status Update Script//

	    include'connection.php';
		// Reward Status Update Query //
		date_default_timezone_set("asia/kolkata");
	    $date=Date("Y-m-d");
	    $PrintDate=Date("d-M-Y");
	    
	    $TotalRechamount=0;
	        
		
		$query2="select * from distributer";
		$result2=mysqli_query($con,$query2);
		while($data=mysqli_fetch_array($result2))
		{
			$DisId=$data['Userid'];
			$TotalRechamount=0;
	        $date1=date_create($date);
		    date_sub($date1,date_interval_create_from_date_string("10 days"));
		    $resultdate=date_format($date1,"Y-m-d");
		    
	        $quGetPer="select * from recharge where Status='SUCCESS' and Parentid='$DisId' and Rechargedate<='$date' and Rechargedate>'$resultdate'";
	        $reGetPer=mysqli_query($con,$quGetPer);
	        while($dataGetPer=mysqli_fetch_array($reGetPer))
	        {
	            $RechAmount=$dataGetPer['Amount'];
	            $TotalRechamount=$TotalRechamount+$RechAmount;
	        }
	        
	        if($TotalRechamount>=100000)
	        {
	            $quGetReward="select * from distributer where Userid='$DisId'";
	            $reGetReward=mysqli_query($con,$quGetReward);
	            $dataGetReward=mysqli_fetch_array($reGetReward);
	            $Reward1=$dataGetReward['Reward1'];
	            if(empty($Reward1))
	            {
	                $updateReward="update distributer set Reward1='$PrintDate' where Userid='$DisId'";
	                mysqli_query($con,$updateReward);
	            }
	        }
			
	        $TotalRechamount2=0;
	        $date2=date_create($date);
		    date_sub($date2,date_interval_create_from_date_string("30 days"));
		    $resultdate2=date_format($date2,"Y-m-d");
	        $quGetPer2="select * from recharge where Status='SUCCESS' and Parentid='$DisId' and Rechargedate<='$date' and Rechargedate>'$resultdate2'";
	        $reGetPer2=mysqli_query($con,$quGetPer2);
	        while($dataGetPer2=mysqli_fetch_array($reGetPer2))
	        {
	            $RechAmount2=$dataGetPer2['Amount'];
	            $TotalRechamount2=$TotalRechamount2+$RechAmount2;
	        }
	        
	        if($TotalRechamount2>=500000)
	        {
	            $quGetReward="select * from distributer where Userid='$DisId'";
	            $reGetReward=mysqli_query($con,$quGetReward);
	            $dataGetReward=mysqli_fetch_array($reGetReward);
	            $Reward2=$dataGetReward['Reward2'];
	            if(empty($Reward2))
	            {
	                $updateReward="update distributer set Reward2='$PrintDate' where Userid='$DisId'";
	                mysqli_query($con,$updateReward);
	            }
	        }
			
			$TotalRechamount3=0;
	        $date3=date_create($date);
		    date_sub($date3,date_interval_create_from_date_string("60 days"));
		    $resultdate3=date_format($date3,"Y-m-d");
	        $quGetPer3="select * from recharge where Status='SUCCESS' and Parentid='$DisId' and Rechargedate<='$date' and Rechargedate>'$resultdate3'";
	        $reGetPer3=mysqli_query($con,$quGetPer3);
	        while($dataGetPer3=mysqli_fetch_array($reGetPer3))
	        {
	            $RechAmount3=$dataGetPer3['Amount'];
	            $TotalRechamount3=$TotalRechamount3+$RechAmount3;
	        }
	        
	        if($TotalRechamount3>=2000000)
	        {   
	            $quGetReward="select * from distributer where Userid='$DisId'";
	            $reGetReward=mysqli_query($con,$quGetReward);
	            $dataGetReward=mysqli_fetch_array($reGetReward);
	            $Reward3=$dataGetReward['Reward3'];
	            if(empty($Reward3))
	            {
	                $updateReward="update distributer set Reward3='$PrintDate' where Userid='$DisId'";
	                mysqli_query($con,$updateReward);
	            }
	        }
			
			$TotalRechamount4=0;
	        $date4=date_create($date);
		    date_sub($date4,date_interval_create_from_date_string("180 days"));
		    $resultdate4=date_format($date4,"Y-m-d");
	         $quGetPer4="select * from recharge where Status='SUCCESS' and Parentid='$DisId' and Rechargedate<='$date' and Rechargedate>'$resultdate4'";
	        $reGetPer4=mysqli_query($con,$quGetPer4);
	        while($dataGetPer4=mysqli_fetch_array($reGetPer4))
	        {
	            $RechAmount4=$dataGetPer4['Amount'];
	            $TotalRechamount4=$TotalRechamount4+$RechAmount4;
	        }
	        
	        if($TotalRechamount4>=9000000)
	        {   
	            $quGetReward="select * from distributer where Userid='$DisId'";
	            $reGetReward=mysqli_query($con,$quGetReward);
	            $dataGetReward=mysqli_fetch_array($reGetReward);
	            $Reward4=$dataGetReward['Reward4'];
	            if(empty($Reward4))
	            {
	                $updateReward="update distributer set Reward4='$PrintDate' where Userid='$DisId'";
	                mysqli_query($con,$updateReward);
	            }
	        }
			
			$TotalRechamount5=0;
	        $date5=date_create($date);
		    date_sub($date5,date_interval_create_from_date_string("360 days"));
		    $resultdate5=date_format($date5,"Y-m-d");
	        $quGetPer5="select * from recharge where Status='SUCCESS' and Parentid='$DisId' and Rechargedate<='$date' and Rechargedate>'$resultdate5'";
	        $reGetPer5=mysqli_query($con,$quGetPer5);
	        while($dataGetPer5=mysqli_fetch_array($reGetPer5))
	        {
	             $RechAmount5=$dataGetPer5['Amount'];
	            $TotalRechamount5=$TotalRechamount5+$RechAmount5;
	        }
	        if($TotalRechamount5>=36000000)
	        {   
	            $quGetReward="select * from distributer where Userid='$DisId'";
	            $reGetReward=mysqli_query($con,$quGetReward);
	            $dataGetReward=mysqli_fetch_array($reGetReward);
	            $Reward5=$dataGetReward['Reward5'];
	            if(empty($Reward5))
	            {
	                $updateReward="update distributer set Reward5='$PrintDate' where Userid='$DisId'";
	                mysqli_query($con,$updateReward);
	            }
	        }
		}
		
echo "<h1 align='center'><font color='#008000'> &#x2714; Reward Status Updated Successfully !</font></h1><br>";
?>