133 lines
3.1 KiB
PHP
133 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\EventStatus;
|
|
use App\Enums\EventVisibility;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
final class Event extends Model
|
|
{
|
|
use HasFactory;
|
|
use HasUlids;
|
|
|
|
protected $fillable = [
|
|
'title',
|
|
'description',
|
|
'location_id',
|
|
'customer_id',
|
|
'setlist_id',
|
|
'event_date',
|
|
'start_time',
|
|
'end_time',
|
|
'load_in_time',
|
|
'soundcheck_time',
|
|
'fee',
|
|
'currency',
|
|
'status',
|
|
'visibility',
|
|
'rsvp_deadline',
|
|
'notes',
|
|
'internal_notes',
|
|
'is_public_setlist',
|
|
'created_by',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'event_date' => 'date',
|
|
'start_time' => 'datetime:H:i',
|
|
'end_time' => 'datetime:H:i',
|
|
'load_in_time' => 'datetime:H:i',
|
|
'soundcheck_time' => 'datetime:H:i',
|
|
'fee' => 'decimal:2',
|
|
'status' => EventStatus::class,
|
|
'visibility' => EventVisibility::class,
|
|
'rsvp_deadline' => 'datetime',
|
|
'is_public_setlist' => 'boolean',
|
|
];
|
|
}
|
|
|
|
// Relationships
|
|
|
|
public function location(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Location::class);
|
|
}
|
|
|
|
public function customer(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Customer::class);
|
|
}
|
|
|
|
public function setlist(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Setlist::class);
|
|
}
|
|
|
|
public function creator(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
}
|
|
|
|
public function invitations(): HasMany
|
|
{
|
|
return $this->hasMany(EventInvitation::class);
|
|
}
|
|
|
|
// Scopes
|
|
|
|
public function scopeUpcoming(Builder $query): Builder
|
|
{
|
|
return $query->where('event_date', '>=', now()->toDateString())
|
|
->orderBy('event_date');
|
|
}
|
|
|
|
public function scopePast(Builder $query): Builder
|
|
{
|
|
return $query->where('event_date', '<', now()->toDateString())
|
|
->orderByDesc('event_date');
|
|
}
|
|
|
|
public function scopeConfirmed(Builder $query): Builder
|
|
{
|
|
return $query->where('status', EventStatus::Confirmed);
|
|
}
|
|
|
|
public function scopeForUser(Builder $query, User $user): Builder
|
|
{
|
|
return $query->whereHas('invitations', fn (Builder $q) => $q->where('user_id', $user->id));
|
|
}
|
|
|
|
// Helper methods
|
|
|
|
public function isUpcoming(): bool
|
|
{
|
|
return $this->event_date->isFuture() || $this->event_date->isToday();
|
|
}
|
|
|
|
public function isPast(): bool
|
|
{
|
|
return $this->event_date->isPast() && !$this->event_date->isToday();
|
|
}
|
|
|
|
public function isConfirmed(): bool
|
|
{
|
|
return $this->status === EventStatus::Confirmed;
|
|
}
|
|
|
|
public function isCancelled(): bool
|
|
{
|
|
return $this->status === EventStatus::Cancelled;
|
|
}
|
|
}
|
|
|