configRetriever->hasLimit($vacationRequest->type)) { return true; } $limit = $this->getUserVacationLimit($vacationRequest->user, $vacationRequest->yearPeriod); $vacationDays = $this->getVacationDaysWithLimit($vacationRequest->user, $vacationRequest->yearPeriod); $estimatedDays = $this->vacationDaysCalculator->calculateDays($vacationRequest->yearPeriod, $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()->where("year_period_id", $yearPeriod->id)->first()->days ?? 0; } protected function getVacationDaysWithLimit(User $user, YearPeriod $yearPeriod): int { return $user->vacations() ->where("year_period_id", $yearPeriod->id) ->whereRelation( "vacationRequest", fn(Builder $query) => $query ->whereIn("type", $this->getLimitableVacationTypes()) ->noStates(VacationRequestState::failedStates()), ) ->count(); } protected function getLimitableVacationTypes(): Collection { $types = new Collection(VacationType::cases()); return $types->filter(fn(VacationType $type) => $this->configRetriever->hasLimit($type)); } }