first(); if (!$setting) { return config('ifnex.financial_defaults.aed_to_irr', 455000); } return (float) $setting->value; } /** * تنظیم نرخ جدید (توسط کاربر) */ public function setRate( string $from, string $to, float $newRate, ?int $userId = null, ?string $notes = null ): ExchangeRateHistory { $key = strtolower("{$from}_to_{$to}"); $oldRate = $this->getCurrentRate($from, $to); // Update system_settings SystemSetting::updateOrCreate( ['key' => $key], [ 'value' => (string) $newRate, 'type' => 'number', 'description' => "{$from} to {$to} exchange rate", ] ); // ثبت در تاریخچه return ExchangeRateHistory::recordChange( from: $from, to: $to, oldRate: $oldRate, newRate: $newRate, source: 'manual', userId: $userId, notes: $notes ); } /** * دریافت آخرین تغییرات */ public function getLatestChanges(int $limit = 10) { return ExchangeRateHistory::with('changedBy') ->orderBy('created_at', 'desc') ->limit($limit) ->get(); } /** * دریافت اطلاعات آخرین نرخ برای Widget */ public function getLatestRateInfo(string $from = 'AED', string $to = 'IRR'): array { $latest = ExchangeRateHistory::latest($from, $to)->with('changedBy')->first(); $currentRate = $this->getCurrentRate($from, $to); return [ 'current_rate' => $currentRate, 'last_change' => $latest, 'last_updated' => $latest?->created_at, 'changed_by' => $latest?->changedBy?->name, 'source' => $latest?->change_source, 'change_percent' => $latest?->change_percent, ]; } }