<?php


$output = '';

    if(isset($_POST["export"]))
    {
        $data1 = "SUM(t.Amount),";
        $data2 = "s.Name,";
        $data3 = "f.FirmName";
        $quGroupBy = "GROUP BY t.$groupBy";
        
        
        $IJtNs = "LEFT JOIN Seller AS s ON t.SellerId=s.Id";
        $IJtNf = "LEFT JOIN seller_firm AS f ON f.Id=t.FirmId";
        
        $where1 = "t.Date>='$fdate' AND t.Date<='$ldate'";
        
        $query = "SELECT $data1 $data2 $data3 FROM seller_txn AS t $IJtNs $IJtNf WHERE $where1 $quGroupBy ORDER BY s.Name";
        $result = mysqli_query($con, $query);
        if(mysqli_num_rows($result) > 0)
        {
            $output .= '<table class="table" Border="1" cellpadding="10"> 
                    <tr>  
                        <th>From</th> 
                        <th>To</th> 
                        <th>Seller</th> 
                        <th>Firm</th>   
                        <th>Amount</th>
                    </tr>';
            while($row = mysqli_fetch_array($result))
            {
                $Amount = $row[0];
                $Seller = $row[1];
                $Firm   = $row[2];
                
                
                $OpName = strtok($OpName, ' ');
                
                $output .= ' <tr>  
                    <td>'.$fdate.'</td>
                    <td>'.$ldate.'</td>   
                    <td>'.$Seller.'</td>  
                    <td>'.$Firm.'</td>  
                    <td>'.$Amount.'</td>
                    </tr>';
                    $totTxn=$totTxn+$Amount;
            }
            $output .= '<tr>  
                            <th colspan="2">Total Txn</th>
                            <th  colspan="3"><center><h3>Rs. '.$totTxn.'</h3></center></th>
                        </tr>';
            $output .= '</table>';
            $fileName=$fdateStyle."to".$ldateStyle."_Rs.".$totTxn;
            header('Content-Type: application/xls');
            header('Content-Disposition: attachment; filename='.$fileName.'.xls');
            echo $output;
        }
    }    
?>
