toby/app/Infrastructure/Http/Controllers/DashboardController.php
Adrian Hopek 474707e691 Merge branch 'main' into #43-vacation-summary-for-employee
# Conflicts:
#	app/Infrastructure/Http/Controllers/HolidayController.php
#	app/Infrastructure/Http/Controllers/VacationLimitController.php
#	app/Infrastructure/Http/Controllers/VacationRequestController.php
#	composer.lock
#	resources/js/Pages/Holidays/Index.vue
#	resources/js/Pages/VacationRequest/Create.vue
2022-03-02 10:44:07 +01:00

69 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Toby\Infrastructure\Http\Controllers;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Inertia\Response;
use Toby\Domain\UserVacationStatsRetriever;
use Toby\Domain\VacationRequestStatesRetriever;
use Toby\Eloquent\Models\Holiday;
use Toby\Eloquent\Models\Vacation;
use Toby\Eloquent\Models\VacationRequest;
use Toby\Eloquent\Models\YearPeriod;
use Toby\Infrastructure\Http\Resources\AbsenceResource;
use Toby\Infrastructure\Http\Resources\HolidayResource;
use Toby\Infrastructure\Http\Resources\VacationRequestResource;
class DashboardController extends Controller
{
public function __invoke(Request $request, UserVacationStatsRetriever $vacationStatsRetriever): Response
{
$user = $request->user();
$now = Carbon::now();
$yearPeriod = YearPeriod::findByYear($now->year);
$absences = Vacation::query()
->with(["user", "vacationRequest"])
->whereDate("date", $now)
->whereRelation(
"vacationRequest",
fn(Builder $query) => $query->states(VacationRequestStatesRetriever::successStates()),
)
->get();
$vacationRequests = VacationRequest::query()
->latest("updated_at")
->limit(3)
->get();
$holidays = Holiday::query()
->whereDate("date", ">=", $now)
->latest()
->limit(3)
->get();
$limit = $vacationStatsRetriever->getVacationDaysLimit($user, $yearPeriod);
$used = $vacationStatsRetriever->getUsedVacationDays($user, $yearPeriod);
$pending = $vacationStatsRetriever->getPendingVacationDays($user, $yearPeriod);
$other = $vacationStatsRetriever->getOtherApprovedVacationDays($user, $yearPeriod);
$remaining = $limit - $used - $pending;
return inertia("Dashboard", [
"absences" => AbsenceResource::collection($absences),
"vacationRequests" => VacationRequestResource::collection($vacationRequests),
"holidays" => HolidayResource::collection($holidays),
"stats" => [
"limit" => $limit,
"remaining" => $remaining,
"used" => $used,
"pending" => $pending,
"other" => $other,
],
]);
}
}