File: /home/imensosw/public_html/mpl.imenso.co/app/Models/ArtistAvailability.php
<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class ArtistAvailability extends Model
{
protected $table = 'artist_availabilities';
protected $casts = [
'availability_at' => 'datetime',
];
public function Artist()
{
return $this->belongsTo(\App\Models\User::class, 'artist_id');
}
public static function addNew($data)
{
$format = 'd/m/Y';
$start_date = Carbon::createFromFormat($format, $data->start_date);
$format = 'd/m/Y';
$end_date = Carbon::createFromFormat($format, $data->end_date);
$group_id = Auth::user()->id.'-'.strtotime(Carbon::now());
if ($start_date != $end_date) { //Range of dates has been sent
for ($date = $start_date; $date->lte($end_date); $date->addDay()) {
//Looping through date range
$avail = new self;
$avail->artist_id = Auth::user()->id;
$avail->group_id = $group_id;
$avail->distance = $data->distance;
$avail->guarentee = $data->gaurentee_option;
if ($data->gaurentee_option === 'yes') {
$avail->guarentee_value = $data->min_guarantee;
}
if ($data->tour == 'yes') {
$avail->tour = 1;
}
$avail->availability_at = $date;
$avail->save();
}
} else { //Only one day
$avail = new self;
$avail->artist_id = Auth::user()->id;
$avail->group_id = $group_id;
$avail->distance = $data->distance;
$avail->guarentee = $data->gaurentee_option;
if ($data->gaurentee_option === 'yes') {
$avail->guarentee_value = $data->min_guarantee;
}
if ($data->tour == 'yes') {
$avail->tour = 1;
}
$avail->availability_at = $start_date;
$avail->save();
}
if (isset($data->save) && $data->save == true) {
$avails = self::where('artist_id', Auth::user()->id)->get();
foreach ($avails as $tmp_avail) {
if ($tmp_avail->is_default == 1) {
$tmp_avail->is_default = 0;
$tmp_avail->save();
}
}
$avail->is_default = 1;
$avail->save();
}
}
public function remove()
{
$this->delete();
}
public function firstInGroup()
{
$group_id = $this->group_id;
$avail = self::where('group_id', $group_id)->orderBy('availability_at')->first();
return $avail;
}
public function lastInGroup()
{
$group_id = $this->group_id;
$avail = self::where('group_id', $group_id)->orderByDesc('availability_at')->first();
return $avail;
}
}