Esempio n. 1
0
 function initialize($params = array())
 {
     eZExchangeRatesUpdateHandler::initialize($params);
     $shopINI = eZINI::instance('shop.ini');
     if (!isset($params['ServerName'])) {
         $params['ServerName'] = '';
         if ($shopINI->hasVariable('ECBExchangeRatesSettings', 'ServerName')) {
             $params['ServerName'] = $shopINI->variable('ECBExchangeRatesSettings', 'ServerName');
         }
     }
     if (!isset($params['ServerPort'])) {
         $params['ServerPort'] = '';
         if ($shopINI->hasVariable('ECBExchangeRatesSettings', 'ServerPort')) {
             $params['ServerPort'] = $shopINI->variable('ECBExchangeRatesSettings', 'ServerPort');
         }
     }
     if (!isset($params['RatesURI'])) {
         $params['RatesURI'] = '';
         if ($shopINI->hasVariable('ECBExchangeRatesSettings', 'RatesURI')) {
             $params['RatesURI'] = $shopINI->variable('ECBExchangeRatesSettings', 'RatesURI');
         }
     }
     if (!isset($params['BaseCurrency'])) {
         // the ECB returns currencies against 'EUR'
         $params['BaseCurrency'] = 'EUR';
     }
     $this->setServerName($params['ServerName']);
     $this->setServerPort($params['ServerPort']);
     $this->setRatesURI($params['RatesURI']);
     $this->setBaseCurrency($params['BaseCurrency']);
 }
Esempio n. 2
0
 static function updateAutoRates()
 {
     $error = array('code' => eZExchangeRatesUpdateHandler::OK, 'description' => '');
     $handler = eZExchangeRatesUpdateHandler::create();
     if ($handler) {
         $error = $handler->requestRates();
         if ($error['code'] === eZExchangeRatesUpdateHandler::OK) {
             $rateList = $handler->rateList();
             if (is_array($rateList) && count($rateList) > 0) {
                 $handlerBaseCurrency = $handler->baseCurrency();
                 if ($handlerBaseCurrency) {
                     $shopBaseCurrency = false;
                     $shopINI = eZINI::instance('shop.ini');
                     if ($shopINI->hasVariable('ExchangeRatesSettings', 'BaseCurrency')) {
                         $shopBaseCurrency = $shopINI->variable('ExchangeRatesSettings', 'BaseCurrency');
                     }
                     if (!$shopBaseCurrency) {
                         $shopBaseCurrency = $handlerBaseCurrency;
                     }
                     // update rates for existing currencies
                     //$baseCurrencyCode = $handler->baseCurrency();
                     if (isset($rateList[$shopBaseCurrency]) || $shopBaseCurrency === $handlerBaseCurrency) {
                         // to avoid unnecessary multiplication set $crossBaseRate to 'false';
                         $crossBaseRate = false;
                         if ($shopBaseCurrency !== $handlerBaseCurrency) {
                             $crossBaseRate = 1.0 / (double) $rateList[$shopBaseCurrency];
                             $rateList[$handlerBaseCurrency] = '1.0000';
                         }
                         $currencyList = eZCurrencyData::fetchList();
                         if (is_array($currencyList) && count($currencyList) > 0) {
                             foreach ($currencyList as $currency) {
                                 $rateValue = false;
                                 $currencyCode = $currency->attribute('code');
                                 if (isset($rateList[$currencyCode])) {
                                     $rateValue = $rateList[$currencyCode];
                                     if ($crossBaseRate !== false) {
                                         $rateValue *= $crossBaseRate;
                                     }
                                 } else {
                                     if ($currencyCode === $shopBaseCurrency) {
                                         $rateValue = '1.0000';
                                     }
                                 }
                                 $currency->setAttribute('auto_rate_value', $rateValue);
                                 $currency->sync();
                             }
                         }
                         $error['code'] = eZExchangeRatesUpdateHandler::OK;
                         $error['description'] = ezpI18n::tr('kernel/shop', "'Auto' rates were updated successfully.");
                     } else {
                         $error['code'] = eZExchangeRatesUpdateHandler::INVALID_BASE_CROSS_RATE;
                         $error['description'] = ezpI18n::tr('kernel/shop', "Unable to calculate cross-rate for currency-pair '%1'/'%2'", null, array($handlerBaseCurrency, $shopBaseCurrency));
                     }
                 } else {
                     $error['code'] = eZExchangeRatesUpdateHandler::UNKNOWN_BASE_CURRENCY;
                     $error['description'] = ezpI18n::tr('kernel/shop', 'Unable to determine currency for retrieved rates.');
                 }
             } else {
                 $error['code'] = eZExchangeRatesUpdateHandler::EMPTY_RATE_LIST;
                 $error['description'] = ezpI18n::tr('kernel/shop', 'Retrieved empty list of rates.');
             }
         }
     } else {
         $error['code'] = eZExchangeRatesUpdateHandler::CANT_CREATE_HANDLER;
         $error['description'] = ezpI18n::tr('kernel/shop', 'Unable to create handler to update auto rates.');
     }
     if ($error['code'] !== eZExchangeRatesUpdateHandler::OK) {
         eZDebug::writeError($error['description'], __METHOD__);
     }
     return $error;
 }