<?php
session_start();
include 'koneksi.php';

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

$user_id = $_SESSION['user_id'];

$bulan = isset($_GET['bulan']) ? $_GET['bulan'] : date('m');
$tahun = isset($_GET['tahun']) ? $_GET['tahun'] : date('Y');

header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=laporan_pengeluaran_$bulan-$tahun.xls");

$query = mysqli_query($conn,"
SELECT tanggal,keterangan,jumlah
FROM transaksi
WHERE user_id='$user_id'
AND jenis='pengeluaran'
AND MONTH(tanggal)='$bulan'
AND YEAR(tanggal)='$tahun'
ORDER BY tanggal DESC
");
?>

<h3>Laporan Pengeluaran</h3>

<table border="1">
<tr>
<th>Tanggal</th>
<th>Keterangan</th>
<th>Jumlah</th>
</tr>

<?php

$total = 0;

while($row = mysqli_fetch_assoc($query)){

$total += $row['jumlah'];

echo "<tr>";
echo "<td>".$row['tanggal']."</td>";
echo "<td>".$row['keterangan']."</td>";
echo "<td>".$row['jumlah']."</td>";
echo "</tr>";

}

?>

<tr>
<td colspan="2"><b>Total</b></td>
<td><b><?php echo $total; ?></b></td>
</tr>

</table>
