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
67 lines
3.0 KiB
PHP
67 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets;
|
|
|
|
use App\Services\ExchangeRateService;
|
|
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
|
|
use Filament\Widgets\StatsOverviewWidget\Stat;
|
|
use App\Models\ExchangeRateHistory;
|
|
|
|
class ExchangeRateWidget extends BaseWidget
|
|
{
|
|
protected static ?int $sort = 2;
|
|
protected static bool $isLazy = false;
|
|
|
|
protected int | string | array $columnSpan = 'full';
|
|
|
|
protected function getStats(): array
|
|
{
|
|
$service = app(ExchangeRateService::class);
|
|
$aedInfo = $service->getLatestRateInfo('AED', 'IRR');
|
|
$usdInfo = $service->getLatestRateInfo('USD', 'IRR');
|
|
|
|
$aedChangePercent = $aedInfo['change_percent'];
|
|
$usdChangePercent = $usdInfo['change_percent'];
|
|
|
|
// ✅ استفاده از متد جدید getRateChart (بدون scope)
|
|
$aedChart = $service->getRateChart('AED', 'IRR', 7);
|
|
$usdChart = $service->getRateChart('USD', 'IRR', 7);
|
|
|
|
return [
|
|
Stat::make('نرخ درهم (AED)', number_format($aedInfo['current_rate']) . ' ریال')
|
|
->description($aedChangePercent !== null
|
|
? 'تغییر: ' . number_format($aedChangePercent, 2) . '% — توسط ' . ($aedInfo['changed_by'] ?? 'سیستم')
|
|
: 'آخرین تغییر: ' . ($aedInfo['last_updated']?->format('Y/m/d H:i') ?? '—'))
|
|
->descriptionIcon(
|
|
$aedChangePercent > 0 ? 'heroicon-m-arrow-trending-up' :
|
|
($aedChangePercent < 0 ? 'heroicon-m-arrow-trending-down' : 'heroicon-m-minus')
|
|
)
|
|
->color(
|
|
$aedChangePercent > 0 ? 'danger' :
|
|
($aedChangePercent < 0 ? 'success' : 'gray')
|
|
)
|
|
->chart(!empty($aedChart) ? $aedChart : [0]),
|
|
|
|
Stat::make('نرخ دلار (USD)', number_format($usdInfo['current_rate']) . ' ریال')
|
|
->description($usdChangePercent !== null
|
|
? 'تغییر: ' . number_format($usdChangePercent, 2) . '% — توسط ' . ($usdInfo['changed_by'] ?? 'سیستم')
|
|
: 'آخرین تغییر: ' . ($usdInfo['last_updated']?->format('Y/m/d H:i') ?? '—'))
|
|
->descriptionIcon(
|
|
$usdChangePercent > 0 ? 'heroicon-m-arrow-trending-up' :
|
|
($usdChangePercent < 0 ? 'heroicon-m-arrow-trending-down' : 'heroicon-m-minus')
|
|
)
|
|
->color(
|
|
$usdChangePercent > 0 ? 'danger' :
|
|
($usdChangePercent < 0 ? 'success' : 'gray')
|
|
)
|
|
->chart(!empty($usdChart) ? $usdChart : [0]),
|
|
|
|
Stat::make('تغییرات امروز',
|
|
ExchangeRateHistory::whereDate('created_at', today())->count()
|
|
)
|
|
->description('تغییرات نرخ ثبتشده در امروز')
|
|
->descriptionIcon('heroicon-m-arrow-path')
|
|
->color('info'),
|
|
];
|
|
}
|
|
} |