77 lines
2.3 KiB
PHP
77 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class BrandContract extends Model
|
|
{
|
|
protected $fillable = [
|
|
'title', 'client_id', 'category_id',
|
|
'start_date', 'end_date', 'notes', 'is_active',
|
|
];
|
|
|
|
protected $casts = [
|
|
'start_date' => 'date',
|
|
'end_date' => 'date',
|
|
'is_active' => 'boolean',
|
|
];
|
|
|
|
// ── Relationships ────────────────────────────────────────────
|
|
|
|
public function client(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Client::class);
|
|
}
|
|
|
|
public function category(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ClientCategory::class, 'category_id');
|
|
}
|
|
|
|
// ── Scopes ───────────────────────────────────────────────────
|
|
|
|
public function scopeActive($query, $date = null)
|
|
{
|
|
$date = $date ?? today();
|
|
return $query->where('is_active', true)
|
|
->where('start_date', '<=', $date)
|
|
->where('end_date', '>=', $date);
|
|
}
|
|
|
|
// ── Helpers ──────────────────────────────────────────────────
|
|
|
|
public function isCurrentlyActive(): bool
|
|
{
|
|
return $this->is_active
|
|
&& $this->start_date->lte(today())
|
|
&& $this->end_date->gte(today());
|
|
}
|
|
|
|
public function getDaysRemainingAttribute(): int
|
|
{
|
|
return (int) max(0, today()->diffInDays($this->end_date, false));
|
|
}
|
|
|
|
public function getStatusLabelAttribute(): string
|
|
{
|
|
if (! $this->is_active) return 'Inactive';
|
|
if ($this->start_date->gt(today())) return 'Upcoming';
|
|
if ($this->end_date->lt(today())) return 'Expired';
|
|
return 'Active';
|
|
}
|
|
|
|
public function getStatusColorAttribute(): string
|
|
{
|
|
return match ($this->statusLabel) {
|
|
'Active' => 'badge-success',
|
|
'Upcoming' => 'badge-info',
|
|
'Expired' => 'badge-neutral',
|
|
default => 'badge-ghost',
|
|
};
|
|
}
|
|
}
|