Attendize/app/Models/Order.php

182 lines
4.5 KiB
PHP

<?php
namespace App\Models;
use File;
use Illuminate\Database\Eloquent\SoftDeletes;
use PDF;
class Order extends MyBaseModel
{
use SoftDeletes;
use \Backpack\CRUD\CrudTrait;
/**
* The validation rules of the model.
*
* @var array $rules
*/
public $rules = [
'order_first_name' => ['required'],
'order_last_name' => ['required'],
'order_email' => ['required', 'email'],
];
/**
* The validation error messages.
*
* @var array $messages
*/
public $messages = [
'order_first_name.required' => 'Please enter a valid first name',
'order_last_name.required' => 'Please enter a valid last name',
'order_email.email' => 'Please enter a valid email',
];
/**
* The items associated with the order.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function orderItems()
{
return $this->hasMany(\App\Models\OrderItem::class);
}
/**
* The attendees associated with the order.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function attendees()
{
return $this->hasMany(\App\Models\Attendee::class);
}
/**
* The account associated with the order.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function account()
{
return $this->belongsTo(\App\Models\Account::class);
}
/**
* The event associated with the order.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function event()
{
return $this->belongsTo(\App\Models\Event::class);
}
/**
* The tickets associated with the order.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function tickets()
{
return $this->hasMany(\App\Models\Ticket::class);
}
public function payment_gateway()
{
return $this->belongsTo(\App\Models\PaymentGateway::class);
}
/**
* The status associated with the order.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function orderStatus()
{
return $this->belongsTo(\App\Models\OrderStatus::class);
}
/**
* Get the organizer fee of the order.
*
* @return \Illuminate\Support\Collection|mixed|static
*/
public function getOrganiserAmountAttribute()
{
return $this->amount + $this->organiser_booking_fee + $this->taxamt;
}
/**
* Get the total amount of the order.
*
* @return \Illuminate\Support\Collection|mixed|static
*/
public function getTotalAmountAttribute()
{
return $this->amount + $this->organiser_booking_fee + $this->booking_fee;
}
/**
* Get the full name of the order.
*
* @return string
*/
public function getFullNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
/**
* Generate and save the PDF tickets.
*
* @todo Move this from the order model
*
* @return bool
*/
public function generatePdfTickets()
{
$data = [
'order' => $this,
'event' => $this->event,
'tickets' => $this->event->tickets,
'attendees' => $this->attendees,
'css' => file_get_contents(public_path('assets/stylesheet/ticket.css')),
'image' => base64_encode(file_get_contents(public_path($this->event->organiser->full_logo_path))),
];
$pdf_file_path = public_path(config('attendize.event_pdf_tickets_path')) . '/' . $this->order_reference;
$pdf_file = $pdf_file_path . '.pdf';
if (file_exists($pdf_file)) {
return true;
}
if (!is_dir($pdf_file_path)) {
File::makeDirectory(dirname($pdf_file_path), 0777, true, true);
}
PDF::setOutputMode('F'); // force to file
PDF::html('Public.ViewEvent.Partials.PDFTicket', $data, $pdf_file_path);
$this->ticket_pdf_path = config('attendize.event_pdf_tickets_path') . '/' . $this->order_reference . '.pdf';
$this->save();
return file_exists($pdf_file);
}
/**
* Boot all of the bootable traits on the model.
*/
public static function boot()
{
parent::boot();
static::creating(function ($order) {
$order->order_reference = strtoupper(str_random(5)) . date('jn');
});
}
}