
<?php
    include'connection.php';
		
	date_default_timezone_set("asia/kolkata");
    $date=date("Y-m-d");
    $time=date("h:i:s A");
?>
<html>
    <head>
        <style>
            #inp{width:220px; height:50px; margin:10px; font-size:22px;}
        </style>
    </head>
    <body>
        <form method="post">
        <select name="usrs" id="inp">
            <option>ApiUsers</option>
            <option>Retailers</option>
        </select>
        <input type="date" name="fdate" value="<?php echo $date; ?>" id="inp">
        <input type="date" name="ldate" value="<?php echo $date; ?>" id="inp">
        <input type="submit" name="submit" id="inp">
        </form>
</html>

<?php
	echo"<table border='1' cellpadding='10'>";
	    echo "<tr>";
            echo "<th>From</th>";
            echo "<th>To</th>";
            echo "<th>Userid</th>";
	        echo "<th>Opening Bal</th>";
	        echo "<th>Add Bal</th>";
            echo "<th>Debit Amount</th>";
		    echo "<th>Closing Bal</th>";
		    echo "<th>Difference</th>";
		echo "</tr>";
		            
    if(isset($_POST['submit']))
    {
        $fdate=$_POST['fdate'];
        $ldate=$_POST['ldate'];
        $usrs=$_POST['usrs'];
        
        if($usrs=="Distributers")
        {
            $quGetUsers="select * from distributer";
        }
        elseif($usrs=="Retailers")
        {
            $quGetUsers="select * from retailer where ApiActive='0'";
        }
        elseif($usrs=="ApiUsers")
        {
            $quGetUsers="select * from retailer where ApiActive='1'";
        }
        $reGetUsers=mysqli_query($con,$quGetUsers);
        while($dataGetUsers=mysqli_fetch_array($reGetUsers))
        {   
		    $TotalCredit=0;
		    $TotalCreditPayment=0;
		    $TotalDebit=0;
            $RetalerId=$dataGetUsers['Userid'];
			
		    $quOpBal="select * from wallet where Userid='$RetalerId' and Date>='$fdate' and Date<='$ldate' order by Id asc";
			$reOpBal=mysqli_query($con,$quOpBal);
			$rowOpBal=mysqli_fetch_array($reOpBal);
			$OpeningBal=$rowOpBal['Openingbalance'];
			
			$quOpBal2="select * from wallet where Userid='$RetalerId' and Date>='$fdate' and Date<='$ldate' order by Id asc";
			$reOpBal2=mysqli_query($con,$quOpBal2);
			while($rowOpBal2=mysqli_fetch_array($reOpBal2))
			{
				if($rowOpBal2['Transactiontype']=="Payment_Transfer")
				{
				    $CreditPayment=$rowOpBal2['Credit'];
				    $TotalCreditPayment=$TotalCreditPayment+$CreditPayment;
				}
			}
			
			$quClBal="select * from wallet where Userid='$RetalerId' and Date>='$fdate' and Date<='$ldate' order by Id desc";
			$reClBal=mysqli_query($con,$quClBal);
			$rowClBal=mysqli_fetch_array($reClBal);
			$ClosingBal=$rowClBal['Balance'];
			
			$getrechhistq="select * from recharge where Userid='$RetalerId' and Status='SUCCESS' and Rechargedate>='$fdate' and Rechargedate<='$ldate'";
			$resultgetrechhid=mysqli_query($con,$getrechhistq);
			while($row=mysqli_fetch_array($resultgetrechhid))
			{
				$TotalDebit=$TotalDebit+$row['Debitamount'];
			}
			
		    $RemainBal=$OpeningBal+$TotalCreditPayment-$TotalDebit;
		    
		    $Diff = $ClosingBal-$RemainBal;
		    if(floor($Diff)==!0)
		    {
		            echo"<tr>";
		                echo"<td>".$fdate."</td>";
		                echo"<td>".$ldate."</td>";
		                echo"<td>".$RetalerId."</td>";
		                echo "<td>".floor($OpeningBal)."</td>";
		                echo "<td>".floor($TotalCreditPayment)."</td>";
		                echo "<td>".floor($TotalDebit)."</td>";
		                echo "<td>".floor($ClosingBal)."</td>";
		                echo "<td>".floor($Diff)."</td>";
		            echo "</tr>";
		    }
		    
        }
        echo "Completed";
    }
    
        
		        echo "</table>";
?>