b494d45ed8
- 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.
175 lines
6.3 KiB
PHP
175 lines
6.3 KiB
PHP
<?php
|
||
|
||
namespace App\Jobs;
|
||
|
||
use Illuminate\Bus\Queueable;
|
||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
use Illuminate\Foundation\Bus\Dispatchable;
|
||
use Illuminate\Queue\InteractsWithQueue;
|
||
use Illuminate\Queue\SerializesModels;
|
||
use Illuminate\Support\Facades\Http;
|
||
use Illuminate\Support\Facades\Storage;
|
||
use App\Helpers\NumberFormatter;
|
||
|
||
class FetchCurrencyRates implements ShouldQueue
|
||
{
|
||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||
|
||
public function handle()
|
||
{
|
||
$data = [];
|
||
$data['Update_Date'] = now()->format('Y-m-d H:i:s');
|
||
|
||
// Döviz kurları - ana kaynak
|
||
$response = $this->fetchData('https://kur.doviz.com');
|
||
|
||
// DOM işlemleri için veri çekme
|
||
$dom = new \DOMDocument();
|
||
@$dom->loadHTML($response->body());
|
||
$xpath = new \DOMXPath($dom);
|
||
|
||
// Döviz kurlarını çekme
|
||
$elements = $xpath->query("//*[@data-socket-key]");
|
||
foreach ($elements as $element) {
|
||
$name = $element->getAttribute('data-socket-key');
|
||
$type = $element->getAttribute('data-socket-attr');
|
||
$value = NumberFormatter::commaToDot($element->nodeValue);
|
||
|
||
if (trim($name) !== '') {
|
||
if ($name == "JPY") $value = $value / 100;
|
||
if (strlen($name) === 3) {
|
||
// Para birimi ismini bul
|
||
$nameElement = $xpath->query(".//div[@class='cname']", $element->parentNode);
|
||
if ($nameElement->length > 0) {
|
||
$data[$name]['Name'] = trim($nameElement->item(0)->nodeValue);
|
||
}
|
||
|
||
if ($type == "bid") $data[$name]['Buying'] = (float)$value;
|
||
if ($type == "ask") $data[$name]['Selling'] = (float)$value;
|
||
if ($type == "c") $data[$name]['Change'] = round((float)$value, 2);
|
||
|
||
$data[$name]['Type'] = "Currency";
|
||
}
|
||
}
|
||
}
|
||
|
||
// 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",
|
||
'Type' => "Currency"
|
||
];
|
||
}
|
||
|
||
// JSON dosyasını kaydet
|
||
Storage::put('currency/today.json', json_encode($data, JSON_UNESCAPED_UNICODE));
|
||
|
||
return $data;
|
||
}
|
||
|
||
private function fetchData($url)
|
||
{
|
||
return Http::withHeaders([
|
||
'User-Agent' => 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10',
|
||
'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 [];
|
||
}
|
||
}
|
||
}
|