/**
  * @return string
  */
 public static function getBaseCurrency()
 {
     if (self::$baseCurrency === '') {
         $skipCache = defined('CURRENCY_SKIP_CACHE') && CURRENCY_SKIP_CACHE;
         $tableName = CurrencyTable::getTableName();
         $currencyFound = false;
         $currencyFromCache = false;
         if (!$skipCache) {
             $cacheTime = (int) (defined('CURRENCY_CACHE_TIME') ? CURRENCY_CACHE_TIME : CURRENCY_CACHE_DEFAULT_TIME);
             $managedCache = Application::getInstance()->getManagedCache();
             $currencyFromCache = $managedCache->read($cacheTime, self::CACHE_BASE_CURRENCY_ID, $tableName);
             if ($currencyFromCache) {
                 $currencyFound = true;
                 self::$baseCurrency = (string) $managedCache->get(self::CACHE_BASE_CURRENCY_ID, $tableName);
             }
         }
         if ($skipCache || !$currencyFound) {
             $currencyIterator = CurrencyTable::getList(array('select' => array('CURRENCY'), 'filter' => array('BASE' => 'Y', 'AMOUNT' => 1)));
             if ($currency = $currencyIterator->fetch()) {
                 $currencyFound = true;
                 self::$baseCurrency = $currency['CURRENCY'];
             }
             unset($currency, $currencyIterator);
         }
         if (!$skipCache && $currencyFound && !$currencyFromCache) {
             $managedCache->set(self::CACHE_BASE_CURRENCY_ID, self::$baseCurrency, $tableName);
         }
     }
     return self::$baseCurrency;
 }
 /**
  * Update base currency.
  *
  * @param string $currency			Currency id.
  * @return bool
  */
 public static function updateBaseCurrency($currency)
 {
     /** @global \CUser $USER */
     global $USER;
     $currency = CurrencyManager::checkCurrencyID($currency);
     if ($currency === false) {
         return false;
     }
     $event = new Main\Event('currency', self::EVENT_ON_UPDATE_BASE_CURRENCY, array('NEW_BASE_CURRENCY' => $currency));
     $event->send();
     unset($event);
     $conn = Main\Application::getConnection();
     $helper = $conn->getSqlHelper();
     $userID = isset($USER) && $USER instanceof \CUser ? (int) $USER->getID() : 0;
     $tableName = $helper->quote(CurrencyTable::getTableName());
     $baseField = $helper->quote('BASE');
     $dateUpdateField = $helper->quote('DATE_UPDATE');
     $modifiedByField = $helper->quote('MODIFIED_BY');
     $amountField = $helper->quote('AMOUNT');
     $amountCntField = $helper->quote('AMOUNT_CNT');
     $currencyField = $helper->quote('CURRENCY');
     $query = 'update ' . $tableName . ' set ' . $baseField . ' = \'N\', ' . $dateUpdateField . ' = ' . $helper->getCurrentDateTimeFunction() . ', ' . $modifiedByField . ' = ' . ($userID == 0 ? 'NULL' : $userID) . ' where ' . $currencyField . ' <> \'' . $helper->forSql($currency) . '\' and ' . $baseField . ' = \'Y\'';
     $conn->queryExecute($query);
     $query = 'update ' . $tableName . ' set ' . $baseField . ' = \'Y\', ' . $dateUpdateField . ' = ' . $helper->getCurrentDateTimeFunction() . ', ' . $modifiedByField . ' = ' . ($userID == 0 ? 'NULL' : $userID) . ', ' . $amountField . ' = 1, ' . $amountCntField . ' = 1 where ' . $currencyField . ' = \'' . $helper->forSql($currency) . '\'';
     $conn->queryExecute($query);
     static::updateBaseRates();
     $event = new Main\Event('currency', self::EVENT_ON_AFTER_UPDATE_BASE_CURRENCY, array('NEW_BASE_CURRENCY' => $currency));
     $event->send();
     unset($event);
     self::$baseCurrency = '';
     return true;
 }