Refactor the Filament dashboard by replacing the legacy FinanceOverviewWidget with a new set of specialized widgets: - DashboardInfoWidget for general information - ExchangeRateWidget for real-time rate monitoring - WalletStats for financial overview Additionally, refactor the ExchangeRateService to improve encapsulation and clean up the ExchangeRateHistory model by moving business logic (change calculation and recording) from the model to the service layer. Changes include: - Removing deprecated helper methods from ExchangeRateHistory model - Implementing direct queries in ExchangeRateService to replace model scopes - Adding support for rate chart data retrieval - Reordering and updating widget sorting in AdminPanelProvider
72 lines
3.1 KiB
PHP
72 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets;
|
|
|
|
use App\Models\Wallet;
|
|
use App\Models\WalletTransaction;
|
|
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
|
|
use Filament\Widgets\StatsOverviewWidget\Stat;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class WalletStats extends BaseWidget
|
|
{
|
|
protected static ?int $sort = 3; // سومین ویجت
|
|
protected static bool $isLazy = false;
|
|
|
|
protected int | string | array $columnSpan = 'full';
|
|
|
|
protected function getStats(): array
|
|
{
|
|
$totalBalance = Wallet::sum('balance');
|
|
$totalDeposits = WalletTransaction::where('status', 'completed')
|
|
->where('amount', '>', 0)
|
|
->sum('amount');
|
|
$totalWithdrawals = WalletTransaction::where('status', 'completed')
|
|
->where('amount', '<', 0)
|
|
->sum(DB::raw('ABS(amount)'));
|
|
$todayTransactions = WalletTransaction::whereDate('created_at', today())->count();
|
|
$activeWallets = Wallet::where('balance', '>', 0)->count();
|
|
$pendingTransactions = WalletTransaction::where('status', 'pending')->count();
|
|
|
|
$chartData = [];
|
|
for ($i = 6; $i >= 0; $i--) {
|
|
$date = now()->subDays($i)->format('Y-m-d');
|
|
$chartData[] = WalletTransaction::whereDate('created_at', $date)
|
|
->where('status', 'completed')
|
|
->sum('amount');
|
|
}
|
|
|
|
return [
|
|
Stat::make('موجودی کل', number_format($totalBalance) . ' ریال')
|
|
->description('مجموع موجودی همه کاربران')
|
|
->descriptionIcon('heroicon-m-banknotes')
|
|
->color('success')
|
|
->chart($chartData),
|
|
|
|
Stat::make('مجموع واریزیها', number_format($totalDeposits) . ' ریال')
|
|
->description('کل مبالغ واریز شده موفق')
|
|
->descriptionIcon('heroicon-m-arrow-down-on-square')
|
|
->color('primary'),
|
|
|
|
Stat::make('مجموع برداشتها', number_format($totalWithdrawals) . ' ریال')
|
|
->description('کل مبالغ برداشت شده موفق')
|
|
->descriptionIcon('heroicon-m-arrow-up-on-square')
|
|
->color('danger'),
|
|
|
|
Stat::make('تراکنشهای امروز', number_format($todayTransactions))
|
|
->description('تعداد تراکنشهای ثبتشده در امروز')
|
|
->descriptionIcon('heroicon-m-calendar')
|
|
->color('info'),
|
|
|
|
Stat::make('کیف پولهای فعال', number_format($activeWallets))
|
|
->description('کاربرانی با موجودی مثبت')
|
|
->descriptionIcon('heroicon-m-user-group')
|
|
->color('success'),
|
|
|
|
Stat::make('در انتظار بررسی', number_format($pendingTransactions))
|
|
->description('تراکنشهای نیاز به تأیید')
|
|
->descriptionIcon('heroicon-m-clock')
|
|
->color($pendingTransactions > 0 ? 'warning' : 'gray'),
|
|
];
|
|
}
|
|
} |