<?php
    // Ad This Page Into Recharge Home Page Of Retailers//
    include'connection.php';
    
    $quBalId="SELECT * FROM RetBal WHERE Userid='$userid'";
    $reBalId=mysqli_query($con,$quBalId);
    $dataBalId=mysqli_fetch_array($reBalId);
    $balDate=$dataBalId['Date'];
    $Walletid=$dataBalId['Walletid'];
    
    $quUsrBal="SELECT * FROM wallet WHERE Userid='$userid' AND Id>='$Walletid'";
    $reUsrBal=mysqli_query($con,$quUsrBal);
    $countUsrBal=mysqli_num_rows($reUsrBal);
    
    
    if($balDate<$date && $countUsrBal>2000)
    {
        $bal=0;
        while($dataUsrBal=mysqli_fetch_array($reUsrBal))
        {
            $bal=$bal+$dataUsrBal['Txn'];
        }
        
        $insertWallet="INSERT INTO wallet(Userid,Date,Time,Txn,Remarks,Balance) VALUES('$userid','$date','$time','$bal','Update Wallet Id','$bal')";
        if(mysqli_query($con,$insertWallet))
        {
            $newWalletId=mysqli_insert_id($con);
            
            $updateBalId="UPDATE RetBal SET Date='$date', Time='$time', Walletid='$newWalletId', Balance='$bal' WHERE Userid='$userid'";
            mysqli_query($con,$updateBalId);
        }
    }

?>