35 lines
1.1 KiB
PHP
Executable File
35 lines
1.1 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class UserSubscription extends Model
|
|
{
|
|
protected $fillable = ['user_id', 'subscription_plan_id', 'start_date', 'end_date', 'status', 'payment_approved', 'cancelled_at'];
|
|
|
|
protected $casts = [
|
|
'start_date' => 'datetime',
|
|
'end_date' => 'datetime',
|
|
'payment_approved' => 'boolean',
|
|
'cancelled_at' => 'datetime',
|
|
];
|
|
|
|
public function user(): BelongsTo { return $this->belongsTo(User::class); }
|
|
public function plan(): BelongsTo { return $this->belongsTo(SubscriptionPlan::class, 'subscription_plan_id'); }
|
|
public function invoices(): HasMany { return $this->hasMany(Invoice::class); }
|
|
|
|
public function isActive(): bool
|
|
{
|
|
return $this->status === 'active' && $this->end_date > now();
|
|
}
|
|
|
|
public function isExpiringSoon(int $days = 7): bool
|
|
{
|
|
return $this->isActive() && $this->end_date <= now()->addDays($days);
|
|
}
|
|
} |