<?php
	session_start();
	if($_SESSION['subadm'])
	{
		$userid=1111;
	}
	else
	{
		header('location:../login.php');
	}
?>
<?php
	include'connection.php';
		
		$query8="select * from wallet where Userid='$userid' order by Id desc";
		$result8=mysqli_query($con,$query8);
		$data8=mysqli_fetch_array($result8);
		$currentbal= $data8['Balance'];
	
?>
<html>
<head>
<style>
body{background:#e6d9c8; background-image:linear-gradient(to bottom right, #e6d9c8,#996420); background-size:cover; background-repeat:repeat; background-repeat:repeat-x; margin:0; padding:0; width:1580px;}
.head{background:; width:1580px; height:80px; margin:0px;}
#p1{font-size:34px; float:left; Margin:10 0 0 45px; color:#996420;
	font-weight:bold;}
#p2{font-size:20px; float:left; margin:28 0 0 -8px; color:#eaa040;
	font-family:Courier;}
#p3{font-size:15px; font-family:Comic Sans MS; float:left; color:#996420;
	margin-top:48px; margin-left:-153px;}
.h2{background:white; width:400px; height:70px; margin:10 200px;}

#link0{float:right; margin:10 10 0 0px; font-size:20px; text-decoration:none; color:white; background:red; padding:5px; border-radius:4px;}
 #link1{float:right; margin:10 10 0 0px; font-size:20px; text-decoration:none; color:white; background:green; padding:5px; border-radius:4px;}
#link0:hover{ padding:5px; border-radius:4px; color:black;}

