diff --git a/app/Http/Controllers/Admin/SyncController.php b/app/Http/Controllers/Admin/SyncController.php index 04da4a0..82b0f8a 100644 --- a/app/Http/Controllers/Admin/SyncController.php +++ b/app/Http/Controllers/Admin/SyncController.php @@ -76,7 +76,11 @@ class SyncController extends Controller Setting::setValue('kesiswaan_base_url', $validated['base_url'], 'integration', 'URL Database Kesiswaan'); Setting::setValue('kesiswaan_token', $validated['token'], 'integration', 'Token sinkronisasi'); + $deactivated = $result['students_deactivated'] ?? 0; $msg = 'Sinkron berhasil! ' . $result['students_created'] . ' siswa baru, ' . $result['students_updated'] . ' diperbarui, ' . $result['classes_created'] . ' kelas baru.'; + if ($deactivated > 0) { + $msg .= ' ' . $deactivated . ' siswa dinonaktifkan (tidak aktif tahun ajaran baru).'; + } if (!empty($result['errors'])) { $skipped = count($result['errors']); diff --git a/app/Services/StudentSyncService.php b/app/Services/StudentSyncService.php index 806bf52..a4f1c87 100644 --- a/app/Services/StudentSyncService.php +++ b/app/Services/StudentSyncService.php @@ -34,7 +34,9 @@ class StudentSyncService $payload = $response->json(); $items = Arr::get($payload, 'data', []); - DB::transaction(function () use ($items, &$results) { + \$syncedIds = []; + + DB::transaction(function () use ($items, &$results, &$syncedIds) { foreach ($items as $item) { // Only sync active students $status = $item['status'] ?? 'active'; @@ -107,9 +109,17 @@ class StudentSyncService if ($student->wasRecentlyCreated) $results['students_created']++; else $results['students_updated']++; + + $syncedIds[] = $student->id; } }); + // Deactivate students not in this sync (graduated/dropped out) + \$deactivated = Student::where('is_active', true) + ->whereNotIn('id', $syncedIds) + ->update(['is_active' => false]); + $results['students_deactivated'] = $deactivated; + return $results; } catch (\Exception $e) { Log::error('Student sync failed: ' . $e->getMessage());