Files
2026-09-16 23:20:08 +07:00

101 lines
4.1 KiB
PHP

<?php
require_once 'config.php';
include 'header.php';
// ดึงข้อมูล KPI
$stmtTotalItems = $pdo->prepare("SELECT COUNT(*) FROM procurement_items WHERE budget_year = ?");
$stmtTotalItems->execute([$active_year]);
$totalItems = $stmtTotalItems->fetchColumn();
$stmtTotalBudget = $pdo->prepare("SELECT SUM(total_price) FROM procurement_items WHERE budget_year = ?");
$stmtTotalBudget->execute([$active_year]);
$totalBudget = $stmtTotalBudget->fetchColumn();
$stmtActualSpent = $pdo->prepare("SELECT SUM(procured_price) FROM procurement_items WHERE status='ดำเนินการเสร็จสิ้น' AND budget_year = ?");
$stmtActualSpent->execute([$active_year]);
$actualSpent = $stmtActualSpent->fetchColumn();
$savings = $totalBudget - $actualSpent;
// สรุปตามสถานะ
$stmtStatusCounts = $pdo->prepare("SELECT status, COUNT(*) as cnt, SUM(total_price) as sum_price FROM procurement_items WHERE budget_year = ? GROUP BY status");
$stmtStatusCounts->execute([$active_year]);
$statusCounts = $stmtStatusCounts->fetchAll();
?>
<div class="row g-3 mb-4">
<div class="col-md-3">
<div class="card p-3 kpi-card border-primary">
<small class="text-muted fw-bold">รายการจัดซื้อทั้งหมด</small>
<h3 class="fw-bold text-primary mt-1"><?= number_format($totalItems) ?> <span class="fs-6 text-muted">รายการ</span></h3>
</div>
</div>
<div class="col-md-3">
<div class="card p-3 kpi-card border-info">
<small class="text-muted fw-bold">งบประมาณตั้งต้นรวม</small>
<h3 class="fw-bold text-info mt-1">฿<?= number_format($totalBudget, 2) ?></h3>
</div>
</div>
<div class="col-md-3">
<div class="card p-3 kpi-card border-success">
<small class="text-muted fw-bold">จัดซื้อจริงได้แล้ว</small>
<h3 class="fw-bold text-success mt-1">฿<?= number_format($actualSpent, 2) ?></h3>
</div>
</div>
<div class="col-md-3">
<div class="card p-3 kpi-card border-warning">
<small class="text-muted fw-bold">ประหยัดงบประมาณได้</small>
<h3 class="fw-bold text-warning mt-1">฿<?= number_format($savings, 2) ?></h3>
</div>
</div>
</div>
<div class="row g-4">
<div class="col-md-6">
<div class="card p-4 h-100">
<h5 class="fw-bold mb-3"><i class="bi bi-pie-chart-fill me-2 text-primary"></i>สัดส่วนสถานะการจัดซื้อ</h5>
<canvas id="statusChart"></canvas>
</div>
</div>
<div class="col-md-6">
<div class="card p-4 h-100">
<h5 class="fw-bold mb-3"><i class="bi bi-bar-chart-line-fill me-2 text-success"></i>งบประมาณแยกตามหมวดหมู่</h5>
<canvas id="categoryChart"></canvas>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
const statusData = <?= json_encode($statusCounts) ?>;
const labels = statusData.map(item => item.status);
const counts = statusData.map(item => item.cnt);
new Chart(document.getElementById('statusChart'), {
type: 'doughnut',
data: {
labels: labels,
datasets: [{
data: counts,
backgroundColor: ['#ffc107', '#0d6efd', '#0dcaf0', '#198754', '#dc3545']
}]
},
options: { responsive: true, plugins: { legend: { position: 'bottom' } } }
});
// กราฟจำลองแยกตามประเภท
new Chart(document.getElementById('categoryChart'), {
type: 'bar',
data: {
labels: ['ค.การแพทย์', 'บำรุงรักษา', 'ค่าใช้สอย', 'จ้างเหมา', 'คอมพิวเตอร์', 'สำนักงาน'],
datasets: [{
label: 'งบประมาณ (บาท)',
data: [4500000, 2800000, 1500000, 1200000, 950000, 600000],
backgroundColor: '#0d6efd'
}]
},
options: { responsive: true, plugins: { legend: { display: false } } }
});
</script>
</body>
</html>