Files
alrahma_sunday_school_api/app/Models/ParentAttendanceReport.php
T
root e13df69885
API CI/CD / Validate (composer + pint) (push) Successful in 3m6s
API CI/CD / Test (PHPUnit) (push) Failing after 4m53s
API CI/CD / Build frontend assets (push) Successful in 1m2s
API CI/CD / Security audit (push) Failing after 59s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
fix unittests issues
2026-07-07 20:56:32 -04:00

80 lines
2.5 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Support\Facades\DB;
class ParentAttendanceReport extends BaseModel
{
protected $table = 'parent_attendance_reports';
// ✅ legacy: useTimestamps = true
public $timestamps = true;
protected $fillable = ['parent_id', 'student_id', 'class_section_id', 'report_date', 'type', 'arrival_time', 'dismiss_time', 'reason', 'semester', 'school_year', 'status', 'created_at', 'updated_at'];
protected $casts = [
'parent_id' => 'integer',
'student_id' => 'integer',
'class_section_id' => 'integer',
];
/* Optional relationships */
public function parent()
{
return $this->belongsTo(User::class, 'parent_id');
}
public function student()
{
return $this->belongsTo(Student::class, 'student_id');
}
public function classSection()
{
// classSection table uses class_section_id as “business key”; your join uses that.
return $this->belongsTo(ClassSection::class, 'class_section_id', 'class_section_id');
}
/**
* Laravel equivalent of legacy listForDateRange()
* Returns array rows with:
* parent_attendance_reports.* + students firstname/lastname + classSection class_section_name
*/
public static function listForDateRange(
?string $startDate = null,
?string $endDate = null,
?string $schoolYear = null,
?string $semester = null,
?int $parentId = null,
): array {
$q = DB::table('parent_attendance_reports as par');
if ($parentId !== null && $parentId > 0) {
$q->where('par.parent_id', $parentId);
}
if (! empty($startDate)) {
$q->where('par.report_date', '>=', $startDate);
}
if (! empty($endDate)) {
$q->where('par.report_date', '<=', $endDate);
}
if (is_string($schoolYear) && $schoolYear !== '') {
$q->where('par.school_year', $schoolYear);
}
if (is_string($semester) && $semester !== '') {
$q->where('par.semester', $semester);
}
$q->leftJoin('students as s', 's.id', '=', 'par.student_id')
->leftJoin('classSection as cs', 'cs.class_section_id', '=', 'par.class_section_id')
->select('par.*', 's.firstname', 's.lastname', 'cs.class_section_name')
->orderBy('par.report_date', 'asc')
->orderBy('par.class_section_id', 'asc')
->orderBy('par.student_id', 'asc');
return $q->get()->map(fn ($r) => (array) $r)->all();
}
}