| 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/reports/ |
Upload File : |
<?php
// reports/export_low_stock.php
// ไม่ต้อง include header/footer เพราะจะส่ง output เป็น Excel
include_once __DIR__ . '/../config.php'; // จำเป็นต้องใช้ $conn
include_once __DIR__ . '/../functions.php'; // จำเป็นต้องใช้ formatThaiDate (ถ้าต้องการ) และ auth check
include_once __DIR__ . '/../includes/auth_check.php'; // ตรวจสอบ Login ก่อน export
// requireRole(['admin', 'supply_manager']); // กำหนดสิทธิ์ถ้าต้องการ
// --- ตั้งชื่อไฟล์ Excel ---
$filename = "report_low_stock_" . date('YmdHis') . ".xls";
// --- ตั้งค่า HTTP Headers สำหรับ Excel Download ---
header("Content-Type: application/vnd.ms-excel; charset=utf-8"); // ใช้ UTF-8
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Pragma: no-cache");
header("Expires: 0");
// --- Fetch Low Stock Items (Same query as report_low_stock.php) ---
$low_stock_items = [];
$sql = "SELECT s.id, s.supply_code, s.supply_name, c.category_name, s.unit, s.quantity_in_stock, s.min_stock_level
FROM supplies s
JOIN categories c ON s.category_id = c.id
WHERE s.status = 'active' AND s.quantity_in_stock <= s.min_stock_level AND s.min_stock_level > 0
ORDER BY s.supply_code ASC";
$result = mysqli_query($conn, $sql);
// --- สร้าง Output HTML Table สำหรับ Excel ---
// เพิ่ม BOM (Byte Order Mark) สำหรับ 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>"; // ระบุ charset
echo "<body>";
echo "<h1>รายงานพัสดุใกล้หมด / ต่ำกว่าจุดสั่งซื้อ</h1>";
echo "<p>ข้อมูล ณ วันที่: " . formatThaiDate(date('Y-m-d H:i:s'), true) . "</p>"; // ใช้ฟังก์ชัน formatThaiDate
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 "</tr>";
echo "</thead>";
echo "<tbody>";
if ($result && mysqli_num_rows($result) > 0) {
$counter = 1;
while ($item = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $counter++ . "</td>";
// ใช้ vnd.ms-excel.numberformat:@ เพื่อบังคับให้เป็น Text ป้องกัน Excel ตัด 0 นำหน้า (ถ้ามี)
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; color:red;'>" . number_format($item['quantity_in_stock']) . "</td>";
echo "<td style='text-align:right;'>" . number_format($item['min_stock_level']) . "</td>";
echo "<td style='text-align:right; color:red;'>" . number_format($item['min_stock_level'] - $item['quantity_in_stock']) . "</td>";
echo "</tr>";
}
mysqli_free_result($result);
} else {
echo "<tr><td colspan='8' style='text-align:center;'>ไม่พบข้อมูลพัสดุที่ต่ำกว่าจุดสั่งซื้อ</td></tr>";
}
echo "</tbody>";
echo "</table>";
echo "</body>";
echo "</html>";
mysqli_close($conn);
exit(); // จบการทำงานหลังจากสร้างไฟล์
?>