Introduce a mechanism to track and audit changes in exchange rates. This includes: - Creating `ExchangeRateHistory` model and migration to store rate changes. - Implementing `ExchangeRateService` to encapsulate rate logic. - Updating `UpdateExchangeRates` command to record history when rates change. - Adding `ExchangeRateHistoryResource` to the Filament admin panel for monitoring.
88 lines
2.4 KiB
PHP
88 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\SystemSetting;
|
|
use App\Models\ExchangeRateHistory;
|
|
|
|
class ExchangeRateService
|
|
{
|
|
/**
|
|
* دریافت نرخ فعلی
|
|
*/
|
|
public function getCurrentRate(string $from = 'AED', string $to = 'IRR'): float
|
|
{
|
|
$key = strtolower("{$from}_to_{$to}");
|
|
$setting = SystemSetting::where('key', $key)->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,
|
|
];
|
|
}
|
|
} |