Attendize/app/Models/Order.php

173 lines
4.1 KiB
PHP
Raw Normal View History

2016-02-29 15:59:36 +00:00
<?php
namespace App\Models;
2016-03-05 00:18:10 +00:00
use File;
2016-02-29 15:59:36 +00:00
use Illuminate\Database\Eloquent\SoftDeletes;
use PDF;
2016-03-05 00:18:10 +00:00
class Order extends MyBaseModel
{
2016-02-29 15:59:36 +00:00
use SoftDeletes;
2016-03-14 16:37:38 +00:00
/**
* The validation rules of the model.
*
* @var array $rules
*/
2016-02-29 15:59:36 +00:00
public $rules = [
'order_first_name' => ['required'],
2016-03-05 00:18:10 +00:00
'order_last_name' => ['required'],
'order_email' => ['required', 'email'],
2016-02-29 15:59:36 +00:00
];
2016-03-14 16:37:38 +00:00
/**
* The validation error messages.
*
* @var array $messages
*/
2016-02-29 15:59:36 +00:00
public $messages = [
'order_first_name.required' => 'Please enter a valid first name',
2016-03-05 00:18:10 +00:00
'order_last_name.required' => 'Please enter a valid last name',
'order_email.email' => 'Please enter a valid email',
2016-02-29 15:59:36 +00:00
];
2016-03-14 16:37:38 +00:00
/**
* The items associated with the order.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2016-03-05 00:18:10 +00:00
public function orderItems()
{
2016-02-29 15:59:36 +00:00
return $this->hasMany('\App\Models\OrderItem');
}
2016-03-14 16:37:38 +00:00
/**
* The attendees associated with the order.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2016-03-05 00:18:10 +00:00
public function attendees()
{
2016-02-29 15:59:36 +00:00
return $this->hasMany('\App\Models\Attendee');
}
2016-03-14 16:37:38 +00:00
/**
* The account associated with the order.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2016-03-05 00:18:10 +00:00
public function account()
{
2016-02-29 15:59:36 +00:00
return $this->belongsTo('\App\Models\Account');
}
2016-03-14 16:37:38 +00:00
/**
* The event associated with the order.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2016-03-05 00:18:10 +00:00
public function event()
{
2016-02-29 15:59:36 +00:00
return $this->belongsTo('\App\Models\Event');
}
2016-03-14 16:37:38 +00:00
/**
* The tickets associated with the order.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2016-03-05 00:18:10 +00:00
public function tickets()
{
2016-02-29 15:59:36 +00:00
return $this->hasMany('\App\Models\Ticket');
}
2016-03-14 16:37:38 +00:00
/**
* The status associated with the order.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2016-03-05 00:18:10 +00:00
public function orderStatus()
{
2016-02-29 15:59:36 +00:00
return $this->belongsTo('\App\Models\OrderStatus');
}
2016-03-14 16:37:38 +00:00
/**
* Get the organizer fee of the order.
*
* @return \Illuminate\Support\Collection|mixed|static
*/
2016-03-05 00:18:10 +00:00
public function getOrganiserAmountAttribute()
{
2016-02-29 15:59:36 +00:00
return $this->amount + $this->organiser_booking_fee;
}
2016-03-14 16:37:38 +00:00
/**
* Get the total amount of the order.
*
* @return \Illuminate\Support\Collection|mixed|static
*/
2016-03-05 00:18:10 +00:00
public function getTotalAmountAttribute()
{
2016-02-29 15:59:36 +00:00
return $this->amount + $this->organiser_booking_fee + $this->booking_fee;
}
2016-03-14 16:37:38 +00:00
/**
* Get the full name of the order.
*
* @return string
*/
2016-03-05 00:18:10 +00:00
public function getFullNameAttribute()
{
return $this->first_name.' '.$this->last_name;
2016-02-29 15:59:36 +00:00
}
/**
2016-03-05 00:18:10 +00:00
* Generate and save the PDF tickets.
*
2016-02-29 15:59:36 +00:00
* @todo Move this from the order model
2016-03-05 00:18:10 +00:00
*
* @return bool
2016-02-29 15:59:36 +00:00
*/
2016-03-05 00:18:10 +00:00
public function generatePdfTickets()
{
2016-02-29 15:59:36 +00:00
$data = [
2016-03-05 00:18:10 +00:00
'order' => $this,
'event' => $this->event,
'tickets' => $this->event->tickets,
'attendees' => $this->attendees,
2016-02-29 15:59:36 +00:00
];
2016-03-05 00:18:10 +00:00
$pdf_file_path = public_path(config('attendize.event_pdf_tickets_path')).'/'.$this->order_reference;
$pdf_file = $pdf_file_path.'.pdf';
2016-02-29 15:59:36 +00:00
if (file_exists($pdf_file)) {
return true;
}
2016-03-05 00:18:10 +00:00
if (!is_dir($pdf_file_path)) {
2016-02-29 15:59:36 +00:00
File::makeDirectory($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';
2016-02-29 15:59:36 +00:00
$this->save();
return file_exists($pdf_file);
}
2016-03-14 16:37:38 +00:00
/**
* Boot all of the bootable traits on the model.
*/
2016-03-05 00:18:10 +00:00
public static function boot()
{
2016-02-29 15:59:36 +00:00
parent::boot();
2016-03-05 00:18:10 +00:00
static::creating(function ($order) {
$order->order_reference = strtoupper(str_random(5)).date('jn');
2016-02-29 15:59:36 +00:00
});
}
}