diff --git a/.gitignore b/.gitignore index f9cac8a5..e9785d14 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ google-credentials.json .composer .deployment /.phpunit.cache +.php-cs-fixer.cache diff --git a/app/Actions/CreateUserAction.php b/app/Actions/CreateUserAction.php index 41051055..fca87ac2 100644 --- a/app/Actions/CreateUserAction.php +++ b/app/Actions/CreateUserAction.php @@ -5,7 +5,6 @@ namespace Toby\Actions; use Toby\Models\User; -use Toby\Models\YearPeriod; class CreateUserAction { @@ -17,19 +16,6 @@ public function execute(array $userData, array $profileData): User $user->profile()->create($profileData); - $this->createVacationLimitsFor($user); - return $user; } - - protected function createVacationLimitsFor(User $user): void - { - $yearPeriods = YearPeriod::all(); - - foreach ($yearPeriods as $yearPeriod) { - $user->vacationLimits()->create([ - "year_period_id" => $yearPeriod->id, - ]); - } - } } diff --git a/app/Actions/CreateYearPeriodAction.php b/app/Actions/CreateYearPeriodAction.php deleted file mode 100644 index 3c392323..00000000 --- a/app/Actions/CreateYearPeriodAction.php +++ /dev/null @@ -1,53 +0,0 @@ - $year, - ]); - - $yearPeriod->save(); - - $this->createVacationLimitsFor($yearPeriod); - $this->createHolidaysFor($yearPeriod); - - return $yearPeriod; - } - - protected function createVacationLimitsFor(YearPeriod $yearPeriod): void - { - $users = User::all(); - - foreach ($users as $user) { - $yearPeriod->vacationLimits()->create([ - "user_id" => $user->id, - ]); - } - } - - protected function createHolidaysFor(YearPeriod $yearPeriod): void - { - $holidays = $this->polishHolidaysRetriever->getForYearPeriod($yearPeriod); - - foreach ($holidays as $holiday) { - $yearPeriod->holidays()->create([ - "name" => $holiday["name"], - "date" => $holiday["date"], - ]); - } - } -} diff --git a/app/Actions/VacationRequest/CreateAction.php b/app/Actions/VacationRequest/CreateAction.php index bc92ddc9..34441ac9 100644 --- a/app/Actions/VacationRequest/CreateAction.php +++ b/app/Actions/VacationRequest/CreateAction.php @@ -63,7 +63,6 @@ protected function createVacationRequest(array $data, User $creator): VacationRe $vacationRequest->vacations()->create([ "date" => $day, "user_id" => $vacationRequest->user->id, - "year_period_id" => $vacationRequest->yearPeriod->id, ]); } diff --git a/app/Console/Commands/GenerateHolidays.php b/app/Console/Commands/GenerateHolidays.php new file mode 100644 index 00000000..eb30ea05 --- /dev/null +++ b/app/Console/Commands/GenerateHolidays.php @@ -0,0 +1,31 @@ +argument("year") ?? Carbon::now()->year; + + $holidays = $polishHolidaysRetriever->getForYear($year); + + foreach ($holidays as $holiday) { + Holiday::query() + ->updateOrCreate([ + "name" => $holiday["name"], + "date" => $holiday["date"], + ]); + } + } +} diff --git a/app/Console/Commands/MigrateYearPeriodYearToVacationLimits.php b/app/Console/Commands/MigrateYearPeriodYearToVacationLimits.php new file mode 100644 index 00000000..a1c82fe3 --- /dev/null +++ b/app/Console/Commands/MigrateYearPeriodYearToVacationLimits.php @@ -0,0 +1,31 @@ +error("Year periods don't exist"); + + return; + } + + DB::statement(" + UPDATE vacation_limits + SET year = year_periods.year + FROM year_periods + WHERE year_periods.id = vacation_limits.year_period_id + "); + } +} diff --git a/app/Console/Commands/RebuildDocumentNumberingSystem.php b/app/Console/Commands/RebuildDocumentNumberingSystem.php index dd506ef7..82714a45 100644 --- a/app/Console/Commands/RebuildDocumentNumberingSystem.php +++ b/app/Console/Commands/RebuildDocumentNumberingSystem.php @@ -5,7 +5,8 @@ namespace Toby\Console\Commands; use Illuminate\Console\Command; -use Toby\Models\YearPeriod; +use Illuminate\Support\Facades\DB; +use Toby\Models\VacationRequest; class RebuildDocumentNumberingSystem extends Command { @@ -14,18 +15,20 @@ class RebuildDocumentNumberingSystem extends Command public function handle(): void { - $yearPeriods = YearPeriod::all(); + $years = DB::table(VacationRequest::class) + ->select([DB::raw("YEAR(from) as year")]) + ->groupBy("year") + ->value("year"); - foreach ($yearPeriods as $yearPeriod) { + foreach ($years as $year) { $number = 1; - $vacationRequests = $yearPeriod - ->vacationRequests() - ->oldest() + $vacationRequests = VacationRequest::query() + ->whereYear("date", $year) ->get(); foreach ($vacationRequests as $vacationRequest) { - $vacationRequest->update(["name" => "{$number}/{$yearPeriod->year}"]); + $vacationRequest->update(["name" => "{$number}/{$year}"]); $number++; } diff --git a/app/Domain/CalendarGenerator.php b/app/Domain/CalendarGenerator.php index 4d04c395..dd106a09 100644 --- a/app/Domain/CalendarGenerator.php +++ b/app/Domain/CalendarGenerator.php @@ -7,23 +7,19 @@ use Carbon\CarbonPeriod; use Illuminate\Support\Carbon; use Illuminate\Support\Collection; -use Toby\Helpers\YearPeriodRetriever; use Toby\Http\Resources\SimpleVacationRequestResource; +use Toby\Models\Holiday; use Toby\Models\Vacation; -use Toby\Models\YearPeriod; class CalendarGenerator { - public function __construct( - protected YearPeriodRetriever $yearPeriodRetriever, - ) {} - public function generate(Carbon $month): array { $period = CarbonPeriod::create($month->copy()->startOfMonth(), $month->copy()->endOfMonth()); - $yearPeriod = YearPeriod::findByYear($month->year); - $holidays = $yearPeriod->holidays()->pluck("date"); + $holidays = Holiday::query() + ->whereYear("date", $month->year) + ->pluck("date"); return $this->generateCalendar($period, $holidays); } @@ -63,14 +59,4 @@ protected function getVacationsForPeriod(CarbonPeriod $period): Collection ->get() ->groupBy(fn(Vacation $vacation): string => $vacation->date->toDateString()); } - - protected function getPendingVacationsForPeriod(CarbonPeriod $period): Collection - { - return Vacation::query() - ->whereBetween("date", [$period->start, $period->end]) - ->pending() - ->with("vacationRequest") - ->get() - ->groupBy(fn(Vacation $vacation): string => $vacation->date->toDateString()); - } } diff --git a/app/Domain/DashboardAggregator.php b/app/Domain/DashboardAggregator.php index 67493275..f6c09a26 100644 --- a/app/Domain/DashboardAggregator.php +++ b/app/Domain/DashboardAggregator.php @@ -14,7 +14,7 @@ use Toby\Models\Holiday; use Toby\Models\User; use Toby\Models\Vacation; -use Toby\Models\YearPeriod; +use Toby\Models\VacationRequest; class DashboardAggregator { @@ -24,14 +24,16 @@ public function __construct( protected UserBenefitsRetriever $benefitsRetriever, ) {} - public function aggregateStats(User $user, YearPeriod $yearPeriod): array + public function aggregateStats(User $user, ?int $year = null): array { - $limit = $this->vacationStatsRetriever->getVacationDaysLimit($user, $yearPeriod); - $hasLimit = $this->vacationStatsRetriever->hasVacationDaysLimit($user, $yearPeriod); - $used = $this->vacationStatsRetriever->getUsedVacationDays($user, $yearPeriod); - $pending = $this->vacationStatsRetriever->getPendingVacationDays($user, $yearPeriod); - $remoteWork = $this->vacationStatsRetriever->getRemoteWorkDays($user, $yearPeriod); - $other = $this->vacationStatsRetriever->getOtherApprovedVacationDays($user, $yearPeriod); + $year ??= Carbon::now()->year; + + $limit = $this->vacationStatsRetriever->getVacationDaysLimit($user, $year); + $hasLimit = $this->vacationStatsRetriever->hasVacationDaysLimit($user, $year); + $used = $this->vacationStatsRetriever->getUsedVacationDays($user, $year); + $pending = $this->vacationStatsRetriever->getPendingVacationDays($user, $year); + $remoteWork = $this->vacationStatsRetriever->getRemoteWorkDays($user, $year); + $other = $this->vacationStatsRetriever->getOtherApprovedVacationDays($user, $year); $remaining = $limit - $used - $pending; return [ @@ -45,12 +47,12 @@ public function aggregateStats(User $user, YearPeriod $yearPeriod): array ]; } - public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array + public function aggregateCalendarData(User $user, ?int $year = null): array { $approvedVacations = $user ->vacations() ->with(["vacationRequest.vacations", "vacationRequest.user.profile"]) - ->whereBelongsTo($yearPeriod) + ->whereYear("date", $year ?? Carbon::now()->year) ->cache("vacations:{$user->id}") ->approved() ->get() @@ -63,7 +65,7 @@ public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array $pendingVacations = $user ->vacations() ->with(["vacationRequest.vacations", "vacationRequest.user.profile"]) - ->whereBelongsTo($yearPeriod) + ->whereYear("date", $year ?? Carbon::now()->year) ->cache("vacations:{$user->id}") ->pending() ->get() @@ -73,8 +75,9 @@ public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array ], ); - $holidays = $yearPeriod - ->holidays + $holidays = Holiday::query() + ->whereYear("date", $year) + ->get() ->mapWithKeys(fn(Holiday $holiday): array => [$holiday->date->toDateString() => $holiday->name]); return [ @@ -84,23 +87,21 @@ public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array ]; } - public function aggregateVacationRequests(User $user, YearPeriod $yearPeriod): JsonResource + public function aggregateVacationRequests(User $user, ?int $year = null): JsonResource { - if ($user->can("listAllRequests")) { - $vacationRequests = $yearPeriod->vacationRequests() - ->with(["user", "vacations", "vacations.user", "vacations.user.profile", "user.permissions", "user.profile"]) + $year ??= Carbon::now()->year; + + $query = $user->can("listAllRequests") + ? VacationRequest::query() ->states(VacationRequestStatesRetriever::waitingForUserActionStates($user)) - ->latest("updated_at") - ->limit(3) - ->get(); - } else { - $vacationRequests = $user->vacationRequests() - ->with(["user", "vacations", "vacations.user", "vacations.user.profile", "user.permissions", "user.profile"]) - ->whereBelongsTo($yearPeriod) - ->latest("updated_at") - ->limit(3) - ->get(); - } + : $user->vacationRequests(); + + $vacationRequests = $query + ->with(["user", "vacations", "vacations.user", "vacations.user.profile", "user.permissions", "user.profile"]) + ->whereYear("from", $year) + ->latest("updated_at") + ->limit(3) + ->get(); return VacationRequestResource::collection($vacationRequests); } diff --git a/app/Domain/PolishHolidaysRetriever.php b/app/Domain/PolishHolidaysRetriever.php index 858279ce..cb507f15 100644 --- a/app/Domain/PolishHolidaysRetriever.php +++ b/app/Domain/PolishHolidaysRetriever.php @@ -6,7 +6,6 @@ use Illuminate\Support\Carbon; use Illuminate\Support\Collection; -use Toby\Models\YearPeriod; use Yasumi\Holiday; use Yasumi\Yasumi; @@ -15,9 +14,9 @@ class PolishHolidaysRetriever protected const string PROVIDER_KEY = "Poland"; protected const string LANG_KEY = "pl"; - public function getForYearPeriod(YearPeriod $yearPeriod): Collection + public function getForYear(int $year): Collection { - $polishProvider = Yasumi::create(static::PROVIDER_KEY, $yearPeriod->year); + $polishProvider = Yasumi::create(static::PROVIDER_KEY, $year); $holidays = $polishProvider->getHolidays(); diff --git a/app/Domain/UnavailableDaysRetriever.php b/app/Domain/UnavailableDaysRetriever.php index a896a56e..70c2b2d4 100644 --- a/app/Domain/UnavailableDaysRetriever.php +++ b/app/Domain/UnavailableDaysRetriever.php @@ -5,10 +5,11 @@ namespace Toby\Domain; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Toby\Enums\VacationType; +use Toby\Models\Holiday; use Toby\Models\User; -use Toby\Models\YearPeriod; class UnavailableDaysRetriever { @@ -17,10 +18,12 @@ public function __construct( protected VacationTypeConfigRetriever $configRetriever, ) {} - public function getUnavailableDays(User $user, YearPeriod $yearPeriod, ?VacationType $vacationType = null): Collection + public function getUnavailableDays(User $user, ?int $year = null, ?VacationType $vacationType = null): Collection { + $year ??= Carbon::now()->year; + $unavailableDays = $user->vacations() - ->whereBelongsTo($yearPeriod) + ->whereYear("date", $year) ->whereRelation( "vacationRequest", fn(Builder $query): Builder => $query->noStates(VacationRequestStatesRetriever::failedStates()), @@ -28,8 +31,8 @@ public function getUnavailableDays(User $user, YearPeriod $yearPeriod, ?Vacation ->pluck("date"); if (!$vacationType || !$this->configRetriever->isDuringNonWorkDays($vacationType)) { - $unavailableDays->push(...$yearPeriod->weekends()); - $unavailableDays->push(...$yearPeriod->holidays()->pluck("date")); + $unavailableDays->push(...$this->getWeekends($year)); + $unavailableDays->push(...$this->getHolidays($year)); } return $unavailableDays @@ -37,4 +40,29 @@ public function getUnavailableDays(User $user, YearPeriod $yearPeriod, ?Vacation ->sort() ->values(); } + + protected function getWeekends(int $year): Collection + { + $start = Carbon::create($year); + $end = Carbon::create($year)->endOfYear(); + + $weekends = new Collection(); + + while ($start->lessThanOrEqualTo($end)) { + if ($start->isWeekend()) { + $weekends->push($start->copy()); + } + + $start->addDay(); + } + + return $weekends; + } + + protected function getHolidays(int $year): Collection + { + return Holiday::query() + ->whereYear("date", $year) + ->pluck("date"); + } } diff --git a/app/Domain/UserVacationStatsRetriever.php b/app/Domain/UserVacationStatsRetriever.php index 2f666b6b..dc060653 100644 --- a/app/Domain/UserVacationStatsRetriever.php +++ b/app/Domain/UserVacationStatsRetriever.php @@ -5,11 +5,11 @@ namespace Toby\Domain; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Toby\Enums\VacationType; use Toby\Models\User; use Toby\Models\Vacation; -use Toby\Models\YearPeriod; class UserVacationStatsRetriever { @@ -17,11 +17,11 @@ public function __construct( protected VacationTypeConfigRetriever $configRetriever, ) {} - public function getUsedVacationDays(User $user, YearPeriod $yearPeriod): int + public function getUsedVacationDays(User $user, ?int $year = null): int { return $user ->vacations() - ->whereBelongsTo($yearPeriod) + ->whereYear("date", $year) ->whereRelation( "vacationRequest", fn(Builder $query): Builder => $query @@ -31,10 +31,10 @@ public function getUsedVacationDays(User $user, YearPeriod $yearPeriod): int ->count(); } - public function getUsedVacationDaysByMonth(User $user, YearPeriod $yearPeriod): Collection + public function getUsedVacationDaysByMonth(User $user, ?int $year = null): Collection { return $user->vacations() - ->whereBelongsTo($yearPeriod) + ->whereYear("date", $year) ->whereRelation( "vacationRequest", fn(Builder $query): Builder => $query @@ -46,11 +46,11 @@ public function getUsedVacationDaysByMonth(User $user, YearPeriod $yearPeriod): ->map(fn(Collection $items): int => $items->count()); } - public function getPendingVacationDays(User $user, YearPeriod $yearPeriod): int + public function getPendingVacationDays(User $user, ?int $year = null): int { return $user ->vacations() - ->whereBelongsTo($yearPeriod) + ->whereYear("date", $year) ->whereRelation( "vacationRequest", fn(Builder $query): Builder => $query @@ -60,11 +60,11 @@ public function getPendingVacationDays(User $user, YearPeriod $yearPeriod): int ->count(); } - public function getOtherApprovedVacationDays(User $user, YearPeriod $yearPeriod): int + public function getOtherApprovedVacationDays(User $user, ?int $year = null): int { return $user ->vacations() - ->whereBelongsTo($yearPeriod) + ->whereYear("date", $year) ->whereRelation( "vacationRequest", fn(Builder $query): Builder => $query @@ -76,11 +76,11 @@ public function getOtherApprovedVacationDays(User $user, YearPeriod $yearPeriod) ->count(); } - public function getRemoteWorkDays(User $user, YearPeriod $yearPeriod): int + public function getRemoteWorkDays(User $user, ?int $year = null): int { return $user ->vacations() - ->whereBelongsTo($yearPeriod) + ->whereYear("date", $year) ->whereRelation( "vacationRequest", fn(Builder $query): Builder => $query @@ -90,27 +90,27 @@ public function getRemoteWorkDays(User $user, YearPeriod $yearPeriod): int ->count(); } - public function getRemainingVacationDays(User $user, YearPeriod $yearPeriod): int + public function getRemainingVacationDays(User $user, ?int $year = null): int { - $limit = $this->getVacationDaysLimit($user, $yearPeriod); - $used = $this->getUsedVacationDays($user, $yearPeriod); - $pending = $this->getPendingVacationDays($user, $yearPeriod); + $limit = $this->getVacationDaysLimit($user, $year); + $used = $this->getUsedVacationDays($user, $year); + $pending = $this->getPendingVacationDays($user, $year); return $limit - $used - $pending; } - public function getVacationDaysLimit(User $user, YearPeriod $yearPeriod): int + public function getVacationDaysLimit(User $user, ?int $year = null): int { return $user->vacationLimits() - ->whereBelongsTo($yearPeriod) + ->where("year", $year ?? Carbon::today()->year) ->cache("vacations:{$user->id}") ->first()?->limit ?? 0; } - public function hasVacationDaysLimit(User $user, YearPeriod $yearPeriod): bool + public function hasVacationDaysLimit(User $user, ?int $year = null): bool { return $user->vacationLimits() - ->whereBelongsTo($yearPeriod) + ->where("year", $year ?? Carbon::today()->year) ->cache("vacations:{$user->id}") ->first()?->hasVacation() ?? false; } diff --git a/app/Domain/WorkDaysCalculator.php b/app/Domain/WorkDaysCalculator.php index 1363d81f..c9fd44f9 100644 --- a/app/Domain/WorkDaysCalculator.php +++ b/app/Domain/WorkDaysCalculator.php @@ -8,7 +8,7 @@ use Carbon\CarbonPeriod; use Illuminate\Support\Collection; use Toby\Enums\VacationType; -use Toby\Models\YearPeriod; +use Toby\Models\Holiday; class WorkDaysCalculator { @@ -19,8 +19,7 @@ public function __construct( public function calculateDays(CarbonInterface $from, CarbonInterface $to, ?VacationType $vacationType = null): Collection { $period = CarbonPeriod::create($from, $to); - $yearPeriod = YearPeriod::findByYear($from->year); - $holidays = $yearPeriod->holidays()->pluck("date"); + $holidays = $this->getHolidays($from->year); $validDays = new Collection(); @@ -49,4 +48,11 @@ protected function passes(CarbonInterface $day, Collection $holidays, ?VacationT return true; } + + protected function getHolidays(int $year): Collection + { + return Holiday::query() + ->whereYear("date", $year) + ->pluck("date"); + } } diff --git a/app/Helpers/YearPeriodRetriever.php b/app/Helpers/YearPeriodRetriever.php deleted file mode 100644 index a9b04e60..00000000 --- a/app/Helpers/YearPeriodRetriever.php +++ /dev/null @@ -1,54 +0,0 @@ -find($this->session->get(static::SESSION_KEY)); - - return $yearPeriod !== null ? $yearPeriod : $this->current(); - } - - public function current(): YearPeriod - { - return YearPeriod::current(); - } - - public function links(): array - { - $selected = $this->selected(); - $current = $this->current(); - - $years = YearPeriod::query()->cache()->get(); - - $navigation = $years->map(fn(YearPeriod $yearPeriod): array => $this->toNavigation($yearPeriod)); - - return [ - "current" => $this->toNavigation($current), - "selected" => $this->toNavigation($selected), - "navigation" => $navigation->toArray(), - ]; - } - - protected function toNavigation(YearPeriod $yearPeriod): array - { - return [ - "year" => $yearPeriod->year, - "link" => route("year-periods.select", $yearPeriod->id), - ]; - } -} diff --git a/app/Http/Controllers/AnnualSummaryController.php b/app/Http/Controllers/AnnualSummaryController.php index 6100e71c..2278fa6d 100644 --- a/app/Http/Controllers/AnnualSummaryController.php +++ b/app/Http/Controllers/AnnualSummaryController.php @@ -5,36 +5,38 @@ namespace Toby\Http\Controllers; use Illuminate\Http\Request; +use Illuminate\Support\Carbon; use Inertia\Response; -use Toby\Helpers\YearPeriodRetriever; use Toby\Http\Resources\SimpleVacationRequestResource; use Toby\Models\Holiday; use Toby\Models\Vacation; class AnnualSummaryController extends Controller { - public function __invoke(Request $request, YearPeriodRetriever $yearPeriodRetriever): Response + public function __invoke(Request $request): Response { - $yearPeriod = $yearPeriodRetriever->selected(); + $year = $request->integer("year", Carbon::now()->year); - $holidays = $yearPeriod->holidays() + $holidays = Holiday::query() + ->whereYear("date", $year) ->get(); $vacations = $request->user() ->vacations() ->with("vacationRequest.vacations") - ->whereBelongsTo($yearPeriod) + ->whereYear("date", $year) ->approved() ->get(); $pendingVacations = $request->user() ->vacations() ->with("vacationRequest.vacations") - ->whereBelongsTo($yearPeriod) + ->whereYear("date", $year) ->pending() ->get(); return inertia("AnnualSummary", [ + "year" => $year, "holidays" => $holidays->mapWithKeys( fn(Holiday $holiday): array => [$holiday->date->toDateString() => $holiday->name], ), diff --git a/app/Http/Controllers/Api/CalculateUserUnavailableDaysController.php b/app/Http/Controllers/Api/CalculateUserUnavailableDaysController.php index 31cbf642..9442c8f6 100644 --- a/app/Http/Controllers/Api/CalculateUserUnavailableDaysController.php +++ b/app/Http/Controllers/Api/CalculateUserUnavailableDaysController.php @@ -8,7 +8,6 @@ use Illuminate\Support\Carbon; use Toby\Domain\UnavailableDaysRetriever; use Toby\Domain\UserVacationStatsRetriever; -use Toby\Helpers\YearPeriodRetriever; use Toby\Http\Controllers\Controller; use Toby\Http\Requests\Api\CalculateUserUnavailableDaysRequest; use Toby\Models\User; @@ -18,14 +17,13 @@ class CalculateUserUnavailableDaysController extends Controller public function __invoke( CalculateUserUnavailableDaysRequest $request, UserVacationStatsRetriever $vacationStatsRetriever, - YearPeriodRetriever $yearPeriodRetriever, UnavailableDaysRetriever $unavailableDaysRetriever, ): JsonResponse { /** @var User $user */ $user = User::query()->find($request->get("user")); - $yearPeriod = $yearPeriodRetriever->selected(); + $year = $request->getYear(); - $unavailableDays = $unavailableDaysRetriever->getUnavailableDays($user, $yearPeriod, $request->vacationType()) + $unavailableDays = $unavailableDaysRetriever->getUnavailableDays($user, $year, $request->vacationType()) ->map(fn(Carbon $date): string => $date->toDateString()) ->toArray(); diff --git a/app/Http/Controllers/Api/CalculateUserVacationStatsController.php b/app/Http/Controllers/Api/CalculateUserVacationStatsController.php index 88bfe710..ec013b30 100644 --- a/app/Http/Controllers/Api/CalculateUserVacationStatsController.php +++ b/app/Http/Controllers/Api/CalculateUserVacationStatsController.php @@ -6,7 +6,6 @@ use Illuminate\Http\JsonResponse; use Toby\Domain\UserVacationStatsRetriever; -use Toby\Helpers\YearPeriodRetriever; use Toby\Http\Controllers\Controller; use Toby\Http\Requests\Api\CalculateVacationStatsRequest; use Toby\Models\User; @@ -16,16 +15,15 @@ class CalculateUserVacationStatsController extends Controller public function __invoke( CalculateVacationStatsRequest $request, UserVacationStatsRetriever $vacationStatsRetriever, - YearPeriodRetriever $yearPeriodRetriever, ): JsonResponse { /** @var User $user */ $user = User::query()->find($request->get("user")); - $yearPeriod = $yearPeriodRetriever->selected(); + $year = $request->getYear(); - $limit = $vacationStatsRetriever->getVacationDaysLimit($user, $yearPeriod); - $used = $vacationStatsRetriever->getUsedVacationDays($user, $yearPeriod); - $pending = $vacationStatsRetriever->getPendingVacationDays($user, $yearPeriod); - $other = $vacationStatsRetriever->getOtherApprovedVacationDays($user, $yearPeriod); + $limit = $vacationStatsRetriever->getVacationDaysLimit($user, $year); + $used = $vacationStatsRetriever->getUsedVacationDays($user, $year); + $pending = $vacationStatsRetriever->getPendingVacationDays($user, $year); + $other = $vacationStatsRetriever->getOtherApprovedVacationDays($user, $year); $remaining = $limit - $used - $pending; return new JsonResponse([ diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 6798a49c..23cf14a5 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -5,33 +5,32 @@ namespace Toby\Http\Controllers; use Illuminate\Http\Request; +use Illuminate\Support\Carbon; use Inertia\Response; use Toby\Domain\DailySummaryRetriever; use Toby\Domain\DashboardAggregator; use Toby\Domain\UserVacationStatsRetriever; use Toby\Domain\VacationTypeConfigRetriever; -use Toby\Helpers\YearPeriodRetriever; class DashboardController extends Controller { public function __invoke( Request $request, - YearPeriodRetriever $yearPeriodRetriever, UserVacationStatsRetriever $vacationStatsRetriever, VacationTypeConfigRetriever $configRetriever, DailySummaryRetriever $dailySummaryRetriever, DashboardAggregator $dashboardAggregator, ): Response { $user = $request->user(); - $yearPeriod = $yearPeriodRetriever->selected(); + $year = Carbon::now()->year; return inertia("Dashboard", [ "current" => $dashboardAggregator->aggregateCurrentData(), "upcoming" => $dashboardAggregator->aggregateUpcomingData(), - "vacationRequests" => $dashboardAggregator->aggregateVacationRequests($user, $yearPeriod), + "vacationRequests" => $dashboardAggregator->aggregateVacationRequests($user, $year), "benefits" => $dashboardAggregator->aggregateUserBenefits($user), - "calendar" => $dashboardAggregator->aggregateCalendarData($user, $yearPeriod), - "stats" => $dashboardAggregator->aggregateStats($user, $yearPeriod), + "calendar" => $dashboardAggregator->aggregateCalendarData($user, $year), + "stats" => $dashboardAggregator->aggregateStats($user, $year), ]); } } diff --git a/app/Http/Controllers/HolidayController.php b/app/Http/Controllers/HolidayController.php index 94cb60c1..968f0829 100644 --- a/app/Http/Controllers/HolidayController.php +++ b/app/Http/Controllers/HolidayController.php @@ -7,8 +7,8 @@ use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; +use Illuminate\Support\Carbon; use Inertia\Response; -use Toby\Helpers\YearPeriodRetriever; use Toby\Http\Requests\HolidayRequest; use Toby\Http\Resources\HolidayFormDataResource; use Toby\Http\Resources\HolidayResource; @@ -16,17 +16,18 @@ class HolidayController extends Controller { - public function index(Request $request, YearPeriodRetriever $yearPeriodRetriever): Response + public function index(Request $request): Response { - $yearPeriod = $yearPeriodRetriever->selected(); + $year = $request->integer("year", Carbon::now()->year); - $holidays = $yearPeriod - ->holidays() + $holidays = Holiday::query() + ->whereYear("date", $year) ->orderBy("date") ->get(); return inertia("Holidays/Index", [ "holidays" => HolidayResource::collection($holidays), + "year" => $year, ]); } diff --git a/app/Http/Controllers/MonthlyUsageController.php b/app/Http/Controllers/MonthlyUsageController.php index de62e046..5e572d7b 100644 --- a/app/Http/Controllers/MonthlyUsageController.php +++ b/app/Http/Controllers/MonthlyUsageController.php @@ -5,10 +5,10 @@ namespace Toby\Http\Controllers; use Illuminate\Http\Request; +use Illuminate\Support\Carbon; use Inertia\Response; use Toby\Domain\UserVacationStatsRetriever; use Toby\Enums\Month; -use Toby\Helpers\YearPeriodRetriever; use Toby\Http\Resources\SimpleUserResource; use Toby\Models\User; @@ -16,33 +16,32 @@ class MonthlyUsageController extends Controller { public function __invoke( Request $request, - YearPeriodRetriever $yearPeriodRetriever, UserVacationStatsRetriever $statsRetriever, ): Response { $this->authorize("listMonthlyUsage"); - $currentYearPeriod = $yearPeriodRetriever->selected(); + $year = $request->integer("year", Carbon::now()->year); $currentUser = $request->user(); $users = User::query() ->withTrashed($currentUser->canSeeInactiveUsers()) - ->withVacationLimitIn($currentYearPeriod) + ->withVacationLimitIn($year) ->where("id", "!=", $currentUser->id) ->orderByProfileField("last_name") ->orderByProfileField("first_name") ->get(); - if ($currentUser->hasVacationLimit($currentYearPeriod)) { + if ($currentUser->hasVacationLimit($year)) { $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); + $vacationsByMonth = $statsRetriever->getUsedVacationDaysByMonth($user, $year); + $limit = $statsRetriever->getVacationDaysLimit($user, $year); + $used = $statsRetriever->getUsedVacationDays($user, $year); + $pending = $statsRetriever->getPendingVacationDays($user, $year); $remaining = $limit - $used - $pending; $monthlyUsage[] = [ @@ -57,6 +56,7 @@ public function __invoke( } return inertia("MonthlyUsage", [ + "year" => $year, "monthlyUsage" => $monthlyUsage, "currentMonth" => Month::current(), ]); diff --git a/app/Http/Controllers/OvertimeRequestController.php b/app/Http/Controllers/OvertimeRequestController.php index ec3c8dde..9274c666 100644 --- a/app/Http/Controllers/OvertimeRequestController.php +++ b/app/Http/Controllers/OvertimeRequestController.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; +use Illuminate\Support\Carbon; use Inertia\Response; use Toby\Actions\OvertimeRequest\AcceptAsTechnicalAction; use Toby\Actions\OvertimeRequest\CancelAction; @@ -16,7 +17,6 @@ use Toby\Actions\OvertimeRequest\SettleAction; use Toby\Domain\OvertimeRequestStatesRetriever; use Toby\Enums\SettlementType; -use Toby\Helpers\YearPeriodRetriever; use Toby\Http\Requests\OvertimeRequestRequest; use Toby\Http\Resources\OvertimeRequestActivityResource; use Toby\Http\Resources\OvertimeRequestResource; @@ -26,48 +26,50 @@ class OvertimeRequestController extends Controller { - public function index(Request $request, YearPeriodRetriever $yearPeriodRetriever): RedirectResponse|Response + public function index(Request $request): RedirectResponse|Response { if ($request->user()->can("listAllOvertimeRequests")) { return redirect()->route("overtime.requests.indexForApprovers"); } + $user = $request->user(); $this->authorize("canUseOvertimeRequestFunctionality", $user); $status = $request->get("status", "all"); + $year = $request->integer("year", Carbon::now()->year); $overtimeRequests = $user ->overtimeRequests() + ->whereYear("from", $year) ->with(["user.permissions", "user.profile"]) - ->whereBelongsTo($yearPeriodRetriever->selected()) ->latest() ->states(OvertimeRequestStatesRetriever::filterByStatusGroup($status, $request->user())) ->paginate(); $pending = $user ->overtimeRequests() - ->whereBelongsTo($yearPeriodRetriever->selected()) + ->whereYear("from", $year) ->states(OvertimeRequestStatesRetriever::pendingStates()) ->cache(key: "overtime:{$user->id}") ->count(); $success = $user ->overtimeRequests() - ->whereBelongsTo($yearPeriodRetriever->selected()) + ->whereYear("from", $year) ->states(OvertimeRequestStatesRetriever::successStates()) ->cache(key: "overtime:{$user->id}") ->count(); $failed = $user ->overtimeRequests() - ->whereBelongsTo($yearPeriodRetriever->selected()) + ->whereYear("from", $year) ->states(OvertimeRequestStatesRetriever::failedStates()) ->cache(key: "overtime:{$user->id}") ->count(); $settled = $user ->overtimeRequests() - ->whereBelongsTo($yearPeriodRetriever->selected()) + ->whereYear("from", $year) ->states(OvertimeRequestStatesRetriever::settledStates()) ->cache(key: "overtime:{$user->id}") ->count(); @@ -83,29 +85,30 @@ public function index(Request $request, YearPeriodRetriever $yearPeriodRetriever ], "filters" => [ "status" => $status, + "year" => $year, ], ]); } public function indexForApprovers( Request $request, - YearPeriodRetriever $yearPeriodRetriever, ): RedirectResponse|Response { if ($request->user()->cannot("listAllOvertimeRequests")) { abort(403); } - $yearPeriod = $yearPeriodRetriever->selected(); $status = $request->get("status", "all"); $user = $request->get("user"); + $year = $request->get("year"); + $authUser = $request->user(); $withTrashedUsers = $authUser->canSeeInactiveUsers(); $overtimeRequests = OvertimeRequest::query() ->with(["user.permissions", "user.profile"]) - ->whereBelongsTo($yearPeriod) ->whereRelation("user", fn(Builder $query): Builder => $query->withTrashed($withTrashedUsers)) ->when($user !== null, fn(Builder $query): Builder => $query->where("user_id", $user)) + ->when($year !== null, fn(Builder $query): Builder => $query->whereYear("from", $year)) ->states(OvertimeRequestStatesRetriever::filterByStatusGroup($status, $authUser)) ->latest() ->paginate(); @@ -122,6 +125,7 @@ public function indexForApprovers( "filters" => [ "status" => $status, "user" => (int)$user, + "year" => $year === null ? $year : (int)$year, ], ]); } diff --git a/app/Http/Controllers/OvertimeTimesheetController.php b/app/Http/Controllers/OvertimeTimesheetController.php index 108b4b10..616f2e06 100644 --- a/app/Http/Controllers/OvertimeTimesheetController.php +++ b/app/Http/Controllers/OvertimeTimesheetController.php @@ -9,20 +9,17 @@ use Symfony\Component\HttpFoundation\BinaryFileResponse; use Toby\Domain\OvertimeTimesheetExport; use Toby\Enums\EmploymentForm; -use Toby\Enums\Month; -use Toby\Helpers\YearPeriodRetriever; use Toby\Models\User; class OvertimeTimesheetController extends Controller { - public function __invoke( - Month $month, - YearPeriodRetriever $yearPeriodRetriever, - ): BinaryFileResponse { + public function __invoke(?string $month = null): BinaryFileResponse + { $this->authorize("manageRequestsAsAdministrativeApprover"); - $yearPeriod = $yearPeriodRetriever->selected(); - $carbonMonth = Carbon::create($yearPeriod->year, $month->toCarbonNumber()); + $month = Carbon::canBeCreatedFromFormat($month, "m-Y") + ? Carbon::createFromFormat("m-Y", $month) + : Carbon::now(); $users = User::query() ->whereRelation("profile", "employment_form", EmploymentForm::EmploymentContract) @@ -30,10 +27,10 @@ public function __invoke( ->orderByProfileField("first_name") ->get(); - $filename = "overtime-{$carbonMonth->translatedFormat("F Y")}.xlsx"; + $filename = "overtime-{$month->translatedFormat("F Y")}.xlsx"; $timesheet = (new OvertimeTimesheetExport()) - ->forMonth($carbonMonth) + ->forMonth($month) ->forUsers($users); return Excel::download($timesheet, $filename); diff --git a/app/Http/Controllers/SelectYearPeriodController.php b/app/Http/Controllers/SelectYearPeriodController.php deleted file mode 100644 index b35c08a2..00000000 --- a/app/Http/Controllers/SelectYearPeriodController.php +++ /dev/null @@ -1,22 +0,0 @@ -session()->put(YearPeriodRetriever::SESSION_KEY, $yearPeriod->id); - - return redirect() - ->back() - ->with("info", __("Selected year period changed.")); - } -} diff --git a/app/Http/Controllers/TimesheetController.php b/app/Http/Controllers/TimesheetController.php index f78f0745..a1956238 100644 --- a/app/Http/Controllers/TimesheetController.php +++ b/app/Http/Controllers/TimesheetController.php @@ -10,22 +10,18 @@ use Toby\Domain\TimesheetExport; use Toby\Domain\VacationTypeConfigRetriever; use Toby\Enums\EmploymentForm; -use Toby\Enums\Month; use Toby\Enums\VacationType; -use Toby\Helpers\YearPeriodRetriever; use Toby\Models\User; class TimesheetController extends Controller { - public function __invoke( - Month $month, - YearPeriodRetriever $yearPeriodRetriever, - VacationTypeConfigRetriever $configRetriever, - ): BinaryFileResponse { + public function __invoke(VacationTypeConfigRetriever $configRetriever, ?string $month = null): BinaryFileResponse + { $this->authorize("manageRequestsAsAdministrativeApprover"); - $yearPeriod = $yearPeriodRetriever->selected(); - $carbonMonth = Carbon::create($yearPeriod->year, $month->toCarbonNumber()); + $month = Carbon::canBeCreatedFromFormat($month, "m-Y") + ? Carbon::createFromFormat("m-Y", $month) + : Carbon::now(); $users = User::query() ->whereRelation("profile", "employment_form", EmploymentForm::EmploymentContract) @@ -41,10 +37,10 @@ public function __invoke( ) && $configRetriever->isVacation($type), ); - $filename = "{$carbonMonth->translatedFormat("F Y")}.xlsx"; + $filename = "{$month->translatedFormat("F Y")}.xlsx"; $timesheet = (new TimesheetExport()) - ->forMonth($carbonMonth) + ->forMonth($month) ->forUsers($users) ->forVacationTypes($types); diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 529226a9..05776d0b 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -7,6 +7,7 @@ use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; +use Illuminate\Support\Carbon; use Inertia\Response; use Toby\Actions\CreateUserAction; use Toby\Actions\SyncUserPermissionsWithRoleAction; @@ -14,7 +15,6 @@ use Toby\Domain\DashboardAggregator; use Toby\Enums\EmploymentForm; use Toby\Enums\Role; -use Toby\Helpers\YearPeriodRetriever; use Toby\Http\Requests\UserRequest; use Toby\Http\Resources\BirthdayResource; use Toby\Http\Resources\EquipmentItemResource; @@ -152,12 +152,11 @@ public function restore(User $user): RedirectResponse public function show( User $user, - YearPeriodRetriever $yearPeriodRetriever, DashboardAggregator $dashboardAggregator, ): Response { $this->authorize("manageUsers"); - $yearPeriod = $yearPeriodRetriever->selected(); + $year = Carbon::now()->year; $equipment = EquipmentItem::query() ->with("assignee") ->where("assignee_id", $user->id) @@ -165,13 +164,13 @@ public function show( ->get(); $vacationRequests = $user->vacationRequests() ->with(["user", "vacations", "vacations.user", "vacations.user.profile", "user.permissions", "user.profile"]) - ->whereBelongsTo($yearPeriod) + ->whereYear("from", $year) ->latest("updated_at") ->limit(2) ->get(); $overtimeRequests = $user->overtimeRequests() ->with(["user", "user.profile", "user.permissions"]) - ->whereBelongsTo($yearPeriod) + ->whereYear("from", $year) ->latest("updated_at") ->limit(2) ->get(); @@ -181,8 +180,8 @@ public function show( "vacationRequests" => VacationRequestResource::collection($vacationRequests), "overtimeRequests" => OvertimeRequestResource::collection($overtimeRequests), "benefits" => $dashboardAggregator->aggregateUserBenefits($user), - "calendar" => $dashboardAggregator->aggregateCalendarData($user, $yearPeriod), - "stats" => $dashboardAggregator->aggregateStats($user, $yearPeriod), + "calendar" => $dashboardAggregator->aggregateCalendarData($user, $year), + "stats" => $dashboardAggregator->aggregateStats($user, $year), "equipmentItems" => EquipmentItemResource::collection($equipment), "upcomingBirthday" => new BirthdayResource($user), "seniority" => $user->seniority(), diff --git a/app/Http/Controllers/VacationCalendarController.php b/app/Http/Controllers/VacationCalendarController.php index 67f404b8..0a7e8cb1 100644 --- a/app/Http/Controllers/VacationCalendarController.php +++ b/app/Http/Controllers/VacationCalendarController.php @@ -10,42 +10,25 @@ use Inertia\Response; use Toby\Domain\CalendarGenerator; use Toby\Domain\MonthWorkingHoursCalculator; -use Toby\Enums\Month; -use Toby\Helpers\YearPeriodRetriever; use Toby\Http\Resources\SimpleUserResource; use Toby\Models\User; -use Toby\Models\YearPeriod; class VacationCalendarController extends Controller { public function index( Request $request, - YearPeriodRetriever $yearPeriodRetriever, CalendarGenerator $calendarGenerator, MonthWorkingHoursCalculator $monthWorkingHoursCalculator, ?string $month = null, - ?int $year = null, ): Response|RedirectResponse { - if ($year !== null) { - return $this->changeYearPeriod($request, $month, $year); - } + $month = Carbon::canBeCreatedFromFormat($month, "m-Y") + ? Carbon::createFromFormat("m-Y", $month) + : Carbon::now(); - $month = Month::fromNameOrCurrent((string)$month); /** @var User $currentUser */ $currentUser = $request->user(); $withTrashedUsers = $currentUser->canSeeInactiveUsers(); - $yearPeriod = $yearPeriodRetriever->selected(); - $previousYearPeriod = YearPeriod::query() - ->where("year", "<", $yearPeriod->year) - ->orderBy("year", "desc") - ->first(); - $nextYearPeriod = YearPeriod::query() - ->where("year", ">", $yearPeriod->year) - ->orderBy("year") - ->first(); - $carbonMonth = Carbon::create($yearPeriod->year, $month->toCarbonNumber()); - $users = User::query() ->withTrashed($withTrashedUsers) ->where("id", "!=", $currentUser->id) @@ -55,7 +38,7 @@ public function index( $users->prepend($currentUser); - $calendar = $calendarGenerator->generate($carbonMonth); + $calendar = $calendarGenerator->generate($month); $workingHours = $currentUser->isEmployedOnEmploymentForm() ? $monthWorkingHoursCalculator->calculateHours($calendar, $currentUser) * config("toby.number_of_hours_on_employment_contract.full_time") : null; @@ -63,28 +46,9 @@ public function index( return inertia("Calendar", [ "calendar" => $calendar, "workingHours" => $workingHours, - "currentMonth" => Month::current(), - "currentYear" => Carbon::now()->year, - "selectedMonth" => $month->value, - "selectedYear" => $yearPeriod->year, + "selectedDate" => $month->toDateString(), "users" => SimpleUserResource::collection($users), "withBlockedUsers" => $withTrashedUsers, - "previousYearPeriod" => $previousYearPeriod, - "nextYearPeriod" => $nextYearPeriod, ]); } - - private function changeYearPeriod(Request $request, string $month, int $year): RedirectResponse - { - $yearPeriod = YearPeriod::query()->where("year", $year)->firstOrFail(); - - if ($yearPeriod->id !== $request->session()->get(YearPeriodRetriever::SESSION_KEY)) { - $request->session()->put(YearPeriodRetriever::SESSION_KEY, $yearPeriod->id); - - return redirect()->route("calendar", ["month" => $month]) - ->with("info", __("Year period changed.")); - } - - return redirect()->route("calendar", ["month" => $month]); - } } diff --git a/app/Http/Controllers/VacationLimitController.php b/app/Http/Controllers/VacationLimitController.php index 9434aca0..f24c1a1e 100644 --- a/app/Http/Controllers/VacationLimitController.php +++ b/app/Http/Controllers/VacationLimitController.php @@ -4,54 +4,49 @@ namespace Toby\Http\Controllers; -use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; +use Illuminate\Support\Carbon; use Inertia\Response; use Toby\Domain\UserVacationStatsRetriever; -use Toby\Helpers\YearPeriodRetriever; use Toby\Http\Requests\TakeDaysFromLastYearRequest; use Toby\Http\Requests\VacationLimitRequest; -use Toby\Http\Resources\UserResource; +use Toby\Http\Resources\SimpleUserResource; +use Toby\Models\User; use Toby\Models\VacationLimit; -use Toby\Models\YearPeriod; class VacationLimitController extends Controller { - public function edit(Request $request, YearPeriodRetriever $yearPeriodRetriever, UserVacationStatsRetriever $statsRetriever): Response + public function edit(Request $request, UserVacationStatsRetriever $statsRetriever): Response { $this->authorize("manageVacationLimits"); - $yearPeriod = $yearPeriodRetriever->selected(); - $previousYearPeriod = YearPeriod::findByYear($yearPeriod->year - 1); + $year = $request->integer("year", Carbon::now()->year); - $limits = $yearPeriod - ->vacationLimits() - ->whereRelation("user", fn(Builder $query): Builder => $query->withTrashed($request->user()->canSeeInactiveUsers())) - ->with("user.profile") - ->has("user") + $users = User::query() + ->with(["vacationLimits" => fn(HasMany $query): HasMany => $query->where("year", $year)->limit(1)]) + ->withTrashed($request->user()->canSeeInactiveUsers()) ->get() - ->sortBy( - fn( - VacationLimit $limit, - ): string => "{$limit->user->profile->last_name} {$limit->user->profile->first_name}", - ) + ->sortBy(fn(User $user): string => "{$user->profile->last_name} {$user->profile->first_name}") ->values(); - $limitsResource = $limits->map(fn(VacationLimit $limit): array => [ - "id" => $limit->id, - "user" => new UserResource($limit->user), - "hasVacation" => $limit->hasVacation(), - "days" => $limit->days, - "limit" => $limit->limit, - "fromPreviousYear" => $limit->from_previous_year, - "toNextYear" => $limit->to_next_year, - "remainingLastYear" => $previousYearPeriod - ? $statsRetriever->getRemainingVacationDays($limit->user, $previousYearPeriod) - : 0, - ]); + $limitsResource = $users->map(function (User $user) use ($statsRetriever, $year): array { + $limit = $user->vacationLimits->first(); + + return [ + "user" => new SimpleUserResource($user), + "hasVacation" => $limit?->hasVacation() ?? false, + "days" => $limit?->days ?? 0, + "limit" => $limit?->limit ?? 0, + "fromPreviousYear" => $limit?->from_previous_year ?? 0, + "toNextYear" => $limit?->to_next_year ?? 0, + "remainingLastYear" => $statsRetriever->getRemainingVacationDays($user, $year - 1), + ]; + }); return inertia("VacationLimits", [ + "year" => $year, "limits" => $limitsResource, ]); } @@ -60,10 +55,14 @@ public function update(VacationLimitRequest $request): RedirectResponse { $this->authorize("manageVacationLimits"); - $data = $request->data(); - - foreach ($request->vacationLimits() as $limit) { - $limit->update($data[$limit->id]); + foreach ($request->data() as $limit) { + VacationLimit::query()->updateOrCreate( + [ + "user_id" => $limit["user"], + "year" => $limit["year"], + ], + ["days" => $limit["days"]], + ); } return redirect() @@ -71,26 +70,25 @@ public function update(VacationLimitRequest $request): RedirectResponse ->with("success", __("Vacation limits updated.")); } - public function takeFromLastYear( - TakeDaysFromLastYearRequest $request, - VacationLimit $limit, - ): RedirectResponse { + public function takeFromLastYear(TakeDaysFromLastYearRequest $request): RedirectResponse + { $this->authorize("manageVacationLimits"); - $days = $request->getDays(); - $yearPeriod = $limit->yearPeriod; - $previousYearPeriod = YearPeriod::findByYear($yearPeriod->year - 1); + $data = $request->getData(); - if ($previousYearPeriod) { - $previousLimit = $limit->user->vacationLimits()->whereBelongsTo($previousYearPeriod)->first(); + $limit = VacationLimit::query()->updateOrCreate([ + "user_id" => $data["user"], + "year" => $data["year"], + ], [ + "from_previous_year" => $data["days"], + ]); - $previousLimit->update([ - "to_next_year" => $days, - ]); - } + $previousLimit = $limit->user->vacationLimits() + ->where("year", $limit->year - 1) + ->first(); - $limit->update([ - "from_previous_year" => $days, + $previousLimit?->update([ + "to_next_year" => $data["days"], ]); return redirect() diff --git a/app/Http/Controllers/VacationRequestController.php b/app/Http/Controllers/VacationRequestController.php index 693b0a1e..832b3390 100644 --- a/app/Http/Controllers/VacationRequestController.php +++ b/app/Http/Controllers/VacationRequestController.php @@ -10,6 +10,7 @@ use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Http\Response as LaravelResponse; +use Illuminate\Support\Carbon; use Illuminate\Validation\ValidationException; use Inertia\Response; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; @@ -22,7 +23,6 @@ use Toby\Domain\VacationRequestStatesRetriever; use Toby\Domain\VacationTypeConfigRetriever; use Toby\Enums\VacationType; -use Toby\Helpers\YearPeriodRetriever; use Toby\Http\Requests\VacationRequestRequest; use Toby\Http\Resources\SimpleUserResource; use Toby\Http\Resources\SimpleVacationRequestResource; @@ -35,7 +35,7 @@ class VacationRequestController extends Controller { - public function index(Request $request, YearPeriodRetriever $yearPeriodRetriever): Response|RedirectResponse + public function index(Request $request): Response|RedirectResponse { $user = $request->user(); @@ -43,13 +43,14 @@ public function index(Request $request, YearPeriodRetriever $yearPeriodRetriever return redirect()->route("vacation.requests.indexForApprovers"); } + $year = $request->integer("year", Carbon::now()->year); $status = $request->get("status", "all"); - $withoutRemote = $request->boolean("withoutRemote", default: false); + $withoutRemote = $request->boolean("withoutRemote"); $vacationRequests = $user ->vacationRequests() ->with(["vacations.user.profile", "user.permissions", "user.profile"]) - ->whereBelongsTo($yearPeriodRetriever->selected()) + ->whereYear("from", $year) ->latest() ->states(VacationRequestStatesRetriever::filterByStatusGroup($status, $user)) ->when($withoutRemote, fn(Builder $query): Builder => $query->excludeType(VacationType::RemoteWork)) @@ -57,7 +58,7 @@ public function index(Request $request, YearPeriodRetriever $yearPeriodRetriever $pending = $user ->vacationRequests() - ->whereBelongsTo($yearPeriodRetriever->selected()) + ->whereYear("from", $year) ->states(VacationRequestStatesRetriever::pendingStates()) ->when($withoutRemote, fn(Builder $query): Builder => $query->excludeType(VacationType::RemoteWork)) ->cache(key: "vacations:{$user->id}") @@ -65,7 +66,7 @@ public function index(Request $request, YearPeriodRetriever $yearPeriodRetriever $success = $user ->vacationRequests() - ->whereBelongsTo($yearPeriodRetriever->selected()) + ->whereYear("from", $year) ->states(VacationRequestStatesRetriever::successStates()) ->when($withoutRemote, fn(Builder $query): Builder => $query->excludeType(VacationType::RemoteWork)) ->cache(key: "vacations:{$user->id}") @@ -73,7 +74,7 @@ public function index(Request $request, YearPeriodRetriever $yearPeriodRetriever $failed = $user ->vacationRequests() - ->whereBelongsTo($yearPeriodRetriever->selected()) + ->whereYear("from", $year) ->states(VacationRequestStatesRetriever::failedStates()) ->when($withoutRemote, fn(Builder $query): Builder => $query->excludeType(VacationType::RemoteWork)) ->cache(key: "vacations:{$user->id}") @@ -89,19 +90,19 @@ public function index(Request $request, YearPeriodRetriever $yearPeriodRetriever ], "filters" => [ "status" => $status, + "year" => $year, ], ]); } public function indexForApprovers( Request $request, - YearPeriodRetriever $yearPeriodRetriever, ): RedirectResponse|Response { if ($request->user()->cannot("listAllRequests")) { return redirect()->route("vacation.requests.index"); } - $yearPeriod = $yearPeriodRetriever->selected(); + $year = $request->get("year"); $status = $request->get("status", "all"); $user = $request->get("user"); $type = $request->get("type"); @@ -110,10 +111,10 @@ public function indexForApprovers( $vacationRequests = VacationRequest::query() ->with(["vacations.user.profile", "user.permissions", "user.profile"]) - ->whereBelongsTo($yearPeriod) ->whereRelation("user", fn(Builder $query): Builder => $query->withTrashed($withTrashedUsers)) ->when($user !== null, fn(Builder $query): Builder => $query->where("user_id", $user)) ->when($type !== null, fn(Builder $query): Builder => $query->where("type", $type)) + ->when($year !== null, fn(Builder $query): Builder => $query->whereYear("from", $year)) ->states(VacationRequestStatesRetriever::filterByStatusGroup($status, $authUser)) ->latest() ->paginate(); @@ -132,6 +133,7 @@ public function indexForApprovers( "status" => $status, "user" => (int)$user, "type" => $type, + "year" => $year === null ? $year : (int)$year, ], ]); } @@ -142,21 +144,22 @@ public function indexForApprovers( public function show( VacationRequest $vacationRequest, UserVacationStatsRetriever $statsRetriever, - YearPeriodRetriever $yearPeriodRetriever, ): Response { $this->authorize("show", $vacationRequest); $vacationRequest->load(["vacations.user.profile", "user.permissions", "user.profile", "activities.user.profile"]); - $limit = $statsRetriever->getVacationDaysLimit($vacationRequest->user, $vacationRequest->yearPeriod); - $used = $statsRetriever->getUsedVacationDays($vacationRequest->user, $vacationRequest->yearPeriod); - $pending = $statsRetriever->getPendingVacationDays($vacationRequest->user, $vacationRequest->yearPeriod); + $year = $vacationRequest->from->year; + + $limit = $statsRetriever->getVacationDaysLimit($vacationRequest->user, $year); + $used = $statsRetriever->getUsedVacationDays($vacationRequest->user, $year); + $pending = $statsRetriever->getPendingVacationDays($vacationRequest->user, $year); $remaining = $limit - $used - $pending; - $yearPeriod = $yearPeriodRetriever->selected(); $requestFromDateMonth = $vacationRequest->from->month; $requestToDateMonth = $vacationRequest->to->month; - $holidays = $yearPeriod->holidays() + $holidays = Holiday::query() + ->whereYear("date", $year) ->get(); $user = $vacationRequest->user; @@ -164,14 +167,14 @@ public function show( $vacations = $user ->vacations() ->with("vacationRequest.vacations") - ->whereBelongsTo($yearPeriod) + ->whereYear("date", $year) ->approved() ->get(); $pendingVacations = $user ->vacations() ->with("vacationRequest.vacations") - ->whereBelongsTo($yearPeriod) + ->whereYear("date", $year) ->pending() ->get(); diff --git a/app/Http/Middleware/HandleInertiaRequests.php b/app/Http/Middleware/HandleInertiaRequests.php index 23544729..0f053905 100644 --- a/app/Http/Middleware/HandleInertiaRequests.php +++ b/app/Http/Middleware/HandleInertiaRequests.php @@ -12,7 +12,6 @@ use Spatie\Permission\Models\Permission; use Toby\Domain\OvertimeRequestStatesRetriever; use Toby\Domain\VacationRequestStatesRetriever; -use Toby\Helpers\YearPeriodRetriever; use Toby\Http\Resources\UserResource; use Toby\Models\OvertimeRequest; use Toby\Models\User; @@ -21,7 +20,6 @@ class HandleInertiaRequests extends Middleware { public function __construct( - protected YearPeriodRetriever $yearPeriodRetriever, protected CacheManager $cache, ) {} @@ -30,7 +28,6 @@ public function share(Request $request): array return array_merge(parent::share($request), [ "auth" => $this->getAuthData($request), "flash" => $this->getFlashData($request), - "years" => $this->getYearsData($request), "vacationRequestsCount" => $this->getVacationRequestsCount($request), "overtimeRequestsCount" => $this->getOvertimeRequestsCount($request), "deployInformation" => $this->getDeployInformation(), @@ -64,18 +61,12 @@ protected function getFlashData(Request $request): Closure ]; } - protected function getYearsData(Request $request): Closure - { - return fn(): array => $request->user() ? $this->yearPeriodRetriever->links() : []; - } - protected function getVacationRequestsCount(Request $request): Closure { $user = $request->user(); return fn(): ?int => $user && $user->can("listAllRequests") ? VacationRequest::query() - ->whereBelongsTo($this->yearPeriodRetriever->selected()) ->states( VacationRequestStatesRetriever::waitingForUserActionStates($user), ) @@ -89,7 +80,6 @@ protected function getOvertimeRequestsCount(Request $request): Closure return fn(): ?int => $user && $user->can("listAllRequests") ? OvertimeRequest::query() - ->whereBelongsTo($this->yearPeriodRetriever->selected()) ->states( OvertimeRequestStatesRetriever::waitingForUserActionStates($user), ) diff --git a/app/Http/Requests/Api/CalculateOvertimeHoursRequest.php b/app/Http/Requests/Api/CalculateOvertimeHoursRequest.php index 0529e5da..5b87808b 100644 --- a/app/Http/Requests/Api/CalculateOvertimeHoursRequest.php +++ b/app/Http/Requests/Api/CalculateOvertimeHoursRequest.php @@ -7,16 +7,14 @@ use Illuminate\Foundation\Http\FormRequest; use Illuminate\Support\Carbon; use Toby\Helpers\DateFormats; -use Toby\Http\Rules\YearPeriodExists; -use Toby\Models\YearPeriod; class CalculateOvertimeHoursRequest extends FormRequest { public function rules(): array { return [ - "from" => ["required", "date_format:" . DateFormats::DATETIME, new YearPeriodExists()], - "to" => ["required", "date_format:" . DateFormats::DATETIME, new YearPeriodExists()], + "from" => ["required", "date_format:" . DateFormats::DATETIME], + "to" => ["required", "date_format:" . DateFormats::DATETIME], ]; } @@ -29,9 +27,4 @@ public function to(): Carbon { return Carbon::create($this->request->get("to")); } - - public function yearPeriod(): YearPeriod - { - return YearPeriod::findByYear(Carbon::create($this->request->get("from"))->year); - } } diff --git a/app/Http/Requests/Api/CalculateUserUnavailableDaysRequest.php b/app/Http/Requests/Api/CalculateUserUnavailableDaysRequest.php index 3ea65554..7a7c882e 100644 --- a/app/Http/Requests/Api/CalculateUserUnavailableDaysRequest.php +++ b/app/Http/Requests/Api/CalculateUserUnavailableDaysRequest.php @@ -5,6 +5,7 @@ namespace Toby\Http\Requests\Api; use Illuminate\Foundation\Http\FormRequest; +use Illuminate\Support\Carbon; use Illuminate\Validation\Rules\Enum; use Toby\Enums\VacationType; @@ -15,6 +16,7 @@ public function rules(): array return [ "vacationType" => [new Enum(VacationType::class)], "user" => ["required", "exists:users,id"], + "year" => ["required", "integer"], ]; } @@ -24,4 +26,9 @@ public function vacationType(): ?VacationType ? VacationType::from($this->request->get("vacationType")) : null; } + + public function getYear(): int + { + return $this->integer("year", Carbon::now()->year); + } } diff --git a/app/Http/Requests/Api/CalculateVacationDaysRequest.php b/app/Http/Requests/Api/CalculateVacationDaysRequest.php index c8f2240f..e7df9263 100644 --- a/app/Http/Requests/Api/CalculateVacationDaysRequest.php +++ b/app/Http/Requests/Api/CalculateVacationDaysRequest.php @@ -9,8 +9,6 @@ use Illuminate\Validation\Rules\Enum; use Toby\Enums\VacationType; use Toby\Helpers\DateFormats; -use Toby\Http\Rules\YearPeriodExists; -use Toby\Models\YearPeriod; class CalculateVacationDaysRequest extends FormRequest { @@ -18,8 +16,8 @@ public function rules(): array { return [ "vacationType" => ["required", new Enum(VacationType::class)], - "from" => ["required", "date_format:" . DateFormats::DATE, new YearPeriodExists()], - "to" => ["required", "date_format:" . DateFormats::DATE, new YearPeriodExists()], + "from" => ["required", "date_format:" . DateFormats::DATE], + "to" => ["required", "date_format:" . DateFormats::DATE], ]; } @@ -37,9 +35,4 @@ public function to(): Carbon { return Carbon::create($this->request->get("to")); } - - public function yearPeriod(): YearPeriod - { - return YearPeriod::findByYear(Carbon::create($this->request->get("from"))->year); - } } diff --git a/app/Http/Requests/Api/CalculateVacationStatsRequest.php b/app/Http/Requests/Api/CalculateVacationStatsRequest.php index 0499f14b..8c378b1d 100644 --- a/app/Http/Requests/Api/CalculateVacationStatsRequest.php +++ b/app/Http/Requests/Api/CalculateVacationStatsRequest.php @@ -5,6 +5,7 @@ namespace Toby\Http\Requests\Api; use Illuminate\Foundation\Http\FormRequest; +use Illuminate\Support\Carbon; class CalculateVacationStatsRequest extends FormRequest { @@ -12,6 +13,12 @@ public function rules(): array { return [ "user" => ["required", "exists:users,id"], + "year" => ["required", "integer"], ]; } + + public function getYear(): int + { + return $this->integer("year", Carbon::now()->year); + } } diff --git a/app/Http/Requests/HolidayRequest.php b/app/Http/Requests/HolidayRequest.php index 1069f267..955fb966 100644 --- a/app/Http/Requests/HolidayRequest.php +++ b/app/Http/Requests/HolidayRequest.php @@ -5,11 +5,8 @@ namespace Toby\Http\Requests; use Illuminate\Foundation\Http\FormRequest; -use Illuminate\Support\Carbon; use Illuminate\Validation\Rule; use Toby\Helpers\DateFormats; -use Toby\Http\Rules\YearPeriodExists; -use Toby\Models\YearPeriod; class HolidayRequest extends FormRequest { @@ -20,7 +17,6 @@ public function rules(): array "date" => ["required", "date_format:" . DateFormats::DATE, Rule::unique("holidays", "date")->ignore($this->holiday), - new YearPeriodExists(), ], ]; } @@ -32,7 +28,6 @@ public function data(): array return [ "name" => $this->get("name"), "date" => $date, - "year_period_id" => YearPeriod::findByYear(Carbon::create($date)->year)->id, ]; } } diff --git a/app/Http/Requests/OvertimeRequestRequest.php b/app/Http/Requests/OvertimeRequestRequest.php index 1470fb63..9c32cd62 100644 --- a/app/Http/Requests/OvertimeRequestRequest.php +++ b/app/Http/Requests/OvertimeRequestRequest.php @@ -5,12 +5,9 @@ namespace Toby\Http\Requests; use Illuminate\Foundation\Http\FormRequest; -use Illuminate\Support\Carbon; use Illuminate\Validation\Rules\Enum; use Toby\Enums\SettlementType; use Toby\Helpers\DateFormats; -use Toby\Http\Rules\YearPeriodExists; -use Toby\Models\YearPeriod; class OvertimeRequestRequest extends FormRequest { @@ -18,8 +15,8 @@ public function rules(): array { return [ "user" => ["required", "exists:users,id"], - "from" => ["required", "date_format:" . DateFormats::DATETIME, new YearPeriodExists()], - "to" => ["required", "date_format:" . DateFormats::DATETIME, "after:from", new YearPeriodExists()], + "from" => ["required", "date_format:" . DateFormats::DATETIME], + "to" => ["required", "date_format:" . DateFormats::DATETIME, "after:from"], "type" => ["required", new Enum(SettlementType::class)], "comment" => ["nullable"], ]; @@ -32,13 +29,7 @@ public function data(): array "from" => $this->get("from"), "to" => $this->get("to"), "settlement_type" => $this->get("type"), - "year_period_id" => $this->yearPeriod()->id, "comment" => $this->get("comment"), ]; } - - public function yearPeriod(): YearPeriod - { - return YearPeriod::findByYear(Carbon::create($this->get("from"))->year); - } } diff --git a/app/Http/Requests/TakeDaysFromLastYearRequest.php b/app/Http/Requests/TakeDaysFromLastYearRequest.php index be2be566..28d0554d 100644 --- a/app/Http/Requests/TakeDaysFromLastYearRequest.php +++ b/app/Http/Requests/TakeDaysFromLastYearRequest.php @@ -11,12 +11,14 @@ class TakeDaysFromLastYearRequest extends FormRequest public function rules(): array { return [ + "user" => ["required", "exists:users,id"], + "year" => ["required", "integer"], "days" => ["required", "integer"], ]; } - public function getDays(): int + public function getData(): array { - return $this->integer("days"); + return $this->only(["user", "year", "days"]); } } diff --git a/app/Http/Requests/VacationLimitRequest.php b/app/Http/Requests/VacationLimitRequest.php index 9e01677a..e021b3db 100644 --- a/app/Http/Requests/VacationLimitRequest.php +++ b/app/Http/Requests/VacationLimitRequest.php @@ -6,7 +6,6 @@ use Illuminate\Foundation\Http\FormRequest; use Illuminate\Support\Collection; -use Toby\Models\VacationLimit; class VacationLimitRequest extends FormRequest { @@ -14,20 +13,14 @@ public function rules(): array { return [ "items" => ["required", "array"], - "items.*.id" => ["required", "exists:vacation_limits,id"], + "items.*.user" => ["required", "exists:users,id"], + "items.*.year" => ["required", "integer"], "items.*.days" => ["nullable", "integer", "min:0", "max:100"], ]; } - public function vacationLimits(): Collection + public function data(): Collection { - return VacationLimit::query()->find($this->collect("items")->pluck("id")); - } - - public function data(): array - { - return $this->collect("items") - ->keyBy("id") - ->toArray(); + return $this->collect("items"); } } diff --git a/app/Http/Requests/VacationRequestRequest.php b/app/Http/Requests/VacationRequestRequest.php index a089eaa5..4dca727c 100644 --- a/app/Http/Requests/VacationRequestRequest.php +++ b/app/Http/Requests/VacationRequestRequest.php @@ -5,12 +5,9 @@ namespace Toby\Http\Requests; use Illuminate\Foundation\Http\FormRequest; -use Illuminate\Support\Carbon; use Illuminate\Validation\Rules\Enum; use Toby\Enums\VacationType; use Toby\Helpers\DateFormats; -use Toby\Http\Rules\YearPeriodExists; -use Toby\Models\YearPeriod; class VacationRequestRequest extends FormRequest { @@ -19,8 +16,8 @@ public function rules(): array return [ "user" => ["required", "exists:users,id"], "type" => ["required", new Enum(VacationType::class)], - "from" => ["required", "date_format:" . DateFormats::DATE, new YearPeriodExists()], - "to" => ["required", "date_format:" . DateFormats::DATE, new YearPeriodExists()], + "from" => ["required", "date_format:" . DateFormats::DATE], + "to" => ["required", "date_format:" . DateFormats::DATE], "flowSkipped" => ["nullable", "boolean"], "comment" => ["nullable"], ]; @@ -33,17 +30,11 @@ public function data(): array "type" => $this->get("type"), "from" => $this->get("from"), "to" => $this->get("to"), - "year_period_id" => $this->yearPeriod()->id, "comment" => $this->get("comment"), "flow_skipped" => $this->boolean("flowSkipped"), ]; } - public function yearPeriod(): YearPeriod - { - return YearPeriod::findByYear(Carbon::create($this->get("from"))->year); - } - public function createsOnBehalfOfEmployee(): bool { return $this->user()->id !== $this->get("user"); diff --git a/app/Http/Resources/SimpleUserResource.php b/app/Http/Resources/SimpleUserResource.php index 006c9913..064fc770 100644 --- a/app/Http/Resources/SimpleUserResource.php +++ b/app/Http/Resources/SimpleUserResource.php @@ -16,6 +16,7 @@ public function toArray($request): array "id" => $this->id, "name" => $this->profile->full_name, "last_name" => $this->profile->last_name, + "employmentForm" => $this->profile->employment_form->label(), "email" => $this->email, "avatar" => $this->profile->getAvatar(), "isActive" => $this->deleted_at === null, diff --git a/app/Http/Rules/YearPeriodExists.php b/app/Http/Rules/YearPeriodExists.php deleted file mode 100644 index 1df8053a..00000000 --- a/app/Http/Rules/YearPeriodExists.php +++ /dev/null @@ -1,22 +0,0 @@ -year); - - if ($yearPeriod === null) { - $fail(__("The year period for given year does not exist.")); - } - } -} diff --git a/app/Jobs/CheckYearPeriod.php b/app/Jobs/CheckYearPeriod.php deleted file mode 100644 index 0be0fefa..00000000 --- a/app/Jobs/CheckYearPeriod.php +++ /dev/null @@ -1,32 +0,0 @@ -execute($now->year); - } - - if (YearPeriod::query()->max("year") === $now->year) { - $createYearPeriodAction->execute($now->year + 1); - } - } -} diff --git a/app/Models/Holiday.php b/app/Models/Holiday.php index b8c17542..0e3adf76 100644 --- a/app/Models/Holiday.php +++ b/app/Models/Holiday.php @@ -7,14 +7,12 @@ use Database\Factories\HolidayFactory; 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 string $name * @property Carbon $date - * @property YearPeriod $yearPeriod */ class Holiday extends Model { @@ -25,11 +23,6 @@ class Holiday extends Model "date" => "date", ]; - public function yearPeriod(): BelongsTo - { - return $this->belongsTo(YearPeriod::class); - } - protected static function newFactory(): HolidayFactory { return HolidayFactory::new(); diff --git a/app/Models/OvertimeRequest.php b/app/Models/OvertimeRequest.php index 7fc3ebdb..ea8ada59 100644 --- a/app/Models/OvertimeRequest.php +++ b/app/Models/OvertimeRequest.php @@ -28,7 +28,6 @@ * @property string $comment * @property User $user * @property User $creator - * @property YearPeriod $yearPeriod * @property Collection $activities * @property Carbon $created_at * @property Carbon $updated_at @@ -59,11 +58,6 @@ public function creator(): BelongsTo return $this->belongsTo(User::class, "creator_id"); } - public function yearPeriod(): BelongsTo - { - return $this->belongsTo(YearPeriod::class); - } - public function activities(): HasMany { return $this->hasMany(OvertimeRequestActivity::class); diff --git a/app/Models/User.php b/app/Models/User.php index d4b5f565..26c32163 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -142,10 +142,10 @@ public function keys(): HasMany return $this->hasMany(Key::class); } - public function hasVacationLimit(YearPeriod $yearPeriod): bool + public function hasVacationLimit(int $year): bool { return $this->vacationLimits() - ->whereBelongsTo($yearPeriod) + ->where("year", $year) ->whereNotNull("days") ->exists(); } @@ -181,12 +181,12 @@ public function scopeOrderByUserHistoryField(Builder $query, string $field, stri return $query->orderBy($profileQuery, $direction); } - public function scopeWithVacationLimitIn(Builder $query, YearPeriod $yearPeriod): Builder + public function scopeWithVacationLimitIn(Builder $query, int $year): Builder { return $query->whereRelation( "vacationlimits", fn(Builder $query): Builder => $query - ->whereBelongsTo($yearPeriod) + ->where("year", $year) ->whereNotNull("days"), ); } @@ -211,7 +211,7 @@ public function upcomingBirthday(): ?Carbon $birthday = $this->profile->birthday->setYear($today->year); - if (((int)$birthday->diffInDays(absolute: false)) > 0) { + if (((int)$birthday->diffInDays()) > 0) { $birthday->setYear($today->year + 1); } diff --git a/app/Models/Vacation.php b/app/Models/Vacation.php index f7eba140..daf4a1c9 100644 --- a/app/Models/Vacation.php +++ b/app/Models/Vacation.php @@ -18,7 +18,6 @@ * @property Carbon $date * @property User $user * @property VacationRequest $vacationRequest - * @property YearPeriod $yearPeriod */ class Vacation extends Model { @@ -41,11 +40,6 @@ public function vacationRequest(): BelongsTo return $this->belongsTo(VacationRequest::class); } - public function yearPeriod(): BelongsTo - { - return $this->belongsTo(YearPeriod::class); - } - public function scopeApproved(Builder $query): Builder { return $query->whereRelation( diff --git a/app/Models/VacationLimit.php b/app/Models/VacationLimit.php index 06fbfd33..1dc4d46b 100644 --- a/app/Models/VacationLimit.php +++ b/app/Models/VacationLimit.php @@ -12,7 +12,7 @@ /** * @property int $id * @property User $user - * @property YearPeriod $yearPeriod + * @property int $year * @property int $limit * @property int $days * @property int $from_previous_year @@ -35,11 +35,6 @@ public function user(): BelongsTo ->withTrashed(); } - public function yearPeriod(): BelongsTo - { - return $this->belongsTo(YearPeriod::class); - } - protected static function newFactory(): VacationLimitFactory { return VacationLimitFactory::new(); diff --git a/app/Models/VacationRequest.php b/app/Models/VacationRequest.php index 42c7aab8..6c9bea24 100644 --- a/app/Models/VacationRequest.php +++ b/app/Models/VacationRequest.php @@ -29,7 +29,6 @@ * @property bool $flow_skipped * @property User $user * @property User $creator - * @property YearPeriod $yearPeriod * @property Collection $activities * @property Collection $vacations * @property Collection $event_ids @@ -62,11 +61,6 @@ public function creator(): BelongsTo return $this->belongsTo(User::class, "creator_id"); } - public function yearPeriod(): BelongsTo - { - return $this->belongsTo(YearPeriod::class); - } - public function activities(): HasMany { return $this->hasMany(VacationRequestActivity::class); diff --git a/app/Models/YearPeriod.php b/app/Models/YearPeriod.php deleted file mode 100644 index edb010fa..00000000 --- a/app/Models/YearPeriod.php +++ /dev/null @@ -1,83 +0,0 @@ -year); - } - - public static function findByYear(int $year): ?static - { - /** @var YearPeriod $year */ - $year = static::query()->where("year", $year)->first(); - - return $year; - } - - public function vacationLimits(): HasMany - { - return $this->hasMany(VacationLimit::class); - } - - public function vacationRequests(): HasMany - { - return $this->hasMany(VacationRequest::class); - } - - public function overtimeRequests(): HasMany - { - return $this->hasMany(OvertimeRequest::class); - } - - public function holidays(): HasMany - { - return $this->hasMany(Holiday::class); - } - - public function weekends(): Collection - { - $start = Carbon::create($this->year); - $end = Carbon::create($this->year)->endOfYear(); - - $weekends = new Collection(); - - while ($start->lessThanOrEqualTo($end)) { - if ($start->isWeekend()) { - $weekends->push($start->copy()); - } - - $start->addDay(); - } - - return $weekends; - } - - protected static function newFactory(): YearPeriodFactory - { - return YearPeriodFactory::new(); - } -} diff --git a/app/Observers/OvertimeRequestObserver.php b/app/Observers/OvertimeRequestObserver.php index 700915d6..b01f7636 100644 --- a/app/Observers/OvertimeRequestObserver.php +++ b/app/Observers/OvertimeRequestObserver.php @@ -11,10 +11,13 @@ class OvertimeRequestObserver { public function creating(OvertimeRequest $overtime): void { - $count = $overtime->yearPeriod->overtimeRequests()->count(); + $count = OvertimeRequest::query() + ->whereYear("from", $overtime->from) + ->count(); + $number = $count + 1; - $overtime->name = "N/{$number}/{$overtime->yearPeriod->year}"; + $overtime->name = "N/{$number}/{$overtime->from->year}"; } public function updating(OvertimeRequest $overtime): void diff --git a/app/Observers/VacationRequestObserver.php b/app/Observers/VacationRequestObserver.php index 414ba813..ad848177 100644 --- a/app/Observers/VacationRequestObserver.php +++ b/app/Observers/VacationRequestObserver.php @@ -11,10 +11,13 @@ class VacationRequestObserver { public function creating(VacationRequest $vacationRequest): void { - $count = $vacationRequest->yearPeriod->vacationRequests()->count(); + $count = VacationRequest::query() + ->whereYear("from", $vacationRequest->from) + ->count(); + $number = $count + 1; - $vacationRequest->name = "{$number}/{$vacationRequest->yearPeriod->year}"; + $vacationRequest->name = "{$number}/{$vacationRequest->from->year}"; } public function updating(VacationRequest $vacationRequest): void diff --git a/app/Slack/Handlers/RemoteWork.php b/app/Slack/Handlers/RemoteWork.php index bcf87152..6f28a881 100644 --- a/app/Slack/Handlers/RemoteWork.php +++ b/app/Slack/Handlers/RemoteWork.php @@ -10,7 +10,6 @@ use Toby\Actions\VacationRequest\CreateAction; use Toby\Enums\VacationType; use Toby\Models\User; -use Toby\Models\YearPeriod; use Toby\Slack\Traits\FindsUserBySlackId; class RemoteWork extends SignatureHandler @@ -31,14 +30,11 @@ public function handle(Request $request): Response protected function createRemoteDay(User $user, Carbon $date): void { - $yearPeriod = YearPeriod::findByYear($date->year); - app(CreateAction::class)->execute([ "user_id" => $user->id, "type" => VacationType::RemoteWork, "from" => $date, "to" => $date, - "year_period_id" => $yearPeriod->id, "flow_skipped" => false, ], $user); } diff --git a/app/Validation/Rules/VacationRequest/DoesNotExceedLimitRule.php b/app/Validation/Rules/VacationRequest/DoesNotExceedLimitRule.php index fab4db12..58eb385c 100644 --- a/app/Validation/Rules/VacationRequest/DoesNotExceedLimitRule.php +++ b/app/Validation/Rules/VacationRequest/DoesNotExceedLimitRule.php @@ -12,7 +12,6 @@ use Toby\Enums\VacationType; use Toby\Models\User; use Toby\Models\VacationRequest; -use Toby\Models\YearPeriod; class DoesNotExceedLimitRule implements VacationRequestRule { @@ -27,8 +26,8 @@ public function check(VacationRequest $vacationRequest): bool return true; } - $limit = $this->getUserVacationLimit($vacationRequest->user, $vacationRequest->yearPeriod); - $vacationDays = $this->getVacationDaysWithLimit($vacationRequest->user, $vacationRequest->yearPeriod); + $limit = $this->getUserVacationLimit($vacationRequest->user, $vacationRequest->from->year); + $vacationDays = $this->getVacationDaysWithLimit($vacationRequest->user, $vacationRequest->from->year); $estimatedDays = $this->workDaysCalculator ->calculateDays($vacationRequest->from, $vacationRequest->to, $vacationRequest->type) ->count(); @@ -41,18 +40,18 @@ public function errorMessage(): string return __("Vacation limit has been exceeded."); } - protected function getUserVacationLimit(User $user, YearPeriod $yearPeriod): int + protected function getUserVacationLimit(User $user, int $year): int { return $user->vacationLimits() - ->whereBelongsTo($yearPeriod) + ->where("year", $year) ->first() ?->limit ?? 0; } - protected function getVacationDaysWithLimit(User $user, YearPeriod $yearPeriod): int + protected function getVacationDaysWithLimit(User $user, int $year): int { return $user->vacations() - ->whereBelongsTo($yearPeriod) + ->whereYear("date", $year) ->whereRelation( "vacationRequest", fn(Builder $query): Builder => $query diff --git a/database/factories/HolidayFactory.php b/database/factories/HolidayFactory.php index 7d4ca0bd..686bdd3c 100644 --- a/database/factories/HolidayFactory.php +++ b/database/factories/HolidayFactory.php @@ -6,7 +6,6 @@ use Illuminate\Database\Eloquent\Factories\Factory; use Toby\Models\Holiday; -use Toby\Models\YearPeriod; class HolidayFactory extends Factory { @@ -17,7 +16,6 @@ public function definition(): array return [ "name" => $this->faker->word, "date" => $this->faker->unique->date, - "year_period_id" => YearPeriod::current()->id, ]; } } diff --git a/database/factories/OvertimeRequestFactory.php b/database/factories/OvertimeRequestFactory.php index 37e665cb..fe3e0561 100644 --- a/database/factories/OvertimeRequestFactory.php +++ b/database/factories/OvertimeRequestFactory.php @@ -10,7 +10,6 @@ use Toby\Enums\SettlementType; use Toby\Models\OvertimeRequest; use Toby\Models\User; -use Toby\Models\YearPeriod; class OvertimeRequestFactory extends Factory { @@ -25,7 +24,6 @@ public function definition(): array return [ "user_id" => User::factory(), "creator_id" => fn(array $attributes): int => $attributes["user_id"], - "year_period_id" => YearPeriod::factory(), "state" => $this->faker->randomElement(OvertimeRequestStatesRetriever::all()), "from" => $from, "to" => $to, diff --git a/database/factories/VacationFactory.php b/database/factories/VacationFactory.php index 9abea039..f74676b0 100644 --- a/database/factories/VacationFactory.php +++ b/database/factories/VacationFactory.php @@ -9,7 +9,6 @@ use Toby\Models\User; use Toby\Models\Vacation; use Toby\Models\VacationRequest; -use Toby\Models\YearPeriod; class VacationFactory extends Factory { @@ -20,7 +19,6 @@ public function definition(): array return [ "user_id" => User::factory(), "vacation_request_id" => VacationRequest::factory(), - "year_period_id" => YearPeriod::factory(), "date" => CarbonImmutable::create($this->faker->dateTimeThisYear), ]; } diff --git a/database/factories/VacationLimitFactory.php b/database/factories/VacationLimitFactory.php index 20a63c56..e2875d7d 100644 --- a/database/factories/VacationLimitFactory.php +++ b/database/factories/VacationLimitFactory.php @@ -7,7 +7,6 @@ use Illuminate\Database\Eloquent\Factories\Factory; use Toby\Models\User; use Toby\Models\VacationLimit; -use Toby\Models\YearPeriod; class VacationLimitFactory extends Factory { @@ -19,7 +18,7 @@ public function definition(): array return [ "user_id" => User::factory(), - "year_period_id" => YearPeriod::factory(), + "year" => fake()->dateTimeThisDecade()->format("Y"), "days" => $hasVacation ? $this->faker->numberBetween(20, 26) : null, "from_previous_year" => $hasVacation ? 0 : null, "to_next_year" => $hasVacation ? 0 : null, diff --git a/database/factories/VacationRequestFactory.php b/database/factories/VacationRequestFactory.php index 235f50ce..6451a7df 100644 --- a/database/factories/VacationRequestFactory.php +++ b/database/factories/VacationRequestFactory.php @@ -10,7 +10,6 @@ use Toby\Enums\VacationType; use Toby\Models\User; use Toby\Models\VacationRequest; -use Toby\Models\YearPeriod; class VacationRequestFactory extends Factory { @@ -24,7 +23,6 @@ public function definition(): array return [ "user_id" => User::factory(), "creator_id" => fn(array $attributes): int => $attributes["user_id"], - "year_period_id" => YearPeriod::factory(), "type" => $this->faker->randomElement(VacationType::cases()), "state" => $this->faker->randomElement(VacationRequestStatesRetriever::all()), "from" => $from, diff --git a/database/factories/YearPeriodFactory.php b/database/factories/YearPeriodFactory.php deleted file mode 100644 index f9fe4a27..00000000 --- a/database/factories/YearPeriodFactory.php +++ /dev/null @@ -1,20 +0,0 @@ - (int)$this->faker->unique()->year, - ]; - } -} diff --git a/database/migrations/2022_01_19_140630_create_vacation_limits_table.php b/database/migrations/2022_01_19_140630_create_vacation_limits_table.php index c2b29d50..0a2ab4c6 100644 --- a/database/migrations/2022_01_19_140630_create_vacation_limits_table.php +++ b/database/migrations/2022_01_19_140630_create_vacation_limits_table.php @@ -6,7 +6,6 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; use Toby\Models\User; -use Toby\Models\YearPeriod; return new class() extends Migration { public function up(): void @@ -14,7 +13,7 @@ public function up(): void Schema::create("vacation_limits", function (Blueprint $table): void { $table->id(); $table->foreignIdFor(User::class)->constrained()->cascadeOnDelete(); - $table->foreignIdFor(YearPeriod::class)->constrained()->cascadeOnDelete(); + $table->foreignId("year_period_id")->constrained()->cascadeOnDelete(); $table->integer("days")->nullable(); $table->timestamps(); }); diff --git a/database/migrations/2022_01_20_140544_create_holidays_table.php b/database/migrations/2022_01_20_140544_create_holidays_table.php index 941418c3..f802ccc3 100644 --- a/database/migrations/2022_01_20_140544_create_holidays_table.php +++ b/database/migrations/2022_01_20_140544_create_holidays_table.php @@ -5,14 +5,13 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -use Toby\Models\YearPeriod; return new class() extends Migration { public function up(): void { Schema::create("holidays", function (Blueprint $table): void { $table->id(); - $table->foreignIdFor(YearPeriod::class)->constrained()->cascadeOnDelete(); + $table->foreignId("year_period_id")->constrained()->cascadeOnDelete(); $table->string("name"); $table->date("date")->unique(); $table->timestamps(); diff --git a/database/migrations/2022_01_26_100039_create_vacation_requests_table.php b/database/migrations/2022_01_26_100039_create_vacation_requests_table.php index f5513517..f333847d 100644 --- a/database/migrations/2022_01_26_100039_create_vacation_requests_table.php +++ b/database/migrations/2022_01_26_100039_create_vacation_requests_table.php @@ -6,7 +6,6 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; use Toby\Models\User; -use Toby\Models\YearPeriod; return new class() extends Migration { public function up(): void @@ -16,7 +15,7 @@ public function up(): void $table->string("name"); $table->foreignIdFor(User::class, "creator_id")->constrained("users")->cascadeOnDelete(); $table->foreignIdFor(User::class)->constrained()->cascadeOnDelete(); - $table->foreignIdFor(YearPeriod::class)->constrained()->cascadeOnDelete(); + $table->foreignId("year_period_id")->constrained()->cascadeOnDelete(); $table->string("type"); $table->string("state")->nullable(); $table->date("from"); diff --git a/database/migrations/2022_02_07_133018_create_vacations_table.php b/database/migrations/2022_02_07_133018_create_vacations_table.php index 3ddadd51..ae51f02c 100644 --- a/database/migrations/2022_02_07_133018_create_vacations_table.php +++ b/database/migrations/2022_02_07_133018_create_vacations_table.php @@ -7,7 +7,6 @@ use Illuminate\Support\Facades\Schema; use Toby\Models\User; use Toby\Models\VacationRequest; -use Toby\Models\YearPeriod; return new class() extends Migration { public function up(): void @@ -16,7 +15,7 @@ public function up(): void $table->id(); $table->foreignIdFor(User::class)->constrained()->cascadeOnDelete(); $table->foreignIdFor(VacationRequest::class)->constrained()->cascadeOnDelete(); - $table->foreignIdFor(YearPeriod::class)->constrained()->cascadeOnDelete(); + $table->foreignId("year_period_id")->constrained()->cascadeOnDelete(); $table->string("event_id")->nullable(); $table->date("date"); }); diff --git a/database/migrations/2024_06_06_080039_create_overtime_requests_table.php b/database/migrations/2024_06_06_080039_create_overtime_requests_table.php index 5967cd4d..76b7c696 100644 --- a/database/migrations/2024_06_06_080039_create_overtime_requests_table.php +++ b/database/migrations/2024_06_06_080039_create_overtime_requests_table.php @@ -6,7 +6,6 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; use Toby\Models\User; -use Toby\Models\YearPeriod; return new class() extends Migration { public function up(): void @@ -16,7 +15,7 @@ public function up(): void $table->string("name"); $table->foreignIdFor(User::class, "creator_id")->constrained("users")->cascadeOnDelete(); $table->foreignIdFor(User::class)->constrained()->cascadeOnDelete(); - $table->foreignIdFor(YearPeriod::class)->constrained()->cascadeOnDelete(); + $table->foreignId("year_period_id")->constrained()->cascadeOnDelete(); $table->string("state")->nullable(); $table->string("settlement_type"); $table->boolean("settled")->default(false); diff --git a/database/migrations/2024_10_14_110718_delete_year_period_foreign_and_add_year_to_vacation_limits_table.php b/database/migrations/2024_10_14_110718_delete_year_period_foreign_and_add_year_to_vacation_limits_table.php new file mode 100644 index 00000000..88cbdf29 --- /dev/null +++ b/database/migrations/2024_10_14_110718_delete_year_period_foreign_and_add_year_to_vacation_limits_table.php @@ -0,0 +1,25 @@ +integer("year")->nullable(); + }); + + Artisan::call(MigrateYearPeriodYearToVacationLimits::class); + + Schema::table("vacation_limits", function (Blueprint $table): void { + $table->integer("year")->nullable(false)->change(); + $table->dropConstrainedForeignId("year_period_id"); + }); + } +}; diff --git a/database/migrations/2024_10_14_110737_delete_year_period_foreign_from_holidays_table.php b/database/migrations/2024_10_14_110737_delete_year_period_foreign_from_holidays_table.php new file mode 100644 index 00000000..d7e9d73e --- /dev/null +++ b/database/migrations/2024_10_14_110737_delete_year_period_foreign_from_holidays_table.php @@ -0,0 +1,16 @@ +dropConstrainedForeignId("year_period_id"); + }); + } +}; diff --git a/database/migrations/2024_10_14_110744_delete_year_period_foreign_from_vacations_table.php b/database/migrations/2024_10_14_110744_delete_year_period_foreign_from_vacations_table.php new file mode 100644 index 00000000..f69f9824 --- /dev/null +++ b/database/migrations/2024_10_14_110744_delete_year_period_foreign_from_vacations_table.php @@ -0,0 +1,16 @@ +dropConstrainedForeignId("year_period_id"); + }); + } +}; diff --git a/database/migrations/2024_10_14_110753_delete_year_period_foreign_from_overtime_requests_table.php b/database/migrations/2024_10_14_110753_delete_year_period_foreign_from_overtime_requests_table.php new file mode 100644 index 00000000..84921162 --- /dev/null +++ b/database/migrations/2024_10_14_110753_delete_year_period_foreign_from_overtime_requests_table.php @@ -0,0 +1,16 @@ +dropConstrainedForeignId("year_period_id"); + }); + } +}; diff --git a/database/migrations/2024_10_14_110830_delete_year_period_foreign_from_vacation_requests_table.php b/database/migrations/2024_10_14_110830_delete_year_period_foreign_from_vacation_requests_table.php new file mode 100644 index 00000000..f5156a43 --- /dev/null +++ b/database/migrations/2024_10_14_110830_delete_year_period_foreign_from_vacation_requests_table.php @@ -0,0 +1,16 @@ +dropConstrainedForeignId("year_period_id"); + }); + } +}; diff --git a/database/migrations/2024_10_14_110841_delete_year_periods_table.php b/database/migrations/2024_10_14_110841_delete_year_periods_table.php new file mode 100644 index 00000000..9d929f98 --- /dev/null +++ b/database/migrations/2024_10_14_110841_delete_year_periods_table.php @@ -0,0 +1,13 @@ +count(3) - ->sequence( - [ - "year" => Carbon::now()->year - 1, - ], - [ - "year" => Carbon::now()->year, - ], - [ - "year" => Carbon::now()->year + 1, - ], - ) - ->afterCreating(function (YearPeriod $yearPeriod) use ($users): void { - foreach ($users as $user) { - VacationLimit::factory() - ->for($yearPeriod) - ->for($user) - ->create(); - } - }) - ->afterCreating(function (YearPeriod $yearPeriod): void { - $polishHolidaysRetriever = new PolishHolidaysRetriever(); - - foreach ($polishHolidaysRetriever->getForYearPeriod($yearPeriod) as $holiday) { - $yearPeriod->holidays()->create([ - "name" => $holiday["name"], - "date" => $holiday["date"], - ]); - } - }) - ->create(); + $year = Carbon::now()->year; + + foreach ([$year - 1, $year, $year + 1] as $i) { + $polishHolidaysRetriever = new PolishHolidaysRetriever(); - $yearPeriods = YearPeriod::all(); + foreach ($polishHolidaysRetriever->getForYear($i) as $holiday) { + Holiday::query()->create([ + "name" => $holiday["name"], + "date" => $holiday["date"], + ]); + } + } foreach ($users as $user) { VacationRequest::factory() ->count(50) ->for($user) ->for($user, "creator") - ->sequence(fn() => [ - "year_period_id" => $yearPeriods->random()->id, - ]) ->afterCreating(function (VacationRequest $vacationRequest): void { $days = app(WorkDaysCalculator::class)->calculateDays( $vacationRequest->from, @@ -87,7 +61,6 @@ public function run(): void $vacationRequest->vacations()->create([ "date" => $day, "user_id" => $vacationRequest->user->id, - "year_period_id" => $vacationRequest->yearPeriod->id, ]); } }) diff --git a/database/seeders/DemoSeeder.php b/database/seeders/DemoSeeder.php index 18689544..e9cd6f11 100644 --- a/database/seeders/DemoSeeder.php +++ b/database/seeders/DemoSeeder.php @@ -17,6 +17,7 @@ use Toby\Models\BenefitsReport; use Toby\Models\EquipmentItem; use Toby\Models\EquipmentLabel; +use Toby\Models\Holiday; use Toby\Models\Key; use Toby\Models\Resume; use Toby\Models\Technology; @@ -24,7 +25,6 @@ use Toby\Models\VacationLimit; use Toby\Models\VacationRequest; use Toby\Models\VacationRequestActivity; -use Toby\Models\YearPeriod; use Toby\States\VacationRequest\AcceptedByAdministrative; use Toby\States\VacationRequest\AcceptedByTechnical; use Toby\States\VacationRequest\Approved; @@ -184,54 +184,33 @@ public function run(): void $year = Carbon::now()->year; - YearPeriod::factory() - ->count(3) - ->sequence( - [ - "year" => Carbon::createFromDate($year - 1)->year, - ], - [ - "year" => Carbon::createFromDate($year)->year, - ], - [ - "year" => Carbon::createFromDate($year + 1)->year, - ], - ) - ->afterCreating(function (YearPeriod $yearPeriod) use ($users): void { - foreach ($users as $user) { - VacationLimit::factory([ - "days" => $user->profile->employment_form === EmploymentForm::EmploymentContract ? 26 : null, - ]) - ->for($yearPeriod) - ->for($user) - ->create(); - } - }) - ->afterCreating(function (YearPeriod $yearPeriod): void { - $polishHolidaysRetriever = new PolishHolidaysRetriever(); + foreach ([$year - 1, $year, $year + 1] as $i) { + $polishHolidaysRetriever = new PolishHolidaysRetriever(); - foreach ($polishHolidaysRetriever->getForYearPeriod($yearPeriod) as $holiday) { - $yearPeriod->holidays()->create([ - "name" => $holiday["name"], - "date" => $holiday["date"], - ]); - } - }) - ->create(); + foreach ($polishHolidaysRetriever->getForYear($i) as $holiday) { + Holiday::query()->create([ + "name" => $holiday["name"], + "date" => $holiday["date"], + ]); + } + } - $currentYearPeriod = YearPeriod::query()->where("year", $year)->first(); + foreach ($users as $item) { + VacationLimit::factory() + ->for($item) + ->create(["year" => $year]); + } /** @var VacationRequest $vacationRequestApproved */ $vacationRequestApproved = VacationRequest::factory([ "type" => VacationType::Vacation->value, "state" => Created::class, - "from" => Carbon::create($currentYearPeriod->year, 1, 31)->toDateString(), - "to" => Carbon::create($currentYearPeriod->year, 2, 4)->toDateString(), + "from" => Carbon::create($year, 1, 31)->toDateString(), + "to" => Carbon::create($year, 2, 4)->toDateString(), "comment" => "Komentarz do wniosku urlopowego.", ]) ->for($user) ->for($user, "creator") - ->for($currentYearPeriod) ->afterCreating(function (VacationRequest $vacationRequest): void { $days = app(WorkDaysCalculator::class)->calculateDays( $vacationRequest->from, @@ -243,7 +222,6 @@ public function run(): void $vacationRequest->vacations()->create([ "date" => $day, "user_id" => $vacationRequest->user->id, - "year_period_id" => $vacationRequest->yearPeriod->id, ]); } }) @@ -295,13 +273,12 @@ public function run(): void $vacationRequestWaitsForAdminApproval = VacationRequest::factory([ "type" => VacationType::Vacation->value, "state" => Created::class, - "from" => Carbon::create($currentYearPeriod->year, 2, 14)->toDateString(), - "to" => Carbon::create($currentYearPeriod->year, 2, 14)->toDateString(), + "from" => Carbon::create($year, 2, 14)->toDateString(), + "to" => Carbon::create($year, 2, 14)->toDateString(), "comment" => "Komentarz do wniosku urlopowego.", ]) ->for($user) ->for($user, "creator") - ->for($currentYearPeriod) ->afterCreating(function (VacationRequest $vacationRequest): void { $days = app(WorkDaysCalculator::class)->calculateDays( $vacationRequest->from, @@ -313,7 +290,6 @@ public function run(): void $vacationRequest->vacations()->create([ "date" => $day, "user_id" => $vacationRequest->user->id, - "year_period_id" => $vacationRequest->yearPeriod->id, ]); } }) @@ -354,13 +330,12 @@ public function run(): void $vacationRequestRejected = VacationRequest::factory([ "type" => VacationType::Vacation->value, "state" => Created::class, - "from" => Carbon::create($currentYearPeriod->year, 2, 7)->toDateString(), - "to" => Carbon::create($currentYearPeriod->year, 2, 7)->toDateString(), + "from" => Carbon::create($year, 2, 7)->toDateString(), + "to" => Carbon::create($year, 2, 7)->toDateString(), "comment" => "", ]) ->for($user) ->for($user, "creator") - ->for($currentYearPeriod) ->afterCreating(function (VacationRequest $vacationRequest): void { $days = app(WorkDaysCalculator::class)->calculateDays( $vacationRequest->from, @@ -372,7 +347,6 @@ public function run(): void $vacationRequest->vacations()->create([ "date" => $day, "user_id" => $vacationRequest->user->id, - "year_period_id" => $vacationRequest->yearPeriod->id, ]); } }) diff --git a/package-lock.json b/package-lock.json index 08b070a6..9d552085 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "toby", + "name": "application", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/resources/js/Composables/yearPeriodInfo.js b/resources/js/Composables/yearPeriodInfo.js deleted file mode 100644 index ede213c9..00000000 --- a/resources/js/Composables/yearPeriodInfo.js +++ /dev/null @@ -1,14 +0,0 @@ -import { computed } from 'vue' -import { usePage } from '@inertiajs/inertia-vue3' - -export default function useCurrentYearPeriodInfo() { - const minDate = computed(() => new Date(usePage().props.value.years.selected.year, 0, 1)) - const maxDate = computed(() => new Date(usePage().props.value.years.selected.year, 11, 31)) - const year = computed(() => usePage().props.value.years.selected.year) - - return { - minDate, - maxDate, - year, - } -} diff --git a/resources/js/Pages/AnnualSummary.vue b/resources/js/Pages/AnnualSummary.vue index ca75bde6..c45c5430 100644 --- a/resources/js/Pages/AnnualSummary.vue +++ b/resources/js/Pages/AnnualSummary.vue @@ -1,12 +1,28 @@