configRetriever->hasLimit($vacationRequest->type)) { return true; } $limit = $this->getUserVacationLimit($vacationRequest->user, $vacationRequest->yearPeriod); $vacationDays = $this->getVacationDaysWithLimit($vacationRequest->user, $vacationRequest->yearPeriod); $estimatedDays = $this->workDaysCalculator ->calculateDays($vacationRequest->from, $vacationRequest->to) ->count(); return $limit >= ($vacationDays + $estimatedDays); } public function errorMessage(): string { return __("Vacation limit has been exceeded."); } protected function getUserVacationLimit(User $user, YearPeriod $yearPeriod): int { return $user->vacationLimits() ->whereBelongsTo($yearPeriod) ->first() ?->days ?? 0; } protected function getVacationDaysWithLimit(User $user, YearPeriod $yearPeriod): int { return $user->vacations() ->whereBelongsTo($yearPeriod) ->whereRelation( "vacationRequest", fn(Builder $query): Builder => $query ->whereIn("type", $this->getLimitableVacationTypes()) ->noStates(VacationRequestStatesRetriever::failedStates()), ) ->count(); } protected function getLimitableVacationTypes(): Collection { $types = VacationType::all(); return $types->filter(fn(VacationType $type): bool => $this->configRetriever->hasLimit($type)); } }