feat: endpoint rekap kehadiran wali (per tanggal + ringkasan bulan)

This commit is contained in:
Novy San87
2026-08-06 11:06:28 +07:00
parent 8d10ca3edf
commit 3c6f35f23c
2 changed files with 65 additions and 0 deletions
@@ -84,6 +84,70 @@ class ParentStudentController extends Controller
}
}
/**
* Rekap kehadiran putra/putri (bulan berjalan + ringkasan).
*/
public function attendance(Request $request, Student $student): JsonResponse
{
$hasLink = ParentStudent::where('user_id', $request->user()->id)
->where('student_id', $student->id)
->where('status', 'active')
->exists();
abort_unless($hasLink, 403, 'Putra/putri belum tertaut aktif.');
$today = now()->toDateString();
$monthStart = now()->startOfMonth()->toDateString();
$first = $student->attendances()->orderBy('date')->value('date');
$from = $first && $first > $monthStart ? $first : $monthStart;
$rows = $student->attendances()
->whereBetween('date', [$from, $today])
->orderBy('date')
->orderBy('lesson_hour')
->get(['date', 'status']);
$statuses = ['hadir', 'izin', 'sakit', 'alpha'];
$byDate = $rows->groupBy('date');
$records = $byDate
->map(function ($items, $date) use ($statuses) {
$counts = [];
foreach ($statuses as $s) {
$counts[$s] = $items->where('status', $s)->count();
}
$primary = null;
foreach ($statuses as $s) {
if ($counts[$s] > 0) {
$primary = $s;
break;
}
}
return [
'date' => \Illuminate\Support\Str::substr((string) $date, 0, 10),
'primary' => $primary,
'statuses' => $counts,
];
})
->values()
->sortByDesc('date')
->values();
$summary = [];
foreach ($statuses as $s) {
$summary[$s] = $rows->where('status', $s)->count();
}
$summary['days'] = $byDate->count();
return response()->json([
'from' => $from,
'month' => now()->format('Y-m'),
'summary' => $summary,
'records' => $records,
]);
}
/**
* Tautkan anak tambahan (kakak/adik) ke akun wali.
*/