You are here

function uc_order_pane_quotes in Ubercart 6.2

Same name and namespace in other branches
  1. 5 shipping/uc_quote/uc_quote.module \uc_order_pane_quotes()
  2. 7.3 shipping/uc_quote/uc_quote.module \uc_order_pane_quotes()

Shipping quote order pane callback.

See also

uc_quote_order_pane_quotes_submit()

uc_quote_apply_quote_to_order()

1 string reference to 'uc_order_pane_quotes'
uc_quote_order_pane in shipping/uc_quote/uc_quote.module
Implements hook_order_pane().

File

shipping/uc_quote/uc_quote.module, line 975
The controller module for fulfillment modules that process physical goods.

Code

function uc_order_pane_quotes($op, $arg1) {
  switch ($op) {
    case 'edit-form':

      // Let Javascript know where we are.
      $form['quotes']['page'] = array(
        '#type' => 'hidden',
        '#value' => 'order-edit',
      );
      $form['quotes']['quote_button'] = array(
        '#type' => 'submit',
        '#value' => t('Get shipping quotes'),
      );
      $form['quotes']['add_quote'] = array(
        '#type' => 'submit',
        '#value' => t('Apply to order'),
        '#attributes' => array(
          'class' => 'save-button',
        ),
        '#disabled' => TRUE,
      );
      $form['quotes']['quote'] = array(
        '#type' => 'markup',
        '#value' => '<div id="quote"></div>',
      );
      drupal_add_js(array(
        'uc_quote' => array(
          'progress_msg' => t('Receiving quotes...'),
          'err_msg' => check_markup(variable_get('uc_quote_err_msg', t("There were problems getting a shipping quote. Please verify the delivery and product information and try again.\nIf this does not resolve the issue, please call in to complete your order.")), variable_get('uc_quote_msg_format', FILTER_FORMAT_DEFAULT), FALSE),
        ),
        'ucURL' => array(
          'shippingQuotes' => url('cart/checkout/shipping/quote'),
        ),
      ), 'setting');
      drupal_add_js('misc/progress.js');
      drupal_add_js(drupal_get_path('module', 'uc_quote') . '/uc_quote.js');
      $default = $arg1->quote['accessorials'] ? $arg1->quote['accessorials'] : 0;

      // If a previous quote gets loaded, make sure it gets saved again.
      // Also, make sure the previously checked option is checked by default.
      drupal_add_js('$(function() {
        setQuoteCallbacks();
        $("input:radio[name=quote-option]").filter("[value=' . $default . ']").attr("checked", "checked");
        var quoteDiv = $("#quote");
        if (quoteDiv.length && $("#quote input[name=quote-form]").length == 0) {
          quoteDiv.append("<input type=\\"hidden\\" name=\\"quote-form\\" value=\\"" + Drupal.encodeURIComponent(quoteDiv.html()) + "\\" />");
        }
      })', 'inline');
      return $form;
    case 'edit-theme':
      return drupal_render($arg1['quotes']);
    case 'edit-process':
      if (isset($_POST['quote-option'])) {
        list($changes['quote']['method'], $changes['quote']['accessorials']) = explode('---', $_POST['quote-option']);
        $changes['quote']['rate'] = $_POST['rate'][$_POST['quote-option']];
        $changes['quote']['quote_form'] = rawurldecode($_POST['quote-form']);
      }
      return $changes;
    case 'edit-ops':
      return array(
        t('Apply to order'),
      );
    case t('Apply to order'):
      if (isset($_POST['quote-option'])) {
        if ($order = uc_order_load($arg1['order_id'])) {
          $user = user_load(array(
            'uid' => $order->uid,
          ));
          $products = array();
          foreach ($order->products as $product) {
            if ($product->nid) {
              $node = (array) node_load($product->nid);
              foreach ($node as $key => $value) {
                if (!isset($product->{$key})) {
                  $product->{$key} = $value;
                }
              }
            }
            $products[] = $product;
          }

          //drupal_set_message('<pre>'. print_r($order, TRUE) .'</pre>');
          $quote_option = explode('---', $_POST['quote-option']);
          $order->quote['method'] = $quote_option[0];
          $order->quote['accessorials'] = $quote_option[1];
          $order->quote['quote_form'] = rawurldecode($_POST['quote-form']);
          $methods = array_filter(module_invoke_all('shipping_method'), '_uc_quote_method_enabled');
          $method = $methods[$quote_option[0]];
          $quote_data = array();
          $predicate = ca_load_trigger_predicates('get_quote_from_' . $method['id']);
          $arguments = array(
            'order' => array(
              '#entity' => 'uc_order',
              '#title' => t('Order'),
              '#data' => $arg1,
            ),
            'method' => array(
              '#entity' => 'quote_method',
              '#title' => t('Quote method'),
              '#data' => $method,
            ),
            'account' => array(
              '#entity' => 'user',
              '#title' => t('User'),
              '#data' => $user,
            ),
          );
          if (ca_evaluate_conditions($predicate, $arguments)) {
            $quote_data = uc_quote_action_get_quote($order, $method);
          }

          //drupal_set_message('Chosen quote method:<pre>'. print_r($method, TRUE) .'</pre>');

          //drupal_set_message('Chosen quote data:<pre>'. print_r($quote_data, TRUE) .'</pre>');
          if (!isset($quote_data[$quote_option[1]])) {
            drupal_set_message(t('Invalid option selected. Recalculate shipping quotes to continue.'), 'error');
            break;
          }
          $label = $method['quote']['accessorials'][$quote_option[1]];
          $order->quote['rate'] = $quote_data[$quote_option[1]]['rate'];
          $result = db_query("SELECT line_item_id FROM {uc_order_line_items} WHERE order_id = %d AND type = 'shipping'", $arg1['order_id']);
          if ($lid = db_result($result)) {
            uc_order_update_line_item($lid, $label, $order->quote['rate']);
          }
          else {
            uc_order_line_item_add($order->order_id, 'shipping', $label, $order->quote['rate']);
          }
        }
      }
      break;
  }
}