first commit
This commit is contained in:
@@ -0,0 +1,267 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\DokSiremun\Kegiatan;
|
||||
use App\Models\Verifikator\Periode;
|
||||
use App\Models\Verifikator\Team;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Spatie\Permission\PermissionRegistrar;
|
||||
|
||||
class DashboardController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$title = 'Dashboard';
|
||||
$user = auth()->user();
|
||||
|
||||
// Load teams untuk user
|
||||
$user->load('teams');
|
||||
|
||||
// Cek apakah user tidak punya team
|
||||
if ($user->teams->count() === 0) {
|
||||
return redirect()->route('login')->with('error', 'Anda tidak terdaftar di tim manapun');
|
||||
}
|
||||
|
||||
// Cek apakah team_id sudah di-set di session
|
||||
$teamId = session('team_id');
|
||||
|
||||
// Jika tidak ada team_id di session
|
||||
if (! $teamId) {
|
||||
// Jika user hanya punya satu team, langsung set ke session
|
||||
if ($user->teams->count() === 1) {
|
||||
$team = $user->teams->first();
|
||||
session(['team_id' => $team->id]);
|
||||
setPermissionsTeamId($team->id);
|
||||
|
||||
$penunjangSiapVerifikasiCount = $this->getPenunjangSiapVerifikasiCount($team);
|
||||
$penunjangStats = $this->getPenunjangStats($team);
|
||||
|
||||
return view('dashboard.index', compact('title', 'penunjangSiapVerifikasiCount', 'penunjangStats'));
|
||||
}
|
||||
|
||||
// Jika user punya lebih dari satu team, tampilkan modal
|
||||
$teams = $user->teams;
|
||||
$showTeamModal = true;
|
||||
$penunjangSiapVerifikasiCount = 0;
|
||||
$penunjangStats = ['total' => 0, 'diterima' => 0, 'ditolak' => 0, 'diajukan' => 0, 'belum_diajukan' => 0];
|
||||
|
||||
return view('dashboard.index', compact('title', 'teams', 'showTeamModal', 'penunjangSiapVerifikasiCount', 'penunjangStats'));
|
||||
}
|
||||
|
||||
// Jika team_id sudah ada tapi user punya lebih dari satu team
|
||||
// dan team_id yang dipilih tidak valid, tampilkan modal lagi
|
||||
if ($user->teams->count() > 1) {
|
||||
$hasTeam = $user->teams->where('id', $teamId)->count() > 0;
|
||||
if (! $hasTeam) {
|
||||
$teams = $user->teams;
|
||||
$showTeamModal = true;
|
||||
$penunjangSiapVerifikasiCount = 0;
|
||||
$penunjangStats = ['total' => 0, 'diterima' => 0, 'ditolak' => 0, 'diajukan' => 0, 'belum_diajukan' => 0];
|
||||
|
||||
return view('dashboard.index', compact('title', 'teams', 'showTeamModal', 'penunjangSiapVerifikasiCount', 'penunjangStats'));
|
||||
}
|
||||
}
|
||||
|
||||
$team = Team::find($teamId);
|
||||
$penunjangSiapVerifikasiCount = $this->getPenunjangSiapVerifikasiCount($team);
|
||||
$penunjangStats = $this->getPenunjangStats($team);
|
||||
|
||||
return view('dashboard.index', compact('title', 'penunjangSiapVerifikasiCount', 'penunjangStats'));
|
||||
}
|
||||
|
||||
private function getPenunjangSiapVerifikasiCount(?Team $team): int
|
||||
{
|
||||
if (! $team || ! auth()->user()->can('Baca Penunjang')) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$periode = Periode::where('status', true)->first()?->id_periode;
|
||||
if (! $periode) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$query = Kegiatan::where('id_periode', $periode)
|
||||
->where(function ($q) {
|
||||
$q->where('status', 1)->orWhereNull('status');
|
||||
})
|
||||
->whereNotIn('uploader', ['Api Praktisi', 'Api Spada', 'API Permatasari', 'API LMS'])
|
||||
->whereHas('syarats', function ($q) {
|
||||
$q->whereNotIn('id_kategori', [1, 2, 3, 5, 6])
|
||||
->whereNotIn('id_rubrik', [...config('rubrik.kompetisi'), ...config('rubrik.prestasi'), ...config('rubrik.asesor'), ...config('rubrik.reviewer')]);
|
||||
})
|
||||
->whereHas('syarats', function ($q) {
|
||||
$q->whereIn('status_verifikasi_id', [3, 4])
|
||||
->whereNotIn('id_kategori', [1, 2, 3, 5, 6])
|
||||
->whereNotIn('id_rubrik', [...config('rubrik.kompetisi'), ...config('rubrik.prestasi'), ...config('rubrik.asesor'), ...config('rubrik.reviewer')]);
|
||||
})
|
||||
->whereDoesntHave('syarats', function ($q) {
|
||||
$q->where('status_verifikasi_id', 1)
|
||||
->whereNotIn('id_kategori', [1, 2, 3, 5, 6])
|
||||
->whereNotIn('id_rubrik', [...config('rubrik.kompetisi'), ...config('rubrik.prestasi'), ...config('rubrik.asesor'), ...config('rubrik.reviewer')]);
|
||||
});
|
||||
|
||||
if (auth()->id() != 1 && $team->unit_kerja_id !== null) {
|
||||
$unit = $team->unit_kerja_id;
|
||||
if ($unit > 0) {
|
||||
$query->where('id_unit_kerja_pimpinan', $unit + 1);
|
||||
} else {
|
||||
$query->where(function ($q) {
|
||||
$q->where('id_unit_kerja_pimpinan', 1)->orWhere('id_unit_kerja_pimpinan', '>', 10);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return $query->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{total: int, diterima: int, ditolak: int, diajukan: int, belum_diajukan: int}
|
||||
*/
|
||||
private function getPenunjangStats(?Team $team): array
|
||||
{
|
||||
if (! $team || ! auth()->user()->can('Baca Penunjang')) {
|
||||
return ['total' => 0, 'diterima' => 0, 'ditolak' => 0, 'diajukan' => 0, 'belum_diajukan' => 0];
|
||||
}
|
||||
|
||||
$periode = Periode::where('status', true)->first()?->id_periode;
|
||||
if (! $periode) {
|
||||
return ['total' => 0, 'diterima' => 0, 'ditolak' => 0, 'diajukan' => 0, 'belum_diajukan' => 0];
|
||||
}
|
||||
|
||||
$base = Kegiatan::where('id_periode', $periode)
|
||||
->where(function ($q) {
|
||||
$q->where('status', 1)->orWhereNull('status');
|
||||
})
|
||||
->whereNotIn('uploader', ['Api Praktisi', 'Api Spada', 'API Permatasari', 'API LMS'])
|
||||
->whereHas('syarats', function ($q) {
|
||||
$q->whereNotIn('id_kategori', [1, 2, 3, 5, 6])
|
||||
->whereNotIn('id_rubrik', [...config('rubrik.kompetisi'), ...config('rubrik.prestasi'), ...config('rubrik.asesor'), ...config('rubrik.reviewer')]);
|
||||
});
|
||||
|
||||
if (auth()->id() != 1 && $team->unit_kerja_id !== null) {
|
||||
$unit = $team->unit_kerja_id;
|
||||
if ($unit > 0) {
|
||||
$base->where('id_unit_kerja_pimpinan', $unit + 1);
|
||||
} else {
|
||||
$base->where(function ($q) {
|
||||
$q->where('id_unit_kerja_pimpinan', 1)->orWhere('id_unit_kerja_pimpinan', '>', 10);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
$penunjangExclude = fn ($q) => $q->whereNotIn('id_kategori', [1, 2, 3, 5, 6])
|
||||
->whereNotIn('id_rubrik', [...config('rubrik.kompetisi'), ...config('rubrik.prestasi'), ...config('rubrik.asesor'), ...config('rubrik.reviewer')]);
|
||||
|
||||
$total = (clone $base)->count();
|
||||
|
||||
$diterima = (clone $base)
|
||||
->whereHas('syarats', function ($q) use ($penunjangExclude) {
|
||||
$penunjangExclude($q)->whereIn('status_verifikasi_id', [6, 7]);
|
||||
})
|
||||
->whereDoesntHave('syarats', function ($q) use ($penunjangExclude) {
|
||||
$penunjangExclude($q)->whereIn('status_verifikasi_id', [1, 3, 4]);
|
||||
})
|
||||
->count();
|
||||
|
||||
$ditolak = (clone $base)
|
||||
->whereHas('syarats', function ($q) use ($penunjangExclude) {
|
||||
$penunjangExclude($q)->whereIn('status_verifikasi_id', [2, 5, 8]);
|
||||
})
|
||||
->whereDoesntHave('syarats', function ($q) use ($penunjangExclude) {
|
||||
$penunjangExclude($q)->whereIn('status_verifikasi_id', [3, 4, 6, 7]);
|
||||
})
|
||||
->count();
|
||||
|
||||
$diajukan = (clone $base)
|
||||
->whereHas('syarats', function ($q) use ($penunjangExclude) {
|
||||
$penunjangExclude($q)->whereIn('status_verifikasi_id', [3, 4]);
|
||||
})
|
||||
->whereDoesntHave('syarats', function ($q) use ($penunjangExclude) {
|
||||
$penunjangExclude($q)->where('status_verifikasi_id', 1);
|
||||
})
|
||||
->count();
|
||||
|
||||
$belumDiajukan = (clone $base)
|
||||
->whereHas('syarats', function ($q) use ($penunjangExclude) {
|
||||
$penunjangExclude($q)->where('status_verifikasi_id', 1);
|
||||
})
|
||||
->count();
|
||||
|
||||
return ['total' => $total, 'diterima' => $diterima, 'ditolak' => $ditolak, 'diajukan' => $diajukan, 'belum_diajukan' => $belumDiajukan];
|
||||
}
|
||||
|
||||
public function getTeams()
|
||||
{
|
||||
$user = auth()->user();
|
||||
$teams = $user->teams;
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'teams' => $teams,
|
||||
]);
|
||||
}
|
||||
|
||||
public function setTeam(Request $request)
|
||||
{
|
||||
try {
|
||||
$request->validate([
|
||||
'team_id' => 'required|integer',
|
||||
]);
|
||||
|
||||
$user = auth()->user();
|
||||
$teamId = (int) $request->input('team_id');
|
||||
|
||||
// Load user teams
|
||||
$user->load('teams');
|
||||
|
||||
// Cek apakah user memiliki akses ke team tersebut
|
||||
$team = $user->teams->where('id', $teamId)->first();
|
||||
|
||||
if (! $team) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Anda tidak memiliki akses ke tim tersebut',
|
||||
], 403);
|
||||
}
|
||||
|
||||
// Set team_id ke session
|
||||
session(['team_id' => $teamId]);
|
||||
|
||||
// Force save session immediately
|
||||
session()->save();
|
||||
|
||||
// Set permissions team ID untuk Spatie Permission
|
||||
setPermissionsTeamId($teamId);
|
||||
|
||||
// Clear all permission caches
|
||||
app(PermissionRegistrar::class)->forgetCachedPermissions();
|
||||
|
||||
// Clear Laravel cache juga
|
||||
\Cache::forget('spatie.permission.cache');
|
||||
|
||||
// Force reload user instance dari database
|
||||
auth()->user()->unsetRelation('permissions');
|
||||
auth()->user()->unsetRelation('roles');
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Team berhasil dipilih',
|
||||
'redirect' => route('dashboard.index'),
|
||||
]);
|
||||
} catch (ValidationException $e) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Data tidak valid: '.implode(', ', $e->validator->errors()->all()),
|
||||
], 422);
|
||||
} catch (\Exception $e) {
|
||||
\Log::error('Error setting team: '.$e->getMessage().' | Trace: '.$e->getTraceAsString());
|
||||
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Terjadi kesalahan: '.$e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user