Attendize/app/Models/Order.php

114 lines
2.7 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;
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
];
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-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-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-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-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-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-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-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-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-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-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
});
}
}