This commit is contained in:
Adrian Hopek
2022-03-01 12:25:07 +01:00
parent 92784846dc
commit a37be18da6
10 changed files with 85 additions and 62 deletions

View File

@@ -6,9 +6,6 @@ namespace Toby\Architecture\Providers;
use Illuminate\Support\Carbon;
use Illuminate\Support\ServiceProvider;
use Toby\Eloquent\Models\Holiday;
use Toby\Eloquent\Models\VacationLimit;
use Toby\Eloquent\Scopes\SelectedYearPeriodScope;
class AppServiceProvider extends ServiceProvider
{
@@ -16,10 +13,5 @@ class AppServiceProvider extends ServiceProvider
{
Carbon::macro("toDisplayString", fn() => $this->translatedFormat("j F Y"));
Carbon::macro("toDisplayDate", fn() => $this->translatedFormat("d.m.Y"));
$selectedYearPeriodScope = $this->app->make(SelectedYearPeriodScope::class);
VacationLimit::addGlobalScope($selectedYearPeriodScope);
Holiday::addGlobalScope($selectedYearPeriodScope);
}
}

View File

@@ -59,6 +59,11 @@ class UserVacationStatsRetriever
->count();
}
public function getRemainingVacationDays(User $user, YearPeriod $yearPeriod): int
{
return $this->getVacationDaysLimit($user, $yearPeriod) - $this->getUsedVacationDays($user, $yearPeriod);
}
public function getVacationDaysLimit(User $user, YearPeriod $yearPeriod): int
{
$limit = $user->vacationLimits()

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Toby\Infrastructure\Http\Controllers\Api;
use Illuminate\Http\JsonResponse;
use Toby\Domain\UserVacationStatsRetriever;
use Toby\Eloquent\Helpers\YearPeriodRetriever;
use Toby\Eloquent\Models\User;
use Toby\Infrastructure\Http\Controllers\Controller;
use Toby\Infrastructure\Http\Requests\Api\CalculateVacationStatsRequest;
class CalculateUserVacationStatsController extends Controller
{
public function __invoke(
CalculateVacationStatsRequest $request,
UserVacationStatsRetriever $vacationStatsRetriever,
YearPeriodRetriever $yearPeriodRetriever,
): JsonResponse {
/** @var User $user */
$user = User::query()->find($request->get("user"));
$yearPeriod = $yearPeriodRetriever->selected();
$limit = $vacationStatsRetriever->getVacationDaysLimit($user, $yearPeriod);
$used = $vacationStatsRetriever->getUsedVacationDays($user, $yearPeriod);
$pending = $vacationStatsRetriever->getPendingVacationDays($user, $yearPeriod);
$other = $vacationStatsRetriever->getOtherApprovedVacationDays($user, $yearPeriod);
return new JsonResponse([
"limit" => $limit,
"remaining" => $limit - $used - $pending,
"used" => $used,
"pending" => $pending,
"other" => $other,
]);
}
}

View File

@@ -6,6 +6,7 @@ namespace Toby\Infrastructure\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Inertia\Response;
use Toby\Eloquent\Helpers\YearPeriodRetriever;
use Toby\Eloquent\Models\Holiday;
use Toby\Infrastructure\Http\Requests\HolidayRequest;
use Toby\Infrastructure\Http\Resources\HolidayFormDataResource;
@@ -13,9 +14,12 @@ use Toby\Infrastructure\Http\Resources\HolidayResource;
class HolidayController extends Controller
{
public function index(): Response
public function index(YearPeriodRetriever $yearPeriodRetriever): Response
{
$holidays = Holiday::query()
$yearPeriod = $yearPeriodRetriever->selected();
$holidays = $yearPeriod
->holidays()
->orderBy("date")
->get();

View File

@@ -6,22 +6,39 @@ namespace Toby\Infrastructure\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Inertia\Response;
use Toby\Domain\UserVacationStatsRetriever;
use Toby\Eloquent\Helpers\YearPeriodRetriever;
use Toby\Eloquent\Models\VacationLimit;
use Toby\Eloquent\Models\YearPeriod;
use Toby\Infrastructure\Http\Requests\VacationLimitRequest;
use Toby\Infrastructure\Http\Resources\VacationLimitResource;
use Toby\Infrastructure\Http\Resources\UserResource;
class VacationLimitController extends Controller
{
public function edit(): Response
public function edit(YearPeriodRetriever $yearPeriodRetriever, UserVacationStatsRetriever $statsRetriever): Response
{
$limits = VacationLimit::query()
$yearPeriod = $yearPeriodRetriever->selected();
$previousYearPeriod = YearPeriod::findByYear($yearPeriod->year - 1);
$limits = $yearPeriod
->vacationLimits()
->with("user")
->orderByUserField("last_name")
->orderByUserField("first_name")
->get();
$limitsResource = $limits->map(fn(VacationLimit $limit) => [
"id" => $limit->id,
"user" => new UserResource($limit->user),
"hasVacation" => $limit->hasVacation(),
"days" => $limit->days,
"remainingLastYear" => $previousYearPeriod
? $statsRetriever->getRemainingVacationDays($limit->user, $previousYearPeriod)
: 0,
]);
return inertia("VacationLimits", [
"limits" => VacationLimitResource::collection($limits),
"limits" => $limitsResource,
]);
}

View File

@@ -1,22 +0,0 @@
<?php
declare(strict_types=1);
namespace Toby\Infrastructure\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class VacationLimitResource extends JsonResource
{
public static $wrap = null;
public function toArray($request): array
{
return [
"id" => $this->id,
"user" => new UserResource($this->user),
"hasVacation" => $this->hasVacation(),
"days" => $this->days,
];
}
}