where('status', CollectionTeam::STATUS_ACTIVE) ->where('driver_id', $driverId) ->when($excludeTeamId, fn ($q, $id) => $q->where('id', '!=', $id)) ->first(); if ($existing) { $conflicts[] = "Driver already on team {$existing->name}"; } } if ($scannerId) { $existing = CollectionTeam::query() ->where('status', CollectionTeam::STATUS_ACTIVE) ->where('scanner_id', $scannerId) ->when($excludeTeamId, fn ($q, $id) => $q->where('id', '!=', $id)) ->first(); if ($existing) { $conflicts[] = "Scanner already on team {$existing->name}"; } } if ($truckId) { $existing = CollectionTeam::query() ->where('status', CollectionTeam::STATUS_ACTIVE) ->where('truck_id', $truckId) ->when($excludeTeamId, fn ($q, $id) => $q->where('id', '!=', $id)) ->first(); if ($existing) { $conflicts[] = "Truck already on team {$existing->name}"; } $truck = Truck::find($truckId); if ($truck && $truck->status !== Truck::STATUS_ACTIVE) { $conflicts[] = "Truck status is {$truck->status}, not active"; } } if (! empty($helperIds)) { $today = now()->toDateString(); $assigned = TeamMember::query() ->whereIn('user_id', $helperIds) ->where('role_in_team', TeamMember::ROLE_HELPER) ->whereDate('assigned_from', '<=', $today) ->where(function ($q) use ($today) { $q->whereNull('assigned_until')->orWhereDate('assigned_until', '>=', $today); }) ->when($excludeTeamId, fn ($q, $id) => $q->where('team_id', '!=', $id)) ->with('user:id,first_name,last_name') ->get(); foreach ($assigned as $tm) { $conflicts[] = "Helper {$tm->user?->first_name} {$tm->user?->last_name} already assigned to another team"; } } return $conflicts; } }