#44 - vacation summary of all employees (#92)

* #44 - ui for summary

* #44 - vacation monthly usage

* #44 - fix

* #44 - fix

Co-authored-by: EwelinaLasowy <ewelina.lasowy@blumilk.pl>
This commit is contained in:
Adrian Hopek
2022-03-24 10:33:34 +01:00
committed by GitHub
parent dcda8c6255
commit 957b07b3eb
18 changed files with 710 additions and 1019 deletions

View File

@@ -31,5 +31,6 @@ class AuthServiceProvider extends ServiceProvider
Gate::define("manageHolidays", fn(User $user) => $user->role === Role::AdministrativeApprover);
Gate::define("manageVacationLimits", fn(User $user) => $user->role === Role::AdministrativeApprover);
Gate::define("generateTimesheet", fn(User $user) => $user->role === Role::AdministrativeApprover);
Gate::define("listMonthlyUsage", fn(User $user) => $user->role === Role::AdministrativeApprover);
}
}

View File

@@ -5,9 +5,10 @@ declare(strict_types=1);
namespace Toby\Domain;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection;
use Toby\Domain\Enums\VacationType;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\Vacation;
use Toby\Eloquent\Models\YearPeriod;
class UserVacationStatsRetriever
@@ -30,6 +31,21 @@ class UserVacationStatsRetriever
->count();
}
public function getUsedVacationDaysByMonth(User $user, YearPeriod $yearPeriod): Collection
{
return $user->vacations()
->whereRelation(
"vacationRequest",
fn(Builder $query) => $query
->where("year_period_id", $yearPeriod->id)
->whereIn("type", $this->getLimitableVacationTypes())
->states(VacationRequestStatesRetriever::successStates()),
)
->get()
->groupBy(fn(Vacation $vacation) => strtolower($vacation->date->englishMonth))
->map(fn(Collection $items) => $items->count());
}
public function getPendingVacationDays(User $user, YearPeriod $yearPeriod): int
{
return $user

View File

@@ -100,6 +100,14 @@ class User extends Authenticatable
return $this->role === $role;
}
public function hasVacationLimit(YearPeriod $yearPeriod): bool
{
return $this->vacationLimits()
->where("year_period_id", $yearPeriod->id)
->whereNotNull("days")
->exists();
}
protected function getAvatarName(): string
{
return mb_substr($this->first_name, 0, 1) . mb_substr($this->last_name, 0, 1);

View File

@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace Toby\Infrastructure\Http\Controllers;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Inertia\Response;
use Toby\Domain\Enums\Month;
use Toby\Domain\UserVacationStatsRetriever;
use Toby\Eloquent\Helpers\YearPeriodRetriever;
use Toby\Eloquent\Models\User;
use Toby\Infrastructure\Http\Resources\UserResource;
class MonthlyUsageController extends Controller
{
public function __invoke(
Request $request,
YearPeriodRetriever $yearPeriodRetriever,
UserVacationStatsRetriever $statsRetriever,
): Response {
$this->authorize("listMonthlyUsage");
$currentYearPeriod = $yearPeriodRetriever->selected();
$currentUser = $request->user();
$users = User::query()
->whereRelation(
"vacationlimits",
fn(Builder $query) => $query->where("year_period_id", $currentYearPeriod->id)->whereNotNull("days"),
)
->where("id", "!=", $currentUser->id)
->orderBy("last_name")
->orderBy("first_name")
->get();
if ($currentUser->hasVacationLimit($currentYearPeriod)) {
$users->prepend($currentUser);
}
$monthlyUsage = [];
foreach ($users as $user) {
$vacationsByMonth = $statsRetriever->getUsedVacationDaysByMonth($user, $currentYearPeriod);
$limit = $statsRetriever->getVacationDaysLimit($user, $currentYearPeriod);
$used = $statsRetriever->getUsedVacationDays($user, $currentYearPeriod);
$pending = $statsRetriever->getPendingVacationDays($user, $currentYearPeriod);
$remaining = $limit - $used - $pending;
$monthlyUsage[] = [
"user" => new UserResource($user),
"months" => $vacationsByMonth,
"stats" => [
"used" => $used,
"pending" => $pending,
"remaining" => $remaining,
],
];
}
return inertia("MonthlyUsage", [
"monthlyUsage" => $monthlyUsage,
"currentMonth" => Month::current(),
"can" => [
"listMonthlyUsage" => $request->user()->can("listMonthlyUsage"),
],
]);
}
}

View File

@@ -17,6 +17,6 @@ class SelectYearPeriodController extends Controller
return redirect()
->back()
->with("success", __("Selected year period has been changed."));
->with("info", __("Selected year period has been changed."));
}
}

View File

@@ -27,11 +27,13 @@ class HandleInertiaRequests extends Middleware
"manageVacationLimits" => $user ? $user->can("manageVacationLimits") : false,
"manageUsers" => $user ? $user->can("manageUsers") : false,
"listAllVacationRequests" => $user ? $user->can("listAll", VacationRequest::class) : false,
"listMonthlyUsage" => $user ? $user->can("listMonthlyUsage") : false,
],
],
"flash" => fn() => [
"success" => $request->session()->get("success"),
"error" => $request->session()->get("error"),
"info" => $request->session()->get("info"),
],
"years" => fn() => $user ? $this->yearPeriodRetriever->links() : [],
]);