<?php

session_start();


if(!isset($_SESSION['user_id'])){
    exit;
}


require_once "config.php";


$id=$_GET['vendor'] ?? 0;



$stmt=$pdo->prepare("

SELECT

v.vendor_name,

e.expense_date,

c.category_name,

e.description,

e.payment_method,

e.total


FROM expenses e


LEFT JOIN vendors v

ON e.vendor_id=v.id


LEFT JOIN expense_categories c

ON e.category_id=c.id


WHERE e.vendor_id=?


ORDER BY e.expense_date DESC


");


$stmt->execute([$id]);


$data=$stmt->fetchAll();




header("Content-Type: application/vnd.ms-excel");

header("Content-Disposition: attachment; filename=vendor_statement.xls");



echo "

Vendor Statement\n\n";


echo "

Vendor\tDate\tCategory\tDescription\tPayment\tAmount\n";



foreach($data as $row){


echo

$row['vendor_name']."\t".

$row['expense_date']."\t".

$row['category_name']."\t".

$row['description']."\t".

$row['payment_method']."\t".

$row['total'].

"\n";


}



?>