webmoney/backend/app/Models/User.php

236 lines
5.4 KiB
PHP

<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable, HasApiTokens;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name',
'first_name',
'last_name',
'email',
'phone_country_code',
'phone',
'accept_whatsapp',
'accept_emails',
'avatar',
'country',
'timezone',
'locale',
'language',
'currency',
'password',
'is_admin',
'email_verified_at',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'is_admin' => 'boolean',
'accept_whatsapp' => 'boolean',
'accept_emails' => 'boolean',
];
}
/**
* Get full name attribute
*/
public function getFullNameAttribute(): string
{
if ($this->first_name && $this->last_name) {
return "{$this->first_name} {$this->last_name}";
}
return $this->name ?? '';
}
/**
* Get full phone with country code
*/
public function getFullPhoneAttribute(): ?string
{
if ($this->phone_country_code && $this->phone) {
return "{$this->phone_country_code} {$this->phone}";
}
return $this->phone;
}
/**
* Verifica se o usuário é administrador
*/
public function isAdmin(): bool
{
return $this->is_admin === true;
}
// ==================== RESOURCE RELATIONSHIPS ====================
/**
* Get all accounts for the user
*/
public function accounts()
{
return $this->hasMany(Account::class);
}
/**
* Get all categories for the user
*/
public function categories()
{
return $this->hasMany(Category::class);
}
/**
* Get all budgets for the user
*/
public function budgets()
{
return $this->hasMany(Budget::class);
}
/**
* Get all transactions for the user
*/
public function transactions()
{
return $this->hasMany(Transaction::class);
}
/**
* Get all goals for the user
*/
public function goals()
{
return $this->hasMany(FinancialGoal::class);
}
// ==================== SUBSCRIPTION RELATIONSHIPS ====================
/**
* Get all subscriptions for the user
*/
public function subscriptions()
{
return $this->hasMany(Subscription::class);
}
/**
* Get the active subscription
*/
public function subscription()
{
return $this->hasOne(Subscription::class)->active()->latest();
}
/**
* Get all invoices for the user
*/
public function invoices()
{
return $this->hasMany(Invoice::class);
}
// ==================== SUBSCRIPTION HELPERS ====================
/**
* Check if user has an active subscription
*/
public function hasActiveSubscription(): bool
{
return $this->subscriptions()->active()->exists();
}
/**
* Check if user is on trial
*/
public function onTrial(): bool
{
$subscription = $this->subscriptions()->active()->first();
return $subscription && $subscription->isOnTrial();
}
/**
* Check if user is subscribed to a specific plan
*/
public function subscribedTo(string $planSlug): bool
{
return $this->subscriptions()
->active()
->whereHas('plan', fn($q) => $q->where('slug', $planSlug))
->exists();
}
/**
* Check if user has access to a feature
*/
public function hasFeature(string $feature): bool
{
$subscription = $this->subscriptions()->active()->with('plan')->first();
if (!$subscription) {
// Check free plan limits
$freePlan = Plan::getFreePlan();
return $freePlan && $freePlan->hasFeature($feature);
}
return $subscription->plan->hasFeature($feature);
}
/**
* Get user's limit for a specific resource
*/
public function getLimit(string $resource, $default = null)
{
$subscription = $this->subscriptions()->active()->with('plan')->first();
if (!$subscription) {
// Check free plan limits
$freePlan = Plan::getFreePlan();
return $freePlan ? $freePlan->getLimit($resource, $default) : $default;
}
return $subscription->plan->getLimit($resource, $default);
}
/**
* Get the current plan
*/
public function currentPlan(): ?Plan
{
$subscription = $this->subscriptions()->active()->with('plan')->first();
return $subscription ? $subscription->plan : Plan::getFreePlan();
}
}