116 lines
4.3 KiB
PHP
116 lines
4.3 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Models\SpLetter;
|
||
use App\Models\Setting;
|
||
use Barryvdh\DomPDF\Facade\Pdf;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Str;
|
||
use Illuminate\View\View;
|
||
|
||
class SpLetterController extends Controller
|
||
{
|
||
public function index(Request $request): View
|
||
{
|
||
$letters = SpLetter::with(['student', 'spThreshold', 'generator'])
|
||
->when(auth()->user()->isScopedWaliKelas(), fn ($q) => $q->whereHas('student', fn ($qq) => $qq->whereIn('class_id', auth()->user()->homeroomClassIds())))
|
||
->latest()
|
||
->paginate(20);
|
||
|
||
return view('sp-letters.index', compact('letters'));
|
||
}
|
||
|
||
public function show(SpLetter $spLetter): View
|
||
{
|
||
abort_unless(auth()->user()->canViewStudent($spLetter->student_id), 403);
|
||
|
||
$spLetter->load(['student', 'spThreshold', 'generator']);
|
||
$school = $this->schoolSettings();
|
||
|
||
return view('sp-letters.show', compact('spLetter', 'school'));
|
||
}
|
||
|
||
public function print(SpLetter $spLetter)
|
||
{
|
||
abort_unless(auth()->user()->canViewStudent($spLetter->student_id), 403);
|
||
|
||
$spLetter->load(['student', 'spThreshold', 'generator']);
|
||
$school = $this->schoolSettings();
|
||
|
||
$spLetter->update(['printed_at' => now(), 'status' => 'printed']);
|
||
|
||
// HTML print view (untuk cetak langsung dari browser)
|
||
return view('sp-letters.print', compact('spLetter', 'school'));
|
||
}
|
||
|
||
/**
|
||
* Unduh PDF surat peringatan (format F4 resmi).
|
||
*/
|
||
public function pdf(SpLetter $spLetter)
|
||
{
|
||
abort_unless(auth()->user()->canViewStudent($spLetter->student_id), 403);
|
||
|
||
$spLetter->load(['student', 'spThreshold', 'generator']);
|
||
$school = $this->schoolSettings();
|
||
$forPdf = true;
|
||
|
||
$spLetter->update(['printed_at' => now(), 'status' => 'printed']);
|
||
|
||
$pdf = Pdf::loadView('sp-letters.print', compact('spLetter', 'school', 'forPdf'))
|
||
->setPaper([0, 0, 595.28, 935.43], 'portrait'); // F4 (210×330 mm)
|
||
|
||
$filename = 'SP-'.Str::slug(str_replace(['/', '\\'], '-', $spLetter->letter_number)).'.pdf';
|
||
|
||
return $pdf->download($filename);
|
||
}
|
||
|
||
/**
|
||
* Ekspor PDF rekap seluruh surat peringatan (daftar, A4 landscape).
|
||
*/
|
||
public function exportPdf()
|
||
{
|
||
$letters = SpLetter::with(['student', 'spThreshold', 'generator'])
|
||
->when(auth()->user()->isScopedWaliKelas(), fn ($q) => $q->whereHas('student', fn ($qq) => $qq->whereIn('class_id', auth()->user()->homeroomClassIds())))
|
||
->latest()
|
||
->get();
|
||
|
||
$summary = [
|
||
'total' => $letters->count(),
|
||
'total_poin' => $letters->sum('total_points_at_time'),
|
||
'siswa' => $letters->pluck('student_id')->unique()->count(),
|
||
'per_status' => $letters->groupBy('status')->map->count(),
|
||
];
|
||
|
||
$school = $this->schoolSettings();
|
||
|
||
$pdf = Pdf::loadView('sp-letters.export-pdf', compact('letters', 'summary', 'school'))
|
||
->setPaper('a4', 'landscape');
|
||
|
||
return $pdf->download('rekap-surat-peringatan-'.now()->format('Ymd-His').'.pdf');
|
||
}
|
||
|
||
/**
|
||
* Data sekolah untuk kop surat (format resmi).
|
||
*/
|
||
protected function schoolSettings(): array
|
||
{
|
||
return [
|
||
'name' => Setting::getValue('school_name', 'SMK'),
|
||
'address' => Setting::getValue('school_address', ''),
|
||
'phone' => Setting::getValue('school_phone', ''),
|
||
'logo' => Setting::getValue('school_logo', ''),
|
||
'kop_logo' => Setting::getValue('kop_logo', ''),
|
||
'government' => Setting::getValue('school_government', 'PEMERINTAH PROVINSI JAWA TIMUR'),
|
||
'agency' => Setting::getValue('school_agency', 'DINAS PENDIDIKAN'),
|
||
'full_name' => Setting::getValue('school_full_name', Setting::getValue('school_name', 'SMK')),
|
||
'address_detail' => Setting::getValue('school_address_detail', ''),
|
||
'website_email' => Setting::getValue('school_website_email', ''),
|
||
'postal' => Setting::getValue('school_postal', ''),
|
||
'kepala_sekolah' => Setting::getValue('kepala_sekolah_name', ''),
|
||
'place' => Setting::getValue('school_place', 'Wonorejo'),
|
||
'kepala_sekolah_nip' => Setting::getValue('kepala_sekolah_nip', ''),
|
||
];
|
||
}
|
||
}
|