Spaces:
Sleeping
Sleeping
File size: 1,408 Bytes
10dc6f2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TicketTier extends Model
{
protected $fillable = [
'event_id', 'name', 'price', 'quota', 'sold_count',
'sales_start', 'sales_end', 'max_per_order', 'is_refundable',
];
protected function casts(): array
{
return [
'price' => 'decimal:2',
'sales_start' => 'datetime',
'sales_end' => 'datetime',
'is_refundable' => 'boolean',
];
}
// ββ Relationships ββββββββββββββββββββββββββββββββββββ
public function event()
{
return $this->belongsTo(Event::class);
}
public function orderItems()
{
return $this->hasMany(OrderItem::class);
}
// ββ Helpers ββββββββββββββββββββββββββββββββββββββββββ
public function availableStock(): int
{
return max(0, $this->quota - $this->sold_count);
}
public function isOnSale(): bool
{
$now = now();
if ($this->sales_start && $now->lt($this->sales_start)) return false;
if ($this->sales_end && $now->gt($this->sales_end)) return false;
if ($this->availableStock() <= 0) return false;
return true;
}
}
|