2016-03-05 00:18:10 +00:00
|
|
|
<?php
|
2016-02-29 15:59:36 +00:00
|
|
|
|
2016-03-05 00:18:10 +00:00
|
|
|
namespace App\Models;
|
2016-02-29 15:59:36 +00:00
|
|
|
|
2016-03-05 00:18:10 +00:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2016-02-29 15:59:36 +00:00
|
|
|
|
2016-03-05 00:18:10 +00:00
|
|
|
class Ticket extends MyBaseModel
|
|
|
|
|
{
|
2016-02-29 15:59:36 +00:00
|
|
|
use SoftDeletes;
|
2016-03-05 00:18:10 +00:00
|
|
|
|
2016-03-14 16:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* The rules to validate the model.
|
|
|
|
|
*
|
|
|
|
|
* @var array $rules
|
|
|
|
|
*/
|
2016-02-29 15:59:36 +00:00
|
|
|
public $rules = [
|
2016-03-05 00:18:10 +00:00
|
|
|
'title' => ['required'],
|
|
|
|
|
'price' => ['required', 'numeric', 'min:0'],
|
|
|
|
|
'start_sale_date' => ['date'],
|
|
|
|
|
'end_sale_date' => ['date', 'after:start_sale_date'],
|
|
|
|
|
'quantity_available' => ['integer', 'min:0'],
|
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 = [
|
2016-03-05 00:18:10 +00:00
|
|
|
'price.numeric' => 'The price must be a valid number (e.g 12.50)',
|
|
|
|
|
'title.required' => 'You must at least give a title for your ticket. (e.g Early Bird)',
|
|
|
|
|
'quantity_available.integer' => 'Please ensure the quantity available is a number.',
|
2016-02-29 15:59:36 +00:00
|
|
|
];
|
2016-09-06 20:39:27 +00:00
|
|
|
protected $perPage = 10;
|
2016-02-29 15:59:36 +00:00
|
|
|
|
2016-03-14 16:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* The event associated with the ticket.
|
|
|
|
|
*
|
|
|
|
|
* @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 order associated with the ticket.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
|
*/
|
2016-03-05 00:18:10 +00:00
|
|
|
public function order()
|
|
|
|
|
{
|
2016-02-29 15:59:36 +00:00
|
|
|
return $this->belongsToMany('\App\Models\Order');
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 16:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* The questions associated with the ticket.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
|
*/
|
2016-03-05 00:18:10 +00:00
|
|
|
public function questions()
|
|
|
|
|
{
|
2016-03-27 13:40:34 +00:00
|
|
|
return $this->belongsToMany('\App\Models\Question');
|
2016-02-29 15:59:36 +00:00
|
|
|
}
|
2016-03-05 00:18:10 +00:00
|
|
|
|
2016-03-14 16:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* TODO:implement the reserved method.
|
|
|
|
|
*/
|
2016-03-05 00:18:10 +00:00
|
|
|
public function reserved()
|
|
|
|
|
{
|
2016-02-29 15:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 16:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Scope a query to only include tickets that are sold out.
|
|
|
|
|
*
|
|
|
|
|
* @param $query
|
|
|
|
|
*/
|
2016-03-05 00:18:10 +00:00
|
|
|
public function scopeSoldOut($query)
|
|
|
|
|
{
|
2016-02-29 15:59:36 +00:00
|
|
|
$query->where('remaining_tickets', '=', 0);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 16:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* The attributes that should be mutated to dates.
|
|
|
|
|
*
|
|
|
|
|
* @var array $dates
|
2016-02-29 15:59:36 +00:00
|
|
|
*/
|
2016-03-05 00:18:10 +00:00
|
|
|
public function getDates()
|
|
|
|
|
{
|
|
|
|
|
return ['created_at', 'updated_at', 'start_sale_date', 'end_sale_date'];
|
2016-02-29 15:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 16:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Get the number of tickets remaining.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Support\Collection|int|mixed|static
|
|
|
|
|
*/
|
2016-03-05 00:18:10 +00:00
|
|
|
public function getQuantityRemainingAttribute()
|
|
|
|
|
{
|
|
|
|
|
if (is_null($this->quantity_available)) {
|
2016-02-29 15:59:36 +00:00
|
|
|
return 9999; //Better way to do this?
|
|
|
|
|
}
|
2016-03-05 00:18:10 +00:00
|
|
|
|
2016-02-29 15:59:36 +00:00
|
|
|
return $this->quantity_available - ($this->quantity_sold + $this->quantity_reserved);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 16:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Get the number of tickets reserved.
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2016-03-05 00:18:10 +00:00
|
|
|
public function getQuantityReservedAttribute()
|
|
|
|
|
{
|
2016-02-29 15:59:36 +00:00
|
|
|
$reserved_total = \DB::table('reserved_tickets')
|
2016-09-06 20:39:27 +00:00
|
|
|
->where('ticket_id', $this->id)
|
|
|
|
|
->where('expires', '>', \Carbon::now())
|
|
|
|
|
->sum('quantity_reserved');
|
2016-02-29 15:59:36 +00:00
|
|
|
|
|
|
|
|
return $reserved_total;
|
|
|
|
|
}
|
2016-03-05 00:18:10 +00:00
|
|
|
|
2016-03-14 16:37:38 +00:00
|
|
|
/**
|
2016-09-06 20:39:27 +00:00
|
|
|
* Get the total price of the ticket.
|
2016-03-14 16:37:38 +00:00
|
|
|
*
|
|
|
|
|
* @return float|int
|
|
|
|
|
*/
|
2016-09-06 20:39:27 +00:00
|
|
|
public function getTotalPriceAttribute()
|
2016-03-05 00:18:10 +00:00
|
|
|
{
|
2016-09-06 20:39:27 +00:00
|
|
|
return $this->getTotalBookingFeeAttribute() + $this->price;
|
2016-02-29 15:59:36 +00:00
|
|
|
}
|
2016-03-05 00:18:10 +00:00
|
|
|
|
2016-03-14 16:37:38 +00:00
|
|
|
/**
|
2016-09-06 20:39:27 +00:00
|
|
|
* Get the total booking fee of the ticket.
|
2016-03-14 16:37:38 +00:00
|
|
|
*
|
|
|
|
|
* @return float|int
|
|
|
|
|
*/
|
2016-09-06 20:39:27 +00:00
|
|
|
public function getTotalBookingFeeAttribute()
|
2016-03-05 00:18:10 +00:00
|
|
|
{
|
2016-09-06 20:39:27 +00:00
|
|
|
return $this->getBookingFeeAttribute() + $this->getOrganiserBookingFeeAttribute();
|
2016-02-29 15:59:36 +00:00
|
|
|
}
|
2016-03-05 00:18:10 +00:00
|
|
|
|
2016-03-14 16:37:38 +00:00
|
|
|
/**
|
2016-09-06 20:39:27 +00:00
|
|
|
* Get the booking fee of the ticket.
|
2016-03-14 16:37:38 +00:00
|
|
|
*
|
|
|
|
|
* @return float|int
|
|
|
|
|
*/
|
2016-09-06 20:39:27 +00:00
|
|
|
public function getBookingFeeAttribute()
|
2016-03-05 00:18:10 +00:00
|
|
|
{
|
2016-09-06 20:39:27 +00:00
|
|
|
return (int)ceil($this->price) === 0 ? 0 : round(($this->price * (config('attendize.ticket_booking_fee_percentage') / 100)) + (config('attendize.ticket_booking_fee_fixed')),
|
|
|
|
|
2);
|
2016-02-29 15:59:36 +00:00
|
|
|
}
|
2016-03-05 00:18:10 +00:00
|
|
|
|
2016-03-14 16:37:38 +00:00
|
|
|
/**
|
2016-09-06 20:39:27 +00:00
|
|
|
* Get the organizer's booking fee.
|
2016-03-14 16:37:38 +00:00
|
|
|
*
|
|
|
|
|
* @return float|int
|
|
|
|
|
*/
|
2016-09-06 20:39:27 +00:00
|
|
|
public function getOrganiserBookingFeeAttribute()
|
2016-03-05 00:18:10 +00:00
|
|
|
{
|
2016-09-06 20:39:27 +00:00
|
|
|
return (int)ceil($this->price) === 0 ? 0 : round(($this->price * ($this->event->organiser_fee_percentage / 100)) + ($this->event->organiser_fee_fixed),
|
|
|
|
|
2);
|
2016-02-29 15:59:36 +00:00
|
|
|
}
|
2016-03-05 00:18:10 +00:00
|
|
|
|
2016-03-14 16:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Get the maximum and minimum range of the ticket.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2016-03-05 00:18:10 +00:00
|
|
|
public function getTicketMaxMinRangAttribute()
|
|
|
|
|
{
|
2016-02-29 15:59:36 +00:00
|
|
|
$range = [];
|
2016-03-05 00:18:10 +00:00
|
|
|
|
|
|
|
|
for ($i = $this->min_per_person; $i <= $this->max_per_person; $i++) {
|
2016-02-29 15:59:36 +00:00
|
|
|
$range[] = [$i => $i];
|
|
|
|
|
}
|
2016-03-05 00:18:10 +00:00
|
|
|
|
2016-02-29 15:59:36 +00:00
|
|
|
return $range;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 16:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Indicates if the ticket is free.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2016-03-16 14:25:14 +00:00
|
|
|
public function getIsFreeAttribute()
|
2016-03-05 00:18:10 +00:00
|
|
|
{
|
2016-03-21 17:43:58 +00:00
|
|
|
return ceil($this->price) == 0;
|
2016-02-29 15:59:36 +00:00
|
|
|
}
|
2016-03-05 00:18:10 +00:00
|
|
|
|
2016-02-29 15:59:36 +00:00
|
|
|
/**
|
2016-03-05 00:18:10 +00:00
|
|
|
* Return the maximum figure to go to on dropdowns.
|
|
|
|
|
*
|
2016-02-29 15:59:36 +00:00
|
|
|
* @return int
|
|
|
|
|
*/
|
2016-03-05 00:18:10 +00:00
|
|
|
public function getSaleStatusAttribute()
|
|
|
|
|
{
|
2016-09-06 20:39:27 +00:00
|
|
|
if ($this->start_sale_date !== null && $this->start_sale_date->isFuture()) {
|
2016-03-16 14:25:14 +00:00
|
|
|
return config('attendize.ticket_status_before_sale_date');
|
2016-09-06 20:39:27 +00:00
|
|
|
}
|
2016-02-29 15:59:36 +00:00
|
|
|
|
2016-09-06 20:39:27 +00:00
|
|
|
if ($this->end_sale_date !== null && $this->end_sale_date->isPast()) {
|
2016-03-16 14:25:14 +00:00
|
|
|
return config('attendize.ticket_status_after_sale_date');
|
2016-09-06 20:39:27 +00:00
|
|
|
}
|
2016-02-29 15:59:36 +00:00
|
|
|
|
2016-09-06 20:39:27 +00:00
|
|
|
if ((int)$this->quantity_available > 0 && (int)$this->quantity_remaining <= 0) {
|
2016-03-16 14:25:14 +00:00
|
|
|
return config('attendize.ticket_status_sold_out');
|
2016-09-06 20:39:27 +00:00
|
|
|
}
|
2016-02-29 15:59:36 +00:00
|
|
|
|
2016-09-06 20:39:27 +00:00
|
|
|
if ($this->event->start_date->lte(\Carbon::now())) {
|
2016-03-04 23:27:13 +00:00
|
|
|
return config('attendize.ticket_status_off_sale');
|
2016-09-06 20:39:27 +00:00
|
|
|
}
|
2016-03-05 00:18:10 +00:00
|
|
|
|
2016-03-04 23:27:13 +00:00
|
|
|
return config('attendize.ticket_status_on_sale');
|
2016-02-29 15:59:36 +00:00
|
|
|
}
|
|
|
|
|
}
|