toby/app/Infrastructure/Http/Controllers/VacationLimitController.php
Adrian Hopek cc981b02b4
#90 - user profile (#125)
* fix css focuses

* #90 - wip

* #90 - fix to generate PDF

* #90 - wip

* #90 - wip

* #90 - wip

* #90 - wip

* #90 - fix to calendar

* #90 - wip

* #90 - fix

* #90 - fix lint

* #90 - fix

* Apply suggestions from code review

Co-authored-by: Krzysztof Rewak <krzysztof.rewak@gmail.com>
Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>

* #90 - cr fixes

* #90 - fix

Co-authored-by: EwelinaLasowy <ewelina.lasowy@blumilk.pl>
Co-authored-by: Krzysztof Rewak <krzysztof.rewak@gmail.com>
Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>
2022-04-14 11:58:45 +02:00

63 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
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\SimpleUserResource;
class VacationLimitController extends Controller
{
public function edit(YearPeriodRetriever $yearPeriodRetriever, UserVacationStatsRetriever $statsRetriever): Response
{
$this->authorize("manageVacationLimits");
$yearPeriod = $yearPeriodRetriever->selected();
$previousYearPeriod = YearPeriod::findByYear($yearPeriod->year - 1);
$limits = $yearPeriod
->vacationLimits()
->with("user.profile")
->has("user")
->get()
->sortBy(fn(VacationLimit $limit): string => "{$limit->user->profile->last_name} {$limit->user->profile->first_name}")
->values();
$limitsResource = $limits->map(fn(VacationLimit $limit) => [
"id" => $limit->id,
"user" => new SimpleUserResource($limit->user),
"hasVacation" => $limit->hasVacation(),
"days" => $limit->days,
"remainingLastYear" => $previousYearPeriod
? $statsRetriever->getRemainingVacationDays($limit->user, $previousYearPeriod)
: 0,
]);
return inertia("VacationLimits", [
"limits" => $limitsResource,
]);
}
public function update(VacationLimitRequest $request): RedirectResponse
{
$this->authorize("manageVacationLimits");
$data = $request->data();
foreach ($request->vacationLimits() as $limit) {
$limit->update($data[$limit->id]);
}
return redirect()
->back()
->with("success", __("Vacation limits have been updated."));
}
}