This commit is contained in:
Adrian Hopek 2022-02-07 14:37:14 +01:00
parent 6deb5b65e2
commit cc90851bca
22 changed files with 155 additions and 45 deletions

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Toby\Eloquent\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon;
/**
* @property int $id
* @property Carbon $date
* @property User $user
* @property VacationRequest $vacationRequest
*/
class Vacation extends Model
{
use HasFactory;
public $timestamps = false;
protected $guarded = [];
protected $casts = [
"date" => "date",
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function vacationRequest(): BelongsTo
{
return $this->belongsTo(VacationRequest::class);
}
}

View File

@ -26,6 +26,7 @@ use Toby\Domain\Enums\VacationType;
* @property User $user * @property User $user
* @property YearPeriod $yearPeriod * @property YearPeriod $yearPeriod
* @property Collection $activities * @property Collection $activities
* @property Collection $vacations
* @property Carbon $created_at * @property Carbon $created_at
* @property Carbon $updated_at * @property Carbon $updated_at
*/ */
@ -57,6 +58,11 @@ class VacationRequest extends Model
return $this->hasMany(VacationRequestActivity::class); return $this->hasMany(VacationRequestActivity::class);
} }
public function vacations(): HasMany
{
return $this->hasMany(Vacation::class);
}
public function changeStateTo(VacationRequestState $state): void public function changeStateTo(VacationRequestState $state): void
{ {
$this->state = $state; $this->state = $state;

View File

@ -7,11 +7,9 @@ namespace Toby\Infrastructure\Http\Controllers;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Inertia\Response; use Inertia\Response;
use Toby\Eloquent\Models\Holiday; use Toby\Eloquent\Models\Holiday;
use Toby\Eloquent\Models\User;
use Toby\Infrastructure\Http\Requests\HolidayRequest; use Toby\Infrastructure\Http\Requests\HolidayRequest;
use Toby\Infrastructure\Http\Resources\HolidayFormDataResource; use Toby\Infrastructure\Http\Resources\HolidayFormDataResource;
use Toby\Infrastructure\Http\Resources\HolidayResource; use Toby\Infrastructure\Http\Resources\HolidayResource;
use Toby\Infrastructure\Http\Resources\UserResource;
class HolidayController extends Controller class HolidayController extends Controller
{ {

View File

@ -28,6 +28,7 @@ class VacationRequestController extends Controller
$vacationRequests = $request->user() $vacationRequests = $request->user()
->vacationRequests() ->vacationRequests()
->with("vacations")
->where("year_period_id", $yearPeriodRetriever->selected()->id) ->where("year_period_id", $yearPeriodRetriever->selected()->id)
->latest() ->latest()
->states(VacationRequestState::filterByStatus($status)) ->states(VacationRequestState::filterByStatus($status))
@ -73,15 +74,11 @@ class VacationRequestController extends Controller
): RedirectResponse { ): RedirectResponse {
/** @var VacationRequest $vacationRequest */ /** @var VacationRequest $vacationRequest */
$vacationRequest = $request->user()->vacationRequests()->make($request->data()); $vacationRequest = $request->user()->vacationRequests()->make($request->data());
$vacationRequest->estimated_days = $vacationDaysCalculator->calculateDays(
$vacationRequest->yearPeriod,
$vacationRequest->from,
$vacationRequest->to,
)->count();
$vacationRequestValidator->validate($vacationRequest); $vacationRequestValidator->validate($vacationRequest);
$vacationRequest->save(); $vacationRequest->save();
$stateManager->markAsCreated($vacationRequest); $stateManager->markAsCreated($vacationRequest);
return redirect() return redirect()

View File

@ -20,8 +20,8 @@ class VacationRequestResource extends JsonResource
"state" => $this->state->label(), "state" => $this->state->label(),
"from" => $this->from->toDisplayString(), "from" => $this->from->toDisplayString(),
"to" => $this->to->toDisplayString(), "to" => $this->to->toDisplayString(),
"estimatedDays" => $this->estimated_days,
"comment" => $this->comment, "comment" => $this->comment,
"days" => VacationResource::collection($this->vacations),
]; ];
} }
} }

View File

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace Toby\Infrastructure\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class VacationResource extends JsonResource
{
public static $wrap = null;
public function toArray($request): array
{
return [
"id" => $this->id,
"displayDate" => $this->date->toDisplayString(),
"date" => $this->date->toDateString(),
];
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
class VacationFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
//
];
}
}

View File

