
<?php
    include'connection.php';
    include'time.php';
?>

<html>
    <head>
        <style>
        </style>
    </head>
    <body>
        <div align="center">
        <form method="post">
        <input type="date" value="<?php echo $date; ?>" name="fdate" id="inp">
        <input type="submit" name="submit" id="inp"><br><br>
        </from>
        </div>
<table border="1" cellpadding="10" align="center">
    <tr>
        <td>Date</td>
        <td>Userid</td>
        <td>Op. Bal</td>
        <td>Total Debit</td>
        <td>Clo. Bal</td>
        <td>Diff</td>
    </tr>
<?php
 if(isset($_POST['submit']))
 {
    $fdate=$_POST['fdate'];
    
    $quRet="select * from retailer";
    $reRet=mysqli_query($con,$quRet);
    while($dataRet=mysqli_fetch_array($reRet))
    {
        $RetUsrId=$dataRet['Userid'];
        
        $quOpBal="select * from wallet where Userid='$RetUsrId' and Date='$fdate'";
        $reOpBal=mysqli_query($con,$quOpBal);
        $dataOpBal=mysqli_fetch_array($reOpBal);
        $opBal=$dataOpBal['Balance'];
        $OpeningBal=$opBal+$dataOpBal['Debit']-$dataOpBal['Credit'];
        
        $quClBal="select * from wallet where Userid='$RetUsrId' and Date='$fdate' order by Id desc";
        $reClBal=mysqli_query($con,$quClBal);
        $dataClBal=mysqli_fetch_array($reClBal);
        $ClosingBal=$dataClBal['Balance'];
        
        $TotDebAmt=0;
        $quDebit="select * from recharge where Userid='$RetUsrId' and Status='SUCCESS' and Rechargedate='$fdate'";
        $reDebit=mysqli_query($con,$quDebit);
        while($dataDebit=mysqli_fetch_array($reDebit))
        {
            $debitAmount=$dataDebit['Debitamount'];
            $TotDebAmt=$TotDebAmt+$debitAmount;
        }
        $Diff=$ClosingBal+$TotDebAmt-$OpeningBal;
        if($Diff>=2 || $Diff<=-2)
        {
        echo "<tr>";
            echo "<td>".$fdate."</td>";
            echo "<td>".$RetUsrId."</td>";
            echo "<td>".$OpeningBal."</td>";
            echo "<td>".$TotDebAmt."</td>";
            echo "<td>".$ClosingBal."</td>";
            echo "<td>".$Diff."</td>";
        echo "</tr>";
        }
    }
 }
?>

</table>

    </body>
</html>