mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 11:14:06 +00:00
98 lines
2.1 KiB
PHP
98 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use App\Exceptions\NotEnoughTicketsException;
|
|
|
|
class Concert extends Model
|
|
{
|
|
protected $guarded = [];
|
|
protected $dates = ['date'];
|
|
|
|
public function scopePublished($query)
|
|
{
|
|
return $query->whereNotNull('published_at');
|
|
}
|
|
|
|
public function getFormattedDateAttribute()
|
|
{
|
|
return $this->date->format('F j, Y');
|
|
}
|
|
|
|
public function getFormattedStartTimeAttribute()
|
|
{
|
|
return $this->date->format('g:ia');
|
|
}
|
|
|
|
public function getTicketPriceInDollarsAttribute()
|
|
{
|
|
return number_format($this->ticket_price / 100, 2);
|
|
}
|
|
|
|
public function orders()
|
|
{
|
|
return $this->hasMany(Order::class);
|
|
}
|
|
|
|
public function hasOrderFor($customerEmail)
|
|
{
|
|
return $this->orders()->where('email', $customerEmail)->count() > 0;
|
|
}
|
|
|
|
public function ordersFor($customerEmail)
|
|
{
|
|
return $this->orders()->where('email', $customerEmail)->get();
|
|
}
|
|
|
|
public function tickets()
|
|
{
|
|
return $this->hasMany(Ticket::class);
|
|
}
|
|
|
|
public function orderTickets($email, $ticketQuantity)
|
|
{
|
|
$tickets = $this->findTickets($ticketQuantity);
|
|
return $this->createOrder($email, $tickets);
|
|
}
|
|
|
|
public function findTickets($quantity)
|
|
{
|
|
$tickets = $this->tickets()->available()->take($quantity)->get();
|
|
|
|
if ($tickets->count() < $quantity) {
|
|
throw new NotEnoughTicketsException;
|
|
}
|
|
|
|
return $tickets;
|
|
}
|
|
|
|
public function createOrder($email, $tickets)
|
|
{
|
|
$order = $this->orders()->create([
|
|
'email' => $email,
|
|
'amount' => $tickets->count() * $this->ticket_price,
|
|
]);
|
|
|
|
foreach ($tickets as $ticket) {
|
|
$order->tickets()->save($ticket);
|
|
}
|
|
|
|
return $order;
|
|
}
|
|
|
|
public function addTickets($quantity)
|
|
{
|
|
foreach (range(1, $quantity) as $i) {
|
|
$this->tickets()->create([]);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function ticketsRemaining()
|
|
{
|
|
return $this->tickets()->available()->count();
|
|
}
|
|
}
|