This commit is contained in:
Adrian Hopek 2022-02-21 14:57:18 +01:00
parent 0f5b9ecc0b
commit 9ada0f843d
6 changed files with 81 additions and 61 deletions

View File

@ -4,9 +4,8 @@ declare(strict_types=1);
namespace Toby\Domain; namespace Toby\Domain;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Carbon\CarbonPeriod; use Carbon\CarbonPeriod;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Toby\Domain\Enums\VacationRequestState; use Toby\Domain\Enums\VacationRequestState;
use Toby\Eloquent\Helpers\YearPeriodRetriever; use Toby\Eloquent\Helpers\YearPeriodRetriever;
@ -20,33 +19,16 @@ class CalendarGenerator
) { ) {
} }
public function generate(YearPeriod $yearPeriod, string $month): array public function generate(Carbon $month): array
{ {
$date = CarbonImmutable::create($yearPeriod->year, $this->monthNameToNumber($month)); $period = CarbonPeriod::create($month->copy()->startOfMonth(), $month->copy()->endOfMonth());
$period = CarbonPeriod::create($date->startOfMonth(), $date->endOfMonth()); $yearPeriod = YearPeriod::findByYear($month->year);
$holidays = $yearPeriod->holidays()->pluck("date"); $holidays = $yearPeriod->holidays()->pluck("date");
return $this->generateCalendar($period, $holidays); return $this->generateCalendar($period, $holidays);
} }
protected function monthNameToNumber(string $name): int
{
return match ($name) {
default => CarbonInterface::JANUARY,
"february" => CarbonInterface::FEBRUARY,
"march" => CarbonInterface::MARCH,
"april" => CarbonInterface::APRIL,
"may" => CarbonInterface::MAY,
"june" => CarbonInterface::JUNE,
"july" => CarbonInterface::JULY,
"august" => CarbonInterface::AUGUST,
"september" => CarbonInterface::SEPTEMBER,
"october" => CarbonInterface::OCTOBER,
"november" => CarbonInterface::NOVEMBER,
"december" => CarbonInterface::DECEMBER,
};
}
protected function generateCalendar(CarbonPeriod $period, Collection $holidays): array protected function generateCalendar(CarbonPeriod $period, Collection $holidays): array
{ {
$calendar = []; $calendar = [];

View File

@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace Toby\Domain\Enums;
use Carbon\CarbonInterface;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
enum Month: string
{
case January = "january";
case February = "february";
case March = "march";
case April = "april";
case May = "may";
case June = "june";
case July = "july";
case August = "august";
case September = "september";
case October = "october";
case November = "november";
case December = "december";
public function toCarbonNumber(): int
{
return match ($this) {
self::January => CarbonInterface::JANUARY,
self::February => CarbonInterface::FEBRUARY,
self::March => CarbonInterface::MARCH,
self::April => CarbonInterface::APRIL,
self::May => CarbonInterface::MAY,
self::June => CarbonInterface::JUNE,
self::July => CarbonInterface::JULY,
self::August => CarbonInterface::AUGUST,
self::September => CarbonInterface::SEPTEMBER,
self::October => CarbonInterface::OCTOBER,
self::November => CarbonInterface::NOVEMBER,
self::December => CarbonInterface::DECEMBER,
};
}
public static function current(): Month
{
return Month::from(Str::lower(Carbon::now()->englishMonth));
}
public static function fromNameOrCurrent(string $name): Month
{
$month = Month::tryFrom($name);
if ($month === null) {
return Month::current();
}
return $month;
}
}

View File

@ -4,54 +4,32 @@ declare(strict_types=1);
namespace Toby\Infrastructure\Http\Controllers; namespace Toby\Infrastructure\Http\Controllers;
use Carbon\CarbonInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Facades\Excel; use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Toby\Domain\Enums\Month;
use Toby\Domain\TimesheetExport; use Toby\Domain\TimesheetExport;
use Toby\Eloquent\Helpers\YearPeriodRetriever; use Toby\Eloquent\Helpers\YearPeriodRetriever;
use Toby\Eloquent\Models\User; use Toby\Eloquent\Models\User;
class TimesheetController extends Controller class TimesheetController extends Controller
{ {
public function __invoke(Request $request, YearPeriodRetriever $yearPeriodRetriever): BinaryFileResponse public function __invoke(Month $month, YearPeriodRetriever $yearPeriodRetriever): BinaryFileResponse
{ {
$yearPeriod = $yearPeriodRetriever->selected(); $yearPeriod = $yearPeriodRetriever->selected();
$month = Str::lower($request->query("month", Carbon::now()->englishMonth)); $carbonMonth = Carbon::create($yearPeriod->year, $month->toCarbonNumber());
$month = Carbon::create($yearPeriod->year, $this->monthNameToNumber($month));
$users = User::query() $users = User::query()
->orderBy("last_name") ->orderBy("last_name")
->orderBy("first_name") ->orderBy("first_name")
->get(); ->get();
$filename = "{$month->translatedFormat("F Y")}.xlsx"; $filename = "{$carbonMonth->translatedFormat("F Y")}.xlsx";
$timesheet = (new TimesheetExport()) $timesheet = (new TimesheetExport())
->forMonth($month) ->forMonth($carbonMonth)
->forUsers($users); ->forUsers($users);
return Excel::download($timesheet, $filename); return Excel::download($timesheet, $filename);
} }
protected function monthNameToNumber(string $name): int
{
return match ($name) {
default => CarbonInterface::JANUARY,
"february" => CarbonInterface::FEBRUARY,
"march" => CarbonInterface::MARCH,
"april" => CarbonInterface::APRIL,
"may" => CarbonInterface::MAY,
"june" => CarbonInterface::JUNE,
"july" => CarbonInterface::JULY,
"august" => CarbonInterface::AUGUST,
"september" => CarbonInterface::SEPTEMBER,
"october" => CarbonInterface::OCTOBER,
"november" => CarbonInterface::NOVEMBER,
"december" => CarbonInterface::DECEMBER,
};
}
} }

View File

@ -4,11 +4,10 @@ declare(strict_types=1);
namespace Toby\Infrastructure\Http\Controllers; namespace Toby\Infrastructure\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Inertia\Response; use Inertia\Response;
use Toby\Domain\CalendarGenerator; use Toby\Domain\CalendarGenerator;
use Toby\Domain\Enums\Month;
use Toby\Eloquent\Helpers\YearPeriodRetriever; use Toby\Eloquent\Helpers\YearPeriodRetriever;
use Toby\Eloquent\Models\User; use Toby\Eloquent\Models\User;
use Toby\Infrastructure\Http\Resources\UserResource; use Toby\Infrastructure\Http\Resources\UserResource;
@ -16,22 +15,25 @@ use Toby\Infrastructure\Http\Resources\UserResource;
class VacationCalendarController extends Controller class VacationCalendarController extends Controller
{ {
public function index( public function index(
Request $request,
YearPeriodRetriever $yearPeriodRetriever, YearPeriodRetriever $yearPeriodRetriever,
CalendarGenerator $calendarGenerator, CalendarGenerator $calendarGenerator,
?string $month = null,
): Response { ): Response {
$month = Str::lower($request->query("month", Carbon::now()->englishMonth)); $month = Month::fromNameOrCurrent((string)$month);
$yearPeriod = $yearPeriodRetriever->selected(); $yearPeriod = $yearPeriodRetriever->selected();
$carbonMonth = Carbon::create($yearPeriod->year, $month->toCarbonNumber());
$users = User::query() $users = User::query()
->orderBy("last_name") ->orderBy("last_name")
->orderBy("first_name") ->orderBy("first_name")
->get(); ->get();
$calendar = $calendarGenerator->generate($yearPeriod, $month); $calendar = $calendarGenerator->generate($carbonMonth);
return inertia("Calendar", [ return inertia("Calendar", [
"calendar" => $calendar, "calendar" => $calendar,
"currentMonth" => $month, "currentMonth" => $month->value,
"users" => UserResource::collection($users), "users" => UserResource::collection($users),
]); ]);
} }

View File

@ -9,7 +9,7 @@
</div> </div>
<div> <div>
<a <a
:href="`timesheet?month=${currentMonth}`" :href="`timesheet/${selectedMonth.value}`"
class="inline-flex items-center px-4 py-3 border border-transparent text-sm leading-4 font-medium rounded-md shadow-sm text-white bg-blumilk-600 hover:bg-blumilk-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blumilk-500" class="inline-flex items-center px-4 py-3 border border-transparent text-sm leading-4 font-medium rounded-md shadow-sm text-white bg-blumilk-600 hover:bg-blumilk-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blumilk-500"
> >
Pobierz plik excel Pobierz plik excel
@ -52,8 +52,7 @@
v-slot="{ active }" v-slot="{ active }"
> >
<InertiaLink <InertiaLink
href="/vacation-calendar" :href="`/vacation-calendar/${month.value}`"
:data="{ month: month.value }"
:class="[active ? 'bg-gray-100 text-gray-900' : 'text-gray-700', 'flex w-full font-normal px-4 py-2 text-sm']" :class="[active ? 'bg-gray-100 text-gray-900' : 'text-gray-700', 'flex w-full font-normal px-4 py-2 text-sm']"
> >
{{ month.name }} {{ month.name }}

View File

@ -25,9 +25,9 @@ Route::middleware("auth")->group(function (): void {
Route::get("/vacation-limits", [VacationLimitController::class, "edit"]) Route::get("/vacation-limits", [VacationLimitController::class, "edit"])
->name("vacation.limits"); ->name("vacation.limits");
Route::get("/vacation-calendar", [VacationCalendarController::class, "index"]) Route::get("/vacation-calendar/{month?}", [VacationCalendarController::class, "index"])
->name("vacation.calendar"); ->name("vacation.calendar");
Route::get("/timesheet", TimesheetController::class) Route::get("/timesheet/{month}", TimesheetController::class)
->name("timesheet"); ->name("timesheet");
Route::get("/vacation-limits", [VacationLimitController::class, "edit"])->name("vacation.limits"); Route::get("/vacation-limits", [VacationLimitController::class, "edit"])->name("vacation.limits");