@ -5,7 +5,6 @@ declare(strict_types=1);
namespace Database\Factories; namespace Database\Factories;
use Carbon\CarbonImmutable; use Carbon\CarbonImmutable;
use Carbon\CarbonPeriod;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
use Toby\Domain\Enums\VacationRequestState; use Toby\Domain\Enums\VacationRequestState;
use Toby\Domain\Enums\VacationType; use Toby\Domain\Enums\VacationType;
@ -31,7 +30,6 @@ class VacationRequestFactory extends Factory
"state" => $this->faker->randomElement(VacationRequestState::cases()), "state" => $this->faker->randomElement(VacationRequestState::cases()),
"from" => $from, "from" => $from,
"to" => $from->addDays($days), "to" => $from->addDays($days),
"estimated_days" => fn(array $attributes) => $this->estimateDays($attributes),
"comment" => $this->faker->boolean ? $this->faker->paragraph() : null, "comment" => $this->faker->boolean ? $this->faker->paragraph() : null,
]; ];
} }
@ -43,11 +41,4 @@ class VacationRequestFactory extends Factory
return "{$number}/{$year}"; return "{$number}/{$year}";
} }
protected function estimateDays(array $attributes): int
{
$period = CarbonPeriod::create($attributes["from"], $attributes["to"]);
return $period->count();
}
} }

View File

@ -18,7 +18,6 @@ return new class() extends Migration {
$table->foreignIdFor(YearPeriod::class)->constrained()->cascadeOnDelete(); $table->foreignIdFor(YearPeriod::class)->constrained()->cascadeOnDelete();
$table->string("type"); $table->string("type");
$table->string("state")->nullable(); $table->string("state")->nullable();
$table->integer("estimated_days");
$table->date("from"); $table->date("from");
$table->date("to"); $table->date("to");
$table->text("comment")->nullable(); $table->text("comment")->nullable();

View File

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\VacationRequest;
return new class() extends Migration {
public function up(): void
{
Schema::create("vacations", function (Blueprint $table) {
$table->id();
$table->foreignIdFor(User::class)->constrained()->cascadeOnDelete();
$table->foreignIdFor(VacationRequest::class)->constrained()->cascadeOnDelete();
$table->date("date");
});
}
public function down(): void
{
Schema::dropIfExists("vacations");
}
};

View File

@ -8,6 +8,7 @@ use Illuminate\Database\Seeder;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Toby\Domain\PolishHolidaysRetriever; use Toby\Domain\PolishHolidaysRetriever;
use Toby\Domain\VacationDaysCalculator;
use Toby\Eloquent\Helpers\UserAvatarGenerator; use Toby\Eloquent\Helpers\UserAvatarGenerator;
use Toby\Eloquent\Models\User; use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\VacationLimit; use Toby\Eloquent\Models\VacationLimit;
@ -79,6 +80,20 @@ class DatabaseSeeder extends Seeder
->sequence(fn() => [ ->sequence(fn() => [
"year_period_id" => $yearPeriods->random()->id, "year_period_id" => $yearPeriods->random()->id,
]) ])
->afterCreating(function (VacationRequest $vacationRequest) {
$days = app(VacationDaysCalculator::class)->calculateDays(
$vacationRequest->yearPeriod,
$vacationRequest->from,
$vacationRequest->to
);
foreach ($days as $day) {
$vacationRequest->vacations()->create([
"date" => $day,
"user_id" => $vacationRequest->user->id
]);
}
})
->create(); ->create();
} }
} }

View File

@ -1,6 +1,6 @@
<template> <template>
<InertiaHead title="Kalendarz urlopów" /> <InertiaHead title="Kalendarz urlopów" />
<div class="bg-white sm:rounded-lg shadow-md"> <div class="bg-white shadow-md">
<div class="p-4 sm:px-6"> <div class="p-4 sm:px-6">
<h2 class="text-lg leading-6 font-medium text-gray-900"> <h2 class="text-lg leading-6 font-medium text-gray-900">
Kalendarz urlopów Kalendarz urlopów

View File

@ -1,6 +1,6 @@
<template> <template>
<InertiaHead title="Dodaj dzień wolny" /> <InertiaHead title="Dodaj dzień wolny" />
<div class="bg-white sm:rounded-lg shadow-md"> <div class="bg-white shadow-md">
<div class="p-4 sm:px-6"> <div class="p-4 sm:px-6">
<h2 class="text-lg leading-6 font-medium text-gray-900"> <h2 class="text-lg leading-6 font-medium text-gray-900">
Dodaj dzień wolny Dodaj dzień wolny

View File

@ -1,6 +1,6 @@
<template> <template>
<InertiaHead title="Edytuj dzień wolny" /> <InertiaHead title="Edytuj dzień wolny" />
<div class="bg-white sm:rounded-lg shadow-md"> <div class="bg-white shadow-md">
<div class="p-4 sm:px-6"> <div class="p-4 sm:px-6">
<h2 class="text-lg leading-6 font-medium text-gray-900"> <h2 class="text-lg leading-6 font-medium text-gray-900">
Edytuj dzień wolny Edytuj dzień wolny

View File

