/** * Gets the exchange rate * * @param float $amount * @param str $currencyFrom * @param str $currencyTo * @return boolean */ function getExchangeRate($currencyFrom, $currencyTo = 'USD', $refresh = false) { JTable::addIncludePath(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_sample' . DS . 'tables'); $date = JFactory::getDate(); $now = $date->toSql(); $database = JFactory::getDBO(); $database->setQuery("SELECT DATE_SUB( '{$now}', INTERVAL 1 HOUR )"); $expire_datetime = $database->loadResult(); if ($currencyTo == 'USD') { // get from DB table $tableFrom = JTable::getInstance('Currencies', 'DSCTable'); $tableFrom->load(array('currency_code' => $currencyFrom)); if (!empty($tableFrom->currency_id)) { // Auto Update Enabled? if (DSCConfig::getInstance()->get('currency_exchange_autoupdate', 1)) { // refresh if it's too old or refresh forced if ($tableFrom->updated_date < $expire_datetime || $refresh) { if ($currencyFrom == "USD") { $tableFrom->exchange_rate = (double) 1.0; } else { $tableFrom->exchange_rate = DSCHelperCurrency::getExchangeRateYahoo($currencyFrom, $currencyTo); } $tableFrom->updated_date = $now; $tableFrom->save(); } } return (double) $tableFrom->exchange_rate * 1.0; } else { // invalid currency, fail JError::raiseError('1', JText::_("Invalid Currency Type")); return; } } // Auto Update Enabled? if (DSCConfig::getInstance()->get('currency_exchange_autoupdate', 1)) { $exchange_rate = DSCHelperCurrency::getExchangeRateYahoo($currencyFrom, $currencyTo); } else { // get from DB table $tableFrom = JTable::getInstance('Currencies', 'DSCTable'); $tableTo = JTable::getInstance('Currencies', 'DSCTable'); $tableFrom->load(array('currency_code' => $currencyFrom)); $tableTo->load(array('currency_code' => $currencyTo)); if (!empty($tableFrom->currency_id) && !empty($tableTo->currency_id)) { // Get the exchange rate manually // All Values are USD based, so if (1$ = 1,3�) and (1$ = 1,6�), we have that (1� = 1,23�) // so if we want to the exchange rate � => � is 1,23 $exchange_rate = $tableFrom->exchange_rate / $tableTo->exchange_rate; } else { // invalid currency, fail JError::raiseError('1', JText::_("Invalid Currency Type")); return; } } echo '<h5>' . (double) $exchange_rate * 1.0 . '</h5>'; return (double) $exchange_rate * 1.0; }
/** * Formats and converts a number according to currency rules * As of v0.5.0 is a wrapper * * @param unknown_type $amount * @param unknown_type $currency * @return unknown_type */ public static function currency($amount, $currency = '', $options = '') { $amount = DSCHelperCurrency::_($amount, $currency, $options); return $amount; }