| Server IP : 104.21.80.248 / 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 : E:/Inetpub/www/supply_system/supplies/ |
Upload File : |
<?php
include_once __DIR__ . '/../config.php';
include_once __DIR__ . '/../functions.php';
include_once __DIR__ . '/../includes/auth_check.php';
// requireRole(['admin', 'supply_manager']);
$page_title = "รายการพัสดุทั้งหมด";
// --- ดึงข้อมูลหมวดหมู่ (Categories) สำหรับ Filter ---
$categories = [];
$sql_cat = "SELECT id, category_name FROM categories ORDER BY category_name ASC";
$res_cat = mysqli_query($conn, $sql_cat);
if ($res_cat) {
while ($row = mysqli_fetch_assoc($res_cat)) {
$categories[] = $row;
}
mysqli_free_result($res_cat);
}
// --- Handle Filters ---
$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']) : 'all'; // Default to 'all' to see inactive ones too now
$sort_by = isset($_GET['sort']) ? sanitize_input($conn, $_GET['sort']) : 'code';
// --- Build WHERE clause ---
$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 ---
$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 (include image_filename) ---
$supplies = [];
$sql = "SELECT s.*, c.category_name
FROM supplies s
JOIN categories c ON s.category_id = c.id
$where_sql
$order_clause";
$result = mysqli_query($conn, $sql);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$supplies[] = $row;
}
mysqli_free_result($result);
} else {
echo "Error fetching supplies: " . mysqli_error($conn);
}
mysqli_close($conn);
// --- Include Header and Sidebar ---
include_once __DIR__ . '/../includes/header.php';
include_once __DIR__ . '/../includes/sidebar_supply.php';
?>
<style>
.supply-img-thumbnail {
max-width: 50px;
max-height: 50px;
object-fit: cover; /* Crop image nicely */
cursor: pointer; /* Add pointer cursor */
}
.modal-body img {
max-width: 100%;
max-height: 70vh; /* Limit modal image height */
display: block;
margin: 0 auto; /* Center image */
}
.image-placeholder {
cursor: default; /* No pointer for placeholder */
}
/* Style for inactive rows */
tr.inactive-row td {
/* background-color: #f8f9fa; */
color: #6c757d; /* Muted text */
text-decoration: line-through; /* Strikethrough */
}
tr.inactive-row .badge {
opacity: 0.7;
}
</style>
<div class="container-fluid">
<div class="d-flex justify-content-between align-items-center mt-4 mb-3">
<h1><?php echo $page_title; ?></h1>
<div>
<a href="supply_add.php" class="btn btn-primary me-1"><i class="bi bi-journal-plus"></i> เพิ่มพัสดุใหม่</a>
<a href="batch_add.php" class="btn btn-success me-1"><i class="bi bi-plus-circle"></i> เพิ่มสต็อก (รับเข้า)</a>
<button type="button" class="btn btn-outline-success" id="exportBtn">
<i class="bi bi-file-earmark-excel-fill"></i> ส่งออก Excel
</button>
</div>
</div>
<div class="card mb-4">
<div class="card-header"><i class="bi bi-filter me-1"></i> ตัวกรอง / ค้นหา</div>
<div class="card-body">
<form method="GET" action="" id="filterForm" class="row g-3 align-items-end">
<div class="col-md-3">
<label for="search" class="form-label">ค้นหา</label>
<input type="text" id="search" class="form-control" name="search" placeholder="รหัส หรือ ชื่อพัสดุ..." value="<?php echo htmlspecialchars($search); ?>">
</div>
<div class="col-md-2">
<label for="category" class="form-label">หมวดหมู่</label>
<select id="category" class="form-select" name="category">
<option value="0">-- ทุกหมวดหมู่ --</option>
<?php foreach ($categories as $cat): ?>
<option value="<?php echo $cat['id']; ?>" <?php echo ($category_filter == $cat['id']) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($cat['category_name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<label for="status" class="form-label">สถานะ</label>
<select id="status" name="status" class="form-select">
<option value="all" <?php echo ($status_filter == 'all') ? 'selected' : ''; ?>>ทั้งหมด</option> <option value="active" <?php echo ($status_filter == 'active') ? 'selected' : ''; ?>>ใช้งาน</option>
<option value="inactive" <?php echo ($status_filter == 'inactive') ? 'selected' : ''; ?>>ไม่ใช้งาน</option>
</select>
</div>
<div class="col-md-2">
<label for="sort" class="form-label">เรียงตาม</label>
<select id="sort" class="form-select" name="sort">
<option value="code" <?php echo $sort_by == 'code' ? 'selected' : ''; ?>>รหัสพัสดุ</option>
<option value="name" <?php echo $sort_by == 'name' ? 'selected' : ''; ?>>ชื่อพัสดุ</option>
<option value="category" <?php echo $sort_by == 'category' ? 'selected' : ''; ?>>หมวดหมู่</option>
<option value="stock_asc" <?php echo $sort_by == 'stock_asc' ? 'selected' : ''; ?>>สต็อกน้อย-มาก</option>
<option value="stock_desc" <?php echo $sort_by == 'stock_desc' ? 'selected' : ''; ?>>สต็อกมาก-น้อย</option>
</select>
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-primary w-100">
<i class="bi bi-search me-1"></i> กรองข้อมูล
</button>
</div>
<div class="col-md-1">
<a href="index.php" class="btn btn-outline-secondary w-100" title="ล้างตัวกรอง">
<i class="bi bi-arrow-clockwise"></i>
</a>
</div>
</form>
</div>
</div>
<?php if (isset($_SESSION['message'])): ?>
<div class="alert alert-<?php echo $_SESSION['message_type']; ?> alert-dismissible fade show" role="alert">
<?php echo $_SESSION['message']; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php unset($_SESSION['message'], $_SESSION['message_type']); ?>
<?php endif; ?>
<div class="card mb-4">
<div class="card-header">
<i class="bi bi-boxes me-1"></i>
รายการพัสดุ
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover table-sm">
<thead class="table-dark">
<tr>
<th>#</th>
<th>รูปภาพ</th>
<th>รหัสพัสดุ</th>
<th>ชื่อพัสดุ</th>
<th>หมวดหมู่</th>
<th>หน่วยนับ</th>
<th class="text-end">คงคลัง</th>
<th class="text-end">มูลค่าเฉลี่ย/หน่วย</th>
<th class="text-end">มูลค่ารวม</th>
<th>สถานะ</th>
<th>ดำเนินการ</th>
</tr>
</thead>
<tbody>
<?php if (!empty($supplies)): ?>
<?php $counter = 1; ?>
<?php foreach ($supplies as $item):
$image_url = BASE_URL . '/uploads/supplies/' . $item['image_filename'];
$image_path = __DIR__ . '/../uploads/supplies/' . $item['image_filename'];
$image_exists = !empty($item['image_filename']) && file_exists($image_path);
$is_inactive = ($item['status'] == 'inactive');
?>
<tr class="<?php echo $is_inactive ? 'inactive-row' : ''; ?>">
<td><?php echo $counter++; ?></td>
<td class="text-center">
<?php if ($image_exists): ?>
<img src="<?php echo $image_url; ?>"
alt="<?php echo htmlspecialchars($item['supply_name']); ?>"
class="img-thumbnail supply-img-thumbnail"
data-bs-toggle="modal"
data-bs-target="#imageModal"
data-image-url="<?php echo $image_url; ?>"
data-image-title="<?php echo htmlspecialchars($item['supply_name'] . ' (' . $item['supply_code'] . ')'); ?>">
<?php else: ?>
<i class="bi bi-image text-muted fs-4 image-placeholder"></i>
<?php endif; ?>
</td>
<td><?php echo htmlspecialchars($item['supply_code']); ?></td>
<td><?php echo htmlspecialchars($item['supply_name']); ?></td>
<td><?php echo htmlspecialchars($item['category_name']); ?></td>
<td><?php echo htmlspecialchars($item['unit']); ?></td>
<td class="text-end <?php echo (!$is_inactive && $item['quantity_in_stock'] <= $item['min_stock_level'] && $item['min_stock_level'] > 0) ? 'table-danger fw-bold' : ''; ?>">
<?php echo number_format($item['quantity_in_stock']); ?>
</td>
<td class="text-end">
<?php echo number_format($item['average_unit_price'], 2); ?>
</td>
<td class="text-end">
<?php echo number_format($item['total_value'], 2); ?>
</td>
<td>
<?php if ($item['status'] == 'active'): ?>
<span class="badge bg-success">ใช้งาน</span>
<?php else: ?>
<span class="badge bg-secondary">ไม่ใช้งาน</span>
<?php endif; ?>
<?php if ($item['status'] == 'active' && $item['quantity_in_stock'] <= $item['min_stock_level'] && $item['min_stock_level'] > 0): ?>
<span class="badge bg-warning text-dark ms-1">ต่ำกว่าจุดสั่งซื้อ</span>
<?php endif; ?>
</td>
<td>
<a href="supply_view.php?id=<?php echo $item['id']; ?>" class="btn btn-sm btn-info" title="ดูรายละเอียด">
<i class="bi bi-search"></i>
</a>
<a href="supply_edit.php?id=<?php echo $item['id']; ?>" class="btn btn-sm btn-warning" title="แก้ไข">
<i class="bi bi-pencil-fill"></i>
</a>
<?php
$toggle_icon = $is_inactive ? 'bi-eye-fill' : 'bi-eye-slash-fill';
$toggle_title = $is_inactive ? 'เปิดใช้งาน' : 'ปิดใช้งาน';
$toggle_btn_class = $is_inactive ? 'btn-success' : 'btn-danger';
$toggle_confirm_msg = $is_inactive ? 'ต้องการเปิดใช้งานพัสดุรายการนี้ใช่หรือไม่?' : 'ต้องการปิดใช้งานพัสดุรายการนี้ใช่หรือไม่? (จะไม่สามารถเบิกได้)';
?>
<a href="action_supply_toggle_status.php?id=<?php echo $item['id']; ?>&status=<?php echo $item['status']; ?>"
class="btn btn-sm <?php echo $toggle_btn_class; ?>"
title="<?php echo $toggle_title; ?>"
onclick="return confirm('<?php echo $toggle_confirm_msg; ?>');">
<i class="bi <?php echo $toggle_icon; ?>"></i>
</a>
</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="11" class="text-center">ไม่พบข้อมูลพัสดุตามเงื่อนไข</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="modal fade" id="imageModal" tabindex="-1" aria-labelledby="imageModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="imageModalLabel">รูปภาพพัสดุ</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body text-center">
<img src="" id="modalImage" alt="Supply Image">
</div>
</div>
</div>
</div>
<?php
// --- Include Footer ---
include_once __DIR__ . '/../includes/footer.php';
?>
<script>
$(document).ready(function() {
// Export Button Handler
$('#exportBtn').on('click', function() {
const form = document.getElementById('filterForm');
const params = new URLSearchParams(new FormData(form)).toString();
window.location.href = 'export_supplies.php?' + params;
});
// Image Modal Handler
var imageModal = document.getElementById('imageModal');
if (imageModal) {
imageModal.addEventListener('show.bs.modal', function (event) {
var button = event.relatedTarget;
var imageUrl = button.getAttribute('data-image-url');
var imageTitle = button.getAttribute('data-image-title');
var modalTitle = imageModal.querySelector('.modal-title');
var modalImage = imageModal.querySelector('#modalImage');
modalTitle.textContent = imageTitle;
modalImage.src = imageUrl;
modalImage.alt = imageTitle;
});
}
});
</script>