<?php
	include'session.php';
	include'connection.php';
	include'../Retailers/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=="DATEWISE")
			            {
			                document.getElementById("date1").style.display= "inline";
			                document.getElementById("date2").style.display= "inline";
			                document.getElementById("Diff").style.display = "inline";
			                document.getElementById("account").style.display= "none";
			                document.getElementById("orderid").style.display= "none";
			                
			                diffType();
			            }
			            else if(searchType=="ACCOUNT_NO")
			            {
			                document.getElementById("date1").style.display= "none";
			                document.getElementById("date2").style.display= "none";
			                document.getElementById("status").style.display= "none";
			                document.getElementById("inpNum").style.display = "none";
			                document.getElementById("Diff").style.display = "none";
			                document.getElementById("seller").style.display = "none";
			                document.getElementById("account").style.display= "inline";
			                document.getElementById("orderid").style.display= "none";
			            }
			            else if(searchType=="ORDER_ID")
			            {
			                document.getElementById("date1").style.display = "none";
			                document.getElementById("date2").style.display = "none";
			                document.getElementById("status").style.display = "none";
			                document.getElementById("inpNum").style.display = "none";
			                document.getElementById("Diff").style.display = "none";
			                document.getElementById("seller").style.display = "none";
			                document.getElementById("account").style.display = "none";
			                document.getElementById("orderid").style.display = "inline";
			            }
			        }
			        
			        
			        function diffType()
			        {
			            var DiffType = document.getElementById("Diff").value;
			            
			            if(DiffType=="ALL")
			            {
			                document.getElementById("status").style.display= "inline";
			                document.getElementById("seller").style.display= "none";
			                document.getElementById("inpNum").style.display= "none";
			            }
			            else
			            {
			                document.getElementById("status").style.display= "none";
			                document.getElementById("seller").style.display= "inline";
			                document.getElementById("inpNum").style.display= "inline";
			            }
			        }
			    </script>
			    <?php
			        if(isset($_POST['submit']))
			        {
			            $fDateValue=$_POST['fdate'];
			            $lDateValue=$_POST['ldate'];
						$seller = $_POST['seller'];
			        }
			        else
			        {
			            $fDateValue=$date;
			            $lDateValue=$date;
			        }
			    ?>
				<form method="post">
					<div class="div1">
						<select name="sarchType" onchange="searchType()" id="sarchType">
						    <option>DATEWISE</option>
						    <option>ACCOUNT_NO</option>
						    <option>ORDER_ID</option>
						</select>
						<input type="date" name="fdate" id="date1" value="<?php echo $fDateValue; ?>">
						<input type="date" name="ldate" id="date2" value="<?php echo $lDateValue; ?>">
						<input type="number" name="account" id="account" placeholder="Mobile/DTH No....">
						<input type="number" name="orderid" id="orderid" placeholder="Order Id....">
		                <select name="diff" id="Diff" onchange="diffType()">
		                    <option>PLUS</option>
		                    <option>MINUS</option>
		                    <option>ALL</option>
		                </select>
		                <input style="width:70px" type="number" name="DiffAmt" id="inpNum" value="100">
		                <select name="seller" id="seller">
		                    <option>ALL</option>
		                    <?php
		                        $quGetSeller="SELECT * FROM Seller WHERE Status='Active' ORDER BY Name ASC";
		                        $reGetSeller=mysqli_query($con,$quGetSeller);
		                        while($dataGetSeller=mysqli_fetch_array($reGetSeller))
		                        {
		                            if($seller==$dataGetSeller['Name'])
		                            {
		                                echo "<option selected>".$dataGetSeller['Name']."</option>";
		                            }
		                            else
		                            {
		                                echo "<option>".$dataGetSeller['Name']."</option>";
		                            }
		                        }
		                    ?>
		                </select>
						<select name="status" id="status">
						    <option>ALL</option>
						    <option>SUCCESS</option>
						    <option>FAILED</option>
						    <option>PENDING</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>RC</th>
							<th>DC</th>
							<th>MC</th>
							<th>Operator</th>
							<th>Mobile</th>
							<th>Amt.</th>
							<th>RO [%]</th>
							<th>%</th>
							<th>API</th>
							<th>NO</th>
							<th>RT</th>
							<th>L.Owner</th>
							<th>Lapu No.</th>
							<th>Api Bal</th>
							<th>Api Diff</th>
							<th>Op. Id</th>
							<th>#</th>
							<th>Status</th>
							<th>CB</th>
							<th>RS</th>
							<th>@</th>
							<th>Parent</th>
						</tr>
						<?PHP
							if(isset($_POST['submit']))
							{
								$sarchType = $_POST['sarchType'];
								$fdate = $_POST['fdate'];
								$ldate = $_POST['ldate'];
								$diff = $_POST['diff'];
								$DiffAmt = $_POST['DiffAmt'];
								$seller = $_POST['seller'];
								$status = $_POST['status'];
								$account = $_POST['account'];
								$orderid = $_POST['orderid'];
								
								$sl=0;
								if($sarchType=="DATEWISE")
								{
								    if($diff=="ALL")
								    {
								        if($status=="ALL")
								        {
								            $quPayment="SELECT * FROM recharge WHERE Rechargedate>='$fdate' AND Rechargedate<='$ldate'";
								        }
								        else
								        {
								            $quPayment="SELECT * FROM recharge WHERE Status='$status' AND Rechargedate>='$fdate' AND Rechargedate<='$ldate'";
								        }
								    }
								    else
								    {
							            if($diff=="PLUS")
								        {
								            $quPayment="SELECT * FROM recharge WHERE Diff>=$DiffAmt AND Rechargedate>='$fdate' AND Rechargedate<='$ldate'";
								        }
								        else
								        {
								            $quPayment="SELECT * FROM recharge WHERE Diff<='-$DiffAmt' AND Rechargedate>='$fdate' AND Rechargedate<='$ldate'";
								        }
								    }
								}
								elseif($sarchType=="ACCOUNT_NO")
								{
								    $quPayment="SELECT * FROM recharge WHERE Mobile='$account' ORDER BY Id DESC";
								}
								elseif($sarchType=="ORDER_ID")
								{
								    $quPayment="SELECT * FROM recharge WHERE Id='$orderid'";
								}
								
								$rePayment=mysqli_query($con,$quPayment);
								$totalRound500=0;
								$totalDiff=0;
								$totalRound1000=0;
								$totalRound5000=0;
								$totalR_offer=0;
								$totalExtra=0;
								while($dataPayment=mysqli_fetch_array($rePayment))
								{
									if($dataPayment['Status']=="FAILED")
									{
										$color="red";
									}
									elseif($dataPayment['Status']=="SUCCESS")
									{
										$color="green";
									}
									else
									{
										$color="blue";
									}
									$usrName=getName($dataPayment['Userid']);
									if($seller=="ALL")
									{   
									    echo"<tr>";
										    echo "<td><a href='check_txn_status.php?rechId=".$dataPayment['Id']."' target='_blank'>".$dataPayment['Id']."</a></td>";
    										echo "<td>".$dataPayment['Rechargedate']." ".$dataPayment['Time']."</td>";
	    									echo "<td>".$usrName."</td>";
		    								echo "<td>".floor($dataPayment['Balance'])."</td>";
			    							echo "<td>".$dataPayment['RetComm']."</td>";
				    						echo "<td>".$dataPayment['DisComm']."</td>";
					    					echo "<td>".$dataPayment['MdComm']."</td>";
						    				echo "<td>".$dataPayment['Companyname']."</td>";
							    			echo "<td>".$dataPayment['Mobile']."</td>";
								    		echo "<td>".$dataPayment['Amount']."</td>";
									    	echo "<td>".getRA($dataPayment['R_Offer'])."</td>";
								        	echo "<td>".$dataPayment['RoffPerce']."</td>";
    										echo "<td>".$dataPayment['API']."</td>";
	    									echo "<td>".$dataPayment['API_NO']."</td>";
	    									echo "<td>".$dataPayment['RespoTime']."</td>";
		    								echo "<td>".getLON($dataPayment['LapuNo'])."</td>";
			    							echo "<td><a href='recharge_by_lapu.php?lapuNo=".$dataPayment['LapuNo']."' target='_blank'>".$dataPayment['LapuNo']."</a></td>";
				    						echo "<td>".$dataPayment['ApiBal']."</td>";
					    					echo "<td>".getRA($dataPayment['Diff'])."</td>";
						    				echo "<td>".$dataPayment['Operatorid']."</td>";
							    			echo "<td>".$dataPayment['Responsetxt']."</td>";
								    		echo "<td><p id='".$color."'>".$dataPayment['Status']."</p></td>";
									    	echo "<td>";
										    if($dataPayment['CallBack']==0)
    										{
	    									    echo "<i class='fa fa-times'></i>";
		    								}
			    							else
				    						{
					    					    echo "<i class='fa fa-check'></i>";
						    				}
							    			echo "</td>";
								    		echo "<td>".$dataPayment['Roaming']."</td>";
									    	echo "<td>".$dataPayment['ReHit']."</td>";
	    								    echo "<td>".getName($dataPayment['Parentid'])."</td>";
									    echo"</tr>";
									        $totalR_offer=$totalR_offer+$dataPayment['R_Offer'];
									}
									else
									{
									    if($seller==getLON($dataPayment['LapuNo']))
									    {   
									        $dataGetOp=getOp($dataPayment['Companyname']);
									        $profitValue=$dataGetOp['ProfitValue'];
									        $realDiff=$dataPayment['Diff']*1000/$profitValue;
									        
									        $totalDiff=$totalDiff+$dataPayment['Diff'];
									        
									        if($dataPayment['API']=="My_API3")
									        {
									            $round5000=floor($dataPayment['Diff']/5000)*5000;
									            $totalRound5000=$totalRound5000+$round5000;
									        }
									        else
									        {
									            $round500=floor($realDiff/500)*500;
									            $totalRound500=$totalRound500+$round500;
									        
									            $round1000=floor($realDiff/1000)*1000;
									            $totalRound1000=$totalRound1000+$round1000;
									        }
									        
									        
									        
									        if($dataPayment['OpCode']==10)
									        {
									            $diffAmount=$round1000;
									            $RTAC=$diffAmount*3.792/100;
									            $TRTA=$RTAC+$diffAmount;
									            if($dataPayment['Diff']>1000)
									            {
									                $extra=floor($dataPayment['Diff']-$TRTA);
									            }
									            else
									            {
									               $extra= floor($dataPayment['Diff']);
									            }
									            $totalExtra=$totalExtra+$extra;
									        }
									        elseif($dataPayment['LapuNo']==8295760039 || $dataPayment['LapuNo']==8865063210)
									        {
									            $diffAmount=$round5000;
									        }
									        else
									        {
									            $diffAmount=$round500;
									        }
									        
									        
									        echo"<tr>";
										        echo "<td><a href='check_txn_status.php?rechId=".$dataPayment['Id']."' target='_blank'>".$dataPayment['Id']."</a></td>";
		        								echo "<td>".$dataPayment['Rechargedate']." ".$dataPayment['Time']."</td>";
				        						echo "<td><a href='recharge_by_user.php?uid=".$dataPayment['Userid']."' target='_blank'>".$usrName."</a></td>";
						        				echo "<td>".floor($dataPayment['Balance'])."</td>";
								        		echo "<td>".$dataPayment['RetComm']."</td>";
										        echo "<td>".$dataPayment['DisComm']."</td>";
        										echo "<td>".$dataPayment['MdComm']."</td>";
		        								echo "<td>".$dataPayment['Companyname']."</td>";
				        						echo "<td>".$dataPayment['Mobile']."</td>";
						        				echo "<td>".$dataPayment['Amount']."</td>";
								        		echo "<td>".getRA($dataPayment['R_Offer'])."</td>";
								        		echo "<td>".$dataPayment['RoffPerce']."</td>";
										        echo "<td>".$dataPayment['API']."</td>";
        										echo "<td>".$dataPayment['API_NO']."</td>";
	    									    echo "<td>".$dataPayment['RespoTime']."</td>";
		        								echo "<td>".getLON($dataPayment['LapuNo'])."</td>";
			    							    echo "<td><a href='recharge_by_lapu.php?lapuNo=".$dataPayment['LapuNo']."' target='_blank'>".$dataPayment['LapuNo']."</a></td>";
						        				echo "<td>".$dataPayment['ApiBal']."</td>";
								        		echo "<td title='".$diffAmount." [".$extra."]'>".getRA($dataPayment['Diff'])."</td>";
										        echo "<td>".$dataPayment['Operatorid']."</td>";
        										echo "<td>".$dataPayment['Responsetxt']."</td>";
		        								echo "<td><p id='".$color."'>".$dataPayment['Status']."</p></td>";
				        						echo "<td>";
						        				if($dataPayment['CallBack']==0)
								        		{
										            echo "<i class='fa fa-times'></i>";
										        }
										        else
										        {
										            echo "<i class='fa fa-check'></i>";
										        }
										        echo "</td>";
										        echo "<td>".$dataPayment['Roaming']."</td>";
										        echo "<td>".$dataPayment['ReHit']."</td>";
	    								        echo "<td>".getName($dataPayment['Parentid'])."</td>";
									        echo"</tr>";
									        $totalR_offer=$totalR_offer+$dataPayment['R_Offer'];
									    }
									}
								}
							}
							else
							{
								$sl=0;
								$quPayment="SELECT * FROM recharge ORDER BY Id DESC LIMIT 20";
								$rePayment=mysqli_query($con,$quPayment);
								while($dataPayment=mysqli_fetch_array($rePayment))
								{
									if($dataPayment['Status']=="FAILED")
									{
										$color="red";
									}
									elseif($dataPayment['Status']=="SUCCESS")
									{
										$color="green";
									}
									else
									{
										$color="blue";
									}
									$usrName=getName($dataPayment['Userid']);
									if($dataPayment['ApiBal']>1000 || $dataPayment['ApiBal']==0)
									{
									    $colorBal="green";
									}
									else
									{
									    $colorBal="red";
									}
									
									$r_offer = getRA($dataPayment['R_Offer']);
									$r_offerP = $r_offer*100/$dataPayment['Amount'];
									$r_offerP = getRA($r_offerP);
									
									
									echo"<tr>";
										echo "<td><a href='check_txn_status.php?rechId=".$dataPayment['Id']."' target='_blank'>".$dataPayment['Id']."</a></td>";
										echo "<td id='dateTd'>".$dataPayment['Rechargedate']." ".$dataPayment['Time']."</td>";
										echo "<td><a href='recharge_by_user.php?uid=".$dataPayment['Userid']."' target='_blank'>".$usrName."</a></td>";
										if($dataPayment['Balance']>=5000)
										{
										    echo "<td>".floor($dataPayment['Balance'])."</td>";
										}
										else
										{
										    echo "<td style='color:red'>".floor($dataPayment['Balance'])."</td>";
										}
										echo "<td>".$dataPayment['RetComm']."</td>";
										echo "<td>".$dataPayment['DisComm']."</td>";
										echo "<td>".$dataPayment['MdComm']."</td>";
										echo "<td>".$dataPayment['Companyname']."</td>";
										echo "<td>".$dataPayment['Mobile']."</td>";
										echo "<td>".$dataPayment['Amount']."</td>";
										echo "<td>".$r_offer." [".$r_offerP."]</td>";
								        echo "<td>".$dataPayment['RoffPerce']."</td>";
										echo "<td>".$dataPayment['API']."</td>";
										echo "<td>".$dataPayment['API_NO']."</td>";
	    								echo "<td>".$dataPayment['RespoTime']."</td>";
										echo "<td>".getLON($dataPayment['LapuNo'])."</td>";
			    						echo "<td><a href='recharge_by_lapu.php?lapuNo=".$dataPayment['LapuNo']."' target='_blank'>".$dataPayment['LapuNo']."</a></td>";
										echo "<td style='color:".$colorBal."; font-size:13px'><b>".floor($dataPayment['ApiBal'])."</b></td>";
										echo "<td>".getRA($dataPayment['Diff'])."</td>";
										echo "<td style='padding:0;'><textarea id='opText'>".$dataPayment['Operatorid']."</textarea></td>";
										echo "<td>".$dataPayment['Responsetxt']."</td>";
										echo "<td><p id='".$color."'>".$dataPayment['Status']."</p></td>";
										echo "<td>";
										if($dataPayment['CallBack']==0)
										{
										    echo "<i class='fa fa-times'></i>";
										}
										else
										{
										    echo "<i class='fa fa-check'></i>";
										}
										echo "</td>";
										echo "<td>".$dataPayment['Roaming']."</td>";
										echo "<td>".$dataPayment['ReHit']."</td>";
	    								echo "<td>".getName($dataPayment['Parentid'])."</td>";
									echo"</tr>";
									   $totalR_offer=$totalR_offer+$dataPayment['R_Offer'];
								}
							}
							
							echo "<tr>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th>Airtel Money</th>";
							    echo "<th>".$totalRound5000."</th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th>".getRA($totalR_offer)."</th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th>".$totalRound1000."</th>";
							    echo "<th>".$totalDiff."</th>";
							    echo "<th>".$totalRound500."</th>";
							    echo "<th></th>";
							    echo "<th>[".$totalExtra."]</th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></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>