177 lines
6.4 KiB
PHP
177 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Exports\HasilSeleksiExport;
|
|
use App\Models\HasilSeleksi;
|
|
use App\Models\Periode;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
use Illuminate\Http\Request;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
class RiwayatSeleksiController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
// 1. Ambil semua daftar periode untuk dropdown filter
|
|
$listPeriode = Periode::orderBy('created_at', 'desc')->get();
|
|
|
|
// 2. Ambil ID periode dari request. Default ke periode terbaru jika belum pilih.
|
|
$selectedPeriodeId = $request->input('periode_id', optional($listPeriode->first())->id);
|
|
|
|
// 3. Inisialisasi variabel default
|
|
$periode = null;
|
|
$results = collect();
|
|
$mean = 0;
|
|
$countUtama = 0;
|
|
$countCadangan = 0;
|
|
$countTidakLayak = 0;
|
|
$countDialihkan = 0;
|
|
|
|
// --- HITUNG RATA-RATA GLOBAL (PASSING GRADE) ---
|
|
// Ini diambil dari seluruh sejarah data di tabel hasil_seleksi
|
|
$passingGrade = HasilSeleksi::avg('skor_akhir') ?? 0;
|
|
|
|
$perPage = $request->input('per_page', 10);
|
|
$search = $request->input('search');
|
|
|
|
// 4. Jika ada periode yang ditemukan/dipilih
|
|
if ($selectedPeriodeId) {
|
|
$periode = Periode::find($selectedPeriodeId);
|
|
|
|
if ($periode) {
|
|
// Query dasar untuk hasil seleksi pada periode terpilih
|
|
$query = HasilSeleksi::with('warga')->where('periode_id', $periode->id);
|
|
|
|
// --- HITUNG STATISTIK PERIODE TERPILIH ---
|
|
// Menggunakan clone agar query utama tidak terganggu agregasi
|
|
$mean = (clone $query)->avg('skor_akhir') ?? 0;
|
|
$countUtama = (clone $query)->where('status_keputusan', 'Layak Prioritas Utama')->count();
|
|
$countCadangan = (clone $query)->where('status_keputusan', 'Layak Cadangan')->count();
|
|
$countTidakLayak = (clone $query)->where('status_keputusan', 'Tidak Layak')->count();
|
|
$countDialihkan = (clone $query)->where('status_keputusan', 'Dialihkan (Pemerataan)')->count();
|
|
|
|
// --- LOGIKA PENCARIAN ---
|
|
if ($search) {
|
|
$query->whereHas('warga', function ($q) use ($search) {
|
|
$q->where('nama', 'like', "%{$search}%")
|
|
->orWhere('nik', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
// --- EKSEKUSI PAGINATION ---
|
|
$results = $query->orderBy('ranking', 'asc')->paginate($perPage);
|
|
|
|
// Menjaga agar filter periode, search, dan per_page tetap ada di link pagination
|
|
$results->appends($request->all());
|
|
}
|
|
}
|
|
|
|
// 5. Kirim data ke View
|
|
return view('riwayat.index', [
|
|
'listPeriode' => $listPeriode,
|
|
'periode' => $periode,
|
|
'results' => $results,
|
|
'selectedPeriodeId' => $selectedPeriodeId,
|
|
'perPage' => $perPage,
|
|
'search' => $search,
|
|
|
|
// Statistik untuk Box Info & Kondisional Export
|
|
'mean' => $mean,
|
|
'passingGrade' => $passingGrade,
|
|
'countUtama' => $countUtama,
|
|
'countCadangan' => $countCadangan,
|
|
'countTidakLayak' => $countTidakLayak,
|
|
'countDialihkan' => $countDialihkan,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Export data riwayat ke Excel menggunakan class HasilSeleksiExport yang sudah ada.
|
|
*/
|
|
public function exportExcel(Request $request)
|
|
{
|
|
$periodeId = $request->query('periode_id');
|
|
$status = $request->query('status');
|
|
$limit = $request->query('limit');
|
|
|
|
if (! $periodeId) {
|
|
return back()->with('error', 'Gagal Export! Pilih periode terlebih dahulu.');
|
|
}
|
|
|
|
$periode = Periode::findOrFail($periodeId);
|
|
|
|
// 1. VALIDASI DATA: Cek apakah ada data yang akan di-export
|
|
$checkQuery = HasilSeleksi::where('periode_id', $periodeId);
|
|
|
|
if ($status) {
|
|
$checkQuery->where('status_keputusan', $status);
|
|
}
|
|
|
|
if ($limit == 'kuota') {
|
|
$checkQuery->take($periode->kuota);
|
|
}
|
|
|
|
// Jika hasil query kosong
|
|
if ($checkQuery->count() === 0) {
|
|
$pesanStatus = $status ? "kategori [$status]" : 'seluruh data';
|
|
|
|
return back()->with('error', "Gagal Export! Tidak ada $pesanStatus pada arsip periode ".$periode->nama_periode);
|
|
}
|
|
|
|
// 2. Jalankan Export
|
|
$fileName = 'Arsip-Seleksi-'.str_replace(' ', '-', $periode->nama_periode).'.xlsx';
|
|
|
|
return Excel::download(new HasilSeleksiExport($periodeId, $status, $limit), $fileName);
|
|
}
|
|
|
|
public function exportPdf(Request $request)
|
|
{
|
|
// --- TAMBAHAN UNTUK MENGATASI ERROR 500 (MEMORY & TIMEOUT) ---
|
|
// Meminta izin ke server untuk melonggarkan batas waktu dan RAM
|
|
// karena proses render PDF sangat berat.
|
|
ini_set('max_execution_time', 600); // Ekstra waktu sampai 10 menit
|
|
set_time_limit(600); // Backup setting waktu
|
|
ini_set('memory_limit', '1024M'); // Meminta RAM hingga 1 GB
|
|
// --------------------------------------------------------------
|
|
|
|
$periodeId = $request->query('periode_id');
|
|
$status = $request->query('status');
|
|
$limit = $request->query('limit');
|
|
|
|
if (! $periodeId) {
|
|
return back()->with('error', 'Gagal Export! Pilih periode terlebih dahulu.');
|
|
}
|
|
|
|
$periode = Periode::findOrFail($periodeId);
|
|
|
|
// 1. Bangun Query
|
|
$query = HasilSeleksi::with('warga')
|
|
->where('periode_id', $periodeId)
|
|
->orderBy('ranking', 'asc');
|
|
|
|
if ($status) {
|
|
$query->where('status_keputusan', $status);
|
|
}
|
|
|
|
if ($limit == 'kuota') {
|
|
$query->take($periode->kuota);
|
|
}
|
|
|
|
// 2. VALIDASI DATA: Ambil data dan cek ketersediaannya
|
|
$results = $query->get();
|
|
|
|
if ($results->isEmpty()) {
|
|
$pesanStatus = $status ? "kategori [$status]" : 'seluruh data';
|
|
|
|
return back()->with('error', "Gagal Export PDF! Tidak ada $pesanStatus pada arsip periode ".$periode->nama_periode);
|
|
}
|
|
|
|
// 3. Generate PDF
|
|
$pdf = Pdf::loadView('seleksi.pdf', compact('results', 'periode'));
|
|
$fileName = 'Arsip-PDF-'.str_replace(' ', '-', $periode->nama_periode).'.pdf';
|
|
|
|
return $pdf->setPaper('a4', 'potrait')->download($fileName);
|
|
}
|
|
}
|