*/ use HasFactory, Notifiable, HasApiTokens; /** * The attributes that are mass assignable. * * @var list */ protected $fillable = [ 'name', 'first_name', 'last_name', 'email', 'phone_country_code', 'phone', 'accept_whatsapp', 'accept_emails', 'avatar', 'country', 'timezone', 'locale', 'password', 'is_admin', ]; /** * The attributes that should be hidden for serialization. * * @var list */ protected $hidden = [ 'password', 'remember_token', ]; /** * Get the attributes that should be cast. * * @return array */ 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(); } }