toby/app/Infrastructure/Http/Controllers/MonthlyUsageController.php
Adrian Hopek 08421b8a69
- small changes (#98)
* - added some test

* - cr fix

* wip

* wip

* Update resources/js/Shared/MainMenu.vue

Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>

* fix

Co-authored-by: EwelinaLasowy <ewelina.lasowy@blumilk.pl>
Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>
2022-03-30 10:33:18 +02:00

67 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Toby\Infrastructure\Http\Controllers;
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()
->withVacationLimitIn($currentYearPeriod)
->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"),
],
]);
}
}