security fix and fix parent pages
API CI/CD / Validate (composer + pint) (push) Successful in 3m7s
API CI/CD / Test (PHPUnit) (push) Failing after 5m46s
API CI/CD / Build frontend assets (push) Successful in 1m2s
API CI/CD / Security audit (push) Failing after 49s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
API CI/CD / Validate (composer + pint) (push) Successful in 3m7s
API CI/CD / Test (PHPUnit) (push) Failing after 5m46s
API CI/CD / Build frontend assets (push) Successful in 1m2s
API CI/CD / Security audit (push) Failing after 49s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
This commit is contained in:
@@ -9,6 +9,7 @@ use App\Models\TeacherClass;
|
||||
use App\Models\User;
|
||||
use App\Services\SchoolYears\SchoolYearContextService;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class ClassProgressQueryService
|
||||
@@ -29,22 +30,37 @@ class ClassProgressQueryService
|
||||
->orderBy($filters['sort_by'] ?? 'week_start', $filters['sort_dir'] ?? 'desc');
|
||||
|
||||
if (! $this->isAdmin($user)) {
|
||||
$relaxedSectionIds = TeacherClass::distinctSectionIdsForTeacher((int) $user->id);
|
||||
if ($this->isParent($user)) {
|
||||
$parentSectionIds = $this->parentSectionIds((int) $user->id);
|
||||
|
||||
if (! empty($filters['class_section_id'])) {
|
||||
$sid = (int) $filters['class_section_id'];
|
||||
// Section filter: if this user has ever taught the section, show all rows for that section
|
||||
// (legacy DB often has `class_progress_reports` without matching current-year `teacher_class`).
|
||||
if (! in_array($sid, $relaxedSectionIds, true)) {
|
||||
$query->where('teacher_id', (int) $user->id);
|
||||
if (! empty($filters['class_section_id'])) {
|
||||
$sid = (int) $filters['class_section_id'];
|
||||
if (! in_array($sid, $parentSectionIds, true)) {
|
||||
$query->whereRaw('1 = 0');
|
||||
}
|
||||
} elseif ($parentSectionIds !== []) {
|
||||
$query->whereIn('class_progress_reports.class_section_id', $parentSectionIds);
|
||||
} else {
|
||||
$query->whereRaw('1 = 0');
|
||||
}
|
||||
} else {
|
||||
$query->where(function ($q) use ($user, $relaxedSectionIds) {
|
||||
$q->where('teacher_id', (int) $user->id);
|
||||
if ($relaxedSectionIds !== []) {
|
||||
$q->orWhereIn('class_section_id', $relaxedSectionIds);
|
||||
$relaxedSectionIds = TeacherClass::distinctSectionIdsForTeacher((int) $user->id);
|
||||
|
||||
if (! empty($filters['class_section_id'])) {
|
||||
$sid = (int) $filters['class_section_id'];
|
||||
// Section filter: if this user has ever taught the section, show all rows for that section
|
||||
// (legacy DB often has `class_progress_reports` without matching current-year `teacher_class`).
|
||||
if (! in_array($sid, $relaxedSectionIds, true)) {
|
||||
$query->where('teacher_id', (int) $user->id);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
$query->where(function ($q) use ($user, $relaxedSectionIds) {
|
||||
$q->where('teacher_id', (int) $user->id);
|
||||
if ($relaxedSectionIds !== []) {
|
||||
$q->orWhereIn('class_section_id', $relaxedSectionIds);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
} elseif (! empty($filters['teacher_id'])) {
|
||||
$query->where('teacher_id', (int) $filters['teacher_id']);
|
||||
@@ -62,12 +78,50 @@ class ClassProgressQueryService
|
||||
$query->where('status', (string) $filters['status']);
|
||||
}
|
||||
|
||||
if (! empty($filters['school_year']) && Schema::hasColumn('class_progress_reports', 'school_year')) {
|
||||
$query->where('school_year', (string) $filters['school_year']);
|
||||
$schoolYear = $this->resolveSchoolYearFilter($filters);
|
||||
$schoolYearRange = $this->schoolYearDateRange($schoolYear);
|
||||
if ($schoolYear !== '' && Schema::hasColumn('class_progress_reports', 'school_year')) {
|
||||
$query->where(function ($yearQuery) use ($schoolYear, $schoolYearRange) {
|
||||
$yearQuery->where('class_progress_reports.school_year', $schoolYear)
|
||||
->orWhere(function ($legacyQuery) use ($schoolYear, $schoolYearRange) {
|
||||
$legacyQuery->where(function ($blankQuery) {
|
||||
$blankQuery->whereNull('class_progress_reports.school_year')
|
||||
->orWhere('class_progress_reports.school_year', '');
|
||||
});
|
||||
|
||||
if ($schoolYearRange !== null) {
|
||||
$legacyQuery->whereBetween('class_progress_reports.week_start', [
|
||||
$schoolYearRange['start'],
|
||||
$schoolYearRange['end'],
|
||||
]);
|
||||
} else {
|
||||
$legacyQuery->whereExists(function ($sectionQuery) use ($schoolYear) {
|
||||
$sectionQuery->select(DB::raw(1))
|
||||
->from('classSection as progress_filter_cs')
|
||||
->whereColumn('progress_filter_cs.class_section_id', 'class_progress_reports.class_section_id')
|
||||
->where('progress_filter_cs.school_year', $schoolYear);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (! empty($filters['semester']) && Schema::hasColumn('class_progress_reports', 'semester')) {
|
||||
$query->where('semester', (string) $filters['semester']);
|
||||
$semester = (string) $filters['semester'];
|
||||
$query->where(function ($semesterQuery) use ($semester) {
|
||||
$semesterQuery->where('class_progress_reports.semester', $semester)
|
||||
->orWhere(function ($legacyQuery) use ($semester) {
|
||||
$legacyQuery->where(function ($blankQuery) {
|
||||
$blankQuery->whereNull('class_progress_reports.semester')
|
||||
->orWhere('class_progress_reports.semester', '');
|
||||
})->whereExists(function ($sectionQuery) use ($semester) {
|
||||
$sectionQuery->select(DB::raw(1))
|
||||
->from('classSection as progress_filter_cs')
|
||||
->whereColumn('progress_filter_cs.class_section_id', 'class_progress_reports.class_section_id')
|
||||
->where('progress_filter_cs.semester', $semester);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (! empty($filters['week_start'])) {
|
||||
@@ -157,6 +211,60 @@ class ClassProgressQueryService
|
||||
return $this->schoolYears->options($schoolYear, $semester);
|
||||
}
|
||||
|
||||
private function resolveSchoolYearFilter(array $filters): string
|
||||
{
|
||||
$schoolYear = trim((string) ($filters['school_year'] ?? ''));
|
||||
if ($schoolYear !== '') {
|
||||
return $schoolYear;
|
||||
}
|
||||
|
||||
$schoolYearId = (int) ($filters['school_year_id'] ?? 0);
|
||||
if ($schoolYearId <= 0 || ! Schema::hasTable('school_years')) {
|
||||
return '';
|
||||
}
|
||||
|
||||
try {
|
||||
return trim((string) DB::table('school_years')->where('id', $schoolYearId)->value('name'));
|
||||
} catch (\Throwable) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{start:string,end:string}|null
|
||||
*/
|
||||
private function schoolYearDateRange(string $schoolYear): ?array
|
||||
{
|
||||
$schoolYear = trim($schoolYear);
|
||||
if ($schoolYear === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Schema::hasTable('school_years')) {
|
||||
try {
|
||||
$row = DB::table('school_years')
|
||||
->where('name', $schoolYear)
|
||||
->first(['start_date', 'end_date']);
|
||||
if ($row && $row->start_date && $row->end_date) {
|
||||
return [
|
||||
'start' => substr((string) $row->start_date, 0, 10),
|
||||
'end' => substr((string) $row->end_date, 0, 10),
|
||||
];
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
}
|
||||
}
|
||||
|
||||
if (preg_match('/^(\d{4})-(\d{4})$/', $schoolYear, $matches) === 1) {
|
||||
return [
|
||||
'start' => $matches[1].'-08-01',
|
||||
'end' => $matches[2].'-07-31',
|
||||
];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function statusLabel(?string $status): string
|
||||
{
|
||||
$options = (array) config('progress.status_options', []);
|
||||
@@ -180,6 +288,35 @@ class ClassProgressQueryService
|
||||
return false;
|
||||
}
|
||||
|
||||
private function isParent(User $user): bool
|
||||
{
|
||||
return $user->roles()
|
||||
->pluck('roles.name')
|
||||
->map(fn ($name) => strtolower((string) $name))
|
||||
->contains('parent');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<int>
|
||||
*/
|
||||
private function parentSectionIds(int $parentId): array
|
||||
{
|
||||
if ($parentId <= 0 || ! Schema::hasTable('enrollments')) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return DB::table('enrollments')
|
||||
->where('parent_id', $parentId)
|
||||
->where('is_withdrawn', 0)
|
||||
->whereNotNull('class_section_id')
|
||||
->distinct()
|
||||
->pluck('class_section_id')
|
||||
->map(fn ($id) => (int) $id)
|
||||
->filter(fn ($id) => $id > 0)
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* legacy `ClassProgressController::resolveAssignedTeacherIds`.
|
||||
*
|
||||
|
||||
@@ -18,6 +18,7 @@ class ParentAttendanceService
|
||||
->join('students', 'students.id', '=', 'attendance_data.student_id')
|
||||
->where('students.parent_id', $parentId)
|
||||
->where('attendance_data.school_year', $selectedYear)
|
||||
->whereRaw("LOWER(TRIM(attendance_data.status)) IN ('absent', 'late')")
|
||||
->orderBy('attendance_data.date', 'desc')
|
||||
->get()
|
||||
->map(fn ($row) => (array) $row)
|
||||
|
||||
@@ -9,6 +9,7 @@ use App\Services\ClassProgress\ClassProgressQueryService;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
/**
|
||||
* legacy {@see ParentProgressController} read-side logic.
|
||||
@@ -101,24 +102,104 @@ class ParentProgressQueryService
|
||||
*
|
||||
* @param list<int> $sectionIds
|
||||
*/
|
||||
public function reportsForSections(array $sectionIds): Collection
|
||||
public function reportsForSections(array $sectionIds, ?string $schoolYear = null, ?string $semester = null): Collection
|
||||
{
|
||||
$sectionIds = array_values(array_filter(array_map('intval', $sectionIds)));
|
||||
if ($sectionIds === []) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
return ClassProgressReport::query()
|
||||
$attachmentMap = [];
|
||||
$query = ClassProgressReport::query()
|
||||
->select('class_progress_reports.*')
|
||||
->addSelect([
|
||||
'cs.class_section_name',
|
||||
DB::raw('CONCAT(IFNULL(u.firstname, ""), " ", IFNULL(u.lastname, "")) AS teacher_name'),
|
||||
'u.firstname as teacher_firstname',
|
||||
'u.lastname as teacher_lastname',
|
||||
])
|
||||
->leftJoin('classSection as cs', 'cs.class_section_id', '=', 'class_progress_reports.class_section_id')
|
||||
->leftJoin('users as u', 'u.id', '=', 'class_progress_reports.teacher_id')
|
||||
->whereIn('class_progress_reports.class_section_id', $sectionIds)
|
||||
->orderByDesc('class_progress_reports.week_start')
|
||||
->get();
|
||||
->whereIn('class_progress_reports.class_section_id', $sectionIds);
|
||||
|
||||
$schoolYear = trim((string) $schoolYear);
|
||||
if ($schoolYear !== '' && Schema::hasColumn('class_progress_reports', 'school_year')) {
|
||||
$schoolYearRange = $this->schoolYearDateRange($schoolYear);
|
||||
$query->where(function ($yearQuery) use ($schoolYear, $schoolYearRange) {
|
||||
$yearQuery->where('class_progress_reports.school_year', $schoolYear)
|
||||
->orWhere(function ($legacyQuery) use ($schoolYear, $schoolYearRange) {
|
||||
$legacyQuery->where(function ($blankQuery) {
|
||||
$blankQuery->whereNull('class_progress_reports.school_year')
|
||||
->orWhere('class_progress_reports.school_year', '');
|
||||
});
|
||||
|
||||
if ($schoolYearRange !== null) {
|
||||
$legacyQuery->whereBetween('class_progress_reports.week_start', [
|
||||
$schoolYearRange['start'],
|
||||
$schoolYearRange['end'],
|
||||
]);
|
||||
} else {
|
||||
$legacyQuery->where('cs.school_year', $schoolYear);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
$semester = trim((string) $semester);
|
||||
if ($semester !== '' && Schema::hasColumn('class_progress_reports', 'semester')) {
|
||||
$query->where(function ($semesterQuery) use ($semester) {
|
||||
$semesterQuery->where('class_progress_reports.semester', $semester)
|
||||
->orWhere(function ($legacyQuery) use ($semester) {
|
||||
$legacyQuery->where(function ($blankQuery) {
|
||||
$blankQuery->whereNull('class_progress_reports.semester')
|
||||
->orWhere('class_progress_reports.semester', '');
|
||||
})->where('cs.semester', $semester);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
$rows = $query->orderByDesc('class_progress_reports.week_start')->get();
|
||||
$attachmentMap = $this->classProgressQuery->attachmentMap($rows->pluck('id')->all());
|
||||
|
||||
return $rows
|
||||
->each(function (ClassProgressReport $row) use ($attachmentMap) {
|
||||
$row->setAttribute('teacher_name', trim((string) ($row->teacher_firstname ?? '').' '.(string) ($row->teacher_lastname ?? '')));
|
||||
$row->setAttribute('attachments', $attachmentMap[$row->id] ?? []);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{start:string,end:string}|null
|
||||
*/
|
||||
private function schoolYearDateRange(string $schoolYear): ?array
|
||||
{
|
||||
$schoolYear = trim($schoolYear);
|
||||
if ($schoolYear === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Schema::hasTable('school_years')) {
|
||||
try {
|
||||
$row = DB::table('school_years')
|
||||
->where('name', $schoolYear)
|
||||
->first(['start_date', 'end_date']);
|
||||
if ($row && $row->start_date && $row->end_date) {
|
||||
return [
|
||||
'start' => substr((string) $row->start_date, 0, 10),
|
||||
'end' => substr((string) $row->end_date, 0, 10),
|
||||
];
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
}
|
||||
}
|
||||
|
||||
if (preg_match('/^(\d{4})-(\d{4})$/', $schoolYear, $matches) === 1) {
|
||||
return [
|
||||
'start' => $matches[1].'-08-01',
|
||||
'end' => $matches[2].'-07-31',
|
||||
];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,7 +227,12 @@ class ParentProgressQueryService
|
||||
|
||||
$key = $weekStart.':'.$sectionId;
|
||||
if (! isset($reportGroups[$key])) {
|
||||
$reportId = (int) $row->id;
|
||||
$reportGroups[$key] = [
|
||||
'id' => $reportId,
|
||||
'report_id' => $reportId,
|
||||
'view_url' => url('/api/v1/class-progress/'.$reportId),
|
||||
'parent_view_url' => url('/api/v1/parents/progress/'.$reportId),
|
||||
'week_start' => $weekStart,
|
||||
'week_end' => $row->week_end instanceof CarbonInterface
|
||||
? $row->week_end->format('Y-m-d')
|
||||
@@ -154,6 +240,10 @@ class ParentProgressQueryService
|
||||
'class_section_name' => $row->class_section_name ?? '',
|
||||
'class_section_id' => $sectionId,
|
||||
'reports' => [],
|
||||
'weekly_reports' => [],
|
||||
'reports_array' => [],
|
||||
'report_list' => [],
|
||||
'subjects' => [],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -162,7 +252,12 @@ class ParentProgressQueryService
|
||||
continue;
|
||||
}
|
||||
|
||||
$reportGroups[$key]['reports'][$subject] = (new ClassProgressReportResource($row))->toArray(request());
|
||||
$report = (new ClassProgressReportResource($row))->toArray(request());
|
||||
$reportGroups[$key]['reports'][$subject] = $report;
|
||||
$reportGroups[$key]['weekly_reports'][] = $report;
|
||||
$reportGroups[$key]['reports_array'][] = $report;
|
||||
$reportGroups[$key]['report_list'][] = $report;
|
||||
$reportGroups[$key]['subjects'][] = $subject;
|
||||
}
|
||||
|
||||
return $reportGroups;
|
||||
@@ -175,13 +270,21 @@ class ParentProgressQueryService
|
||||
*/
|
||||
public function weeklyReportsForSectionWeek(int $classSectionId, string $weekStartYmd): array
|
||||
{
|
||||
return ClassProgressReport::query()
|
||||
$rows = ClassProgressReport::query()
|
||||
->with(['classSection', 'teacher'])
|
||||
->where('class_section_id', $classSectionId)
|
||||
->whereDate('week_start', $weekStartYmd)
|
||||
->orderBy('subject')
|
||||
->get()
|
||||
->all();
|
||||
|
||||
$attachments = $this->attachmentMapForReportIds(array_map(fn (ClassProgressReport $row) => (int) $row->id, $rows));
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$row->setAttribute('attachments', $attachments[$row->id] ?? []);
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,10 +27,11 @@ class ReportCardService
|
||||
$this->semester = trim((string) (Configuration::getConfig('semester') ?? ''));
|
||||
}
|
||||
|
||||
public function meta(?string $schoolYear, ?string $semester): array
|
||||
public function meta(?string $schoolYear, ?string $semester, ?int $parentId = null): array
|
||||
{
|
||||
$year = trim((string) ($schoolYear ?? $this->schoolYear));
|
||||
$sem = trim((string) ($semester ?? $this->semester));
|
||||
$parentId = (int) ($parentId ?? 0);
|
||||
|
||||
if ($year === '') {
|
||||
$yr = DB::table('classSection')->select('school_year')->orderBy('school_year', 'DESC')->first();
|
||||
@@ -117,6 +118,9 @@ class ReportCardService
|
||||
->leftJoin('classSection as cs', 'cs.class_section_id', '=', 'sc.class_section_id')
|
||||
->where('s.is_active', 1)
|
||||
->where('ss.school_year', $year);
|
||||
if ($parentId > 0) {
|
||||
$builder->where('s.parent_id', $parentId);
|
||||
}
|
||||
if ($sem !== '') {
|
||||
$this->applySemesterFilter($builder, $sem, 'ss.semester');
|
||||
}
|
||||
@@ -141,6 +145,9 @@ class ReportCardService
|
||||
if ($year !== '') {
|
||||
$fallback->where('sc.school_year', $year);
|
||||
}
|
||||
if ($parentId > 0) {
|
||||
$fallback->where('students.parent_id', $parentId);
|
||||
}
|
||||
$students = $fallback
|
||||
->where('students.is_active', 1)
|
||||
->groupBy('students.id', 'students.firstname', 'students.lastname')
|
||||
@@ -160,6 +167,9 @@ class ReportCardService
|
||||
->join('students as s', 's.id', '=', 'sc.student_id')
|
||||
->where('s.is_active', 1)
|
||||
->where('sc.school_year', $year);
|
||||
if ($parentId > 0) {
|
||||
$builder->where('s.parent_id', $parentId);
|
||||
}
|
||||
$classSections = $builder
|
||||
->groupBy('cs.id', 'cs.class_section_id', 'cs.class_section_name', 'cs.school_year')
|
||||
->orderBy('cs.class_section_name', 'ASC')
|
||||
@@ -174,6 +184,7 @@ class ReportCardService
|
||||
->join('students as s', 's.id', '=', 'sc.student_id')
|
||||
->where('s.is_active', 1)
|
||||
->where('sc.school_year', $year)
|
||||
->when($parentId > 0, fn ($query) => $query->where('s.parent_id', $parentId))
|
||||
->groupBy('cs.id', 'cs.class_section_id', 'cs.class_section_name', 'cs.school_year')
|
||||
->orderBy('cs.class_section_name', 'ASC')
|
||||
->get()
|
||||
@@ -571,6 +582,10 @@ class ReportCardService
|
||||
$data['report_date'] = $reportDate['iso'];
|
||||
$data['report_date_display'] = $reportDate['display'];
|
||||
|
||||
if (! $this->ensureFpdfAvailable()) {
|
||||
return ['ok' => false, 'message' => 'Report card PDF renderer is unavailable.'];
|
||||
}
|
||||
|
||||
$pdf = new \FPDF('P', 'mm', 'Letter');
|
||||
$this->formatReportPDF($pdf, $data);
|
||||
|
||||
@@ -613,6 +628,10 @@ class ReportCardService
|
||||
return ['ok' => false, 'message' => 'No students found for this class section.'];
|
||||
}
|
||||
|
||||
if (! $this->ensureFpdfAvailable()) {
|
||||
return ['ok' => false, 'message' => 'Report card PDF renderer is unavailable.'];
|
||||
}
|
||||
|
||||
$pdf = new \FPDF('P', 'mm', 'Letter');
|
||||
|
||||
foreach ($scores as $score) {
|
||||
@@ -749,6 +768,22 @@ class ReportCardService
|
||||
];
|
||||
}
|
||||
|
||||
private function ensureFpdfAvailable(): bool
|
||||
{
|
||||
if (class_exists('FPDF', false)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$path = base_path('app/ThirdParty/fpdf/fpdf.php');
|
||||
if (! is_file($path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
require_once $path;
|
||||
|
||||
return class_exists('FPDF', false);
|
||||
}
|
||||
|
||||
private function resolveAnchorSunday(): string
|
||||
{
|
||||
$tzName = (string) (config('School')->attendance['timezone'] ?? config('app.timezone'));
|
||||
|
||||
@@ -151,6 +151,61 @@ class SchoolYearClosureService
|
||||
];
|
||||
}
|
||||
|
||||
public function closingReport(int $id): array
|
||||
{
|
||||
$year = $this->findOrFail($id);
|
||||
$audit = $this->latestClosureAudit((int) $year->id);
|
||||
$metadata = is_array($audit?->metadata) ? $audit->metadata : [];
|
||||
|
||||
$newYearName = trim((string) ($metadata['new_school_year'] ?? ''));
|
||||
if ($newYearName === '') {
|
||||
$newYearName = $this->resolveClosedTargetYearName($year->name);
|
||||
}
|
||||
|
||||
$newYear = $newYearName !== ''
|
||||
? SchoolYear::query()->where('name', $newYearName)->first()
|
||||
: null;
|
||||
|
||||
$promotionCounts = is_array($metadata['promotion_counts'] ?? null)
|
||||
? $metadata['promotion_counts']
|
||||
: [];
|
||||
$balanceCounts = is_array($metadata['balance_counts'] ?? null)
|
||||
? $metadata['balance_counts']
|
||||
: [];
|
||||
$transferTotals = $this->transferTotalsForClosedYear($year->name, $newYearName !== '' ? $newYearName : null);
|
||||
|
||||
$warnings = [];
|
||||
if (! $audit) {
|
||||
$warnings[] = 'No closure audit log was found for this school year. Totals were reconstructed from current records where possible.';
|
||||
}
|
||||
if ($year->status !== SchoolYear::STATUS_CLOSED) {
|
||||
$warnings[] = sprintf('School year %s is currently %s, so this is a live preview rather than a final closure report.', $year->name, $year->status);
|
||||
}
|
||||
if ($newYearName !== '' && ! $newYear) {
|
||||
$warnings[] = sprintf('The closure target school year %s was referenced but no matching school_years row was found.', $newYearName);
|
||||
}
|
||||
|
||||
return [
|
||||
'old_school_year' => $this->presentSchoolYear($year),
|
||||
'new_school_year' => $this->presentSchoolYear($newYear),
|
||||
'closed_by' => $this->resolveUserName((int) ($year->closed_by ?? $audit?->user_id ?? 0)),
|
||||
'closed_at' => optional($year->closed_at ?? $audit?->created_at)->toDateTimeString(),
|
||||
'audit_reference' => $audit ? sprintf('audit_logs:%d', (int) $audit->id) : null,
|
||||
'totals' => [
|
||||
'students_promoted' => (int) ($promotionCounts['promote'] ?? 0),
|
||||
'students_repeated' => (int) ($promotionCounts['repeat'] ?? 0),
|
||||
'students_graduated' => (int) ($promotionCounts['graduate'] ?? 0),
|
||||
'students_transferred' => (int) ($promotionCounts['transfer'] ?? 0),
|
||||
'students_withdrawn' => (int) ($promotionCounts['withdraw'] ?? 0),
|
||||
'parents_with_unpaid_balances' => (int) ($balanceCounts['transferred_parents'] ?? $transferTotals['parents_with_unpaid_balances']),
|
||||
'total_unpaid_balance_transferred' => round((float) ($balanceCounts['transferred_amount'] ?? $transferTotals['total_unpaid_balance_transferred']), 2),
|
||||
'parents_with_credit_balances' => (int) ($balanceCounts['credit_parents'] ?? $transferTotals['parents_with_credit_balances']),
|
||||
'total_credit_transferred' => round((float) ($balanceCounts['credit_amount'] ?? $transferTotals['total_credit_transferred']), 2),
|
||||
],
|
||||
'warnings' => $warnings,
|
||||
];
|
||||
}
|
||||
|
||||
public function validateClose(int $id, array $payload): array
|
||||
{
|
||||
$year = $this->findOrFail($id);
|
||||
@@ -1016,6 +1071,103 @@ class SchoolYearClosureService
|
||||
->all();
|
||||
}
|
||||
|
||||
private function latestClosureAudit(int $schoolYearId): ?AuditLog
|
||||
{
|
||||
if (! Schema::hasTable('audit_logs')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return AuditLog::query()
|
||||
->where('action', 'school_year_closed')
|
||||
->where(function ($query) use ($schoolYearId) {
|
||||
$query->where('record_id', $schoolYearId)
|
||||
->orWhere(function ($nested) use ($schoolYearId) {
|
||||
$nested->where('table_name', 'school_years')
|
||||
->where('record_id', $schoolYearId);
|
||||
});
|
||||
})
|
||||
->orderByDesc('created_at')
|
||||
->orderByDesc('id')
|
||||
->first();
|
||||
}
|
||||
|
||||
private function resolveClosedTargetYearName(string $fromSchoolYear): string
|
||||
{
|
||||
if (Schema::hasTable('parent_balance_transfers')) {
|
||||
$target = DB::table('parent_balance_transfers')
|
||||
->where('from_school_year', $fromSchoolYear)
|
||||
->whereNotNull('to_school_year')
|
||||
->orderByDesc('created_at')
|
||||
->value('to_school_year');
|
||||
|
||||
if (is_string($target) && trim($target) !== '') {
|
||||
return trim($target);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->resolver->suggestNextName($fromSchoolYear) ?? '';
|
||||
}
|
||||
|
||||
private function transferTotalsForClosedYear(string $fromSchoolYear, ?string $toSchoolYear = null): array
|
||||
{
|
||||
$totals = [
|
||||
'parents_with_unpaid_balances' => 0,
|
||||
'total_unpaid_balance_transferred' => 0.0,
|
||||
'parents_with_credit_balances' => 0,
|
||||
'total_credit_transferred' => 0.0,
|
||||
];
|
||||
|
||||
if (! Schema::hasTable('parent_balance_transfers')) {
|
||||
return $totals;
|
||||
}
|
||||
|
||||
$rows = DB::table('parent_balance_transfers')
|
||||
->where('from_school_year', $fromSchoolYear)
|
||||
->when($toSchoolYear !== null && trim($toSchoolYear) !== '', fn ($query) => $query->where('to_school_year', $toSchoolYear))
|
||||
->get(['amount']);
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$amount = round((float) ($row->amount ?? 0), 2);
|
||||
if ($amount > 0.01) {
|
||||
$totals['parents_with_unpaid_balances']++;
|
||||
$totals['total_unpaid_balance_transferred'] += $amount;
|
||||
} elseif ($amount < -0.01) {
|
||||
$totals['parents_with_credit_balances']++;
|
||||
$totals['total_credit_transferred'] += abs($amount);
|
||||
}
|
||||
}
|
||||
|
||||
$totals['total_unpaid_balance_transferred'] = round($totals['total_unpaid_balance_transferred'], 2);
|
||||
$totals['total_credit_transferred'] = round($totals['total_credit_transferred'], 2);
|
||||
|
||||
return $totals;
|
||||
}
|
||||
|
||||
private function resolveUserName(int $userId): string|int|null
|
||||
{
|
||||
if ($userId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! Schema::hasTable('users')) {
|
||||
return $userId;
|
||||
}
|
||||
|
||||
$user = DB::table('users')->where('id', $userId)->first();
|
||||
if (! $user) {
|
||||
return $userId;
|
||||
}
|
||||
|
||||
$name = trim((string) ($user->firstname ?? '').' '.(string) ($user->lastname ?? ''));
|
||||
if ($name !== '') {
|
||||
return $name;
|
||||
}
|
||||
|
||||
$email = trim((string) ($user->email ?? ''));
|
||||
|
||||
return $email !== '' ? $email : $userId;
|
||||
}
|
||||
|
||||
private function presentSchoolYear(?SchoolYear $year): ?array
|
||||
{
|
||||
if (! $year) {
|
||||
|
||||
@@ -36,7 +36,7 @@ class SchoolCalendarMutationService
|
||||
|
||||
public function update(CalendarEvent $event, array $payload, array $targets = []): array
|
||||
{
|
||||
$data = $this->normalizePayload($payload);
|
||||
$data = $this->normalizePayload($payload, true);
|
||||
|
||||
try {
|
||||
DB::transaction(function () use ($event, $data) {
|
||||
@@ -69,8 +69,41 @@ class SchoolCalendarMutationService
|
||||
}
|
||||
}
|
||||
|
||||
private function normalizePayload(array $payload): array
|
||||
private function normalizePayload(array $payload, bool $partial = false): array
|
||||
{
|
||||
if ($partial) {
|
||||
$data = [];
|
||||
foreach ([
|
||||
'title',
|
||||
'description',
|
||||
'event_type',
|
||||
'date',
|
||||
'notify_parent',
|
||||
'notify_teacher',
|
||||
'notify_admin',
|
||||
'no_school',
|
||||
'school_year',
|
||||
'semester',
|
||||
] as $key) {
|
||||
if (! array_key_exists($key, $payload)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (in_array($key, ['notify_parent', 'notify_teacher', 'notify_admin', 'no_school'], true)) {
|
||||
$data[$key] = ! empty($payload[$key]) ? 1 : 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
$data[$key] = $payload[$key];
|
||||
}
|
||||
|
||||
if (! CalendarEvent::supportsEventType()) {
|
||||
unset($data['event_type']);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'title' => (string) ($payload['title'] ?? ''),
|
||||
'description' => $payload['description'] ?? null,
|
||||
|
||||
@@ -12,22 +12,38 @@ class WhatsappContextService
|
||||
|
||||
public function schoolYear(?string $override = null): string
|
||||
{
|
||||
$override = trim((string) $override);
|
||||
if ($override !== '') {
|
||||
return $override;
|
||||
foreach ([
|
||||
$override,
|
||||
request()->query('school_year'),
|
||||
request()->header('X-School-Year'),
|
||||
request()->header('X-Selected-School-Year'),
|
||||
Configuration::getConfig('school_year'),
|
||||
] as $candidate) {
|
||||
$value = trim((string) ($candidate ?? ''));
|
||||
if ($value !== '') {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
return trim((string) (Configuration::getConfig('school_year') ?? ''));
|
||||
return '';
|
||||
}
|
||||
|
||||
public function semester(?string $override = null): string
|
||||
{
|
||||
$override = trim((string) $override);
|
||||
if ($override !== '') {
|
||||
return $override;
|
||||
foreach ([
|
||||
$override,
|
||||
request()->query('semester'),
|
||||
request()->header('X-Semester'),
|
||||
request()->header('X-Selected-Semester'),
|
||||
Configuration::getConfig('semester'),
|
||||
] as $candidate) {
|
||||
$value = trim((string) ($candidate ?? ''));
|
||||
if ($value !== '') {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
return trim((string) (Configuration::getConfig('semester') ?? ''));
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user