File: /home/imensosw/www/mpl.imenso.co/app/Models/Enquiry.php
<?php
namespace App\Models;
use App\Events\NewEnquiry;
use App\Events\PromoterAddsArtistToEnquiry;
use App\Events\PromoterCreatesChallenge;
use App\Events\PromoterCreatesEnquiry;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class Enquiry extends Model
{
protected $table = 'enquiries';
protected $casts = [
'enquiry_at' => 'datetime',
];
protected $fillable = [
'venue_id',
'promoter_id',
'enquiry_at',
];
public function artists()
{
return $this->belongsToMany(\App\Models\User::class, 'enquiry_artists', 'enquiry_id', 'artist_id');
}
public function venue()
{
return $this->belongsTo(\App\Models\User::class, 'venue_id');
}
public function promoter()
{
return $this->belongsTo(\App\Models\User::class, 'promoter_id');
}
public function status()
{
return $this->belongsTo(\App\Models\EventStatus::class, 'status_id');
}
public function announcedEvents()
{
return $this->events()->where('status_id', 3)->get();
}
public function events()
{
return $this->hasMany(\App\Models\Event::class);
}
public function threads()
{
return MessageThread::where('enquiry_id', $this->id)->get();
}
public function tourDate()
{
return $this->belongsTo(TourPlannerDate::class, 'tour_date_id');
}
public function hasSiblingPencils()
{
$siblings = self::where([
'venue_id' => $this->venue_id,
'pencilled' => 1,
])
->where('id', '!=', $this->id)
->whereDate('enquiry_at', '=', $this->enquiry_at->format('Y-m-d'))
->count();
if ($siblings > 0) {
return true;
} else {
return false;
}
}
public function getSiblingPencils()
{
$siblings = self::where([
'venue_id' => $this->venue_id,
'pencilled' => 1,
])
->where('id', '!=', $this->id)
->whereDate('enquiry_at', '=', $this->enquiry_at->format('Y-m-d'))
->get();
return $siblings;
}
public function getSiblingEnquirys()
{
$siblings = self::where([
'venue_id' => $this->venue_id,
])
->where('id', '!=', $this->id)
->whereDate('enquiry_at', '=', $this->enquiry_at->format('Y-m-d'))
->get();
return $siblings;
}
public function hasConfirmedSiblingEnquirys()
{
// dd(
// $this->where([
// 'venue_id' => $this->venue_id,
// 'confirmed' => 1,
// ])
// ->where('id', '!=', $this->id)
// ->whereDate('enquiry_at', '=', $this->enquiry_at->format('Y-m-d'))
// ->exists()
// );
return $this->where([
'venue_id' => $this->venue_id,
'confirmed' => 1,
])
->where('id', '!=', $this->id)
->whereNull('closed')
->whereDate('enquiry_at', '=', $this->enquiry_at->format('Y-m-d'))
->exists();
}
public function isFirstPencil()
{
if ($this->pencil_order == 1) {
return true;
} else {
return false;
}
}
public function isLastPencil()
{
foreach ($this->getSiblingPencils() as $sibling) {
if ($sibling->pencil_order > $this->pencil_order) {
return false;
}
}
return true;
}
public function venue_thread()
{
foreach ($this->threads() as $thread) {
foreach ($thread->participants as $participant) {
if ($participant->isVenue()) {
return $thread;
}
}
}
return false;
}
public function promoter_thread()
{
foreach ($this->threads() as $thread) {
foreach ($thread->participants as $participant) {
if ($participant->isPromoter()) {
foreach ($thread->participants as $participant) {
if ($participant->id == Auth::user()->id) {
return $thread;
}
}
}
}
}
return false;
}
public function artist_threads()
{
$threads = new Collection;
foreach ($this->threads() as $thread) {
foreach ($this->artists as $artist) {
if ($thread->getRecipient()->id == $artist->id) {
$threads->push($thread);
}
}
}
return $threads;
}
public function canView(User $user)
{
if (
(isset($this->promoter) && ($user->id == $this->promoter->id)) ||
$user->id == $this->venue->id
) {
return true;
}
foreach ($this->artists as $artist) {
if ($artist->id == $user->id) {
return true;
}
}
return false;
}
public static function addNew($data)
{
$enquiry = self::create([
'venue_id' => $data['venue_id'],
'promoter_id' => $data['promoter_id'],
'enquiry_at' => \Carbon\Carbon::createFromFormat('Y-m-d h:i:s', $data['date']),
]);
if (isset($data['tour_date_id']) && !empty($data['tour_date_id'])) {
$enquiry->tour_date_id = $data['tour_date_id'];
}
if (isset($data['subject'])) {
$enquiry->subject = $data['subject'];
}
if (isset($data['promoter_id']) && !empty($data['promoter_id'])) {
event(new PromoterCreatesEnquiry($enquiry));
if (isset($data['message'])) {
$enquiry->addVenueMessage($data['message'], Auth::user());
}
}
$enquiry->save();
event(new NewEnquiry($enquiry));
return $enquiry;
}
public static function addNewChallenge($date, User $venue)
{
$enquiry = self::create([
'venue_id' => $venue->id,
'promoter_id' => Auth::user()->id,
'enquiry_at' => \Carbon\Carbon::createFromFormat('Y-m-d', $date),
]);
$enquiry->challenge = 1;
$enquiry->save();
event(new PromoterCreatesChallenge($enquiry));
return $enquiry;
}
public function createVenueThread()
{
if (! $this->venue_thread()) {
$thread = MessageThread::addNew($this->id);
$thread->addParticipant($this->promoter);
$thread->addParticipant($this->venue);
}
}
public function addVenueMessage($message, User $sender)
{
if (! $this->venue_thread()) {
$thread = MessageThread::addNew($this->id);
$thread->addParticipant($this->promoter);
$thread->addParticipant($this->venue);
}
$this->venue_thread()->addMessage($message, $sender);
}
public function addArtist(User $artist)
{
if ($this->artists()->where('id', $artist->id)->count() == 0) {
$now = new \Carbon\Carbon;
DB::table('enquiry_artists')->insert([
'enquiry_id' => $this->id,
'artist_id' => $artist->id,
'created_at' => $now,
'updated_at' => $now,
'status' => 1,
]);
if ($this->promoter) {
$thread = MessageThread::addNew($this->id);
$thread->addParticipant($this->promoter);
$thread->addParticipant($artist);
event(new PromoterAddsArtistToEnquiry($this, $artist));
} else {
$thread = MessageThread::addNew($this->id);
$thread->addParticipant($this->venue);
$thread->addParticipant($artist);
}
return $thread;
}
}
public function hasTourDate()
{
if ($this->tour_date_id) {
return true;
}
return false;
}
public function isAChallenge()
{
return
$this->challenge == 1;
}
public function isChallengeable()
{
return
$this->challenge != 1 &&
$this->venue->isAvailable($this->enquiry_at) &&
! $this->venue->hasEvent($this->enquiry_at) &&
! $this->venue->isChallenged($this->enquiry_at) &&
$this->venue->pencils($this->enquiry_at)->count() > 0;
}
}