You are here

function uc_order_pane_quotes in Ubercart 5

Same name and namespace in other branches
  1. 6.2 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.

1 string reference to 'uc_order_pane_quotes'
uc_quote_order_pane in shipping/uc_quote/uc_quote.module
Defines the shipping quote order pane.

File

shipping/uc_quote/uc_quote.module, line 1207
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>',
      );
      uc_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),
        ),
      ), 'setting');
      uc_add_js('misc/progress.js');
      uc_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.
      uc_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=\\"" + encodeURIComponent(quoteDiv.html()) + "\\" />");
        }
      })', 'inline');
      return $form;
    case 'edit-theme':
      return drupal_render($arg1['quotes']);
    case 'edit-process':

      //drupal_set_message('<pre>'. print_r($_POST, true) .'</pre>');
      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,
          ));
          $details = array();
          foreach ($order as $key => $value) {
            if (strpos($key, 'delivery_') !== false) {
              $details[substr($key, 9)] = $value;
            }
          }
          $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();
          ob_start();
          workflow_ng_invoke_event('get_quote_from_' . $method['id'], $order, $method, $user);
          $quote_string = ob_get_contents();
          ob_end_clean();
          $quote_data = unserialize($quote_string);

          //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;
  }
}