'decimal:2', 'total_deposited' => 'decimal:2', 'total_withdrawn' => 'decimal:2', 'is_frozen' => 'boolean', 'frozen_at' => 'datetime', ]; public function user(): BelongsTo { return $this->belongsTo(User::class); } public function transactions(): HasMany { return $this->hasMany(WalletTransaction::class)->orderByDesc('created_at'); } public function completedTransactions(): HasMany { return $this->transactions()->where('status', 'completed'); } public function pendingTransactions(): HasMany { return $this->transactions()->where('status', 'pending'); } public function activityLogs(): HasMany { return $this->hasMany(WalletActivityLog::class)->orderByDesc('created_at'); } public function frozenByUser(): BelongsTo { return $this->belongsTo(User::class, 'frozen_by'); } // Helper methods public function isFrozen(): bool { return $this->is_frozen; } public function hasSufficientBalance(float $amount): bool { return $this->balance >= $amount && !$this->is_frozen; } public function freeze(string $reason, ?User $admin = null): bool { return $this->update([ 'is_frozen' => true, 'freeze_reason' => $reason, 'frozen_at' => now(), 'frozen_by' => $admin?->id, ]); } public function unfreeze(?User $admin = null): bool { return $this->update([ 'is_frozen' => false, 'freeze_reason' => null, 'frozen_at' => null, 'frozen_by' => null, ]); } public function refreshBalance(): self { $balance = $this->completedTransactions()->sum('amount'); $totalDeposited = $this->completedTransactions() ->where('type', 'deposit') ->sum('amount'); $totalWithdrawn = $this->completedTransactions() ->whereIn('type', ['withdrawal', 'order_payment']) ->sum('amount'); $this->update([ 'balance' => $balance, 'total_deposited' => $totalDeposited, 'total_withdrawn' => abs($totalWithdrawn), ]); return $this->fresh(); } }