| 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/reports/ |
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 = "รายงานการเบิกจ่ายพัสดุ";
// --- Get Departments for Filter ---
$departments = [];
$sql_dept = "SELECT id, dept_name FROM departments ORDER BY id";
$res_dept = mysqli_query($conn, $sql_dept);
if($res_dept) {
while($row = mysqli_fetch_assoc($res_dept)) {
$departments[] = $row;
}
mysqli_free_result($res_dept);
}
// --- Handle Filters ---
$filter_dept_id = isset($_GET['department_id']) ? intval($_GET['department_id']) : 0;
$filter_start_date = isset($_GET['start_date']) ? sanitize_input($conn, $_GET['start_date']) : '';
$filter_end_date = isset($_GET['end_date']) ? sanitize_input($conn, $_GET['end_date']) : '';
// --- Build WHERE clause ---
$where_clauses = ["r.status = 'approved'"]; // Only show approved requisitions
if ($filter_dept_id > 0) {
$where_clauses[] = "r.department_id = " . $filter_dept_id;
}
if (!empty($filter_start_date)) {
// Assuming YYYY-MM-DD format from input type=date
$where_clauses[] = "DATE(r.approved_date) >= '" . $filter_start_date . "'";
}
if (!empty($filter_end_date)) {
$where_clauses[] = "DATE(r.approved_date) <= '" . $filter_end_date . "'";
}
$where_sql = count($where_clauses) > 0 ? "WHERE " . implode(' AND ', $where_clauses) : '';
// --- Fetch Requisition Data ---
$requisitions = [];
$sql = "SELECT r.id, r.requisition_no, r.approved_date, r.total_value, d.dept_name, u_req.full_name as requester_name, u_app.full_name as approver_name
FROM requisitions r
JOIN departments d ON r.department_id = d.id
JOIN users u_req ON r.requested_by = u_req.id
LEFT JOIN users u_app ON r.approved_by = u_app.id -- Join approver details
$where_sql
ORDER BY r.approved_date DESC, r.id DESC";
$result = mysqli_query($conn, $sql);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$requisitions[] = $row;
}
mysqli_free_result($result);
} else {
echo "Error fetching report data: " . mysqli_error($conn);
}
mysqli_close($conn);
// --- Include Header and Sidebar ---
include_once __DIR__ . '/../includes/header.php';
include_once __DIR__ . '/../includes/sidebar_supply.php';
?>
<div class="container-fluid">
<h1 class="mt-4"><?php echo $page_title; ?></h1>
<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="" class="row g-3 align-items-end">
<div class="col-md-4">
<label for="department_id" class="form-label">กลุ่มงาน</label>
<select id="department_id" name="department_id" class="form-select">
<option value="0">-- ทุกกลุ่มงาน --</option>
<?php foreach($departments as $dept): ?>
<option value="<?php echo $dept['id']; ?>" <?php echo ($filter_dept_id == $dept['id']) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($dept['dept_name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-3">
<label for="start_date" class="form-label">วันที่อนุมัติตั้งแต่</label>
<input type="date" id="start_date" name="start_date" class="form-control" value="<?php echo htmlspecialchars($filter_start_date); ?>">
</div>
<div class="col-md-3">
<label for="end_date" class="form-label">ถึงวันที่</label>
<input type="date" id="end_date" name="end_date" class="form-control" value="<?php echo htmlspecialchars($filter_end_date); ?>">
</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>
</form>
</div>
</div>
<div class="card mb-4">
<div class="card-header">
<i class="bi bi-table me-1"></i>
ผลลัพธ์รายงาน
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover">
<thead class="table-dark">
<tr>
<th>#</th>
<th>เลขที่คำขอ</th>
<th>วันที่อนุมัติ</th>
<th>กลุ่มงาน</th>
<th>ผู้ขอเบิก</th>
<th>ผู้อนุมัติ</th>
<th class="text-end">มูลค่ารวม (บาท)</th>
<th>ดำเนินการ</th>
</tr>
</thead>
<tbody>
<?php if (!empty($requisitions)): ?>
<?php $counter = 1; $grand_total = 0; ?>
<?php foreach ($requisitions as $req): ?>
<tr>
<td><?php echo $counter++; ?></td>
<td><?php echo htmlspecialchars($req['requisition_no']); ?></td>
<td><?php echo formatThaiDate($req['approved_date'], true); ?></td>
<td><?php echo htmlspecialchars($req['dept_name']); ?></td>
<td><?php echo htmlspecialchars($req['requester_name']); ?></td>
<td><?php echo htmlspecialchars(isset($req['approver_name']) ? $req['approver_name'] : '-'); // Fixed ?></td>
<td class="text-end"><?php echo number_format($req['total_value'], 2); ?></td>
<td>
<a href="../requisitions/view.php?id=<?php echo $req['id']; ?>" class="btn btn-sm btn-info" title="ดูรายละเอียดคำขอ" target="_blank">
<i class="bi bi-search"></i>
</a>
</td>
</tr>
<?php $grand_total += $req['total_value']; ?>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="8" class="text-center">ไม่พบข้อมูลตามเงื่อนไขที่เลือก</td>
</tr>
<?php endif; ?>
</tbody>
<?php if (!empty($requisitions)): ?>
<tfoot class="table-light fw-bold">
<tr>
<td colspan="6" class="text-end">รวมทั้งสิ้น:</td>
<td class="text-end fs-5"><?php echo number_format($grand_total, 2); ?></td>
<td></td>
</tr>
</tfoot>
<?php endif; ?>
</table>
</div>
</div>
</div>
</div>
<?php
// --- Include Footer ---
include_once __DIR__ . '/../includes/footer.php';
?>