@ -1,6 +1,6 @@
<template> <template>
<InertiaHead title="Dni wolne od pracy" /> <InertiaHead title="Dni wolne od pracy" />
<div class="bg-white sm:rounded-lg shadow-md"> <div class="bg-white shadow-md">
<div class="flex justify-between items-center p-4 sm:px-6"> <div class="flex justify-between items-center p-4 sm:px-6">
<div> <div>
<h2 class="text-lg leading-6 font-medium text-gray-900"> <h2 class="text-lg leading-6 font-medium text-gray-900">

View File

@ -1,6 +1,6 @@
<template> <template>
<InertiaHead title="Dodawanie użytkownika" /> <InertiaHead title="Dodawanie użytkownika" />
<div class="bg-white sm:rounded-lg shadow-md"> <div class="bg-white shadow-md">
<div class="p-4 sm:px-6"> <div class="p-4 sm:px-6">
<h2 class="text-lg leading-6 font-medium text-gray-900"> <h2 class="text-lg leading-6 font-medium text-gray-900">
Dodaj użytkownika Dodaj użytkownika

View File

@ -1,6 +1,6 @@
<template> <template>
<InertiaHead title="Edycja użytkownika" /> <InertiaHead title="Edycja użytkownika" />
<div class="bg-white sm:rounded-lg shadow-md"> <div class="bg-white shadow-md">
<div class="p-4 sm:px-6"> <div class="p-4 sm:px-6">
<h2 class="text-lg leading-6 font-medium text-gray-900"> <h2 class="text-lg leading-6 font-medium text-gray-900">
Edytuj użytkownika Edytuj użytkownika

View File

@ -1,6 +1,6 @@
<template> <template>
<InertiaHead title="Użytkownicy" /> <InertiaHead title="Użytkownicy" />
<div class="bg-white sm:rounded-lg shadow-md"> <div class="bg-white shadow-md">
<div class="flex justify-between items-center p-4 sm:px-6"> <div class="flex justify-between items-center p-4 sm:px-6">
<div> <div>
<h2 class="text-lg leading-6 font-medium text-gray-900"> <h2 class="text-lg leading-6 font-medium text-gray-900">

View File

@ -1,6 +1,6 @@
<template> <template>
<InertiaHead title="Użytkownicy" /> <InertiaHead title="Użytkownicy" />
<div class="bg-white sm:rounded-lg shadow-md"> <div class="bg-white shadow-md">
<div class="flex justify-between items-center p-4 sm:px-6"> <div class="flex justify-between items-center p-4 sm:px-6">
<div> <div>
<h2 class="text-lg leading-6 font-medium text-gray-900"> <h2 class="text-lg leading-6 font-medium text-gray-900">

View File

@ -1,6 +1,6 @@
<template> <template>
<InertiaHead title="Złóż wniosek urlopowy" /> <InertiaHead title="Złóż wniosek urlopowy" />
<div class="bg-white sm:rounded-lg shadow-md"> <div class="bg-white shadow-md">
<div class="p-4 sm:px-6"> <div class="p-4 sm:px-6">
<h2 class="text-lg leading-6 font-medium text-gray-900"> <h2 class="text-lg leading-6 font-medium text-gray-900">
Złóż wniosek urlopowy Złóż wniosek urlopowy

View File

@ -1,6 +1,6 @@
<template> <template>
<InertiaHead title="Twoje wnioski urlopowe" /> <InertiaHead title="Twoje wnioski urlopowe" />
<div class="bg-white sm:rounded-lg shadow-md"> <div class="bg-white shadow-md">
<div class="flex justify-between items-center p-4 sm:px-6"> <div class="flex justify-between items-center p-4 sm:px-6">
<div> <div>
<h2 class="text-lg leading-6 font-medium text-gray-900"> <h2 class="text-lg leading-6 font-medium text-gray-900">
@ -57,7 +57,7 @@
</th> </th>
<th <th
scope="col" scope="col"
class="px-4 py-3 text-right text-xs font-semibold text-gray-500 uppercase tracking-wider" class="px-4 py-3 text-left text-xs font-semibold text-gray-500 uppercase tracking-wider"
> >
Dni urlopu Dni urlopu
</th> </th>
@ -93,17 +93,17 @@
<td class="px-4 py-4 whitespace-nowrap text-sm text-gray-500"> <td class="px-4 py-4 whitespace-nowrap text-sm text-gray-500">
{{ request.to }} {{ request.to }}
</td> </td>
<td class="px-4 py-4 whitespace-nowrap text-right text-sm text-gray-500">
{{ request.estimatedDays }}
</td>
<td class="px-4 py-4 whitespace-nowrap text-sm text-gray-500"> <td class="px-4 py-4 whitespace-nowrap text-sm text-gray-500">
{{ request.state }} {{ request.days.length }}
</td> </td>
<td class="px-4 py-4 whitespace-nowrap text-sm text-gray-500 flex items-center"> <td class="px-4 py-4 whitespace-nowrap text-sm text-gray-500 flex items-center">
<ThumbDownIcon class="w-4 h-4 text-rose-600 mr-1"/> <span>{{ request.state }}</span> <ThumbDownIcon class="w-4 h-4 text-rose-600 mr-1" /> <span>{{ request.state }}</span>
</td> </td>
<td class="px-4 py-4 whitespace-nowrap text-sm text-gray-500"> <td class="px-4 py-4 whitespace-nowrap text-sm text-gray-500">
<InertiaLink :href="`/vacation-requests/${request.id}`"> <InertiaLink
:href="`/vacation-requests/${request.id}`"
class="flex justify-around"
>
<ChevronRightIcon class="block w-6 h-6 fill-blumilk-500" /> <ChevronRightIcon class="block w-6 h-6 fill-blumilk-500" />
</InertiaLink> </InertiaLink>
</td> </td>

