Commit 5ae2e07e authored by Nicolò Caruso's avatar Nicolò Caruso
Browse files

Issue #2692813: Add support for multicurrency

parent 0cef140c
Loading
Loading
Loading
Loading
+8 −12
Original line number Diff line number Diff line
@@ -238,8 +238,12 @@ function rooms_booking_edit_form($form, &$form_state, $booking) {

  $client = '';

  $currency_setting = commerce_currency_load(commerce_default_currency());
  $currency_symbol = $currency_setting['symbol'];
  if (module_exists('commerce_multicurrency')) {
    $currency_code = commerce_multicurrency_get_user_currency_code();
  }
  else {
    $currency_code = commerce_default_currency();
  }

  // Check to see if we are loading an existing booking (existing bookings
  // have customer ids set).
@@ -463,11 +467,7 @@ function rooms_booking_edit_form($form, &$form_state, $booking) {
    $unit_selected .= ' ' . t('(Options: !options)', array('!options' => substr($options_list, 0, strlen($options_list) - 2)));
  }
  if (isset($booking->price)) {
    $unit_selected .= '<br />' . t('Price') . ': ' .
    t('@currency_symbol @amount', array( // TODO: replace with a proper currency formatter
      '@currency_symbol' => $currency_symbol,
      '@amount' => number_format($booking->price / 100, 2, '.', ''),
    ));
    $unit_selected .= '<br />' . t('Price') . ': ' . commerce_currency_format($booking->price, $currency_code);
  }

  $form['availability_fieldset'] = array(
@@ -610,11 +610,7 @@ function rooms_booking_edit_form($form, &$form_state, $booking) {
            // Create elements based on type/price categorisation.
            foreach ($units_per_price as $price => $units) {
              foreach ($units as $unit) {
                $options[$unit['unit']->unit_id] = $unit['unit']->name . ' - ' . t('Cost') . ': ' .
                  t('@currency_symbol @amount', array( // TODO: replace with a proper currency formatter
                      '@currency_symbol' => $currency_symbol,
                      '@amount' => $units[key($units)]['price'])
                  );
                $options[$unit['unit']->unit_id] = $unit['unit']->name . ' - ' . t('Cost') . ': ' . commerce_currency_format($units[key($units)]['price'] * 100, $currency_code);
              }
            }

+32 −55
Original line number Diff line number Diff line
@@ -116,6 +116,8 @@ function rooms_booking_manager_cart_view() {
  // Default to displaying an empty message.
  $content = theme('commerce_cart_empty_page');

  commerce_cart_order_refresh(commerce_cart_order_load($user->uid));

  // First check to make sure we have a valid order.
  if ($order = commerce_cart_order_load($user->uid)) {
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
@@ -824,8 +826,12 @@ function book_unit_form_builder($form_id, $form_state, $unit, $start_date, $end_
  // Add classes.
  $form['#attributes']['class'][] = 'rooms-book-unit-form';

  $currency_setting = commerce_currency_load(commerce_default_currency());
  $currency_symbol = $currency_setting['symbol'];
  if (module_exists('commerce_multicurrency')) {
    $currency_code = commerce_multicurrency_get_user_currency_code();
  }
  else {
    $currency_code = commerce_default_currency();
  }

  $form['unit_id'] = array(
    '#type' => 'hidden',
@@ -869,10 +875,7 @@ function book_unit_form_builder($form_id, $form_state, $unit, $start_date, $end_
    $form['price'] = array(
      '#prefix' => '<div class="rooms-search-result__unit-base-price" id="unit_' . $unit->unit_id . '_base_price">',
      '#markup' => rooms_string('<label>' . t('Base price') . ':</label> <span class="rooms-search-result__base-price-amount">' .
        t('@currency_symbol @amount', array( // TODO: replace with a proper currency formatter
          '@currency_symbol' => $currency_symbol,
          '@amount' => $price,
        )) . '</span>', array(
        commerce_currency_format($price * 100, $currency_code) . '</span>', array(
        '#component' => 'book_unit_form_builder',
        '#purpose' => 'display_base_price',
        '#data' => array(
@@ -893,14 +896,8 @@ function book_unit_form_builder($form_id, $form_state, $unit, $start_date, $end_
    $form['price'] = array(
      '#prefix' => '<div class="rooms-search-result__unit-base-price" id="unit_' . $unit->unit_id . '_base_price">',
      '#markup' => rooms_string('<label>' . t('Base price') . ':</label> <span class="rooms-search-result__base-price-amount">' .
        t('@currency_symbol @amount', array( // TODO: replace with a proper currency formatter
          '@currency_symbol' => $currency_symbol,
          '@amount' => $price,
        )) . '</span>' . '<label>' . ' - '. t('Payable now') . ':</label> <span class="rooms-search-result__base-price-amount">' . 
        t('@currency_symbol @deposit', array(
          '@currency_symbol' => $currency_symbol,
          '@deposit' => $deposit_amount,
          )) . '</span>', array(
        commerce_currency_format($price * 100, $currency_code) . '</span>' . '<label>' . ' - '. t('Payable now') . ':</label> <span class="rooms-search-result__base-price-amount">' . 
        commerce_currency_format($deposit_amount * 100, $currency_code) . '</span>', array(
        '#component' => 'book_unit_form_builder',
        '#purpose' => 'display_base_price',
        '#data' => array(
@@ -926,12 +923,8 @@ function book_unit_form_builder($form_id, $form_state, $unit, $start_date, $end_
    $base_price = $price_per_person / $nights;

    $form['price']['#markup'] = rooms_string(format_plural($nights, t('Book this unit for 1 night at <b>@price per person</b> (@base_price per person per night)'), t('Book this unit for @count nights at <b>@price per person</b> (@base_price per person per night)'), array(
      '@price' => t('@currency_symbol @amount', array( // TODO: replace with a proper currency formatter
        '@currency_symbol' => $currency_symbol,
        '@amount' => $price_per_person)),
      '@base_price' => t('@currency_symbol @amount', array( // TODO: replace with a proper currency formatter
        '@currency_symbol' => $currency_symbol,
        '@amount' => $base_price))
      '@price' => commerce_currency_format($price_per_person * 100, $currency_code),
      '@base_price' => commerce_currency_format($base_price * 100, $currency_code),
    )), array(
      '#component' => 'book_unit_form_builder',
      '#purpose' => 'display_base_price_per_person',
@@ -1166,10 +1159,7 @@ function book_unit_form_builder($form_id, $form_state, $unit, $start_date, $end_
      $form['new_price'] = array(
        '#prefix' => '<div class="rooms-search-result__new-price" id="unit_' . $unit->unit_id . '_price">',
        '#markup' => '<label>' . t('Cost') . ':</label> <span class="rooms-search-result__new-price-amount">' .
          t('@currency_symbol @amount', array( // TODO: replace with a proper currency formatter
            '@currency_symbol' => $currency_symbol,
            '@amount' => $new_price['full_price'],
          )) . '</span>',
          commerce_currency_format($new_price['full_price'] * 100, $currency_code) . '</span>',
        '#suffix' => '</div>',
      );
    }
@@ -1180,13 +1170,7 @@ function book_unit_form_builder($form_id, $form_state, $unit, $start_date, $end_
      $form['new_price'] = array(
        '#prefix' => '<div class="rooms-search-result__new-price" id="unit_' . $unit->unit_id . '_price">',
        '#markup' => '<label>' . t('Cost') . ':</label> <span class="rooms-search-result__new-price-amount">' .
          t('@currency_symbol @amount', array( // TODO: replace with a proper currency formatter
            '@currency_symbol' => $currency_symbol,
            '@amount' => $new_price['full_price'],
          )) . '</span>' . '<label>' . ' - ' . t('Payable Now') . ':</label> <span class="rooms-search-result__new-price-deposit">' . t('@currency_symbol @deposit_amount', array (
          '@currency_symbol' => $currency_symbol,
          '@deposit_amount' => $deposit_amount,
          )) . '</span>',
          commerce_currency_format($new_price['full_price'] * 100, $currency_code) . '</span>' . '<label>' . ' - ' . t('Payable Now') . ':</label> <span class="rooms-search-result__new-price-deposit">' . commerce_currency_format($deposit_amount * 100, $currency_code) . '</span>',
        '#suffix' => '</div>',
      );
    }
@@ -1405,8 +1389,12 @@ function book_units_per_type_form($form, $form_state, $units_per_type, $start_da

  $form = _rooms_booking_manager_setup_hidden_fields($form, $start_date, $end_date, $booking_parameters, $b_units);

  $currency_setting = commerce_currency_load(commerce_default_currency());
  $currency_symbol = $currency_setting['symbol'];
  if (module_exists('commerce_multicurrency')) {
    $currency_code = commerce_multicurrency_get_user_currency_code();
  }
  else {
    $currency_code = commerce_default_currency();
  }

  // Check if there are multiple units chosen, and keep a running tally of
  // the number of elements chosen.
@@ -1484,7 +1472,7 @@ function book_units_per_type_form($form, $form_state, $units_per_type, $start_da

      $form = _rooms_booking_manager_load_description($form, $type_obj, $type, $price);

      $form = _rooms_booking_manager_load_price_info($form, $type_obj, $type, $price, $currency_symbol, $units_per_price, $units);
      $form = _rooms_booking_manager_load_price_info($form, $type_obj, $type, $price, $currency_code, $units_per_price, $units);

      $form = _rooms_booking_manager_display_book_button($form, $type, $price);

@@ -1589,11 +1577,7 @@ function book_units_per_type_form($form, $form_state, $units_per_type, $start_da
          if (empty($deposit_amount)) {
            $form[$type][$price]['fieldset'][$c]['price'] = array(
              '#prefix' => '<div class="rooms-search-result__unit-price" id="' . $type . '_' . $price . '_' . $c . '_price"><label>' . $label . ':</label> <span class="rooms-search-result__unit-price-amount">',
              // TODO: replace with a proper currency formatter.
              '#markup' => t('@currency_symbol @amount', array(
                '@currency_symbol' => $currency_symbol,
                '@amount' => $price,
              )),
              '#markup' => commerce_currency_format($price * 100, $currency_code),
              '#suffix' => '</span></div>',
            );
          }
@@ -1601,11 +1585,10 @@ function book_units_per_type_form($form, $form_state, $units_per_type, $start_da
          else {
            $form[$type][$price]['fieldset'][$c]['price'] = array(
              '#prefix' => '<div class="rooms-search-result__unit-price" id="' . $type . '_' . $price . '_' . $c . '_price"><label>' . $label . ':</label> <span class="rooms-search-result__unit-price-amount">',
              // TODO: replace with a proper currency formatter.
              '#markup' => t('@currency_symbol @amount - Payable now: @currency_symbol @deposit', array(
                '@currency_symbol' => $currency_symbol,
                '@amount' => $price,
                '@deposit' => $deposit_amount,

              '#markup' => t('@@amount - Payable now: @deposit', array(
                '@amount' => commerce_currency_format($price * 100, $currency_code),
                '@deposit' => commerce_currency_format($deposit_amount * 100, $currency_code),
              )),
              '#suffix' => '</span></div>',
            );
@@ -1723,11 +1706,7 @@ function book_units_per_type_form($form, $form_state, $units_per_type, $start_da
            // Replace old price with the new calculated value.
            $form[$type][$price]['fieldset'][$c]['price']['#markup'] =
              rooms_string(
                t('@currency_symbol @amount', array(
                  '@currency_symbol' => $currency_symbol,
                  '@amount' => $new_price,
                  )
                ),
                commerce_currency_format($new_price * 100, $currency_code),
                $context = array(
                  '#component' => 'units_per_type_form',
                  '#purpose' => 'display_room_recalculated_price',
@@ -1747,12 +1726,10 @@ function book_units_per_type_form($form, $form_state, $units_per_type, $start_da
            // Replace old price with the new calculated value.
            $form[$type][$price]['fieldset'][$c]['price']['#markup'] =
              rooms_string(
                t('@currency_symbol @amount - Payable now: @currency_symbol @deposit', array(
                  '@currency_symbol' => $currency_symbol,
                  '@amount' => $new_price,
                  '@deposit' => $deposit_amount,
                  )
                ),
                t('@amount - Payable now: @deposit', array(
                  '@amount' => commerce_currency_format($new_price * 100, $currency_code),
                  '@deposit' => commerce_currency_format($deposit_amount * 100, $currency_code),
                )),
                $context = array(
                  '#component' => 'units_per_type_form',
                  '#purpose' => 'display_room_recalculated_price',
+7 −10
Original line number Diff line number Diff line
@@ -90,13 +90,15 @@ function _rooms_booking_manager_load_description($form, $type_obj, $type, $price
 * @param $type_obj
 * @param $type
 * @param $price
 * @param $currency_symbol
 * @param $currency_code
 * @param $units_per_price
 * @param $units
 *
 * @return mixed
 */
function _rooms_booking_manager_load_price_info($form, $type_obj, $type, $price, $currency_symbol, $units_per_price, $units) {
function _rooms_booking_manager_load_price_info($form, $type_obj, $type, $price, $currency_code, $units_per_price, $units) {
  $currency_setting = commerce_currency_load($currency_code);
  $currency_symbol = $currency_setting['symbol'];

  // Calculate the period.
  $nights = $form['end_date']['#value']->diff($form['start_date']['#value'])->days;
@@ -111,11 +113,7 @@ function _rooms_booking_manager_load_price_info($form, $type_obj, $type, $price,
        . ' @price',
        array(
          '@unit_type' => $type_obj->label,
          '@price' => t('@currency_symbol @amount', array( // TODO: replace with a proper currency formatter
              '@currency_symbol' => $currency_symbol,
              '@amount' => $price,
            )
          )
          '@price' => commerce_currency_format($price * 100, $currency_code),
        )
      ),
      $context = array(
@@ -125,6 +123,7 @@ function _rooms_booking_manager_load_price_info($form, $type_obj, $type, $price,
          'unit_type' => $type_obj->label,
          'price' => $price,
          'currency_symbol' => $currency_symbol,
          'currency_code' => $currency_code,
          'amount' => $price,
          'units' => $units_per_price,
          'nights' => $nights,
@@ -144,9 +143,7 @@ function _rooms_booking_manager_load_price_info($form, $type_obj, $type, $price,
    ($units), '1 @unit_type unit available starting at @price per
        person', '@count @unit_type units available starting at @price per person', array(
        '@unit_type' => $type_obj->label,
        '@price' => t('@currency_symbol @amount', array( // TODO: replace with a proper currency formatter
          '@currency_symbol' => $currency_symbol,
          '@amount' => $base_price))
        '@price' => commerce_currency_format($base_price * 100, $currency_code),
      )
    );
  }
+6 −2
Original line number Diff line number Diff line
@@ -83,8 +83,12 @@ function rooms_periodic_pricing_rooms_string_alter(&$string_suggestions, $contex
  if ($context['#purpose'] == 'display_base_price') {
    $string_suggestion = reset($string_suggestions);

    $currency_setting = commerce_currency_load(commerce_default_currency());
    $currency_symbol = $currency_setting['symbol'];
    if (module_exists('commerce_multicurrency')) {
      $currency_code = commerce_multicurrency_get_user_currency_code();
    }
    else {
      $currency_code = commerce_default_currency();
    }

    $price = $context['#data']['price'];

+7 −6
Original line number Diff line number Diff line
@@ -705,14 +705,15 @@ function rooms_field_formatter_view($entity_type, $entity, $field, $instance, $l
      break;

    case 'rooms_options_price':
      $currency_setting = commerce_currency_load(commerce_default_currency());
      $currency_symbol = $currency_setting['symbol'];
      if (module_exists('commerce_multicurrency')) {
        $currency_code = commerce_multicurrency_get_user_currency_code();
      }
      else {
        $currency_code = commerce_default_currency();
      }

      foreach ($items as $delta => $item) {
        $price = t('@currency_symbol@amount', array(
          '@currency_symbol' => $currency_symbol,
          '@amount' => number_format($item['value'], 2, '.', ''),
        ));
        $price = commerce_currency_format($item['value'], $currency_code);

        if ($item['value'] > 0) {
          $element[$delta] = array('#markup' => "{$item['quantity']} x {$item['name']} - {$price}");