feat: complete administration and reporting
This commit is contained in:
@@ -19,7 +19,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
class ReservationItem extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'reservation_id', 'snipe_asset_id', 'starts_at', 'ends_at', 'status',
|
||||
'reservation_id', 'snipe_asset_id', 'starts_at', 'ends_at', 'buffer_starts_at', 'buffer_ends_at', 'status',
|
||||
'delivered_at', 'returned_at', 'checkout_reference', 'checkin_reference',
|
||||
'condition_out', 'condition_in', 'notes',
|
||||
];
|
||||
@@ -30,6 +30,8 @@ class ReservationItem extends Model
|
||||
'status' => ReservationItemStatus::class,
|
||||
'starts_at' => 'immutable_datetime',
|
||||
'ends_at' => 'immutable_datetime',
|
||||
'buffer_starts_at' => 'immutable_datetime',
|
||||
'buffer_ends_at' => 'immutable_datetime',
|
||||
'delivered_at' => 'immutable_datetime',
|
||||
'returned_at' => 'immutable_datetime',
|
||||
];
|
||||
|
||||
@@ -2,24 +2,30 @@
|
||||
|
||||
namespace App\Domain\Reservations\Services;
|
||||
|
||||
use App\Domain\Administration\Services\ReservationPolicy;
|
||||
use App\Domain\Inventory\Models\SnipeAsset;
|
||||
use App\Domain\Reservations\Enums\ReservationItemStatus;
|
||||
use Carbon\CarbonImmutable;
|
||||
use DateTimeInterface;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class AvailabilityService
|
||||
{
|
||||
public function __construct(private readonly ReservationPolicy $policy) {}
|
||||
|
||||
/** @return Builder<SnipeAsset> */
|
||||
public function availableAssets(DateTimeInterface $startsAt, DateTimeInterface $endsAt): Builder
|
||||
{
|
||||
[$bufferStartsAt, $bufferEndsAt] = $this->bufferedPeriod($startsAt, $endsAt);
|
||||
|
||||
return SnipeAsset::query()
|
||||
->where('active', true)
|
||||
->where('requestable', true)
|
||||
->whereNull('assigned_id')
|
||||
->whereDoesntHave('reservationItems', fn (Builder $query): Builder => $query
|
||||
->whereIn('status', ReservationItemStatus::blockingValues())
|
||||
->where('starts_at', '<', $endsAt)
|
||||
->where('ends_at', '>', $startsAt))
|
||||
->whereRaw('COALESCE(buffer_starts_at, starts_at) < ?', [$bufferEndsAt])
|
||||
->whereRaw('COALESCE(buffer_ends_at, ends_at) > ?', [$bufferStartsAt]))
|
||||
->whereDoesntHave('blocks', fn (Builder $query): Builder => $query
|
||||
->where('starts_at', '<', $endsAt)
|
||||
->where('ends_at', '>', $startsAt));
|
||||
@@ -35,11 +41,12 @@ class AvailabilityService
|
||||
return false;
|
||||
}
|
||||
|
||||
[$bufferStartsAt, $bufferEndsAt] = $this->bufferedPeriod($startsAt, $endsAt);
|
||||
$hasReservationConflict = $asset->reservationItems()
|
||||
->whereIn('status', ReservationItemStatus::blockingValues())
|
||||
->when($exceptReservationId, fn (Builder $query): Builder => $query->where('reservation_id', '!=', $exceptReservationId))
|
||||
->where('starts_at', '<', $endsAt)
|
||||
->where('ends_at', '>', $startsAt)
|
||||
->whereRaw('COALESCE(buffer_starts_at, starts_at) < ?', [$bufferEndsAt])
|
||||
->whereRaw('COALESCE(buffer_ends_at, ends_at) > ?', [$bufferStartsAt])
|
||||
->exists();
|
||||
|
||||
if ($hasReservationConflict) {
|
||||
@@ -51,4 +58,15 @@ class AvailabilityService
|
||||
->where('ends_at', '>', $startsAt)
|
||||
->exists();
|
||||
}
|
||||
|
||||
/** @return array{DateTimeInterface, DateTimeInterface} */
|
||||
public function bufferedPeriod(DateTimeInterface $startsAt, DateTimeInterface $endsAt): array
|
||||
{
|
||||
$margin = $this->policy->integer('reservation.turnaround_minutes');
|
||||
|
||||
return [
|
||||
CarbonImmutable::instance($startsAt)->subMinutes($margin),
|
||||
CarbonImmutable::instance($endsAt)->addMinutes($margin),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Domain\Reservations\Services;
|
||||
|
||||
use App\Domain\Administration\Services\ReservationPolicy;
|
||||
use App\Domain\Approvals\Models\ApprovalAction;
|
||||
use App\Domain\Audit\Models\AuditLog;
|
||||
use App\Domain\Identity\Enums\RoleName;
|
||||
@@ -20,7 +21,10 @@ use Illuminate\Validation\ValidationException;
|
||||
|
||||
class ReservationService
|
||||
{
|
||||
public function __construct(private readonly AvailabilityService $availability) {}
|
||||
public function __construct(
|
||||
private readonly AvailabilityService $availability,
|
||||
private readonly ReservationPolicy $policy,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param array{purpose:string, usage_location:string, starts_at:string, ends_at:string, asset_ids:list<int>} $data
|
||||
@@ -29,8 +33,10 @@ class ReservationService
|
||||
{
|
||||
$startsAt = CarbonImmutable::parse($data['starts_at']);
|
||||
$endsAt = CarbonImmutable::parse($data['ends_at']);
|
||||
$this->validatePolicy($startsAt, $endsAt, count($data['asset_ids']));
|
||||
[$bufferStartsAt, $bufferEndsAt] = $this->availability->bufferedPeriod($startsAt, $endsAt);
|
||||
|
||||
$reservation = DB::transaction(function () use ($requester, $data, $startsAt, $endsAt): Reservation {
|
||||
$reservation = DB::transaction(function () use ($requester, $data, $startsAt, $endsAt, $bufferStartsAt, $bufferEndsAt): Reservation {
|
||||
$assets = SnipeAsset::query()
|
||||
->whereKey($data['asset_ids'])
|
||||
->lockForUpdate()
|
||||
@@ -58,7 +64,7 @@ class ReservationService
|
||||
'starts_at' => $startsAt,
|
||||
'ends_at' => $endsAt,
|
||||
'status' => ReservationStatus::PendingApproval,
|
||||
'approval_deadline' => now()->addHours(48),
|
||||
'approval_deadline' => now()->addHours($this->policy->integer('reservation.approval_deadline_hours')),
|
||||
]);
|
||||
|
||||
foreach ($assets as $asset) {
|
||||
@@ -66,6 +72,8 @@ class ReservationService
|
||||
'snipe_asset_id' => $asset->id,
|
||||
'starts_at' => $startsAt,
|
||||
'ends_at' => $endsAt,
|
||||
'buffer_starts_at' => $bufferStartsAt,
|
||||
'buffer_ends_at' => $bufferEndsAt,
|
||||
'status' => ReservationItemStatus::Held,
|
||||
]);
|
||||
}
|
||||
@@ -156,7 +164,7 @@ class ReservationService
|
||||
|
||||
$locked->update([
|
||||
'status' => ReservationStatus::PendingApproval,
|
||||
'approval_deadline' => now()->addHours(48),
|
||||
'approval_deadline' => now()->addHours($this->policy->integer('reservation.approval_deadline_hours')),
|
||||
]);
|
||||
ApprovalAction::create([
|
||||
'reservation_id' => $locked->id,
|
||||
@@ -258,4 +266,21 @@ class ReservationService
|
||||
|
||||
return $reference;
|
||||
}
|
||||
|
||||
private function validatePolicy(CarbonImmutable $startsAt, CarbonImmutable $endsAt, int $assetCount): void
|
||||
{
|
||||
$minNotice = $this->policy->integer('reservation.min_notice_hours');
|
||||
$maxDuration = $this->policy->integer('reservation.max_duration_hours');
|
||||
$maxAssets = $this->policy->integer('reservation.max_assets');
|
||||
|
||||
if ($startsAt->lessThan(now()->addHours($minNotice))) {
|
||||
throw ValidationException::withMessages(['starts_at' => "A requisição exige pelo menos {$minNotice} horas de antecedência."]);
|
||||
}
|
||||
if ($startsAt->diffInMinutes($endsAt) > $maxDuration * 60) {
|
||||
throw ValidationException::withMessages(['ends_at' => "A duração máxima permitida é de {$maxDuration} horas."]);
|
||||
}
|
||||
if ($assetCount > $maxAssets) {
|
||||
throw ValidationException::withMessages(['asset_ids' => "Selecione no máximo {$maxAssets} equipamentos."]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user