View File

@ -2,7 +2,7 @@
<InertiaHead :title="`Wniosek ${request.name}`" /> <InertiaHead :title="`Wniosek ${request.name}`" />
<div class="grid grid-cols-1 gap-6 xl:grid-flow-col-dense xl:grid-cols-3"> <div class="grid grid-cols-1 gap-6 xl:grid-flow-col-dense xl:grid-cols-3">
<div class="space-y-6 xl:col-start-1 xl:col-span-2"> <div class="space-y-6 xl:col-start-1 xl:col-span-2">
<div class="bg-white sm:rounded-lg shadow-md"> <div class="bg-white shadow-md">
<div class="px-4 py-5 sm:px-6"> <div class="px-4 py-5 sm:px-6">
<h3 class="text-lg leading-6 font-medium text-gray-900"> <h3 class="text-lg leading-6 font-medium text-gray-900">
Informacje na temat wniosku Informacje na temat wniosku
@ -47,7 +47,7 @@
Dni urlopu Dni urlopu
</dt> </dt>
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2"> <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
{{ request.estimatedDays }} {{ request.days.length }}
</dd> </dd>
</div> </div>
<div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6"> <div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
@ -85,7 +85,7 @@
</dl> </dl>
</div> </div>
</div> </div>
<div class="bg-white shadow sm:rounded-lg"> <div class="bg-white shadow">
<div class="px-4 py-5 sm:p-6"> <div class="px-4 py-5 sm:p-6">
<h3 class="text-lg leading-6 font-medium text-gray-900"> <h3 class="text-lg leading-6 font-medium text-gray-900">
Zaakceptuj wniosek jako osoba techniczna Zaakceptuj wniosek jako osoba techniczna
@ -108,7 +108,7 @@
</div> </div>
</div> </div>
</div> </div>
<div class="bg-white shadow sm:rounded-lg"> <div class="bg-white shadow">
<div class="px-4 py-5 sm:p-6"> <div class="px-4 py-5 sm:p-6">
<h3 class="text-lg leading-6 font-medium text-gray-900"> <h3 class="text-lg leading-6 font-medium text-gray-900">
Zaakceptuj wniosek jako osoba administracyjna Zaakceptuj wniosek jako osoba administracyjna
@ -130,7 +130,7 @@
</div> </div>
</div> </div>
</div> </div>
<div class="bg-white shadow sm:rounded-lg"> <div class="bg-white shadow">
<div class="px-4 py-5 sm:p-6"> <div class="px-4 py-5 sm:p-6">
<h3 class="text-lg leading-6 font-medium text-gray-900"> <h3 class="text-lg leading-6 font-medium text-gray-900">
Odrzuć wniosek Odrzuć wniosek
@ -152,7 +152,7 @@
</div> </div>
</div> </div>
</div> </div>
<div class="bg-white shadow sm:rounded-lg border border-red-500"> <div class="bg-white shadow border border-red-500">
<div class="px-4 py-5 sm:p-6"> <div class="px-4 py-5 sm:p-6">
<h3 class="text-lg leading-6 font-medium text-gray-900"> <h3 class="text-lg leading-6 font-medium text-gray-900">
Anuluj wniosek Anuluj wniosek
@ -176,7 +176,7 @@
</div> </div>
</div> </div>
<div class="xl:col-start-3 xl:col-span-1 space-y-6"> <div class="xl:col-start-3 xl:col-span-1 space-y-6">
<div class="bg-white sm:rounded-lg shadow-md"> <div class="bg-white shadow-md">
<div class="px-4 py-5 sm:px-6"> <div class="px-4 py-5 sm:px-6">
<h3 class="text-lg leading-6 font-medium text-gray-900"> <h3 class="text-lg leading-6 font-medium text-gray-900">
Historia wniosku Historia wniosku