diff --git a/app/Architecture/Providers/AppServiceProvider.php b/app/Architecture/Providers/AppServiceProvider.php index b1dc896..23059cb 100644 --- a/app/Architecture/Providers/AppServiceProvider.php +++ b/app/Architecture/Providers/AppServiceProvider.php @@ -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); } } diff --git a/app/Domain/UserVacationStatsRetriever.php b/app/Domain/UserVacationStatsRetriever.php index 02ce455..830a3cd 100644 --- a/app/Domain/UserVacationStatsRetriever.php +++ b/app/Domain/UserVacationStatsRetriever.php @@ -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() diff --git a/app/Infrastructure/Http/Controllers/Api/CalculateUserVacationStatsController.php b/app/Infrastructure/Http/Controllers/Api/CalculateUserVacationStatsController.php new file mode 100644 index 0000000..554027d --- /dev/null +++ b/app/Infrastructure/Http/Controllers/Api/CalculateUserVacationStatsController.php @@ -0,0 +1,38 @@ +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, + ]); + } +} diff --git a/app/Infrastructure/Http/Controllers/HolidayController.php b/app/Infrastructure/Http/Controllers/HolidayController.php index fe256dd..ed28f6b 100644 --- a/app/Infrastructure/Http/Controllers/HolidayController.php +++ b/app/Infrastructure/Http/Controllers/HolidayController.php @@ -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(); diff --git a/app/Infrastructure/Http/Controllers/VacationLimitController.php b/app/Infrastructure/Http/Controllers/VacationLimitController.php index 2762b7d..9cac7a9 100644 --- a/app/Infrastructure/Http/Controllers/VacationLimitController.php +++ b/app/Infrastructure/Http/Controllers/VacationLimitController.php @@ -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, ]); } diff --git a/app/Infrastructure/Http/Resources/VacationLimitResource.php b/app/Infrastructure/Http/Resources/VacationLimitResource.php deleted file mode 100644 index 4226a14..0000000 --- a/app/Infrastructure/Http/Resources/VacationLimitResource.php +++ /dev/null @@ -1,22 +0,0 @@ - $this->id, - "user" => new UserResource($this->user), - "hasVacation" => $this->hasVacation(), - "days" => $this->days, - ]; - } -} diff --git a/resources/js/Pages/VacationLimits.vue b/resources/js/Pages/VacationLimits.vue index 5a23728..c30a845 100644 --- a/resources/js/Pages/VacationLimits.vue +++ b/resources/js/Pages/VacationLimits.vue @@ -34,6 +34,12 @@ > Posiada urlop? +