86 lines
2.9 KiB
PHP
86 lines
2.9 KiB
PHP
<?php
|
|
namespace app\Controllers;
|
|
|
|
use app\Models\PayrollModel;
|
|
use app\Models\SystemSettingModel;
|
|
use PDO;
|
|
|
|
class DashboardController extends Controller {
|
|
|
|
public function __construct() {
|
|
if (empty($_SESSION['user_id'])) {
|
|
$_SESSION['error'] = 'กรุณาเข้าสู่ระบบก่อนใช้งาน';
|
|
header('Location: ' . BASE_URL . '/login');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public function index() {
|
|
$payrollModel = new PayrollModel();
|
|
|
|
// 1. Get latest payroll period
|
|
$periods = $payrollModel->getAllPayrollPeriods();
|
|
$latestPeriod = !empty($periods) ? $periods[0] : null;
|
|
|
|
// 2. Aggregate stats for the latest period
|
|
$totalEmployees = 0;
|
|
$totalSalary = 0;
|
|
|
|
if ($latestPeriod) {
|
|
$stats = $payrollModel->getPeriodStats($latestPeriod['id']);
|
|
$totalEmployees = $stats['total_emp'] ?? 0;
|
|
$totalSalary = $stats['total_net'] ?? 0;
|
|
}
|
|
|
|
// 3. For Chart: Get yearly summary
|
|
$settingModel = new SystemSettingModel();
|
|
$chartHistoryYears = (int)$settingModel->getSetting('chart_history_years', '3');
|
|
|
|
$baseYear = date('Y');
|
|
if ($latestPeriod && !empty($latestPeriod['year_no'])) {
|
|
$baseYear = $latestPeriod['year_no'];
|
|
}
|
|
|
|
$availableYears = [];
|
|
for ($i = 0; $i < $chartHistoryYears; $i++) {
|
|
$availableYears[] = $baseYear - $i;
|
|
}
|
|
|
|
$currentYear = $_GET['year'] ?? $baseYear;
|
|
if (!in_array($currentYear, $availableYears)) {
|
|
$currentYear = $baseYear;
|
|
}
|
|
|
|
$yearBE = $currentYear + 543;
|
|
|
|
$yearlyStats = $payrollModel->getYearlySummary($currentYear);
|
|
|
|
$chartLabels = ['ม.ค.', 'ก.พ.', 'มี.ค.', 'เม.ย.', 'พ.ค.', 'มิ.ย.', 'ก.ค.', 'ส.ค.', 'ก.ย.', 'ต.ค.', 'พ.ย.', 'ธ.ค.'];
|
|
$chartIncome = array_fill(0, 12, 0);
|
|
$chartDeduction = array_fill(0, 12, 0);
|
|
|
|
foreach ($yearlyStats as $monthStr => $data) {
|
|
$mIdx = (int)$monthStr - 1; // '01' -> 0, '12' -> 11
|
|
if ($mIdx >= 0 && $mIdx < 12) {
|
|
$chartIncome[$mIdx] = $data['income'];
|
|
$chartDeduction[$mIdx] = $data['deduction'];
|
|
}
|
|
}
|
|
|
|
$this->view('dashboard/index', [
|
|
'title' => 'Dashboard | ' . APP_NAME,
|
|
'activeMenu' => 'dashboard',
|
|
'latestPeriod' => $latestPeriod,
|
|
'totalEmployees' => $totalEmployees,
|
|
'totalSalary' => $totalSalary,
|
|
'currentYear' => $currentYear,
|
|
'yearBE' => $yearBE,
|
|
'availableYears' => $availableYears,
|
|
'chartLabels' => json_encode($chartLabels),
|
|
'chartIncome' => json_encode($chartIncome),
|
|
'chartDeduction' => json_encode($chartDeduction)
|
|
]);
|
|
}
|
|
}
|
|
?>
|