| Server IP : 172.67.187.206 / Your IP : 172.71.28.156 Web Server : Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/5.6.30 System : Windows NT WIN-ECQAAA40806 6.2 build 9200 (Windows Server 2012 Standard Edition) i586 User : SYSTEM ( 0) PHP Version : 5.6.30 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /Inetpub/www/supply_system/supplies/ |
Upload File : |
<?php
// supplies/export_supplies.php
// No header/footer includes needed for Excel output
include_once __DIR__ . '/../config.php';
include_once __DIR__ . '/../functions.php';
include_once __DIR__ . '/../includes/auth_check.php';
// requireRole(['admin', 'supply_manager']);
// --- Set filename ---
$filename = "supplies_export_" . date('YmdHis') . ".xls";
// --- Set HTTP Headers for Excel Download ---
header("Content-Type: application/vnd.ms-excel; charset=utf-8"); // Use UTF-8
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Pragma: no-cache");
header("Expires: 0");
// --- Get Filters from URL parameters (same as index.php) ---
$search = isset($_GET['search']) ? sanitize_input($conn, $_GET['search']) : '';
$category_filter = isset($_GET['category']) ? intval($_GET['category']) : 0;
$status_filter = isset($_GET['status']) ? sanitize_input($conn, $_GET['status']) : 'active'; // Default to active
$sort_by = isset($_GET['sort']) ? sanitize_input($conn, $_GET['sort']) : 'code'; // Default sort
// --- Build WHERE clause (same logic as index.php) ---
$where_conditions = [];
if (!empty($search)) {
$where_conditions[] = "(s.supply_code LIKE '%$search%' OR s.supply_name LIKE '%$search%')";
}
if ($category_filter > 0) {
$where_conditions[] = "s.category_id = $category_filter";
}
if (!empty($status_filter) && $status_filter != 'all') {
$where_conditions[] = "s.status = '" . $status_filter . "'";
}
$where_sql = count($where_conditions) > 0 ? "WHERE " . implode(' AND ', $where_conditions) : '';
// --- Build ORDER BY clause (same logic as index.php) ---
$order_clause = "ORDER BY ";
switch ($sort_by) {
case 'name':
$order_clause .= " s.supply_name ASC";
break;
case 'category':
$order_clause .= " c.category_name ASC, s.supply_code ASC";
break;
case 'stock_asc':
$order_clause .= " s.quantity_in_stock ASC, s.supply_code ASC";
break;
case 'stock_desc':
$order_clause .= " s.quantity_in_stock DESC, s.supply_code ASC";
break;
case 'code':
default:
$order_clause .= " s.supply_code ASC";
break;
}
// --- Fetch Supplies Data (same query as index.php) ---
$sql = "SELECT s.supply_code, s.supply_name, c.category_name, s.unit,
s.quantity_in_stock, s.min_stock_level, s.average_unit_price, s.total_value, s.status, s.description, s.updated_at
FROM supplies s
JOIN categories c ON s.category_id = c.id
$where_sql
$order_clause";
$result = mysqli_query($conn, $sql);
// --- Generate HTML Table Output for Excel ---
// BOM for UTF-8 Excel compatibility
echo "\xEF\xBB\xBF";
echo "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">";
echo "<head><meta charset=\"UTF-8\"></head>"; // Specify charset
echo "<body>";
echo "<h1>รายงานพัสดุทั้งหมด</h1>";
echo "<p>ข้อมูล ณ วันที่: " . formatThaiDate(date('Y-m-d H:i:s'), true) . "</p>";
// Optional: Display applied filters
echo "<p>เงื่อนไข: ";
$filters_display = [];
if (!empty($search)) $filters_display[] = "ค้นหา '" . htmlspecialchars($search) . "'";
if ($category_filter > 0) {
// Fetch category name for display (optional, requires another query or passing from index)
$filters_display[] = "หมวดหมู่ ID " . $category_filter;
}
if (!empty($status_filter) && $status_filter != 'all') $filters_display[] = "สถานะ '" . htmlspecialchars($status_filter) . "'";
echo count($filters_display) > 0 ? implode(', ', $filters_display) : "ทั้งหมด";
echo "</p>";
echo "<table border='1'>";
echo "<thead style='background-color: #f2f2f2; font-weight: bold;'>";
echo "<tr>";
echo "<th>รหัสพัสดุ</th>";
echo "<th>ชื่อพัสดุ</th>";
echo "<th>หมวดหมู่</th>";
echo "<th>หน่วยนับ</th>";
echo "<th>คงคลัง</th>";
echo "<th>จุดสั่งซื้อ</th>";
echo "<th>ราคาเฉลี่ย/หน่วย</th>";
echo "<th>มูลค่ารวม</th>";
echo "<th>สถานะ</th>";
echo "<th>คำอธิบาย</th>";
echo "<th>อัพเดทล่าสุด</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
if ($result && mysqli_num_rows($result) > 0) {
while ($item = mysqli_fetch_assoc($result)) {
echo "<tr>";
// Force text format for supply code
echo "<td style='mso-number-format:\"@\"'>" . htmlspecialchars($item['supply_code']) . "</td>";
echo "<td>" . htmlspecialchars($item['supply_name']) . "</td>";
echo "<td>" . htmlspecialchars($item['category_name']) . "</td>";
echo "<td>" . htmlspecialchars($item['unit']) . "</td>";
echo "<td style='text-align:right;'>" . $item['quantity_in_stock'] . "</td>"; // Number
echo "<td style='text-align:right;'>" . $item['min_stock_level'] . "</td>"; // Number
echo "<td style='text-align:right;'>" . number_format($item['average_unit_price'], 2) . "</td>"; // Currency
echo "<td style='text-align:right;'>" . number_format($item['total_value'], 2) . "</td>"; // Currency
echo "<td>" . htmlspecialchars($item['status']) . "</td>";
echo "<td>" . htmlspecialchars($item['description']) . "</td>";
echo "<td>" . ($item['updated_at'] ? formatThaiDate($item['updated_at'], true) : '') . "</td>";
echo "</tr>";
}
mysqli_free_result($result);
} else {
echo "<tr><td colspan='11' style='text-align:center;'>ไม่พบข้อมูลตามเงื่อนไข</td></tr>";
}
echo "</tbody>";
echo "</table>";
echo "</body>";
echo "</html>";
mysqli_close($conn);
exit(); // End script after output
?>