File: /home/imensosw/www/mpl.imenso.co/app/Models/TicketType.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TicketType extends Model
{
protected $table = 'event_ticket_types';
protected $fillable = [
'ticket_type',
'allocation',
'price',
'booking_fee',
'event_id',
];
public function tickets()
{
return $this->hasMany(\App\Models\Ticket::class, 'ticket_type_id');
}
public function event()
{
return $this->belongsTo(\App\Models\Event::class, 'event_id');
}
public function orderItems()
{
return OrderItem::where('orderable_type', self::class)->where('orderable_id', $this->id)->get();
}
public function scannedTotal()
{
$total = 0;
if ($this->tickets()->where('scanned', 1)->count() > 0) {
foreach ($this->tickets()->where('scanned', 1)->get() as $ticket) {
$total += $ticket->quantity;
}
}
return $total;
}
public function isSoldOut()
{
if ($this->remainingTickets() <= 0) {
return true;
}
return false;
}
public function remainingTickets($incReserved = true)
{
$remaining = $this->allocation;
foreach ($this->tickets as $ticket) {
$remaining -= $ticket->quantity;
}
if ($incReserved == true) {
$reservations = ReservedTicket::where('ticket_type_id', $this->id)->get();
if ($reservations->count() > 0) {
foreach ($reservations as $reservation) {
$remaining -= $reservation->quantity;
}
}
}
return $remaining;
}
public function usedTickets($incReserved = true)
{
return $this->allocation - $this->remainingTickets($incReserved);
}
public function maxTicketsPerPurchase()
{
if ($this->remainingTickets() > 8) {
return 8;
} else {
return $this->remainingTickets();
}
}
public function getPrice($symbol = '£')
{
return $symbol.number_format($this->price, 2, '.', '');
}
public function getBookingFee($symbol = '£')
{
return $symbol.number_format($this->booking_fee, 2, '.', '');
}
public function edit($data)
{
if ($this->validateAllocation($data['qty'])) {
$this->ticket_type = $data['name'];
$this->allocation = $data['qty'];
$this->price = $data['price'];
$this->booking_fee = $data['fee'];
$this->save();
} else {
die('An error has occured.');
}
}
public function validateAllocation($new_allocation)
{
if ($new_allocation > $this->usedTickets()) {
return true;
}
return false;
}
public function remove()
{
if ($this->usedTickets() == 0) {
$this->delete();
} else {
die('You cannot delete this ticket type as it has been purchased already.');
}
}
}