MOON
Server: Apache
System: Linux e2e-78-16.ssdcloudindia.net 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
User: imensosw (1005)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
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 = '&pound;')
    {
        return $symbol.number_format($this->price, 2, '.', '');
    }

    public function getBookingFee($symbol = '&pound;')
    {
        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.');
        }
    }
}