mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 18:45:01 +00:00
58 lines
1.3 KiB
PHP
58 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use App\Facades\OrderConfirmationNumber;
|
|
|
|
class Order extends Model
|
|
{
|
|
protected $guarded = [];
|
|
|
|
public static function forTickets($tickets, $email, $charge)
|
|
{
|
|
$order = self::create([
|
|
'confirmation_number' => OrderConfirmationNumber::generate(),
|
|
'email' => $email,
|
|
'amount' => $charge->amount(),
|
|
'card_last_four' => $charge->cardLastFour(),
|
|
]);
|
|
|
|
$tickets->each->claimFor($order);
|
|
|
|
return $order;
|
|
}
|
|
|
|
public static function findByConfirmationNumber($confirmationNumber)
|
|
{
|
|
return self::where('confirmation_number', $confirmationNumber)->firstOrFail();
|
|
}
|
|
|
|
public function concert()
|
|
{
|
|
return $this->belongsTo(Concert::class);
|
|
}
|
|
|
|
public function tickets()
|
|
{
|
|
return $this->hasMany(Ticket::class);
|
|
}
|
|
|
|
public function ticketQuantity()
|
|
{
|
|
return $this->tickets()->count();
|
|
}
|
|
|
|
public function toArray()
|
|
{
|
|
return [
|
|
'confirmation_number' => $this->confirmation_number,
|
|
'email' => $this->email,
|
|
'amount' => $this->amount,
|
|
'tickets' => $this->tickets->map(function ($ticket) {
|
|
return ['code' => $ticket->code];
|
|
})->all(),
|
|
];
|
|
}
|
|
}
|