<?php
	include'session.php';
	include'../connection.php';
	include'../functions.php';
	
?>
<html>
<head>
	<title>Recharge Report</title>
	<!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    
	<link rel="icon" href="images/logo.png" type="image/gif" sizes="16x16">
	
	<!-- Admin CSS File Link -->
	<link rel="stylesheet" href="css/style.css">
	
	<!-- User JAVA SCRIPT File Link -->
	<script type="text/javascript" src="js/script.js"></script>

	
	<!-- Font Awesome Icone -->
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
	
	<style>
		.div1{
			background:;
			width:96%;
			margin:0 2%;
			padding:5px 10px;
		}
		.div1 input, .div1 select{
			height:40px;
			max-width:160px;
			font-size:14px;
			padding:5px;
			border-radius:5px;
			border:solid #6b5127 1px;
			overflow:hidden;
			margin:10px 5px;
		}
		input[type=submit]{
			background:#6b5127;
			color:white;
			transition:1s;
			cursor:pointer;
		}
		input[type=submit]:hover{
			background:white;
			color:#6b5127;
			transition:1s;
		}
		.div2{
			background:#f7f7f7;
			width:100%;
			height:450px;
			float:left;
			text-align:center;
			padding:5px;
			overflow:auto;
		}
		.div2 table{
			width:98%;
			text-align:center;
			font-size:13px;
			border-collapse:collapse;
			padding:2px 5px;
			margin:1%;
		}
        .div2 table tr:nth-child(odd){background:#f6f0e8;}
        .div2 table tr:nth-child(even){background:white; }
		table th{
			background:#6b5127;
			color:white;
			height:35px;
			border:solid white 1px;
			padding:2px 5px;
		}
		table td{
			height:30px;
			border:solid black 1px;
			padding:2px 5px;
		}
		
		#account,#orderid,#status{display:none;}
		#dateTd{min-width:90px;}
		#opText{width:110px; height:100%; overflow:auto; padding:2px; margin:0; border:0; text-align:center; background:none;}
		@media(max-width:768px) 
		{
			.div1{
				background:;
				width:100%;
				height:auto;
				margin:10px 0 0 0;
				padding:0;
				text-align:center;
			}
			.div1 input,.div1 select{
				width:90%;
				margin:10px;
			}
			.div2{
				width:100%;
				margin:10px 0 0 0;
				padding:0px;
				text-align:center;
			    height:auto;
			    overflow:auto;
			}
		}
		

	</style>
	
	<link rel="stylesheet" href="css/mediaStyle.css">
</head>
<body>
	<!--Header Divison-->
	<?php
		require'header.php';
	?>
	
	<!--Container Division-->
	<div class="container">
		<?php
			include'menu.php';
		?>
		<div class="content" id="contentDiv" style="width:<?php echo $width; ?>">
			<div class="head">
				<div class="title"><i class="fa fa-bars" onclick="hideMenu()" style="float:left; margin:10px;"></i><p>Recharge Report :-</p></div>
				<div class="msg"><marquee><?php include'../msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
			    <script>
			        
			        function searchType()
			        {
			            var searchType = document.getElementById("sarchType").value;
			            
			            if(searchType=="USER")
			            {
			                document.getElementById("user").style.display = "inline";
			                document.getElementById("seller").style.display= "none";
			                document.getElementById("firmPlace").style.display= "none";
			                document.getElementById("inpNum").style.display= "none";
			                document.getElementById("status").style.display= "inline";
			                
			                diffType();
			            }
			            else if(searchType=="SELLER")
			            {
			                document.getElementById("api").style.display = "none";
			                document.getElementById("seller").style.display= "inline";
			                document.getElementById("firmPlace").style.display= "inline";
			                document.getElementById("inpNum").style.display= "inline";
			                document.getElementById("status").style.display= "none";
			            }
			        }
			        
			        
			        function sellerFirm()
			        {
			            var AjaxName      = "Ajax8";
			            var Progtype      = "RechReport";
			            var sellerid      = document.getElementById("seller").value;
						var xmlhttpSeller = new XMLHttpRequest();
						xmlhttpSeller.open("GET","Ajax.php?AjaxName="+AjaxName+"&Progtype="+Progtype+"&sellerid="+sellerid, false)
						xmlhttpSeller.send(null);
						alert(xmlhttpSeller.responseText);
						document.getElementById("firmPlace").innerHTML = xmlhttpSeller.responseText;
			        }
			 </script> 
			    <?php
			        if(isset($_POST['submit']))
			        {
			            $fDateValue = $_POST['fdate'];
			            $lDateValue = $_POST['ldate'];
						$sellerId   = $_POST['seller'];
						$firmId     = $_POST['firm'];
						$usrId      = $_POST['usrId'];
						$status    = $_POST['status'];
			        }
			        else
			        {
			            $fDateValue=$date;
			            $lDateValue=$date;
			        }
			    ?>
				<form method="post">
					<div class="div1">
						<input type="date" name="fdate" id="date1" value="<?php echo $fDateValue; ?>">
						<input type="date" name="ldate" id="date2" value="<?php echo $lDateValue; ?>">
						<select name="sarchType" onchange="searchType()" id="sarchType">
						    <option>SELLER</option>
						    <option>USER</option>
						</select>
		                <select name="sellerId" id="seller" onchange="sellerFirm()">
		                    <option>ALL</option>
		                    <?php
		                        $quSel="SELECT * FROM Seller WHERE Status='ACTIVE' ORDER BY Name";
		                        $reSel=mysqli_query($con,$quSel);
		                        while($dataSel=mysqli_fetch_array($reSel))
		                        {
		                            if($sellerId==$dataSel['Id'])
		                            {
		                                echo "<option selected value='".$dataSel['Id']."'>".$dataSel['Name']."</option>";
		                            }
		                            else
		                            {
		                                echo "<option value='".$dataSel['Id']."'>".$dataSel['Name']."</option>";
		                            }
		                        }
		                    ?>
		                </select>
		                <select name="usrId" id="user" style="display:none;">
		                    <option>ALL</option>
		                    <?php
		                        $quUsr="SELECT * FROM user WHERE Status='ACTIVE' ORDER BY Name";
		                        $reUsr=mysqli_query($con,$quUsr);
		                        while($dataUsr=mysqli_fetch_array($reUsr))
		                        {
		                            if($usrId==$dataUsr['Userid'])
		                            {
		                                echo "<option selected value='".$dataUsr['Userid']."'>".$dataUsr['Name']."</option>";
		                            }
		                            else
		                            {
		                                echo "<option value='".$dataUsr['Userid']."'>".$dataUsr['Name']."</option>";
		                            }
		                        }
		                    ?>
		                </select>
		                <select name="firmId" id="firmPlace">
		                    <option>ALL</option>
		                </select>
		                <input style="width:70px" type="number" name="DiffAmt" id="inpNum" value="500">
						<select name="status" id="status">
						    <option>SUCCESS</option>
						    <option>FAILED</option>
						    <option>PENDING</option>
						    <option>ALL</option>
						</select>
						<input type="submit" name="submit" value="Submit"  onclick='showLoader()'>
					</div>
				</form>
				<div class="div2">
					<table cellpadding="20" border="2"> 
						<tr>
							<th>Order Id</th>
							<th>Date Time</th>
							<th>Name</th>
							<th>Bal.</th>
							<th>Op Name</th>
							<th>Mobile</th>
							<th>Amt.</th>
							<th>RO</th>
							<th>%</th>
							<th>RT</th>
							<th>Seller</th></th>
							<th>API / Lapu</th>
							<th>Bal</th>
							<th>Diff</th>
							<th>Op. Id</th>
							<th>C</th>
							<th>Status</th>
							<th>A</th>
						</tr>
						<?PHP
						    $dataR = "recharge.Id, recharge.Date, recharge.Time, recharge.Userid, recharge.UsrBal, recharge.OpCode, recharge.OpName, recharge.Mobile, recharge.Amount, recharge.R_offer, recharge.R_time, recharge.Lapu, recharge.ApiBal, recharge.Diff, recharge.OpId, recharge.Roaming, recharge.Status,";
							$dataL = "seller_lapu.SellerId, seller_lapu.FirmId,";
							$dataS = "Seller.Name,";
							$dataF = "seller_firm.FirmName";
							
							$IJrNl = "INNER JOIN seller_lapu ON seller_lapu.LapuNo=recharge.Lapu";
							$IJrNs = "INNER JOIN Seller ON Seller.Id=seller_lapu.SellerId";
							$IJrNf = "INNER JOIN seller_firm ON seller_firm.Id=seller_lapu.FirmId";
						    
							if(isset($_POST['submit']))
							{
								$sarchType = $_POST['sarchType'];
								$fdate     = $_POST['fdate'];
								$ldate     = $_POST['ldate'];
								$sellerId  = $_POST['sellerId'];
								$firmId    = $_POST['firmId'];
								$usrId     = $_POST['usrId'];
								$status    = $_POST['status'];
								$DiffAmt   = $_POST['DiffAmt'];
								
								$sl          = 0;
								
								
								
								if($sarchType=="SELLER")
								{
								    if($DiffAmt>0)
							        {
							            $whereDiff = "AND recharge.Diff>=$DiffAmt";
							        }
							        elseif($DiffAmt<0)
							        {
							            $whereDiff = "AND recharge.Diff<=$DiffAmt";
							        }
							        $whereSeller = "";
							        if($sellerId!="ALL")
							        {
							            $whereSeller = "AND Seller.Id=$sellerId";
							            if($firmId!="ALL")
							            {
							               $whereSeller = "AND Seller.Id=$sellerId AND seller_firm.Id=$firmId"; 
							            }
							        }
								}
								elseif($sarchType=="USER")
								{
								    if($usrId!="ALL")
								    {
								        $whereUsr = "AND recharge.Userid='$usrId'";
								    }
								    
								    if($status!="ALL")
								    {
								        $whereStatus = "AND recharge.Status='$status'";
								    }
								}
								
								$whereDate = "recharge.Date>='$fdate' AND recharge.Date<='$ldate'";
								$quRech = "SELECT $dataR $dataL $dataS $dataF FROM recharge $IJrNl $IJrNs $IJrNf WHERE $whereDate $whereUsr $whereSeller $whereDiff $whereStatus";
								
							}
							else
							{
								$sl=0;
								$quRech="SELECT $dataR $dataL $dataS $dataF FROM recharge $IJrNl $IJrNs $IJrNf ORDER BY recharge.Id DESC LIMIT 40";
								$sql = "START";
							}
							
							if($sql == "START" || isset($_POST['submit']))
							{   
							    $LSN     = "";
							    $LFN     = "";
							    $totalAmt     = "";
								$reRech=mysqli_query($con,$quRech);
								while($dataRech=mysqli_fetch_array($reRech))
								{
								    $RechId   = $dataRech[0];
								    $RechDate = $dataRech[1];
								    $RechTime = $dataRech[2];
								    $UsrId    = $dataRech[3];
								    $UsrBal   = $dataRech[4];
								    $OpCode   = $dataRech[5];
								    $OpName   = $dataRech[6];
								    $Mobile   = $dataRech[7];
								    $Amount   = $dataRech[8];
								    $R_offer  = $dataRech[9];
								    $R_Time   = $dataRech[10];
								    $LapuNo   = $dataRech[11];
								    $LapuBal  = $dataRech[12];
								    $Diff     = $dataRech[13];
								    $OpId     = $dataRech[14];
								    $Roaming  = $dataRech[15];
								    $Status   = $dataRech[16];
								    $SellerId = $dataRech[17];
								    $FirmId   = $dataRech[18];
								    $SellName = $dataRech[19];
								    $FirmName = $dataRech[20];
								    
									$totalAmt  = $totalAmt+$Amount;
								    $TotRoffer = $TotRoffer+$R_offer;
									$totalDiff = $totalDiff+$Diff;
									$RO_Perc   = $R_offer*100/$Amount;
									
									$quAROP    = "SELECT SUM(R_offer), SUM(Amount) FROM recharge WHERE OpCode='$OpCode' AND Userid='$UsrId' AND Status='SUCCESS'";
									$reAROP    = mysqli_query($con,$quAROP);
									$dataAROP  = mysqli_fetch_array($reAROP);
									$AllRoPerc = $dataAROP[0]*100/$dataAROP[1];
									
									$realDiff  = $Diff*1000/1025;
						            $round500  = floor($realDiff/500)*500;
						            $totR500   = $totR500+$round500;
						            $round1000 = floor($realDiff/1000)*1000;
						            $totR1000  = $totR1000+$round1000;
						            
							        $color="blue";
								    if($Status=="FAILED")
									{
										$color="red";
									}
									elseif($Status=="SUCCESS")
									{
										$color="green";
									}
									$usrName=getName($UsrId);
									$colorBal="green";
									if($LapuBal<1000)
									{
									    $colorBal="red";
									}
									
									echo"<tr>";
										echo "<td><a href='check_txn_status.php?rechId=".$RechId."' target='_blank'>".$RechId."</a></td>";
										echo "<td id='dateTd'>".$RechDate."<br>".$RechTime."</td>";
										echo "<td><a href='recharge_by_user.php?uid=".$UsrId."' target='_blank'>".$usrName."</a></td>";
										$coloUsrBal ="";
										if($UsrBal<5000)
										{
										    $coloUsrBal ="style='color:red'";
										}
										echo "<td $coloUsrBal>".floor($UsrBal)."</td>";
										echo "<td>".$OpName."</td>";
										echo "<td>".$Mobile."</td>";
										echo "<td>".$Amount."</td>";
									    echo "<td>".getRA($R_offer)."<br>".getRA($RO_Perc)."</td>";
									    echo "<td>".getRA($AllRoPerc)."</td>";
	    								echo "<td>".$R_Time."</td>";
	    								echo "<td style='font-size:9px;'>".$SellName."<br>".$FirmName."</td>";
			    						echo "<td><a href='recharge_by_lapu.php?lapuNo=".$LapuNo."' target='_blank'>".$LapuNo."</a></td>";
										echo "<td style='color:".$colorBal."; font-size:13px'><b>".floor($LapuBal)."</b></td>";
										echo "<td>".getRA($Diff)."</td>";
										echo "<td style='padding:0;'><textarea id='opText'>".$OpId."</textarea></td>";
										echo "<td>".$Roaming."</p></td>";
										echo "<td><p id='".$color."'>".$Status."</p></td>";
										echo "<td>".getCAR($RechId)."</td>";
									echo"</tr>";
								    
								}
							}
							
							echo "<tr>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th>Total</th>";
							    echo "<th>".$totalAmt."</th>";
							    echo "<th>".getRA($TotRoffer)."</th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th>1k( ".$totR1000." )</th>";
							    echo "<th>".floor($totalDiff)."</th>";
							    echo "<th>5( ".$totR500." )</th>";
							    echo "<th>[".$totalExtra."]</th>";
							    echo "<th></th>";
							echo "</tr>";
						?>
					</table>
				</div>
			</div>
		</div>
	</div>
	
	<?php
		require'footer.php';
		if(!isset($_POST['submit']))
		{
		    echo "<META HTTP-EQUIV ='Refresh' Content ='90' >";
		}
	?>
</body>
</html>