where('code', $roleCode)->first(); if ($position === null) { return false; } $quota = PositionQuota::query() ->where('election_id', $pusatMengundi->election_id) ->where('pusat_mengundi_id', $pusatMengundi->id) ->whereNull('saluran_mengundi_id') ->where('position_id', $position->id) ->value('quota'); if ($quota === null) { return false; } $used = StaffAssignment::query() ->where('election_id', $pusatMengundi->election_id) ->where('pusat_mengundi_id', $pusatMengundi->id) ->whereNull('saluran_mengundi_id') ->where('position_id', $position->id) ->where('status', 'active') ->count(); return $used < (int) $quota; } public function hasSaluranVacancy(PusatMengundi $pusatMengundi, int $saluranMengundiId, string $roleCode): bool { $position = Position::query()->where('code', $roleCode)->first(); if ($position === null) { return false; } $saluran = SaluranMengundi::query() ->where('id', $saluranMengundiId) ->where('pusat_mengundi_id', $pusatMengundi->id) ->first(); if (! $saluran instanceof SaluranMengundi) { return false; } $quota = PositionQuota::query() ->where('election_id', $pusatMengundi->election_id) ->where('pusat_mengundi_id', $pusatMengundi->id) ->where('saluran_mengundi_id', $saluran->id) ->where('position_id', $position->id) ->value('quota'); if ($quota === null) { return false; } $used = StaffAssignment::query() ->where('election_id', $pusatMengundi->election_id) ->where('pusat_mengundi_id', $pusatMengundi->id) ->where('saluran_mengundi_id', $saluran->id) ->where('position_id', $position->id) ->where('status', 'active') ->count(); return $used < (int) $quota; } /** * @return Collection */ public function availableSaluransForRole(PusatMengundi $pusatMengundi, string $roleCode): Collection { /** @var Collection $salurans */ $salurans = $pusatMengundi->saluranMengundis() ->orderBy('number') ->get() ->filter(fn (mixed $saluran): bool => $saluran instanceof SaluranMengundi && $this->hasSaluranVacancy($pusatMengundi, $saluran->id, $roleCode)) ->values(); return $salurans; } }