| 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 : E:/Inetpub/www/supply_system/ |
Upload File : |
<?php
include_once 'config.php';
include_once 'functions.php';
include_once 'includes/auth_check.php'; // ตรวจสอบการล็อกอิน
$user_type = getUserData('user_type');
$full_name = getUserData('full_name');
$page_title = "หน้าหลัก"; // กำหนด Title
// --- ดึงข้อมูลสำหรับ Dashboard (เฉพาะ Supply Manager) ---
$dashboard_data = [
'total_supplies' => 0,
'pending_approval_count' => 0,
'low_stock_count' => 0,
'total_stock_value' => 0,
'monthly_requisition_value' => 0,
'fiscal_year_requisition_value' => 0,
'pending_requests' => [],
'low_stock_items' => []
];
if ($user_type == 'supply_manager' || $user_type == 'admin') {
// 1. Total Supplies
$sql_total_supplies = "SELECT COUNT(id) as total FROM supplies WHERE status = 'active'";
$res_total_supplies = mysqli_query($conn, $sql_total_supplies);
if ($res_total_supplies && mysqli_num_rows($res_total_supplies) > 0) {
$row_total = mysqli_fetch_assoc($res_total_supplies);
$dashboard_data['total_supplies'] = isset($row_total['total']) ? $row_total['total'] : 0;
mysqli_free_result($res_total_supplies);
}
// 2. Pending Approval Count
$sql_pending = "SELECT COUNT(id) as total FROM requisitions WHERE status = 'head_approved'";
$res_pending = mysqli_query($conn, $sql_pending);
if ($res_pending && mysqli_num_rows($res_pending) > 0) {
$row_pending = mysqli_fetch_assoc($res_pending);
$dashboard_data['pending_approval_count'] = isset($row_pending['total']) ? $row_pending['total'] : 0;
mysqli_free_result($res_pending);
}
// 3. Low Stock Count & Items
$sql_low_stock = "SELECT id, supply_code, supply_name, unit, quantity_in_stock, min_stock_level
FROM supplies
WHERE status = 'active' AND quantity_in_stock <= min_stock_level AND min_stock_level > 0
ORDER BY supply_code ASC";
$res_low_stock = mysqli_query($conn, $sql_low_stock);
if ($res_low_stock) {
$dashboard_data['low_stock_count'] = mysqli_num_rows($res_low_stock);
while($row = mysqli_fetch_assoc($res_low_stock)){
$dashboard_data['low_stock_items'][] = $row;
}
mysqli_free_result($res_low_stock);
}
// 4. Total Stock Value
$sql_total_value = "SELECT SUM(total_value) as total FROM supplies WHERE status = 'active'";
$res_total_value = mysqli_query($conn, $sql_total_value);
if ($res_total_value && mysqli_num_rows($res_total_value) > 0) {
$row_value = mysqli_fetch_assoc($res_total_value);
$dashboard_data['total_stock_value'] = isset($row_value['total']) ? $row_value['total'] : 0;
mysqli_free_result($res_total_value);
}
// 5. Monthly Requisition Value
$current_month_start = date('Y-m-01 00:00:00');
$current_month_end = date('Y-m-t 23:59:59');
$sql_monthly_val = "SELECT SUM(total_value) as total
FROM requisitions
WHERE status = 'approved'
AND approved_date BETWEEN '$current_month_start' AND '$current_month_end'";
$res_monthly_val = mysqli_query($conn, $sql_monthly_val);
if ($res_monthly_val && mysqli_num_rows($res_monthly_val) > 0) {
$row_monthly = mysqli_fetch_assoc($res_monthly_val);
$dashboard_data['monthly_requisition_value'] = isset($row_monthly['total']) ? $row_monthly['total'] : 0;
mysqli_free_result($res_monthly_val);
}
// 6. Fiscal Year Requisition Value (Oct - Sep)
$current_year = date('Y');
$current_month = date('n');
if ($current_month >= 10) { // Fiscal year starts in October
$fiscal_year_start = date('Y-10-01 00:00:00', strtotime("$current_year-10-01"));
$fiscal_year_end = date('Y-09-30 23:59:59', strtotime('+1 year', strtotime("$current_year-10-01")));
} else {
$fiscal_year_start = date('Y-10-01 00:00:00', strtotime('-1 year', strtotime("$current_year-10-01")));
$fiscal_year_end = date('Y-09-30 23:59:59', strtotime("$current_year-10-01"));
}
$sql_fiscal_val = "SELECT SUM(total_value) as total
FROM requisitions
WHERE status = 'approved'
AND approved_date BETWEEN '$fiscal_year_start' AND '$fiscal_year_end'";
$res_fiscal_val = mysqli_query($conn, $sql_fiscal_val);
if ($res_fiscal_val && mysqli_num_rows($res_fiscal_val) > 0) {
$row_fiscal = mysqli_fetch_assoc($res_fiscal_val);
$dashboard_data['fiscal_year_requisition_value'] = isset($row_fiscal['total']) ? $row_fiscal['total'] : 0;
mysqli_free_result($res_fiscal_val);
}
// 7. Pending Requests List (Simplified for dashboard)
$sql_pending_list = "SELECT r.id, r.requisition_no, r.request_date, d.dept_name, u.full_name as requester_name
FROM requisitions r
JOIN departments d ON r.department_id = d.id
JOIN users u ON r.requested_by = u.id
WHERE r.status = 'head_approved'
ORDER BY r.head_approved_date ASC, r.id ASC LIMIT 5"; // Limit for dashboard view
$res_pending_list = mysqli_query($conn, $sql_pending_list);
if ($res_pending_list) {
while ($row = mysqli_fetch_assoc($res_pending_list)) {
$dashboard_data['pending_requests'][] = $row;
}
mysqli_free_result($res_pending_list);
}
} // End if supply_manager or admin
// --- NEW: Fetch supplies for Department user ---
$department_supplies = [];
if ($user_type == 'department') {
$sql_dept_supplies = "SELECT s.id, s.supply_code, s.supply_name, s.unit, s.quantity_in_stock, s.min_stock_level, s.image_filename, c.category_name
FROM supplies s
JOIN categories c ON s.category_id = c.id
WHERE s.status = 'active' -- Department users should only see active supplies
ORDER BY s.supply_code ASC";
$res_dept_supplies = mysqli_query($conn, $sql_dept_supplies);
if ($res_dept_supplies) {
while ($row = mysqli_fetch_assoc($res_dept_supplies)) {
$department_supplies[] = $row;
}
mysqli_free_result($res_dept_supplies);
}
// Don't close $conn here, it will be closed by footer or later
}
// --- End new query ---
// --- Include Header ---
include_once 'includes/header.php';
?>
<style>
.supply-img-thumbnail {
max-width: 50px;
max-height: 50px;
object-fit: cover;
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>
<?php
// --- Include Sidebar based on user type ---
switch ($user_type) {
case 'admin':
case 'supply_manager':
include_once 'includes/sidebar_supply.php';
break;
case 'head_of_department':
include_once 'includes/sidebar_head.php';
break;
case 'department':
include_once 'includes/sidebar_dept.php';
break;
default:
redirect('logout.php');
break;
}
?>
<div class="container-fluid">
<h1 class="mt-4"><?php echo $page_title; ?></h1>
<p>สวัสดีคุณ <?php echo htmlspecialchars($full_name); ?>!</p>
<?php if ($user_type == 'supply_manager' || $user_type == 'admin'): ?>
<div class="row g-3 mb-4">
<div class="col-xl-2 col-md-4 col-sm-6">
<div class="card text-white bg-primary h-100">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center">
<div>
<div class="fs-1 fw-bold"><?php echo number_format($dashboard_data['total_supplies']); ?></div>
<div class="small">รายการพัสดุทั้งหมด</div>
</div>
<i class="bi bi-boxes fs-1 opacity-50"></i>
</div>
</div>
</div>
</div>
<div class="col-xl-2 col-md-4 col-sm-6">
<div class="card text-dark bg-warning h-100">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center">
<div>
<div class="fs-1 fw-bold"><?php echo number_format($dashboard_data['pending_approval_count']); ?></div>
<div class="small">คำขอรอการอนุมัติ<br>(ผอ.รับทราบแล้ว)</div>
</div>
<i class="bi bi-hourglass-split fs-1 opacity-50"></i>
</div>
</div>
<?php if ($dashboard_data['pending_approval_count'] > 0): ?>
<a class="card-footer text-dark d-flex align-items-center justify-content-between small stretched-link text-decoration-none" href="<?php echo BASE_URL; ?>/requisitions/list_pending_supply.php">
ดูรายการ
<i class="bi bi-chevron-right"></i>
</a>
<?php endif; ?>
</div>
</div>
<div class="col-xl-2 col-md-4 col-sm-6">
<div class="card text-white bg-danger h-100">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center">
<div>
<div class="fs-1 fw-bold"><?php echo number_format($dashboard_data['low_stock_count']); ?></div>
<div class="small">พัสดุเหลือน้อย</div>
</div>
<i class="bi bi-exclamation-triangle-fill fs-1 opacity-50"></i>
</div>
</div>
<?php if ($dashboard_data['low_stock_count'] > 0): ?>
<a class="card-footer text-white d-flex align-items-center justify-content-between small stretched-link text-decoration-none" href="reports/report_low_stock.php">
ดูรายการ
<i class="bi bi-chevron-right"></i>
</a>
<?php endif; ?>
</div>
</div>
<div class="col-xl-2 col-md-4 col-sm-6">
<div class="card text-white bg-success h-100">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center">
<div>
<div class="fs-5 fw-bold"><?php echo number_format($dashboard_data['total_stock_value'], 2); ?></div>
<div class="small">มูลค่าพัสดุในคลัง<br>(บาท)</div>
</div>
<i class="bi bi-cash-stack fs-1 opacity-50"></i>
</div>
</div>
</div>
</div>
<div class="col-xl-2 col-md-4 col-sm-6">
<div class="card text-dark bg-light h-100">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center">
<div>
<div class="fs-5 fw-bold"><?php echo number_format($dashboard_data['monthly_requisition_value'], 2); ?></div>
<div class="small">เบิกจ่ายเดือนนี้<br>(บาท)</div>
</div>
<i class="bi bi-graph-up fs-1 opacity-50 text-danger"></i>
</div>
</div>
</div>
</div>
<div class="col-xl-2 col-md-4 col-sm-6">
<div class="card text-dark bg-info h-100">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center">
<div>
<div class="fs-5 fw-bold"><?php echo number_format($dashboard_data['fiscal_year_requisition_value'], 2); ?></div>
<div class="small">เบิกจ่ายปีงบนี้<br>(ต.ค. - ก.ย.) (บาท)</div>
</div>
<i class="bi bi-calendar-check fs-1 opacity-50"></i>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
</div>
<div class="col-lg-6">
</div>
</div>
<?php elseif ($user_type == 'head_of_department'): ?>
<div class="card mt-4">
<div class="card-header">แดชบอร์ด ผอ.กลุ่ม</div>
<div class="card-body">
<p>คุณสามารถรับทราบคำขอเบิกของกลุ่ม และดูประวัติการเบิกได้จากเมนูด้านซ้าย</p>
<a href="<?php echo BASE_URL; ?>/requisitions/list_pending_head.php" class="btn btn-primary">ดูคำขอรอรับทราบ</a>
</div>
</div>
<?php elseif ($user_type == 'department'): ?>
<div class="card mt-4">
<div class="card-header">แดชบอร์ด ธุรการกลุ่ม</div>
<div class="card-body">
<p>คุณสามารถสร้างคำขอเบิก และดูสถานะคำขอของคุณได้จากเมนูด้านซ้าย</p>
<a href="<?php echo BASE_URL; ?>/requisitions/create.php" class="btn btn-success">สร้างคำขอเบิกใหม่</a>
<a href="<?php echo BASE_URL; ?>/requisitions/list_my.php" class="btn btn-info ms-2">ดูคำขอของฉัน</a>
</div>
</div>
<div class="card mt-4">
<div class="card-header"><i class="bi bi-boxes me-1"></i> รายการพัสดุในคลัง (สำหรับตรวจสอบ)</div>
<div class="card-body">
<div class="input-group mb-3">
<span class="input-group-text"><i class="bi bi-search"></i></span>
<input type="text" id="deptSearchInput" class="form-control" placeholder="ค้นหาด้วยรหัส หรือ ชื่อพัสดุ...">
</div>
<div class="table-responsive" style="max-height: 500px; overflow-y: auto;">
<table class="table table-bordered table-striped table-hover table-sm" id="deptSuppliesTable">
<thead class="table-dark sticky-top" style="top: -1px;">
<tr>
<th>#</th>
<th>รูปภาพ</th>
<th>รหัสพัสดุ</th>
<th>ชื่อพัสดุ</th>
<th>หมวดหมู่</th>
<th>หน่วยนับ</th>
<th class="text-end">คงคลัง</th>
</tr>
</thead>
<tbody>
<?php if (!empty($department_supplies)): ?>
<?php $counter = 1; ?>
<?php foreach ($department_supplies as $item):
$image_url = BASE_URL . '/uploads/supplies/' . $item['image_filename'];
$image_path = __DIR__ . '/uploads/supplies/' . $item['image_filename']; // Check path relative to this file
$image_exists = !empty($item['image_filename']) && file_exists($image_path);
?>
<tr>
<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="#imageModalDept"
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 fw-bold <?php echo ($item['quantity_in_stock'] <= $item['min_stock_level'] && $item['min_stock_level'] > 0) ? 'text-danger' : 'text-success'; ?>">
<?php echo number_format($item['quantity_in_stock']); ?>
<?php if ($item['quantity_in_stock'] <= $item['min_stock_level'] && $item['min_stock_level'] > 0): ?>
<i class="bi bi-exclamation-triangle-fill text-warning" title="เหลือน้อย"></i>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="7" class="text-center">ไม่พบข้อมูลพัสดุ</td>
</tr>
<?php endif; ?>
</tbody>
</table>
<div id="deptNoResultsFound" class="alert alert-warning text-center" style="display: none;">
ไม่พบพัสดุที่ตรงกับคำค้นหา
</div>
</div>
</div>
</div>
<?php endif; ?>
</div>
<div class="modal fade" id="imageModalDept" tabindex="-1" aria-labelledby="imageModalDeptLabel" 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="imageModalDeptLabel">รูปภาพพัสดุ</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="modalDeptImage" alt="Supply Image">
</div>
</div>
</div>
</div>
<?php
// --- Include Footer ---
include_once 'includes/footer.php';
// Close connection if it was opened
if (isset($conn) && $conn) {
mysqli_close($conn);
}
?>
<script>
$(document).ready(function(){
// Only run this JS if the department elements exist
if ($("#deptSearchInput").length > 0) {
// Live Search for Department Table
$("#deptSearchInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
var found = false;
$("#deptSuppliesTable tbody tr").filter(function() {
var codeText = $(this).children('td').eq(2).text().toLowerCase(); // Col 2 is code
var nameText = $(this).children('td').eq(3).text().toLowerCase(); // Col 3 is name
var match = codeText.indexOf(value) > -1 || nameText.indexOf(value) > -1;
$(this).toggle(match);
if(match) {
found = true;
}
});
// Show or hide the "No Results" message
if (!found && value !== '') {
$("#deptNoResultsFound").show();
} else {
$("#deptNoResultsFound").hide();
}
});
// Image Modal Handler for Department
var imageModalDept = document.getElementById('imageModalDept');
if (imageModalDept) {
imageModalDept.addEventListener('show.bs.modal', function (event) {
var button = event.relatedTarget; // Image that triggered the modal
if(button){ // Check if button exists (event might be triggered weirdly)
var imageUrl = button.getAttribute('data-image-url');
var imageTitle = button.getAttribute('data-image-title');
var modalTitle = imageModalDept.querySelector('.modal-title');
var modalImage = imageModalDept.querySelector('#modalDeptImage');
if(modalTitle) modalTitle.textContent = imageTitle;
if(modalImage) {
modalImage.src = imageUrl;
modalImage.alt = imageTitle;
}
}
});
}
}
});
</script>