sistem-akademik/app/Filament/Pages/ReportPreview.php
2025-05-16 18:27:42 +07:00

139 lines
3.7 KiB
PHP

<?php
namespace App\Filament\Pages;
use App\Models\AcademicYear;
use App\Models\ClassRoom;
use App\Models\ClassSubject;
use App\Models\Student;
use App\Models\SchoolInformation;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Carbon\Carbon;
use function PHPUnit\Framework\isEmpty;
Carbon::setLocale('id');
class ReportPreview extends Page
{
protected static ?string $navigationIcon = 'heroicon-o-document-text';
protected static string $view = 'filament.pages.report-preview';
public $student;
public $class;
public $class_name;
public $academic_year;
public $semester;
public $school_information;
public $table = [];
public function mount()
{
$this->loadData();
$this->loadAssessment();
}
protected function loadData() : void
{
$this->student = Student::find(request()->query('studentId'));
$this->class = ClassRoom::find(request()->query('classId'));
$this->academic_year = AcademicYear::find(request()->query('yearId'));
$this->semester = request()->query('semester');
$this->school_information = SchoolInformation::first();
$this->class_name = $this->toRoman($this->class->class_level) . ' ' . $this->extractClassLetter($this->class->class_name);
}
protected function loadAssessment(): void
{
$requiredParams = ['studentId', 'classId', 'semester', 'yearId'];
foreach ($requiredParams as $param) {
if (blank(request()->query($param))) {
Notification::make()
->title('Missing Data')
->body("The parameter '{$param}' is required.")
->danger()
->send();
return;
}
}
$student_id = request()->query('studentId');
$class_id = request()->query('classId');
$semester = request()->query('semester');
$year_id = request()->query('yearId');
// Class Student Mapping
$classSubjects = ClassSubject::with(['subject', 'class', 'academicYear'])
->where('class_room_id', $class_id)
->where('academic_year_id', $year_id)
->get()
->sortByDesc(function ($item) {
return $item->subject->name;
})
->toArray();
$subjects = [];
foreach ($classSubjects as $classSubject) {
$category = strtolower($classSubject['subject']['category']);
$subjectName = $classSubject['subject']['name'];
$subjectId = $classSubject['subject']['id'];
$subjects[$category][$subjectId] = $subjectName;
}
$this->table["subjects"] = $subjects;
// dd($subjects);
}
public function extractClassLetter($className)
{
preg_match('/[A-Z]+$/i', trim($className), $matches);
return $matches[0] ?? '';
}
public function toRoman($number)
{
$map = [
'M' => 1000,
'CM' => 900,
'D' => 500,
'CD' => 400,
'C' => 100,
'XC' => 90,
'L' => 50,
'XL' => 40,
'X' => 10,
'IX' => 9,
'V' => 5,
'IV' => 4,
'I' => 1,
];
$returnValue = '';
while ($number > 0) {
foreach ($map as $roman => $int) {
if ($number >= $int) {
$number -= $int;
$returnValue .= $roman;
break;
}
}
}
return $returnValue;
}
public static function shouldRegisterNavigation(): bool
{
return false;
}
}