TIFNJK_E41222887/app/Http/Controllers/DashboardController.php

62 lines
1.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Department;
use App\Models\Employee;
use App\Models\Payroll;
use App\Models\Position;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Inertia\Inertia;
class DashboardController extends Controller
{
public function index()
{
if (auth()->user()->role !== 'admin') {
return redirect()->route('employee.index');
}
$currentPeriod = Carbon::now()->format('Y-m');
$stats = [
'total_employees' => Employee::count(),
'total_departments' => Department::count(),
'total_positions' => Position::count(),
'payroll_realisasi' => Payroll::where('period', $currentPeriod)
->where('status', 'paid')
->sum('net_salary'),
];
$genderData = Employee::selectRaw('gender, count(*) as total')
->groupBy('gender')
->get()
->map(fn($item) => [
'name' => $item->gender == 'L' ? 'Laki-laki' : 'Perempuan',
'value' => $item->total,
'fill' => $item->gender == 'L' ? '#3b82f6' : '#ec4899',
]);
$deptData = Department::withCount('employees')
->having('employees_count', '>', 0)
->get()
->map(fn($item) => [
'name' => $item->name,
'employees' => $item->employees_count,
]);
$latestEmployees = Employee::with(['department', 'position', 'user'])
->latest('join_date')
->take(5)
->get();
return Inertia::render('dashboard', [
'stats' => $stats,
'genderData' => $genderData,
'deptData' => $deptData,
'latestEmployees' => $latestEmployees,
'currentPeriod' => $currentPeriod,
]);
}
}