33 lines
939 B
PHP
33 lines
939 B
PHP
<?php
|
|
|
|
namespace App\Domain\Reservations\Services;
|
|
|
|
use App\Domain\Inventory\Models\SnipeAsset;
|
|
use App\Domain\Reservations\Enums\ReservationItemStatus;
|
|
use DateTimeInterface;
|
|
|
|
class AvailabilityService
|
|
{
|
|
public function isAvailable(SnipeAsset $asset, DateTimeInterface $startsAt, DateTimeInterface $endsAt): bool
|
|
{
|
|
if (! $asset->active || ! $asset->requestable || $asset->assigned_id !== null) {
|
|
return false;
|
|
}
|
|
|
|
$hasReservationConflict = $asset->reservationItems()
|
|
->whereIn('status', ReservationItemStatus::blockingValues())
|
|
->where('starts_at', '<', $endsAt)
|
|
->where('ends_at', '>', $startsAt)
|
|
->exists();
|
|
|
|
if ($hasReservationConflict) {
|
|
return false;
|
|
}
|
|
|
|
return ! $asset->blocks()
|
|
->where('starts_at', '<', $endsAt)
|
|
->where('ends_at', '>', $startsAt)
|
|
->exists();
|
|
}
|
|
}
|