<?php
$output = '';

    if(isset($_POST["export"]))
    {
        $data1 = "t.Id, t.Date,  t.Time, t.Amount,";
        $data2 = "s.Name,";
        $data3 = "f.FirmName,";
        $data4 = "b.BankName, b.AccName,";
        $data5 = "w.BankRefNo";
        
        $IJtNs = "LEFT JOIN Seller AS s ON t.SellerId=s.Id";
        $IJtNf = "LEFT JOIN seller_firm AS f ON f.Id=t.FirmId";
        $IJtNb = "LEFT JOIN bank AS b ON b.Id=t.TxnWith";
        $IJtNw = "LEFT JOIN bank_wallet AS w ON w.Id=t.BankWalletId";
        
        $where1 = "t.Date>='$fdate' AND t.Date<='$ldate'";
        
        $query = "SELECT $dataR $data1 $data2 $data3 $data4 $data5 FROM seller_txn AS t $IJtNs $IJtNf $IJtNb $IJtNw WHERE $where1";
        $result = mysqli_query($con, $query);
        if(mysqli_num_rows($result) > 0)
        {
            $output .= '<table class="table" Border="1" cellpadding="10"> 
                    <tr>  
                        <th>Id</th>  
                        <th>Date</th> 
                        <th>Time</th> 
                        <th>Seller</th>  
                        <th>Firm</th>   
                        <th>Amount</th>
                        <th>LLP Bank</th>
                        <th>LLP Firm</th>
                        <th>Ref No.</th>
                    </tr>';
            while($row = mysqli_fetch_array($result))
            {
                $txnId  = $row[0];
                $Date   = $row[1];
                $Time   = $row[2];
                $Amount = $row[3];
                $Seller = $row[4];
                $Firm   = $row[5];
                $Bank   = $row[6];
                $LLPFrm = $row[7];
                $RefNo  = $row[8];
                
                
                $OpName = strtok($OpName, ' ');
                
                $output .= ' <tr>  
                    <td>'.$txnId.'</td>
                    <td>'.$Date.'</td>
                    <td>'.$Time.'</td>    
                    <td>'.$Seller.'</td>  
                    <td>'.$Firm.'</td>  
                    <td>'.$Amount.'</td>
                    <td>'.$Bank.'</td>
                    <td>'.$LLPFrm.'</td>
                    <td>'.$RefNo.'</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>
                        </tr>';
            $output .= '</table>';
            $fileName=$fdateStyle."to".$ldateStyle."_Rs.".$payment;
            header('Content-Type: application/xls');
            header('Content-Disposition: attachment; filename='.$fileName.'.xls');
            echo $output;
        }
    }    
?>
