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


$date      = $_POST['date'];
$status    = $_POST['status'];
$sellerId  = $_POST['seller'];
$commValue = $_POST['comm'];

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

$output = '';

    if(isset($_POST["export"]))
    {
        $data1 = "r.Id, r.Date,  r.Time, r.Mobile, r.Amount, r.OpName, r.Lapu, r.ApiBal, r.Status, r.OpId,";
        $data2 = "s.Name,";
        $data3 = "f.FirmName";
        
        $IJrNl = "LEFT JOIN seller_lapu AS l ON l.LapuNo=r.Lapu";
        $IJrNS = "LEFT JOIN Seller AS s ON s.Id=l.SellerId";
        $IJrNF = "LEFT JOIN seller_firm AS f ON f.Id=l.FirmId";
        
        $where1 = "r.Date='$date' AND r.Status='$status'";
        $where2 = "AND s.Id='$sellerId'";
        
        $query = "SELECT $dataR $data1 $data2 $data3 FROM recharge AS r $IJrNl $IJrNS $IJrNF WHERE $where1 $where2";
        $result = mysqli_query($con, $query);
        if(mysqli_num_rows($result) > 0)
        {
            $output .= '<table class="table" Border="1" cellpadding="10"> 
                    <tr>  
                        <th>Seller</th>
                        <th>Firm</th>
                        <th>Id</th>  
                        <th>Date</th> 
                        <th>Time</th> 
                        <th>Mobile</th>    
                        <th>Amount</th>  
                        <th>Operator</th>   
                        <th>Lapu No.</th>    
                        <th>Bal</th>    
                        <th>Status</th>
                        <th>Op Id</th>
                    </tr>';
            while($row = mysqli_fetch_array($result))
            {
                $RechId = $row[0];
                $Date   = $row[1];
                $Time   = $row[2];
                $Mobile = $row[3];
                $Amount = $row[4];
                $OpName = $row[5];
                $LapuNo = $row[6];
                $ApiBal = $row[7];
                $Status = $row[8];
                $OpId   = $row[9];
                $S_Name = $row[10];
                $F_Name = $row[11];
                
                $OpName = strtok($OpName, ' ');
                
                $output .= ' <tr>  
                    <td>'.$S_Name.'</td>
                    <td>'.$F_Name.'</td>
                    <td>'.$RechId.'</td>
                    <td>'.$Date.'</td>
                    <td>'.$Time.'</td>    
                    <td>'.$Mobile.'</td>  
                    <td>'.$Amount.'</td>  
                    <td>'.$OpName.'</td>
                    <td>'.$LapuNo.'</td>
                    <td>'.$ApiBal.'</td>
                    <td>'.$Status.'</td>
                    <td>'.$OpId.'</td>
                    </tr>';
                    $totTxn=$totTxn+$Amount;
            }
            $txnComm=$totTxn*$commValue/100;
            $payment=floor($totTxn-$txnComm);
            $output .= '<tr>  
                            <th colspan="1">Total Txn</th>
                            <th  colspan="2"><center>'.$totTxn.'(-'.$commValue.'%)</center></th>
                            <th  colspan="2"><center><h3>Rs. '.$payment.'</h3></center></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            
                        </tr>';
            $output .= '</table>';
            $sNikName = substr($S_Name,0, 6);
            $fileName = $dateStyle."_".$sNikName."_Rs.".$payment;
            header('Content-Type: application/xls');
            header('Content-Disposition: attachment; filename='.$fileName.'.xls');
            echo $output;
            
            
        }
        else
        {
            echo "<center><h3 style='color:red'>Data Not Found !</h3></center><br>";
            echo "<center><a href='https://www.llprecharge.in/LLPRECHARGE/Admin/recharge_by_seller.php'><h3>Back</h3></a></center>";
        }
    }    
?>
