This commit is contained in:
Adrian Hopek 2022-04-07 08:57:17 +02:00
parent fe41fd1cc5
commit fb15811b16
5 changed files with 19 additions and 16 deletions

View File

@ -21,7 +21,7 @@ class UserVacationStatsRetriever
{ {
return $user return $user
->vacations() ->vacations()
->where("year_period_id", $yearPeriod->id) ->whereBelongsTo($yearPeriod)
->whereRelation( ->whereRelation(
"vacationRequest", "vacationRequest",
fn(Builder $query) => $query fn(Builder $query) => $query
@ -37,7 +37,7 @@ class UserVacationStatsRetriever
->whereRelation( ->whereRelation(
"vacationRequest", "vacationRequest",
fn(Builder $query) => $query fn(Builder $query) => $query
->where("year_period_id", $yearPeriod->id) ->whereBelongsTo($yearPeriod)
->whereIn("type", $this->getLimitableVacationTypes()) ->whereIn("type", $this->getLimitableVacationTypes())
->states(VacationRequestStatesRetriever::successStates()), ->states(VacationRequestStatesRetriever::successStates()),
) )
@ -50,7 +50,7 @@ class UserVacationStatsRetriever
{ {
return $user return $user
->vacations() ->vacations()
->where("year_period_id", $yearPeriod->id) ->whereBelongsTo($yearPeriod)
->whereRelation( ->whereRelation(
"vacationRequest", "vacationRequest",
fn(Builder $query) => $query fn(Builder $query) => $query
@ -64,7 +64,7 @@ class UserVacationStatsRetriever
{ {
return $user return $user
->vacations() ->vacations()
->where("year_period_id", $yearPeriod->id) ->whereBelongsTo($yearPeriod)
->whereRelation( ->whereRelation(
"vacationRequest", "vacationRequest",
fn(Builder $query) => $query fn(Builder $query) => $query
@ -86,7 +86,7 @@ class UserVacationStatsRetriever
public function getVacationDaysLimit(User $user, YearPeriod $yearPeriod): int public function getVacationDaysLimit(User $user, YearPeriod $yearPeriod): int
{ {
$limit = $user->vacationLimits() $limit = $user->vacationLimits()
->where("year_period_id", $yearPeriod->id) ->whereBelongsTo($yearPeriod)
->first() ->first()
?->days; ?->days;

View File

@ -41,13 +41,16 @@ class DoesNotExceedLimitRule implements VacationRequestRule
protected function getUserVacationLimit(User $user, YearPeriod $yearPeriod): int protected function getUserVacationLimit(User $user, YearPeriod $yearPeriod): int
{ {
return $user->vacationLimits()->where("year_period_id", $yearPeriod->id)->first()->days ?? 0; return $user->vacationLimits()
->whereBelongsTo($yearPeriod)
->first()
->days ?? 0;
} }
protected function getVacationDaysWithLimit(User $user, YearPeriod $yearPeriod): int protected function getVacationDaysWithLimit(User $user, YearPeriod $yearPeriod): int
{ {
return $user->vacations() return $user->vacations()
->where("year_period_id", $yearPeriod->id) ->whereBelongsTo($yearPeriod)
->whereRelation( ->whereRelation(
"vacationRequest", "vacationRequest",
fn(Builder $query) => $query fn(Builder $query) => $query

View File

@ -91,7 +91,7 @@ class User extends Authenticatable
public function hasVacationLimit(YearPeriod $yearPeriod): bool public function hasVacationLimit(YearPeriod $yearPeriod): bool
{ {
return $this->vacationLimits() return $this->vacationLimits()
->where("year_period_id", $yearPeriod->id) ->whereBelongsTo($yearPeriod)
->whereNotNull("days") ->whereNotNull("days")
->exists(); ->exists();
} }
@ -113,7 +113,7 @@ class User extends Authenticatable
return $query->whereRelation( return $query->whereRelation(
"vacationlimits", "vacationlimits",
fn(Builder $query) => $query fn(Builder $query) => $query
->where("year_period_id", $yearPeriod->id) ->whereBelongsTo($yearPeriod)
->whereNotNull("days"), ->whereNotNull("days"),
); );
} }

View File

@ -27,7 +27,7 @@ class CalculateUserUnavailableDaysController extends Controller
$holidays = $yearPeriod->holidays()->pluck("date"); $holidays = $yearPeriod->holidays()->pluck("date");
$vacationDays = $user->vacations() $vacationDays = $user->vacations()
->where("year_period_id", $yearPeriod->id) ->whereBelongsTo($yearPeriod)
->whereRelation( ->whereRelation(
"vacationRequest", "vacationRequest",
fn(Builder $query) => $query->noStates(VacationRequestStatesRetriever::failedStates()), fn(Builder $query) => $query->noStates(VacationRequestStatesRetriever::failedStates()),

View File

@ -40,26 +40,26 @@ class VacationRequestController extends Controller
$vacationRequests = $request->user() $vacationRequests = $request->user()
->vacationRequests() ->vacationRequests()
->with("vacations") ->with("vacations")
->where("year_period_id", $yearPeriodRetriever->selected()->id) ->whereBelongsTo($yearPeriodRetriever->selected())
->latest() ->latest()
->states(VacationRequestStatesRetriever::filterByStatusGroup($status, $request->user())) ->states(VacationRequestStatesRetriever::filterByStatusGroup($status, $request->user()))
->paginate(); ->paginate();
$pending = $request->user() $pending = $request->user()
->vacationRequests() ->vacationRequests()
->where("year_period_id", $yearPeriodRetriever->selected()->id) ->whereBelongsTo($yearPeriodRetriever->selected())
->states(VacationRequestStatesRetriever::pendingStates()) ->states(VacationRequestStatesRetriever::pendingStates())
->count(); ->count();
$success = $request->user() $success = $request->user()
->vacationRequests() ->vacationRequests()
->where("year_period_id", $yearPeriodRetriever->selected()->id) ->whereBelongsTo($yearPeriodRetriever->selected())
->states(VacationRequestStatesRetriever::successStates()) ->states(VacationRequestStatesRetriever::successStates())
->count(); ->count();
$failed = $request->user() $failed = $request->user()
->vacationRequests() ->vacationRequests()
->where("year_period_id", $yearPeriodRetriever->selected()->id) ->whereBelongsTo($yearPeriodRetriever->selected())
->states(VacationRequestStatesRetriever::failedStates()) ->states(VacationRequestStatesRetriever::failedStates())
->count(); ->count();
@ -91,7 +91,7 @@ class VacationRequestController extends Controller
$vacationRequests = VacationRequest::query() $vacationRequests = VacationRequest::query()
->with(["user", "vacations"]) ->with(["user", "vacations"])
->where("year_period_id", $yearPeriod->id) ->whereBelongsTo($yearPeriod)
->when($user !== null, fn(Builder $query) => $query->where("user_id", $user)) ->when($user !== null, fn(Builder $query) => $query->where("user_id", $user))
->when( ->when(
$status !== null, $status !== null,
@ -156,7 +156,7 @@ class VacationRequestController extends Controller
return $pdf->stream(); return $pdf->stream();
} }
public function create(Request $request, YearPeriodRetriever $yearPeriodRetriever): Response public function create(Request $request): Response
{ {
$users = User::query() $users = User::query()
->orderBy("last_name") ->orderBy("last_name")