user(); $role = $user->role ?? ''; $stats = [ 'total_trips' => 0, 'total_volume_kg' => 0, 'total_scans' => 0, ]; if ($role === 'driver') { // Find teams where user is driver $teamIds = CollectionTeam::where('driver_id', $user->id)->pluck('id'); $stats['total_trips'] = Trip::whereIn('team_id', $teamIds) ->where('status', Trip::STATUS_COMPLETED) ->count(); $stats['total_volume_kg'] = (int) Trip::whereIn('team_id', $teamIds) ->where('status', Trip::STATUS_COMPLETED) ->sum('total_load_kg'); } elseif ($role === 'scanner') { // Scanner only cares about QR scans and volume $stats['total_scans'] = CollectionLog::where('scanned_by_user_id', $user->id) ->where('verification_status', CollectionLog::STATUS_VALID) ->count(); $stats['total_volume_kg'] = (int) CollectionLog::where('scanned_by_user_id', $user->id) ->where('verification_status', CollectionLog::STATUS_VALID) ->sum('weight_kg'); } elseif ($role === 'helper') { // Helper cares about QR scans (if they scan) and total trips $stats['total_scans'] = CollectionLog::where('scanned_by_user_id', $user->id) ->where('verification_status', CollectionLog::STATUS_VALID) ->count(); $teamIds = TeamMember::where('user_id', $user->id) ->where('role_in_team', TeamMember::ROLE_HELPER) ->pluck('team_id'); $stats['total_trips'] = Trip::whereIn('team_id', $teamIds) ->where('status', Trip::STATUS_COMPLETED) ->count(); } return $this->ok($stats); } }