You are here

function commerce_multicurrency_conversion in Commerce Multicurrency 7

Converts a currency amount into another.

Parameters

integer $amount: The amount to convert.

string $currency_code: The currency code of the amount.

string $target_currency_code: The currency code to convert the amount to.

Return value

integer|FALSE The converted amount or FALSE on failure.

1 string reference to 'commerce_multicurrency_conversion'
commerce_multicurrency_commerce_currency_info_alter in ./commerce_multicurrency.module
Implements hook_commerce_currency_info_alter().

File

./commerce_multicurrency.module, line 165
Enhancements for the commerce currency support.

Code

function commerce_multicurrency_conversion($amount, $currency_code, $target_currency_code) {
  $conversion_settings =& drupal_static(__FUNCTION__, FALSE);

  // Skip - makes no sense to calculate here.
  if ($currency_code == $target_currency_code) {
    return $amount;
  }

  // Check if there are conversion settings.
  if ($conversion_settings == FALSE && !($conversion_settings = variable_get('commerce_multicurrency_conversion_settings', FALSE))) {
    watchdog('commerce_multicurrency', 'No conversion rates found - please configure them!', array(), WATCHDOG_ERROR, url('admin/commerce/config/currency/conversion'));
    return FALSE;
  }

  // If there's a conversion rate available use it right away.
  if (!empty($conversion_settings[$currency_code]['rates'][$target_currency_code]['rate'])) {
    return $amount * $conversion_settings[$currency_code]['rates'][$target_currency_code]['rate'];
  }

  // Run cross conversion processing if enabled.
  if (variable_get('commerce_multicurrency_use_cross_conversion', TRUE)) {
    $default_currency_code = commerce_default_currency();

    // If target currency is equal to default currency, we can use reciprocal
    // rate.
    if ($target_currency_code == $default_currency_code && !empty($conversion_settings[$default_currency_code]['rates'][$currency_code]['rate'])) {
      return $amount / $conversion_settings[$default_currency_code]['rates'][$currency_code]['rate'];
    }

    // Using cross rate basing on default currency.
    if (!empty($conversion_settings[$default_currency_code]['rates'][$currency_code]['rate']) && !empty($conversion_settings[$default_currency_code]['rates'][$target_currency_code]['rate'])) {
      $currencies = commerce_currencies();
      if (isset($currencies[$currency_code]['conversion_rate']) && !empty($currencies[$target_currency_code]['conversion_rate'])) {
        return $amount * $currencies[$currency_code]['conversion_rate'] / $currencies[$target_currency_code]['conversion_rate'];
      }
    }
  }

  // If there are no conversion settings for the specified currencies.
  watchdog('commerce_multicurrency', 'No conversion rate from %source_currency to  %target_currency found - please configure it!', array(
    '%source_currency' => $currency_code,
    '%target_currency' => $target_currency_code,
  ), WATCHDOG_ERROR, url('admin/commerce/config/currency/conversion'));
  return FALSE;
}