.menu{background:#996420; height:40px; width:1450px; margin-left:10px; padding:0; line-height:40px;}
.menu ul{padding:0; margin:0; position:relative; display:inline-block;}
.menu ul li{list-style:none; float:left; width:170px; line-height:40px; text-align:center; font-size:15px; margin:0; padding:0; color:white;}
.menu ul li ul {display:none;}
.menu ul li:hover{background:#d1994a; cursor:pointer;}
.menu ul li:hover ul {display:block; z-index:2;}
.menu ul li:hover ul li{background:#d1994a; border-top:solid white 0.1px;}
.menu ul li ul li:hover{background:#996420; color:white;}
.menu ul li ul li:hover a{ color:white;}
.menu ul li a{ text-decoration:none; color:white;}
 #a1{border:none; color:white;}
 .menu{letter-spacing:1px;}
#AFLI{background:white; width:200px;}
#AFLI:hover{background:#f5f5f5;}
 #inp{width:200px; height:35px; padding:3 10px; font-size:20px;}
 #srchby{margin-left:20px; font-family:calibri; font-size:22px;}
 .date{background:; margin:10 0 0 20px;}
 .rechreport{width:1560px; height:40px; background:#996420; color:white; line-height:40px; margin:0 0 0 20px;}
 .rechreport p{margin-left:20px;}
.notifi{background:#995702; width:800px; height:70px; margin:5 5 5 300px; border-radius:8px;}
.imgdiv{background:; width:60px; height:60px; float:left; margin:0 20px;}
#notifidiv{background:red; width:40px; height:40px; margin:0 0 0 30px; border-radius:50%; text-align:center; line-height:40px;
color:white; font-size:20px; z-index:4; float:left; position:absolute;}
img{width:60px; height:60px; float:left; margin:7px;}
#myInput{width:200px; height:40px; font-size:17px; margin:0 10px; border-radius:8px;}

#ROffr{background:gray; color:white; font-size:22px; }

#t1{ width:1560px; text-align:center; border-collapse:collapse; border-color:black; margin-left:20px;}
#t1 th{font-family:calibri; background:#94928f; color:white; font-size:17px;}
#t1 td{font-family:calibri; font-size:15px;}
#tda a{text-decoration:none; font-size:18px; font-family:sans-serif; color:#0b52e2;}
#rechId{text-decoration:none; background:; color:blue; font-size:18px; padding:2 5px; border-radius:4px; font-weight:bold;}
#green{background:green; color:white; font-size:18px; padding:2 5px; border-radius:4px; font-weight:bold;}
#red{background:red; color:white; font-size:18px; padding:2 5px; border-radius:4px; font-weight:bold;}
#blue{background:blue; color:white; font-size:18px; padding:2 5px; border-radius:4px; font-weight:bold;}
#lightPink{background:#f4cfd7;}
#pink{background:#f6f0e8;}
 .foot{float:left; margin:10 0 0 100px; width:1350px;}
 </style>
</head>
<body>
	<div class="head">
		<p id="p1">LLP</p>
		<p id="p2">Recharge</p>
		<p id="p3">The Complete Solution</p>
		<a id="link0" href="logout.php">Log Out</a>
		<p id="link1"><?php echo "Welcome $userid Admin";  ?></p>
    </div>
	<?php include 'menu.php'; ?>
	<div class="date">
	    <?php
	        
			include'connection.php';
			
	        $LapuId=$_GET['lapuId'];
			$RechId=$_GET['rechId'];
			$RDate=$_GET['date'];
			
			        $LapuNo=$LapuId;
    			    if(!empty($LapuNo))
    			    {
			            $quGetLapuOwner="select * from myapi where LapuNumber='$LapuNo'";
			            $reGetLapuOwner=mysqli_query($con,$quGetLapuOwner);
			            $dataGetLapuOwner=mysqli_fetch_array($reGetLapuOwner);
			            $LapuOwner=$dataGetLapuOwner['Seller'];
    			    }
    			    else
    			    {
    			        $LapuOwner="";
    			    }
			
	        date_default_timezone_set("asia/Kolkata");
			$datetime= Date("Y-m-d - h:i:s A");
			$time= Date("h:i:s A");
			$date= Date("Y-m-d");
			
	    ?>
	    <center><table border="1" cellpadding="10" id="ROffr">
	        <?php
	            $TotalTxn=0;
	            $R_Offer=0;
	            $TotRROffer=0;
	            $quTotalTxn="select * from recharge where Rechargedate>='$date' and Status='SUCCESS' and LapuNo='$LapuId'";
	            $reTotalTxn=mysqli_query($con,$quTotalTxn);
	            while($dataTotalTxn=mysqli_fetch_array($reTotalTxn))
	            {
	                $TotalTxn=$TotalTxn+$dataTotalTxn['Amount'];
	                $R_Offer=$R_Offer+$dataTotalTxn['R_Offer'];
	                
    			            if($dataTotalTxn['Diff']<45 && $dataTotalTxn['Diff']>0)
    			            {
    			                $TotRROffer=$TotRROffer+$dataTotalTxn['Diff'];
    			            }
	            }
	            
	           echo "<tr><th>Date</th><td>".$date."</td><th>Lapu Owner</th><td>".$LapuOwner."</td><th>Lapu No.</th><td>".$LapuId."</td><th>Total Txn</th><td>".$TotalTxn."</td><th>Total R-Offer</th><td>".floor($R_Offer)."</td><th>Total R_R-Offer(45)</th><td>".floor($TotRROffer)."</td></tr>";
	        ?>
	    </table></center><br>
	    <form method="post">
		From Date : <input type="date" name="fdate" value="<?php echo $date; ?>" id="inp">
		To Date : <input type="date" name="ldate" value="<?php echo $date; ?>" id="inp">
		<select name="stats" id="inp">
		    <option>SUCCESS</option>
		    <option>FAILED</option>
		    <option>FAILED</option>
		    <option>PENDING</option>
		    <option>ALL</option>
		</select>
		<select name="diffType" id="inp">
		    <option>ALL</option>
		    <option>PLUS</option>
		    <option>MINUS</option>
		</select>
		<input type="submit" name="submit" id="inp">
		</form>
	</div>
	<div class="rechreport">
		<p>Recharge History </p>
	</div>
	
	<table border="1" cellpadding="5" id="t1">
		<tr>
			<th>Date | Time</th>
			<th>Agent Name</th>
			<th>Distributer Name</th>
			<th>Balance</th>
			<th>Company Name</th>
			<th>Mobile No.</th>
			<th>Amount</th>
			<th>R Offer</th>
			<th>API</th>
			<th>Lapu No.</th>
			<th>API Balance</th>
			<th>API Diff</th>
			<th>Operator Id</th>
			<th>#</th>
			<th>Recharge Id</th>
			<th>Status</th>
			<th>Diff. Value</th>
			<th>#</th>
		</tr>
		<?php
			
			if(!empty($RDate))
			{
			    $date=$RDate;
			}
			$ToatlRechAmount=0;
			if(isset($_POST['submit']))
			{
			    $fdate=$_POST['fdate'];
			    $ldate=$_POST['ldate'];
			    $stats=$_POST['stats'];
			    $diffType=$_POST['diffType'];
			    
			    if($stats=="ALL")
			    {
			        $queryGetRechByLapuByDate="select * from recharge where Rechargedate>='$fdate' and Rechargedate<='$ldate' and LapuNo='$LapuId' order by Id desc"; 
			    }
			    else
			    {
			        $queryGetRechByLapuByDate="select * from recharge where Rechargedate>='$fdate' and Rechargedate<='$ldate' and Status='$stats' and LapuNo='$LapuId' order by Id desc";
			    }
			    
			    $resultGetRechByLapuByDate=mysqli_query($con,$queryGetRechByLapuByDate);
			    $sl=0;
			    $TotalRechAmount=0;
			    $TotalROffer=0;
			    $TotalRROffer=0;
			    $totalDiff=0;
			    $totalNetRoundDiff500=0;
			    $totalNetRoundDiff1k=0;
			    
			    while($dataByDate=mysqli_fetch_array($resultGetRechByLapuByDate))
			    {
			        $sl=$sl+1;
			        $TotalRechAmount=$TotalRechAmount+$dataByDate['Amount'];
			        $TotalROffer=$TotalROffer+$dataByDate['R_Offer'];
			        $quGetName1="select * from retailer where Userid='".$dataByDate['Userid']."'";
			         $reGetName1=mysqli_query($con,$quGetName1);
			         $dataGetName1=mysqli_fetch_array($reGetName1);
    			    if($dataByDate['Status']=="SUCCESS")
    			    {
    			        $color="green";
    			    }
    			    elseif($dataByDate['Status']=="FAILED")
    			    {
    			        $color="red";
    			    }
    			    else
    			    {
    			        $color="blue";
    			    }
			        
			        
    			    if($dataByDate['Id']==$RechId)
    			    {
    			        $background="lightPink";
    			    }
    			    else
    			    {
    			        $background="pink";
    			    }
    			    
    			    if($diffType=="PLUS")
    			    {
    			        if(floor($dataByDate['Diff'])>0)
    			        {
    			            $totalDiff=$totalDiff+$dataByDate['Diff'];
    			            
    			            $DiffK500 = $dataByDate['Diff']/500;
    			            $roundDiff500=floor($DiffK500);
    			            $netRoundDiff500=$roundDiff500*500;
    			            $totalNetRoundDiff500=$totalNetRoundDiff500+$netRoundDiff500;
    			            
    			            $Diff1K = $dataByDate['Diff']/1000;
    			            $roundDiff1k=floor($Diff1K);
    			            $netRoundDiff1k=$roundDiff1k*1000;
    			            $totalNetRoundDiff1k=$totalNetRoundDiff1k+$netRoundDiff1k;
    			            
    			            
        			    	echo "<tr id='$background'>";
            					echo "<td>".$dataByDate['Rechargedate']." | ".$dataByDate['Time']."</td>";
    	        				echo "<td id='tda' title='".$dataGetName1['Name']."'><a href='User_profile.php?id=".$dataGetName1['Userid']."' target='_blank'>".$dataGetName1['Userid']."</a></td>";
    		        			echo "<td>".$dataByDate['Agentname']."</td>";
    			        		echo "<td>".$dataByDate['Balance']."</td>";
    				        	echo "<td>".$dataByDate['Companyname']."</td>";
    					        echo "<td>".$dataByDate['Mobile']."</td>";
        					    echo "<td>".$dataByDate['Amount']."</td>";
        					    echo "<td>".$dataByDate['R_Offer']."</td>";
            					echo "<td>".$dataByDate['API']."</td>";
        	    				echo "<td>".$dataByDate['LapuNo']."</td>";
    	    	    			echo "<td>".$dataByDate['ApiBal']."</td>";
    		    	    		echo "<td>".$dataByDate['Diff']."</td>";
    			    	    	echo "<td>".$dataByDate['Operatorid']."</td>";
    				    	    echo "<td>".$row['AdminOpId']."</td>";
        				    	echo "<td><a id='rechId' href='check_status.php?id=".$dataByDate['Id']."' target='_blank'>".$dataByDate['Id']."</a></td>";
        					    echo "<td><p id='$color'>".$dataByDate['Status']."</p></td>";
        	    				echo "<td>".floor($dataByDate['AdminDiffValue'])."</td>";
        		    			if($dataByDate['ReHit']=="1" && $dataByDate['Status']=="PENDING")
    	    		    		{   
    		    		    	    echo "<td><a href='re_hit.php?id=".$dataByDate['Id']."' target='_blank'>Re-Hit</a></td>";
    			    		    }
        			    		else
        				    	{
    	    				        echo "<td></td>";
    		    			    }
    			    	    echo "</tr>";
    			         }
    			        
    			    }
    			    elseif($diffType=="MINUS")
    			    {
    			        if(floor($dataByDate['Diff'])<0)
    			        {
        			    	echo "<tr id='$background'>";
            					echo "<td>".$dataByDate['Rechargedate']." | ".$dataByDate['Time']."</td>";
    	        				echo "<td id='tda'><a href='User_profile.php?id=".$dataGetName1['Userid']."' target='_blank'>".$dataGetName1['Name']."</a></td>";
    		        			echo "<td>".$dataByDate['Agentname']."</td>";
    			        		echo "<td>".$dataByDate['Balance']."</td>";
    				        	echo "<td>".$dataByDate['Companyname']."</td>";
    					        echo "<td>".$dataByDate['Mobile']."</td>";
        					    echo "<td>".$dataByDate['Amount']."</td>";
        					    echo "<td>".$dataByDate['R_Offer']."</td>";
            					echo "<td>".$dataByDate['API']."</td>";
        	    				echo "<td>".$dataByDate['LapuNo']."</td>";
    	    	    			echo "<td>".$dataByDate['ApiBal']."</td>";
    		    	    		echo "<td>".$dataByDate['Diff']."</td>";
    			    	    	echo "<td>".$dataByDate['Operatorid']."</td>";
    				    	    echo "<td>".$row['AdminOpId']."</td>";
        				    	echo "<td><a id='rechId' href='check_status.php?id=".$dataByDate['Id']."' target='_blank'>".$dataByDate['Id']."</a></td>";
        					    echo "<td><p id='$color'>".$dataByDate['Status']."</p></td>";
        	    				echo "<td>".floor($dataByDate['AdminDiffValue'])."</td>";
        		    			if($dataByDate['ReHit']=="1" && $dataByDate['Status']=="PENDING")
    	    		    		{   
    		    		    	    echo "<td><a href='re_hit.php?id=".$dataByDate['Id']."' target='_blank'>Re-Hit</a></td>";
    			    		    }
        			    		else
        				    	{
    	    				        echo "<td></td>";
    		    			    }
    			    	    echo "</tr>";
    			            
    			         }
    			    }
    			    else
    			    {
    			            $DiffK500 = $dataByDate['Diff']/500;
    			            $roundDiff500=floor($DiffK500);
    			            $netRoundDiff500=$roundDiff500*500;
    			            $totalNetRoundDiff500=$totalNetRoundDiff500+$netRoundDiff500;
    			            
    			            $Diff1K = $dataByDate['Diff']/1000;
    			            $roundDiff1k=floor($Diff1K);
    			            $netRoundDiff1k=$roundDiff1k*1000;
    			            $totalNetRoundDiff1k=$totalNetRoundDiff1k+$netRoundDiff1k;
    			            
    			    	echo "<tr id='$background'>";
        					echo "<td>".$dataByDate['Rechargedate']." | ".$dataByDate['Time']."</td>";
    	    				echo "<td id='tda' title='".$dataGetName1['Name']."'><a href='User_profile.php?id=".$dataGetName1['Userid']."' target='_blank'>".$dataGetName1['Userid']."</a></td>";
    		    			echo "<td>".$dataByDate['Agentname']."</td>";
    			    		echo "<td>".$dataByDate['Balance']."</td>";
    				    	echo "<td>".$dataByDate['Companyname']."</td>";
    					    echo "<td>".$dataByDate['Mobile']."</td>";
        					echo "<td>".$dataByDate['Amount']."</td>";
        					echo "<td>".$dataByDate['R_Offer']."</td>";
        					echo "<td>".$dataByDate['API']."</td>";
    	    				echo "<td>".$dataByDate['LapuNo']."</td>";
    		    			echo "<td>".$dataByDate['ApiBal']."</td>";
    			    		echo "<td>".$dataByDate['Diff']."</td>";
    				    	echo "<td>".$dataByDate['Operatorid']."</td>";
    					    echo "<td>".$row['AdminOpId']."</td>";
        					echo "<td><a id='rechId' href='check_status.php?id=".$dataByDate['Id']."' target='_blank'>".$dataByDate['Id']."</a></td>";
        					echo "<td><p id='$color'>".$dataByDate['Status']."</p></td>";
    	    				echo "<td>".floor($dataByDate['AdminDiffValue'])."</td>";
    		    			if($dataByDate['ReHit']=="1" && $dataByDate['Status']=="PENDING")
    			    		{   
    				    	    echo "<td><a href='re_hit.php?id=".$dataByDate['Id']."' target='_blank'>Re-Hit</a></td>";
    					    }
        					else
        					{
    	    				    echo "<td></td>";
    		    			}
    			    	echo "</tr>";
    			            if($dataByDate['Diff']<45 && $dataByDate['Diff']>0)
    			            {
    			                $TotalRROffer=$TotalRROffer+$dataByDate['Diff'];
    			            }
                    }
			    }
			   
			   echo "<tr>";
    					echo "<th>".$fdate."</th>";
    					echo "<th>".$ldate."</th>";
    					echo "<th></th>";
    					echo "<th></th>";
    					echo "<th></th>";
    					echo "<th></th>";
    					echo "<th>".$TotalRechAmount."</th>";
    					echo "<th>".$TotalROffer."</th>";
    					echo "<th></th>";
    					echo "<th></th>";
    					echo "<th>".$totalNetRoundDiff500."</th>";
    					echo "<th>".$totalDiff."</th>";
    					echo "<th>".$totalNetRoundDiff1k."</th>";
    					echo "<th></th>";
    					echo "<th></th>";
    					echo "<th></th>";
    					echo "<th>".$TotalRROffer."</th>";
    					echo "<th></th>";
				echo "</tr>";
			}
			else
			{
			    $queryGetRechByLapuByDate="SELECT * FROM recharge WHERE LapuNo='$LapuId' AND Status='SUCCESS' ORDER BY Id DESC LIMIT 5";
			    $resultGetRechByLapuByDate=mysqli_query($con,$queryGetRechByLapuByDate);
			    $sl=0;
			    $TotalRechAmount=0;
			    $TotalROffer=0;
			    $TotalRROffer=0;
			    while($dataByDate=mysqli_fetch_array($resultGetRechByLapuByDate))
			    {
			        $sl=$sl+1;
			        $TotalRechAmount=$TotalRechAmount+$dataByDate['Amount'];
			        $TotalROffer=$TotalROffer+$dataByDate['R_Offer'];
			        $quGetName1="select * from retailer where Userid='".$dataByDate['Userid']."'";
			         $reGetName1=mysqli_query($con,$quGetName1);
			         $dataGetName1=mysqli_fetch_array($reGetName1);
    			    if($dataByDate['Status']=="SUCCESS")
    			    {
    			        $color="green";
    			    }
    			    elseif($dataByDate['Status']=="FAILED")
    			    {
    			        $color="red";
    			    }
    			    else
    			    {
    			        $color="blue";
    			    }
			        
			        
    			    if($dataByDate['Id']==$RechId)
    			    {
    			        $background="lightPink";
    			    }
    			    else
    			    {
    			        $background="pink";
    			    }
    			
			    echo "<tr id='$background'>";
            					echo "<td>".$dataByDate['Rechargedate']." | ".$dataByDate['Time']."</td>";
    	        				echo "<td id='tda' title='".$dataGetName1['Name']."'><a href='User_profile.php?id=".$dataGetName1['Userid']."' target='_blank'>".$dataGetName1['Userid']."</a></td>";
    		        			echo "<td>".$dataByDate['Agentname']."</td>";
    			        		echo "<td>".$dataByDate['Balance']."</td>";
    				        	echo "<td>".$dataByDate['Companyname']."</td>";
    					        echo "<td>".$dataByDate['Mobile']."</td>";
        					    echo "<td>".$dataByDate['Amount']."</td>";
        					    echo "<td>".$dataByDate['R_Offer']."</td>";
            					echo "<td>".$dataByDate['API']."</td>";
        	    				echo "<td>".$dataByDate['LapuNo']."</td>";
    	    	    			echo "<td>".$dataByDate['ApiBal']."</td>";
    		    	    		echo "<td>".$dataByDate['Diff']."</td>";
    			    	    	echo "<td>".$dataByDate['Operatorid']."</td>";
    				    	    echo "<td>".$row['AdminOpId']."</td>";
        				    	echo "<td><a id='rechId' href='check_status.php?id=".$dataByDate['Id']."' target='_blank'>".$dataByDate['Id']."</a></td>";
        					    echo "<td><p id='$color'>".$dataByDate['Status']."</p></td>";
        	    				echo "<td>".floor($dataByDate['AdminDiffValue'])."</td>";
        		    			if($dataByDate['ReHit']=="1" && $dataByDate['Status']=="PENDING")
    	    		    		{   
    		    		    	    echo "<td><a href='re_hit.php?id=".$dataByDate['Id']."' target='_blank'>Re-Hit</a></td>";
    			    		    }
        			    		else
        				    	{
    	    				        echo "<td></td>";
    		    			    }
    			    	    echo "</tr>";
			    }
			}
		?>
	</table>
	<div class="foot">
		<p>Copyright &copy; 2018 Long Life Product Pvt. Ltd.</p>
	</div>
</body>
</html>