<?php
include "../koneksi.php";

// Ambil filter
$where = "WHERE 1=1";
if (!empty($_GET['bulan'])) {
    $bulan = $conn->real_escape_string($_GET['bulan']);
    $where .= " AND DATE_FORMAT(tanggal, '%Y-%m') = '$bulan'";
}
if (!empty($_GET['customer'])) {
    $customer = $conn->real_escape_string($_GET['customer']);
    $where .= " AND customer LIKE '%$customer%'";
}

// Ambil data transaksi
$sql = "
    SELECT no_transaksi, tanggal, customer, total, grand_total
    FROM tabel_transaksi
    $where
    ORDER BY tanggal DESC
";
$result = $conn->query($sql);

// Header untuk Excel
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=transaksi_" . date('Ymd_His') . ".xls");
header("Pragma: no-cache");
header("Expires: 0");

// Output tabel
echo "<table border='1'>";
echo "<tr>
        <th>No Transaksi</th>
        <th>Tanggal</th>
        <th>Customer</th>
        <th>Total</th>
        <th>Grand Total</th>
      </tr>";

while ($row = $result->fetch_assoc()) {
    echo "<tr>
            <td>{$row['no_transaksi']}</td>
            <td>" . date('d-m-Y', strtotime($row['tanggal'])) . "</td>
            <td>{$row['customer']}</td>
            <td>" . number_format($row['total'], 0, ',', '.') . "</td>
            <td>" . number_format($row['grand_total'], 0, ',', '.') . "</td>
          </tr>";
}

echo "</table>";
?>
