File: /home/imensosw/www/mpl.imenso.co/app/Models/Ticket.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
use QrCode;
class Ticket extends Model
{
use Searchable;
protected $table = 'tickets';
protected $fillable = [
'event_id',
'user_id',
'quantity',
'ticket_type_id',
'order_id',
];
public function toSearchableArray()
{
$array = [
'id' => $this->id,
'quantity' => $this->quantity,
'order_id' => $this->order_id,
'barcode' => $this->barcode,
'event' => $this->event,
];
$array['venue_name'] = is_null($this->event->venue) ? null : $this->event->venue->getName();
$array['ticket_type'] = $this->type->ticket_type;
$array['event'] = $this->event->name;
return $array;
}
public function event()
{
return $this->belongsTo(\App\Models\Event::class);
}
public function order()
{
return $this->belongsTo(\App\Models\Order::class);
}
public function owner()
{
return $this->belongsTo(\App\Models\User::class, 'user_id');
}
public function type()
{
return $this->belongsTo(\App\Models\TicketType::class, 'ticket_type_id');
}
public function images()
{
return $this->morphMany(\App\Models\Image::class, 'imageable');
}
public function price()
{
return TicketType::where('id', $this->ticket_type_id)->pluck('price');
}
public function getPricePaid($inc_booking_fee = false)
{
if ($this->order) {
if ($this->order->items->count() > 0) {
if ($inc_booking_fee) {
return $this->order->items->first()->price + $this->getBookingFee();
} else {
return $this->order->items->first()->price;
}
}
}
return false;
}
public function getBookingFee()
{
return $this->type->booking_fee;
}
public function image($image_type_id = 0)
{
if ($this->images()->where('type_id', $image_type_id)->count() > 0) {
return $this->images()->where('type_id', $image_type_id)->first();
} else {
$image = new Image;
$image->src = ImageType::findOrFail($image_type_id)->default_image;
return $image;
}
}
public function setImage($type_id, $location)
{
$image = new Image;
$image->src = $location;
$image->imageable_id = $this->id;
$image->imageable_type = self::class;
$image->type_id = $type_id;
$image->save();
}
public function deleteImage($type_id)
{
if ($this->images()->where('type_id', $type_id)->count() > 0) {
$this->images()->where('type_id', $type_id)->first()->remove();
}
}
public function generateBarcode()
{
$code_set = false;
while (! $code_set) {
$code = strtoupper('MPL-'.substr(uniqid(), 6).'-'.$this->event->venue->id.'-'.$this->event->id);
if (self::where([
'barcode' => $code,
'event_id' => $this->event->id,
])->count() == 0) {
$this->barcode = $code;
$this->save();
$code_set = true;
}
}
$this->generateQRCode();
}
public function generateQRCode()
{
$this->deleteImage(15);
$filename = uniqid().uniqid().uniqid().uniqid().uniqid().uniqid().uniqid().'.png';
$web_location = 'images/uploads/tickets/qr/'.$filename;
$server_location = public_path('images/uploads/tickets/qr/').$filename;
QrCode::format('png')->size(250)->margin(0)->generate($this->barcode, $server_location);
$this->setImage(15, $web_location);
}
public function getQRCode()
{
return $this->image(15);
}
public function markAsScanned()
{
$this->scanned = 1;
$this->save();
}
public static function getMPLBookingFee($price)
{
$fee = 0.5;
return $fee;
}
public static function validateBookingFee($price, $fee)
{
if ($fee >= 0.5 && $fee <= 3) {
return true;
} else {
return false;
}
}
}