ifnex/04_Laravel/app/Filament/Resources/CurrencyResource.php
Kazem Alghasi 4fac1dae01 refactor(currency): simplify exchange rate logic to use Rial as base
Refactor the currency management system to eliminate the redundant
`rate_to_aed` field and transition to a single-base model where all
rates are stored relative to the Iranian Rial (IRR).

Key changes:
- Removed `rate_to_aed` from `currencies` table via new migration.
- Updated `ExchangeRateService` to handle rates using a single
  `currency_code` parameter instead of currency pairs.
- Refactored `UpdateExchangeRates` command to fetch rates from the
  Exir.io API and support multiple currencies (AED, USD, EUR, CNY).
- Updated Filament resources (`CurrencyResource`,
  `ExchangeRateHistoryResource`) and widgets to reflect the new
  single-base architecture.
- Improved `PriceCalculatorService` to use the updated service
  methodology for fetching the AED/IRR conversion rate.
- Localized console command output to Persian.
2026-10-29 03:54:02 +03:30

124 lines
5.2 KiB
PHP

<?php
namespace App\Filament\Resources;
use App\Filament\Resources\CurrencyResource\Pages;
use App\Models\Currency;
use App\Services\ExchangeRateService;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Auth;
use Filament\Notifications\Notification;
class CurrencyResource extends Resource
{
protected static ?string $model = Currency::class;
protected static ?string $navigationIcon = 'heroicon-o-currency-yen';
protected static ?string $navigationLabel = 'ارزها';
protected static ?string $navigationGroup = 'تنظیمات';
protected static ?int $navigationSort = 4;
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Section::make('اطلاعات ارز')
->schema([
Forms\Components\TextInput::make('code')
->required()
->maxLength(10)
->unique(ignoreRecord: true)
->label('کد ارز (مثل AED, USD, EUR)'),
Forms\Components\TextInput::make('name')
->required()
->maxLength(100)
->label('نام ارز'),
Forms\Components\TextInput::make('symbol')
->maxLength(10)
->nullable()
->label('نماد (مثل $, €, د.إ)'),
])->columns(3),
Forms\Components\Section::make('نرخ تبدیل به ریال')
->schema([
Forms\Components\TextInput::make('rate_to_rial')
->required()
->numeric()
->default(0)
->suffix('ریال')
->label('نرخ نسبت به ریال')
->helperText('این نرخ مبناست. با تغییر این فیلد، نرخ جدید در تاریخچه ثبت می‌شود.'),
Forms\Components\Toggle::make('is_active')
->default(true)
->label('فعال'),
])->columns(2),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('code')
->searchable()
->sortable()
->badge()
->label('کد'),
Tables\Columns\TextColumn::make('name')
->searchable()
->sortable()
->label('نام'),
Tables\Columns\TextColumn::make('symbol')
->label('نماد'),
Tables\Columns\TextColumn::make('rate_to_rial')
->sortable()
->formatStateUsing(fn ($state) => number_format((float) $state, 0))
->label('نرخ به ریال'),
Tables\Columns\IconColumn::make('is_active')
->boolean()
->label('فعال'),
])
->filters([
Tables\Filters\TernaryFilter::make('is_active')
->label('وضعیت'),
])
->actions([
Tables\Actions\EditAction::make()
->mutateFormDataUsing(function (array $data, Currency $record): array {
// اگه نرخ تغییر کرده، در تاریخچه ثبت کن
if ((float) $data['rate_to_rial'] !== (float) $record->getOriginal('rate_to_rial')) {
app(ExchangeRateService::class)->setRate(
currencyCode: $record->code,
newRate: (float) $data['rate_to_rial'],
userId: Auth::id(),
notes: 'تغییر از پنل مدیریت ارزها'
);
Notification::make()
->title('نرخ ارز به‌روز شد')
->body("نرخ {$record->code} به " . number_format($data['rate_to_rial']) . " ریال تغییر یافت و در تاریخچه ثبت شد.")
->success()
->send();
}
return $data;
}),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
public static function getPages(): array
{
return [
'index' => Pages\ListCurrencies::route('/'),
'create' => Pages\CreateCurrency::route('/create'),
'edit' => Pages\EditCurrency::route('/{record}/edit'),
];
}
}