Add currency test data and enhance FetchCurrencyRates job with TCMB integration

- Introduced a new JSON file containing currency rates for various currencies, including USD, EUR, and GBP, to standardize currency data representation.
- Enhanced the FetchCurrencyRates job to fetch USD data from TCMB if not available, improving data reliability.
- Added logging for TCMB data fetching process to assist in monitoring and debugging.

These changes collectively improve the accuracy and robustness of currency rate handling in the Truncgil Finance application.
This commit is contained in:
Ümit Tunç
2025-07-05 09:48:07 +03:00
parent 41eaf7c6df
commit b494d45ed8
2 changed files with 95 additions and 1 deletions
+94 -1
View File
@@ -20,7 +20,7 @@ class FetchCurrencyRates implements ShouldQueue
$data = [];
$data['Update_Date'] = now()->format('Y-m-d H:i:s');
// Döviz kurları
// Döviz kurları - ana kaynak
$response = $this->fetchData('https://kur.doviz.com');
// DOM işlemleri için veri çekme
@@ -53,11 +53,24 @@ class FetchCurrencyRates implements ShouldQueue
}
}
// Eğer USD verisi yoksa veya boşsa, TCMB'den veri çek
if (!isset($data['USD']) || empty($data['USD']['Buying'])) {
\Log::info('USD verisi bulunamadı, TCMB\'den veri çekiliyor...');
$tcmbData = $this->fetchFromTCMB();
if (!empty($tcmbData)) {
\Log::info('TCMB\'den ' . count($tcmbData) . ' kur alındı');
$data = array_merge($data, $tcmbData);
} else {
\Log::error('TCMB\'den veri alınamadı');
}
}
// AZN kuru için
$aznResponse = $this->fetchData('https://wise.com/tr/currency-converter/azn-to-try-rate?amount=1');
preg_match('/(\d+\.\d+)\s+TRY/', $aznResponse->body(), $matches);
if (isset($matches[1])) {
$data['AZN'] = [
'Name' => 'AZERBAYCAN YENİ MANATI',
'Buying' => NumberFormatter::commaToDot($matches[1]),
'Selling' => NumberFormatter::commaToDot($matches[1]),
'Change' => "0.00",
@@ -78,4 +91,84 @@ class FetchCurrencyRates implements ShouldQueue
'Accept-Language' => 'en'
])->get($url);
}
private function fetchFromTCMB()
{
try {
$response = $this->fetchData('https://www.tcmb.gov.tr/kurlar/today.xml');
if (!$response->successful()) {
return [];
}
$xml = simplexml_load_string($response->body());
if (!$xml) {
return [];
}
$data = [];
// Currency mapping - TCMB'deki isimleri kod olarak kullan
$currencyMap = [
'USD' => 'US DOLLAR',
'EUR' => 'EURO',
'GBP' => 'POUND STERLING',
'CHF' => 'SWISS FRANK',
'JPY' => 'JAPENESE YEN',
'CAD' => 'CANADIAN DOLLAR',
'AUD' => 'AUSTRALIAN DOLLAR',
'SEK' => 'SWEDISH KRONA',
'NOK' => 'NORWEGIAN KRONE',
'DKK' => 'DANISH KRONE',
'RUB' => 'RUSSIAN ROUBLE',
'CNY' => 'CHINESE RENMINBI',
'SAR' => 'SAUDI RIYAL',
'KWD' => 'KUWAITI DINAR',
'QAR' => 'QATARI RIAL',
'AED' => 'UNITED ARAB EMIRATES DIRHAM',
'PKR' => 'PAKISTANI RUPEE',
'KRW' => 'SOUTH KOREAN WON',
'AZN' => 'AZERBAIJANI NEW MANAT',
'BGN' => 'BULGARIAN LEV',
'RON' => 'NEW LEU'
];
foreach ($xml->Currency as $currency) {
$code = (string)$currency['CurrencyCode'];
$unit = (float)$currency->Unit;
$name = (string)$currency->CurrencyName;
$buying = (float)$currency->BanknoteBuying;
$selling = (float)$currency->BanknoteSelling;
$forexBuying = (float)$currency->ForexBuying;
$forexSelling = (float)$currency->ForexSelling;
// Birim düzeltmesi (100 yen = 1 birim gibi)
if ($unit > 1) {
$buying = $buying / $unit;
$selling = $selling / $unit;
$forexBuying = $forexBuying / $unit;
$forexSelling = $forexSelling / $unit;
}
// Forex kurları varsa onları kullan, yoksa banknot kurlarını kullan
$finalBuying = $forexBuying > 0 ? $forexBuying : $buying;
$finalSelling = $forexSelling > 0 ? $forexSelling : $selling;
if ($finalBuying > 0 && $finalSelling > 0) {
$data[$code] = [
'Name' => $name,
'Buying' => round($finalBuying, 4),
'Selling' => round($finalSelling, 4),
'Change' => 0.00,
'Type' => 'Currency'
];
}
}
return $data;
} catch (\Exception $e) {
return [];
}
}
}