33c33
< function uc_mlticurrency_help($path, $arg) {
---
> function uc_multicurrency_help($path, $arg) {
652a653
>       $blocks[1]['info'] = t('Currency Selection');
656,658c657,667
<       $block['subject'] = t('Country Selection');
<       $block['content'] = drupal_get_form('uc_multicurrency_country_select_form');
<       return $block;
---
>       switch ($delta) {
>         case 0:
>           $block['subject'] = t('Country Selection');
>           $block['content'] = drupal_get_form('uc_multicurrency_country_select_form');
>           return $block;
>         case 1:
>           global $user;
>           $block['subject'] = t('Currency Selection');
>           $block['content'] = drupal_get_form('uc_multicurrency_currency_select_form');
>           return $block;
>       }
682a692
> 
691a702,745
> /**
>  * Currency selection form
>  */
> function uc_multicurrency_currency_select_form(&$form_state) {
>   global $user;
> 
>   // Define submit handler function
>   $form['#submit'][] = 'uc_multicurrency_currency_select_form_submit';
> 
>   // Get the currently selected currency based on user preference or session variable (or use the default)
>   if ($user->uid > 0 && isset($user->currency_code)) {
>     $current_selection = $user->currency_code;
>   }
>   elseif (isset($_SESSION['uc_currency_code'])) {
>     $current_selection = $_SESSION['uc_currency_code'];
>   }
>   else {
>     $current_selection = variable_get('uc_currency_code', 'USD');
>   }
> 
>   $form['currency_selection'] = array(
>     '#type'     => 'select',
>     '#default_value' => $current_selection,
>     '#options'  => _uc_multicurrency_currencies_in_use(),
>   );
>   $form['select_button'] = array(
>     '#type'     => 'submit',
>     '#value'    => t('Apply'),
>   );
> 
>   return $form;
> }
> 
> 
> 
> /**
>  * Submit handler for country selection form
>  */
> function uc_multicurrency_currency_select_form_submit($form, &$form_state) {
>   $result =  uc_multicurrency_get_country_from_currency($form_state['values']['currency_selection']);
>   uc_multicurrency_set_currency($result['country'], $result['currency']);
> }
> 
> 
808a863,891
> /**
>  * Get a country based on a currency code
>  */
> function uc_multicurrency_get_country_from_currency($currency) {
>   $variable = db_result(db_query("SELECT name FROM {variable} WHERE name LIKE 'uc_multicurrency_country_currency_%' AND value LIKE '%".$currency."%'"));
>   $iso2 = substr($variable, -2);
>   $country = uc_get_country_data(array('country_iso_code_2' => $iso2));
>   $result = db_result(db_query("SELECT country_iso_code_2 FROM {uc_countries} WHERE country_id = %d", $country[0]['country_id']));
>   return array('country' => $result, 'currency' => $currency);
> }
> 
> 
> /**
>  * Get the currency code from the $user object.  Returns default country
>  * if no country set in $user object.
>  *
>  * @return
>  *   ISO 3166 2-character country code
>  */
> function uc_multicurrency_get_currency($user) {
>   if (isset($user->currency_code)) {
>     $currency = $user->currency_code;
>   }
>   else {
>     $currency = variable_get('uc_currency_code', 'USD');
>   }
>   return $currency;
> }
> 
821a905
>     unset($user->currency_code);
827a912,931
>  * Sets the country code in the $user object.
>  *
>  * Works only for logged-in users.
>  *
>  * @param $country
>  *   ISO 3166 2-character country code
>  */
> function uc_multicurrency_set_currency($country, $currency) {
>   global $user;
> 
>   if ($user->uid) {  // Only for logged-in users
>     //unset($user->country_iso_code_2);
>     user_save($user, array('country_iso_code_2' => $country));
>     user_save($user, array('currency_code' => $currency));
>   }
>   $_SESSION['uc_currency_code'] = $currency;
> }
> 
> 
> /**
908a1013
> 
910,913c1015
<  * Format an amount for display with the store's currency settings.
<  *
<  * This is a modified version of the core Ubercart function uc_currency_format()
<  * The core function must be renamed so that this one can be used.
---
>  * Implementation of hook_uc_price_handler().
915c1017,1030
< function uc_currency_format($value, $sign = TRUE, $thou = TRUE, $dec = NULL, $currency_code = NULL) {
---
> function uc_multicurrency_uc_price_handler() {
>   return array(
>     'alter' => array(
>       'title' => t('Multicurrency price handler'),
>       'description' => t('Alter the price for a given currency in multicurrency setups.'),
>       'callback' => 'uc_multicurrency_price_handler_alter',
>     ),
>     'format' => array(
>       'title' => t('Multicurrency price handler'),
>       'description' => t('Format the price based on the user currency settings.'),
>       'callback' => 'uc_multicurrency_price_handler_format',
>     ),
>   );
> }
916a1032,1036
> /**
>  * Default price handler alterer; adds the default prefixes to the various
>  *   product prices when viewing a product node.
>  */
> function uc_multicurrency_price_handler_alter(&$price, &$context, &$options) {
919,925c1039,1047
<   // Decide how to format prices for anonymous users
<   if (!$user->uid) {  // Anonymous
<     if (variable_get('uc_multicurrency_anonymous_behavior', 0)) {
<       return variable_get('uc_multicurrency_price_message',
<                           t('Please log in to see prices'));
<     }
<   }
---
>   switch ($context['location']) {
>     case 'product-view':            // Standard product node view
>     case 'product-kit-view':        // Product kit node view
>     case 'product-tapir-table':     // Catalog table view
>     case 'catalog-grid-product':    // Catalog grid view
>     case 'views-price-handler':     // Prices displayed by Views module
>     case 'product-upsell':
>       $node = $context['subject']['node'];
>       //break; // Enabling this prevented the alter function from being called on product nodes
927,934c1049,1055
<   // Get the chosen currency for this country
<   $default_currency = variable_get('uc_currency_code', 'USD');
<   if (!empty($currency_code)) {
<     // Argument passed in - use it!
<     $currency = $currency_code;
<   }
<   else {
<     // No argument passed in, figure out what currency to use
---
>     case 'checkout-order-total-preview':
>     case 'cart-item':               // Cart item
>     case 'cart-review-item':        // Cart checkout review page
>     //case 'cart-checkout-item': // Enabling this causes incorrect prices in the cart block!
>     //case 'cart-preview-subtotal-item':// Enabling this caused incorrect prices
>     case 'cart-table-subtotal':
>     case 'cart-subtotal':
936,944c1057,1060
<     // Get the country to use
<     if (!$user->uid) { // Anonymous
<       $country = variable_get('uc_multicurrency_default_country', 'US');
<     }
<     else {
<       $country  = uc_multicurrency_get_country();
<     }
<     $currency = variable_get('uc_multicurrency_country_currency_'. $country, $default_currency);
<   }
---
>     case 'minimum-subtotal':
>     case 'cart-checkout-subtotal':
>       $node = $context['extras']['node'];
>       $options = uc_multicurrency_selected_currency($user, $options);
946,948c1062,1071
<   // Get the currency symbol for this currency
<   $symbol = variable_get('uc_currency_symbol_'. $currency,
<                          variable_get('uc_currency_sign', '$'));
---
>       break;
> 
>     // Don't modify prices in other locations.
>     default:
>       if ($context['location'] == 'cart-block-item' || $context['location'] == 'cart-block-total') {
>         $options = uc_multicurrency_selected_currency($user, $options);
>       }
> 
>       return;
>   }
951c1074
<   $factor = (float) uc_multicurrency_get_factor($default_currency, $currency);
---
>   $factor = (float) uc_multicurrency_get_factor($default_currency, $options['currency']);
954c1077
<   $multiplier = (float) uc_multicurrency_get_multiplier($currency);
---
>   $multiplier = (float) uc_multicurrency_get_multiplier($options['currency']);
958c1081
<   $value = (float) $value * $multiplier * $factor;
---
>   $price['price'] = (float) $price['price'] * $multiplier * $factor;
960,961c1083
<   // Set decimal value to $fraction if rounding
<   $fraction = (float) variable_get('uc_multicurrency_decimal_value', 0);
---
> }
963,973c1085,1089
<   // Perform rounding if desired.
<   // NEVER ROUND FOR DEFAULT CURRENCY!  NEVER ROUND ZERO VALUES!
<   // If decimal value is left blank AND rounding is enabled, suppress
<   // display of decimal fraction
<   if ($country != variable_get('uc_multicurrency_default_country', 'US') && $value != 0) {
<     $suppress = FALSE;
<     switch (variable_get('uc_multicurrency_rounding', 0)) {
<       default:
<       case 0:
<         // No rounding
<         break;
---
> /**
>  * Default price handler formatter; formats the price using the store currency
>  *   display settings.
>  */
> function uc_multicurrency_price_handler_format($price, $options) {
975,979c1091,1092
<       case 1:
<         // Round UP
<         if ($fraction=='') $suppress = TRUE;
<         $value = (float) ceil($value) + $fraction / 100.0;
<         break;
---
>   $output = '';
>   global $user;
981,985c1094,1098
<       case 2:
<         // Round DOWN
<         if ($fraction=='') $suppress = TRUE;
<         $value = (float) floor($value) + $fraction / 100.0;
<         break;
---
>   // Decide how to format prices for anonymous users
>   if (!$user->uid) {  // Anonymous
>     if (variable_get('uc_multicurrency_anonymous_behavior', 0)) {
>       return variable_get('uc_multicurrency_price_message',
>                           t('Please log in to see prices'));
989,997c1102,1105
<   $default_sign_after = variable_get('uc_sign_after_amount', FALSE);
<   $default_thou       = variable_get('uc_currency_thou', ',');
<   $default_dec        = variable_get('uc_currency_dec', '.');
<   $default_prec       = variable_get('uc_currency_prec', 2);
< 
<   if (variable_get('uc_currency_prec_'. $currency, $default_prec) > 0) {
<     if (abs($value) < '.'. str_repeat('0', variable_get('uc_currency_prec_'. $currency, $default_prec) - 1) .'1') {
<       $value = 0;
<     }
---
>   // Force the price to a positive value and add a negative sign if necessary.
>   if (abs($price) < 0) {
>     $price = abs($price);
>     $output .= '-';
1000,1002c1108,1110
<   if ($value < 0) {
<     $value = abs($value);
<     $format = '-';
---
>   // Add the currency sign first if specified.
>   if ($options['sign'] && !$options['sign_after']) {
>     $output .= $options['sign'];
1005,1006c1113,1118
<   if ($sign && !variable_get('uc_sign_after_amount_'. $currency, $default_sign_after)) {
<     $format .= $symbol;
---
>   // Format the number, like 1234.567 => 1,234.57
>   $output .= number_format($price, $options['prec'], $options['dec'], $options['thou']);
> 
>   // Add the currency sign last if specified.
>   if ($options['sign'] && $options['sign_after']) {
>     $output .= $options['sign'];
1007a1120,1121
>   return $output;
> }
1009,1014d1122
<   $format .= number_format(
<     $value,
<     $suppress ? 0 : variable_get('uc_currency_prec_'. $currency, $default_prec),
<     !is_null($dec) ? $dec : variable_get('uc_currency_dec_'. $currency, $default_dec),
<     $thou ? variable_get('uc_currency_thou_'. $currency, $default_thou) : ''
<   );
1016,1017c1124,1138
<   if ($sign && variable_get('uc_sign_after_amount_'. $currency, $default_sign_after)) {
<     $format .= $symbol;
---
> /**
>  * Helper function to get the selected currency based on $user settings and $options array
>  */
> function uc_multicurrency_selected_currency($user, &$options) {
> 
>   // Get the chosen currency for this country
>   $default_currency = variable_get('uc_currency_code', 'USD');
> 
>   // If the user has a set currency code, use that first
>   if (!empty($user->currency_code)) {
>     $options['currency'] = $user->currency_code;
>   }
>   // Or we can use it from the session, if it exists
>   else if (isset($_SESSION['uc_currency_code'])) {
>     $options['currency'] = $_SESSION['uc_currency_code'];
1018a1140,1145
>   elseif (!empty($options['currency_code'])) {
>     // Argument passed in - use it!
>     $options['currency'] = $options['currency_code'];
>   }
>   else {
>     // No argument passed in, so...
1020c1147,1158
<   return $format;
---
>     // Get the country to use.
>     if (!$user->uid) { // Anonymous
>       $options['country'] = variable_get('uc_multicurrency_default_country', 'US');
>     }
>     else {
>       $options['country'] = uc_multicurrency_get_country();
>     }
>     $options['currency'] = variable_get('uc_multicurrency_country_currency_'. $options['country'], $default_currency);
>   }
> 
>   $options['sign'] = variable_get('uc_currency_symbol_'. $options['currency'], variable_get('uc_currency_sign', '$'));
>   return $options;
