/** * Convert a currency value to another currency * @param Float $amount * @param String $currency_from Please use constant CURRENCY_CODE_* * @param String $currency_to Please use constant CURRENCY_CODE_* * @return Float */ function convertCurrency($amount, $currency_from, $currency_to) { $exchange_rate = load_config('exchange-rate'); $key = generateExchangeRateKey($currency_from, $currency_to); return $amount * $exchange_rate[$key]; }
<?php global $db; require_once '../confy.php'; require_once '../functions.php'; require_once '../functions-2.php'; tep_db_connect(); use_class('logger'); $logger = new logger('cron', 'daily-exchange-rate'); $api_status_ok = true; $exchange_rate = array(); $exchange_rate['date'] = date('d.m.Y H:i:s'); $currencies = getAllCurrencies(); foreach ($currencies as $c_from) { foreach ($currencies as $c_to) { $key = generateExchangeRateKey($c_from, $c_to); if ($c_from == $c_to) { $rate = 1; } else { $rate = getExchangeRateOnline($c_from, $c_to); if ($rate == 0 || $rate === false) { $api_status_ok = false; } } $exchange_rate[$key] = $rate; $logger->write('Rate for ' . $key . ': ' . $rate); } } if ($api_status_ok) { save_config('exchange-rate', $exchange_rate); $logger->write('Save rate to configuration file');