* change layout * change layout * #22 - wip * wip * wip * #22 - wip * #22 - wip * #22 - wip * #22 - wip * #22 - fix * #22 - wip * #22 - added some tests * #22 - wip * #22 - wip * #22 - fix * #22 - wip * #22 - wip * #22 - wip * #22 - fix * #22 - fix * #22 - fix * #22 - fix * #22 - fix * #22 - fix * #22 - cr fixes * #22 - cr fix Co-authored-by: EwelinaLasowy <ewelina.lasowy@blumilk.pl>
This commit is contained in:
80
app/Domain/CalendarGenerator.php
Normal file
80
app/Domain/CalendarGenerator.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Domain;
|
||||
|
||||
use Carbon\CarbonImmutable;
|
||||
use Carbon\CarbonInterface;
|
||||
use Carbon\CarbonPeriod;
|
||||
use Illuminate\Support\Collection;
|
||||
use Toby\Domain\Enums\VacationRequestState;
|
||||
use Toby\Eloquent\Helpers\YearPeriodRetriever;
|
||||
use Toby\Eloquent\Models\Vacation;
|
||||
use Toby\Eloquent\Models\YearPeriod;
|
||||
|
||||
class CalendarGenerator
|
||||
{
|
||||
public function __construct(
|
||||
protected YearPeriodRetriever $yearPeriodRetriever,
|
||||
) {
|
||||
}
|
||||
|
||||
public function generate(YearPeriod $yearPeriod, string $month): array
|
||||
{
|
||||
$date = CarbonImmutable::create($yearPeriod->year, $this->monthNameToNumber($month));
|
||||
$period = CarbonPeriod::create($date->startOfMonth(), $date->endOfMonth());
|
||||
$holidays = $yearPeriod->holidays()->pluck("date");
|
||||
|
||||
return $this->generateCalendar($period, $holidays);
|
||||
}
|
||||
|
||||
protected function monthNameToNumber($name): int
|
||||
{
|
||||
return match ($name) {
|
||||
default => CarbonInterface::JANUARY,
|
||||
"february" => CarbonInterface::FEBRUARY,
|
||||
"march" => CarbonInterface::MARCH,
|
||||
"april" => CarbonInterface::APRIL,
|
||||
"may" => CarbonInterface::MAY,
|
||||
"june" => CarbonInterface::JUNE,
|
||||
"july" => CarbonInterface::JULY,
|
||||
"august" => CarbonInterface::AUGUST,
|
||||
"september" => CarbonInterface::SEPTEMBER,
|
||||
"october" => CarbonInterface::OCTOBER,
|
||||
"november" => CarbonInterface::NOVEMBER,
|
||||
"december" => CarbonInterface::DECEMBER,
|
||||
};
|
||||
}
|
||||
|
||||
protected function generateCalendar(CarbonPeriod $period, Collection $holidays): array
|
||||
{
|
||||
$calendar = [];
|
||||
$vacations = $this->getVacationsForPeriod($period);
|
||||
|
||||
foreach ($period as $day) {
|
||||
$vacationsForDay = $vacations[$day->toDateString()] ?? new Collection();
|
||||
|
||||
$calendar[] = [
|
||||
"date" => $day->toDateString(),
|
||||
"dayOfMonth" => $day->translatedFormat("j"),
|
||||
"dayOfWeek" => $day->translatedFormat("D"),
|
||||
"isToday" => $day->isToday(),
|
||||
"isWeekend" => $day->isWeekend(),
|
||||
"isHoliday" => $holidays->contains($day),
|
||||
"vacations" => $vacationsForDay->pluck("user_id"),
|
||||
];
|
||||
}
|
||||
|
||||
return $calendar;
|
||||
}
|
||||
|
||||
protected function getVacationsForPeriod(CarbonPeriod $period): Collection
|
||||
{
|
||||
return Vacation::query()
|
||||
->whereBetween("date", [$period->start, $period->end])
|
||||
->whereRelation("vacationRequest", "state", VacationRequestState::Approved->value)
|
||||
->get()
|
||||
->groupBy(fn(Vacation $vacation) => $vacation->date->toDateString());
|
||||
}
|
||||
}
|
@@ -6,10 +6,10 @@ namespace Toby\Domain\Enums;
|
||||
|
||||
enum EmploymentForm: string
|
||||
{
|
||||
case EMPLOYMENT_CONTRACT = "employment_contract";
|
||||
case COMMISSION_CONTRACT = "commission_contract";
|
||||
case B2B_CONTRACT = "b2b_contract";
|
||||
case BOARD_MEMBER_CONTRACT = "board_member_contract";
|
||||
case EmploymentContract = "employment_contract";
|
||||
case ComissionContract = "commission_contract";
|
||||
case B2bContract = "b2b_contract";
|
||||
case BoardMemberContract = "board_member_contract";
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
|
@@ -6,10 +6,10 @@ namespace Toby\Domain\Enums;
|
||||
|
||||
enum Role: string
|
||||
{
|
||||
case EMPLOYEE = "employee";
|
||||
case ADMINISTRATOR = "administrator";
|
||||
case TECHNICAL_APPROVER = "technical_approver";
|
||||
case ADMINISTRATIVE_APPROVER = "administrative_approver";
|
||||
case Employee = "employee";
|
||||
case Administrator = "administrator";
|
||||
case TechnicalApprover = "technical_approver";
|
||||
case AdministrativeApprover = "administrative_approver";
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
|
@@ -6,14 +6,14 @@ namespace Toby\Domain\Enums;
|
||||
|
||||
enum VacationRequestState: string
|
||||
{
|
||||
case CREATED = "created";
|
||||
case CANCELED = "canceled";
|
||||
case REJECTED = "rejected";
|
||||
case APPROVED = "approved";
|
||||
case WAITING_FOR_TECHNICAL = "waiting_for_technical";
|
||||
case WAITING_FOR_ADMINISTRATIVE = "waiting_for_administrative";
|
||||
case ACCEPTED_BY_TECHNICAL = "accepted_by_technical";
|
||||
case ACCEPTED_BY_ADMINSTRATIVE = "accepted_by_administrative";
|
||||
case Created = "created";
|
||||
case Canceled = "canceled";
|
||||
case Rejected = "rejected";
|
||||
case Approved = "approved";
|
||||
case WaitingForTechnical = "waiting_for_technical";
|
||||
case WaitingForAdministrative = "waiting_for_administrative";
|
||||
case AcceptedByTechnical = "accepted_by_technical";
|
||||
case AcceptedByAdministrative = "accepted_by_administrative";
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
@@ -23,24 +23,24 @@ enum VacationRequestState: string
|
||||
public static function pendingStates(): array
|
||||
{
|
||||
return [
|
||||
self::CREATED,
|
||||
self::WAITING_FOR_TECHNICAL,
|
||||
self::WAITING_FOR_ADMINISTRATIVE,
|
||||
self::ACCEPTED_BY_TECHNICAL,
|
||||
self::ACCEPTED_BY_ADMINSTRATIVE,
|
||||
self::Created,
|
||||
self::WaitingForTechnical,
|
||||
self::WaitingForAdministrative,
|
||||
self::AcceptedByTechnical,
|
||||
self::AcceptedByAdministrative,
|
||||
];
|
||||
}
|
||||
|
||||
public static function successStates(): array
|
||||
{
|
||||
return [self::APPROVED];
|
||||
return [self::Approved];
|
||||
}
|
||||
|
||||
public static function failedStates(): array
|
||||
{
|
||||
return [
|
||||
self::REJECTED,
|
||||
self::CANCELED,
|
||||
self::Rejected,
|
||||
self::Canceled,
|
||||
];
|
||||
}
|
||||
|
||||
|
@@ -6,15 +6,15 @@ namespace Toby\Domain\Enums;
|
||||
|
||||
enum VacationType: string
|
||||
{
|
||||
case VACATION = "vacation";
|
||||
case VACATION_ON_REQUEST = "vacation_on_request";
|
||||
case SPECIAL_VACATION = "special_vacation";
|
||||
case CHILDCARE_VACATION = "childcare_vacation";
|
||||
case TRAINING_VACATION = "training_vacation";
|
||||
case UNPAID_VACATION = "unpaid_vacation";
|
||||
case VOLUNTEERING_VACATION = "volunteering_vacation";
|
||||
case TIME_IN_LIEU = "time_in_lieu";
|
||||
case SICK_VACATION = "sick_vacation";
|
||||
case Vacation = "vacation";
|
||||
case OnRequest = "vacation_on_request";
|
||||
case Special = "special_vacation";
|
||||
case Childcare = "childcare_vacation";
|
||||
case Training = "training_vacation";
|
||||
case Unpaid = "unpaid_vacation";
|
||||
case Volunteering = "volunteering_vacation";
|
||||
case TimeInLieu = "time_in_lieu";
|
||||
case Sick = "sick_vacation";
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
|
@@ -16,7 +16,7 @@ class VacationDaysCalculator
|
||||
$period = CarbonPeriod::create($from, $to);
|
||||
$holidays = $yearPeriod->holidays()->pluck("date");
|
||||
|
||||
$validDays = collect();
|
||||
$validDays = new Collection();
|
||||
|
||||
foreach ($period as $day) {
|
||||
if ($this->passes($day, $holidays)) {
|
||||
|
@@ -23,50 +23,50 @@ class VacationRequestStateManager
|
||||
|
||||
public function markAsCreated(VacationRequest $vacationRequest): void
|
||||
{
|
||||
$this->changeState($vacationRequest, VacationRequestState::CREATED);
|
||||
$this->changeState($vacationRequest, VacationRequestState::Created);
|
||||
|
||||
$this->dispatcher->dispatch(new VacationRequestCreated($vacationRequest));
|
||||
}
|
||||
|
||||
public function approve(VacationRequest $vacationRequest): void
|
||||
{
|
||||
$this->changeState($vacationRequest, VacationRequestState::APPROVED);
|
||||
$this->changeState($vacationRequest, VacationRequestState::Approved);
|
||||
|
||||
$this->dispatcher->dispatch(new VacationRequestApproved($vacationRequest));
|
||||
}
|
||||
|
||||
public function reject(VacationRequest $vacationRequest): void
|
||||
{
|
||||
$this->changeState($vacationRequest, VacationRequestState::REJECTED);
|
||||
$this->changeState($vacationRequest, VacationRequestState::Rejected);
|
||||
}
|
||||
|
||||
public function cancel(VacationRequest $vacationRequest): void
|
||||
{
|
||||
$this->changeState($vacationRequest, VacationRequestState::CANCELED);
|
||||
$this->changeState($vacationRequest, VacationRequestState::Canceled);
|
||||
}
|
||||
|
||||
public function acceptAsTechnical(VacationRequest $vacationRequest): void
|
||||
{
|
||||
$this->changeState($vacationRequest, VacationRequestState::ACCEPTED_BY_TECHNICAL);
|
||||
$this->changeState($vacationRequest, VacationRequestState::AcceptedByTechnical);
|
||||
|
||||
$this->dispatcher->dispatch(new VacationRequestAcceptedByTechnical($vacationRequest));
|
||||
}
|
||||
|
||||
public function acceptAsAdministrative(VacationRequest $vacationRequest): void
|
||||
{
|
||||
$this->changeState($vacationRequest, VacationRequestState::ACCEPTED_BY_ADMINSTRATIVE);
|
||||
$this->changeState($vacationRequest, VacationRequestState::AcceptedByAdministrative);
|
||||
|
||||
$this->dispatcher->dispatch(new VacationRequestAcceptedByAdministrative($vacationRequest));
|
||||
}
|
||||
|
||||
public function waitForTechnical(VacationRequest $vacationRequest): void
|
||||
{
|
||||
$this->changeState($vacationRequest, VacationRequestState::WAITING_FOR_TECHNICAL);
|
||||
$this->changeState($vacationRequest, VacationRequestState::WaitingForTechnical);
|
||||
}
|
||||
|
||||
public function waitForAdministrative(VacationRequest $vacationRequest): void
|
||||
{
|
||||
$this->changeState($vacationRequest, VacationRequestState::WAITING_FOR_ADMINISTRATIVE);
|
||||
$this->changeState($vacationRequest, VacationRequestState::WaitingForAdministrative);
|
||||
}
|
||||
|
||||
protected function changeState(VacationRequest $vacationRequest, VacationRequestState $state): void
|
||||
|
@@ -4,23 +4,64 @@ declare(strict_types=1);
|
||||
|
||||
namespace Toby\Domain\Validation\Rules;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Toby\Domain\Enums\VacationRequestState;
|
||||
use Toby\Domain\Enums\VacationType;
|
||||
use Toby\Domain\VacationDaysCalculator;
|
||||
use Toby\Domain\VacationTypeConfigRetriever;
|
||||
use Toby\Eloquent\Models\User;
|
||||
use Toby\Eloquent\Models\VacationRequest;
|
||||
use Toby\Eloquent\Models\YearPeriod;
|
||||
|
||||
class DoesNotExceedLimitRule implements VacationRequestRule
|
||||
{
|
||||
public function __construct(
|
||||
protected VacationTypeConfigRetriever $configRetriever,
|
||||
protected VacationDaysCalculator $vacationDaysCalculator,
|
||||
) {
|
||||
}
|
||||
|
||||
public function check(VacationRequest $vacationRequest): bool
|
||||
{
|
||||
return true;
|
||||
if (!$this->configRetriever->hasLimit($vacationRequest->type)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$limit = $this->getUserVacationLimit($vacationRequest->user, $vacationRequest->yearPeriod);
|
||||
$vacationDays = $this->getVacationDaysWithLimit($vacationRequest->user, $vacationRequest->yearPeriod);
|
||||
$estimatedDays = $this->vacationDaysCalculator->calculateDays($vacationRequest->yearPeriod, $vacationRequest->from, $vacationRequest->to)->count();
|
||||
|
||||
return $limit >= ($vacationDays + $estimatedDays);
|
||||
}
|
||||
|
||||
public function errorMessage(): string
|
||||
{
|
||||
return __("You have exceeded your vacation limit.");
|
||||
return __("Vacation limit has been exceeded.");
|
||||
}
|
||||
|
||||
protected function getUserVacationLimit(User $user, YearPeriod $yearPeriod): int
|
||||
{
|
||||
return $user->vacationLimits()->where("year_period_id", $yearPeriod->id)->first()->days ?? 0;
|
||||
}
|
||||
|
||||
protected function getVacationDaysWithLimit(User $user, YearPeriod $yearPeriod): int
|
||||
{
|
||||
return $user->vacations()
|
||||
->where("year_period_id", $yearPeriod->id)
|
||||
->whereRelation(
|
||||
"vacationRequest",
|
||||
fn(Builder $query) => $query
|
||||
->whereIn("type", $this->getLimitableVacationTypes())
|
||||
->noStates(VacationRequestState::failedStates()),
|
||||
)
|
||||
->count();
|
||||
}
|
||||
|
||||
protected function getLimitableVacationTypes(): Collection
|
||||
{
|
||||
$types = new Collection(VacationType::cases());
|
||||
|
||||
return $types->filter(fn(VacationType $type) => $this->configRetriever->hasLimit($type));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user