feat: endpoint rekap kehadiran wali (per tanggal + ringkasan bulan)
This commit is contained in:
@@ -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.
|
* Tautkan anak tambahan (kakak/adik) ke akun wali.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ Route::middleware('auth:sanctum')->prefix('parent')->group(function () {
|
|||||||
Route::get('/students/{student}/violations', [ParentStudentController::class, 'violations'])->name('api.parent.students.violations');
|
Route::get('/students/{student}/violations', [ParentStudentController::class, 'violations'])->name('api.parent.students.violations');
|
||||||
Route::get('/students/{student}/sp-letters', [ParentStudentController::class, 'spLetters'])->name('api.parent.students.sp-letters');
|
Route::get('/students/{student}/sp-letters', [ParentStudentController::class, 'spLetters'])->name('api.parent.students.sp-letters');
|
||||||
Route::get('/students/{student}/schedule', [ParentStudentController::class, 'schedule'])->name('api.parent.students.schedule');
|
Route::get('/students/{student}/schedule', [ParentStudentController::class, 'schedule'])->name('api.parent.students.schedule');
|
||||||
|
Route::get('/students/{student}/attendance', [ParentStudentController::class, 'attendance'])->name('api.parent.students.attendance');
|
||||||
|
|
||||||
// Notifikasi & perangkat
|
// Notifikasi & perangkat
|
||||||
Route::get('/notifications', [ParentNotificationController::class, 'index'])->name('api.parent.notifications');
|
Route::get('/notifications', [ParentNotificationController::class, 'index'])->name('api.parent.notifications');
|
||||||
|
|||||||
Reference in New Issue
Block a user