<?php
include'session.php';
include'connection.php';
include'../Retailers/functions.php';


$date=$_POST['date'];
$status=$_POST['status'];
$seller=$_POST['seller'];
if($_POST['commValue'])
{
    $commValue=$_POST['commValue'];
}
else
{
    $commValue=0;
}

$dateCreate=date_create($date);
$dateStyle=date_format($dateCreate,"Ymd");

$output = '';

if(isset($_POST["export"]))
{
        $totalTxn=0;
        $query = "SELECT * FROM recharge WHERE Rechargedate='$date' AND Status='$status'";
        $result = mysqli_query($con, $query);
        if(mysqli_num_rows($result) > 0)
        {
            $output .= '<table class="table" Border="1" cellpadding="10"> 
                    <tr>  
                        <th>Date</th>  
                        <th>Time</th> 
                        <th>Recharge Id</th>    
                        <th>Mobile No.</th>  
                        <th>Operator</th>  
                        <th>Amount</th>    
                        <th>Lapu No.</th>    
                        <th>Status</th>
                        <th>Operator Id</th>
                    </tr>';
            while($row = mysqli_fetch_array($result))
            {
                $lapuNo=$row["LapuNo"];
                $lapuOwnerName=getLON($lapuNo);
                if($lapuOwnerName==$seller)
                {
                    $output .= ' <tr>  
                    <td>'.$row["Rechargedate"].'</td>
                    <td>'.$row["Time"].'</td>
                    <td>'.$row["Id"].'</td>    
                    <td>'.$row["Mobile"].'</td>  
                    <td>'.$row["Companyname"].'</td>  
                    <td>'.$row["Amount"].'</td>
                    <td>'.$row["LapuNo"].'</td>
                    <td>'.$row["Status"].'</td>
                    <td>'.$row["Operatorid"].'</td>
                    </tr>';
                    $totalTxn=$totalTxn+$row['Amount'];
                }
            }
             $txnComm=$totalTxn*$commValue/100;
             $payment=floor($totalTxn-$txnComm);
            $output .= '<tr>  
                            <th></th>
                            <th></th>
                            <th></th>
                            <th>Total Txn</th>
                            <th></th>
                            <th><center>'.$totalTxn.'(-'.$commValue.'%)</center></th>
                            <th><center><h3>Rs. '.$payment.'</h3></center></th>
                            <th></th>
                            <th></th>
                            <th></th>
                        </tr>';
            $output .= '</table>';
            $sNikName=substr($seller,0, 8);
            $fileName=$dateStyle."_".$sNikName."_Rs.".$payment;
            header('Content-Type: application/xls');
            header('Content-Disposition: attachment; filename='.$fileName.'.xls');
            echo $output;
        }
        else
        {
            echo "<h2 style='color:red;'>Data Not Found !</h2>";
        }
    
}
?>
