Hello, I have started to rewrite this module for 7.x, can we please start a 7.x version and can anyone familiar with the project, the maintainer or another person interested, chime in to test against the current 7.x branch, and get code updated. I updated the queries to conform to new db api, still testing and making changes, need to check hook_shipping_method changes and some other things.

 # arthur nemirovsky , arthur@rofsky.com started port to 7.x on 2/12/2012

/**
 * Implementation of hook_menu().
 */

function uc_tablequote_menu(){
  $items = array();

  $items['admin/store/settings/quotes/methods/tablequote'] = array(
    'title' => 'Weight/Order quote',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('uc_tablequote_admin_settings'),
    'access arguments' => array('configure quotes'),
    'type' => MENU_LOCAL_TASK,
  );

  return $items;
}

/**
 * Implementation of Ubercart's hook_shipping_method().
 */
function uc_tablequote_shipping_method() {
  $methods = array();

  //$enabled = variable_get('uc_quote_enabled', array('tablequote' => true));
  $enabled = variable_get('uc_quote_enabled', array());
  $weight = variable_get('uc_quote_method_weight', array('tablequote' => 0));
  $methods['tablequote'] = array(
    'id' => 'tablequote',
    'module' => 'uc_tablequote',
    'title' => t('Weight/order quote shipping'),
    'enabled' => $enabled['tablequote'],
    'quote' => array(
      'type' => 'order',
      'callback' => 'uc_tablequote_quote',
      'accessorials' => array(
        t('Standard delivery'),
      ),
    ),
    'weight' => $weight['tablequote'],
  );
  return $methods;
}

/******************************************************************************
 * Conditional Actions Hooks                                                  *
 ******************************************************************************/

/**
 * Implementation of hook_ca_predicate().
 */
function uc_tablequote_ca_predicate() {
  $enabled = variable_get('uc_quote_enabled', array());

  $predicates = array(
    'uc_tablequote_get_quote' => array(
      '#title' => t('Shipping quote via weight/order'),
      '#trigger' => 'get_quote_from_tablequote',
      '#class' => 'uc_tablequote',
      '#status' => $enabled['tablequote'],
      '#actions' => array(
        array(
          '#name' => 'uc_quote_action_get_quote',
          '#title' => t('Fetch a weight/order shipping quote'),
          '#argument_map' => array(
            'order' => 'order',
            'method' => 'method',
          ),
        ),
      ),
    ),
  );

  return $predicates;
}

/**
 * Configures the store shipping rates
 */
function uc_tablequote_admin_settings(){
  $form = array();
  $form['rates'] = array(
    '#tree' => true
  );
  $result = db_query("SELECT * FROM {uc_tablequote}");
  // while ($r = db_fetch_object($result)) { // 6.x
  foreach ($result as $r) {
    $qid = $r->qid;
    $form['rates'][$qid]['delete'] = array(
      '#type' => 'checkbox',
      '#default_value' => 0,
    );
  }
  $form['uc_tablequote'] = array(
    '#type' => 'fieldset',
    '#title' => t('Add a quote'),
    '#description' => t('Defines a range of weights or prices for the total weight or the order total. Example: to give free shipping to all orders greater than $100, set Minimum to 100, Maximum to a very large number like 1000000, and set Rate to 0.'),
    '#collapsible' => true,
    '#collapsed' => false,
  );
  $form['uc_tablequote']['uc_tablequote_minimum'] = array(
    '#type' => 'textfield',
    '#title' => t('Minimum value'),
    '#default_value' => 0,
    '#size' => 12,
  );
  $form['uc_tablequote']['uc_tablequote_maximum'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum value'),
    '#default_value' => 0,
    '#size' => 12,
  );
  $form['uc_tablequote']['uc_tablequote_rate'] = array(
    '#type' => 'textfield',
    '#title' => t('Shipping Rate'),
    '#default_value' => variable_get('uc_tablequote_rate', 0),
    '#size' => 12,
  );
  
  $result = db_query("SELECT DISTINCT type FROM {uc_tablequote} tq");
  $types = 0;
  
  // while ($row = db_fetch_object($result)) { // 6.x
  foreach ($result as $row) {
    variable_set('uc_tablequote_type',$row->type);
    ++$types;
  }
  
  if ($types == 0) {
    // No type has been selected yet, allow choice.
    $form['uc_tablequote']['uc_tablequote_type'] = array(
      '#type' => 'select',
      '#title' => t('Type of calculation'),
      '#description' => t('Choose whether shipping costs should be calculated on the total weight or total order.'),
      '#default_value' => variable_get('uc_tablequote_type', 'weight'),
      '#options' => array(
        'weight' => t('Weight'),
        'order' => t('Total order'),
      ),
    );
  }
  else {
    // Some type has already been selected, use it.
    $form['uc_tablequote']['uc_tablequote_type'] = array(
      '#value' => t('<p><strong>Type of calculation:</strong> ').variable_get('uc_tablequote_type','weight'),
    );
    
    if ($types > 1) {
      // But if more than one type has been selected, report error.
      drupal_set_message('Error: there is more than one quote type defined.', 'warning', FALSE);
    }
  }
  
  $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Submit Changes') );
  
  return $form;
}

function uc_tablequote_admin_settings_validate($form, &$form_state){
  // check if everything is a number greater than or equal to 0
  if ($form_state['values']['uc_tablequote_minimum'] < 0 || !is_numeric($form_state['values']['uc_tablequote_minimum'])) {
    form_set_error('uc_tablequote_minimum', t('The minimum value must be a number not less than 0.'));
  }
  if ($form_state['values']['uc_tablequote_maximum'] < 0 || !is_numeric($form_state['values']['uc_tablequote_maximum'])) {
    form_set_error('uc_tablequote_maximum', t('The maximum value must be a number greater than 0.'));
  }
  if (($form_state['values']['uc_tablequote_rate'] < 0) ||
      (!is_numeric($form_state['values']['uc_tablequote_rate']))) {
    form_set_error('uc_tablequote_rate', t('Shipping rate must be a number not less than 0.'));
  }
  
  // check that max is bigger than min
  if ($form_state['values']['uc_tablequote_maximum'] && $form_state['values']['uc_tablequote_maximum'] <= $form_state['values']['uc_tablequote_minimum']) {
    form_set_error('uc_tablequote_maximum', t('The maximum value must be greater than the minumum value.'));
  }
  
  if ($form_state['values']['uc_tablequote_maximum'] > 0) {
    //check to make sure the new settings dont overlap any existing ranges
    $result = db_query("SELECT * FROM {uc_tablequote} ORDER BY min");
    // while ($r = db_fetch_object($result)) { // 6.x
	foreach ($result as $r) {
      if ($form_state['values']['uc_tablequote_minimum'] < $r->max && $form_state['values']['uc_tablequote_minimum'] >= $r->min) {
        form_set_error('uc_tablequote_minimum', t('The minimum value conflicts with an existing Range.'));
      }
      if($form_state['values']['uc_tablequote_maximum'] <= $r->max && $form_state['values']['uc_tablequote_maximum'] > $r->min) {
        form_set_error('uc_tablequote_maximum', t('The maximum value conflicts with an existing Range.'));
      }
    }
  }
}

function uc_tablequote_admin_settings_submit($form, &$form_state){
  // check for and handle any deletions
  if(is_array($form_state['values']['rates'])) {
    foreach ($form_state['values']['rates'] as $qid => $value)
      if ($value['delete']) {
        // db_query("DELETE FROM {uc_tablequote} WHERE qid = %d", $qid); // 6.x
		db_query("DELETE FROM {uc_tablequote} WHERE qid = :qid", array(':qid' => $qid));
      }
  }

  // check for and insert new rate quote
  if ($form_state['values']['uc_tablequote_maximum'] > $form_state['values']['uc_tablequote_minimum'] && $form_state['values']['uc_tablequote_maximum'] > 0){
    //variable_set('uc_tablequote_type',$form_state['values']['uc_tablequote_type']);
    $result = db_query("SELECT type FROM {uc_tablequote} tq");
    $num_rows = db_query('SELECT COUNT(type) FROM {uc_tablequote} tq')->fetchField();
    if ($num_rows == 0) {
      // db_query("INSERT INTO {uc_tablequote} (min, max, rate, type) VALUES (%f, %f, %f, '%s')", $form_state['values']['uc_tablequote_minimum'], $form_state['values']['uc_tablequote_maximum'], $form_state['values']['uc_tablequote_rate'], $form_state['values']['uc_tablequote_type']); // 6.x
	  db_query("INSERT INTO {uc_tablequote} (min, max, rate, type) VALUES (:min, :max, :rate, :type)", 
	  			array(':min' => $form_state['values']['uc_tablequote_minimum'], 
					  ':max' => $form_state['values']['uc_tablequote_maximum'], 
					  ':rate' => $form_state['values']['uc_tablequote_rate'], 
					  ':type' => $form_state['values']['uc_tablequote_type']));
      variable_set('uc_tablequote_type',$form_state['values']['uc_tablequote_type']);
    }
    else {
      // db_query("INSERT INTO {uc_tablequote} (min, max, rate, type) VALUES (%f, %f, %f, '%s')", $form_state['values']['uc_tablequote_minimum'], $form_state['values']['uc_tablequote_maximum'], $form_state['values']['uc_tablequote_rate'], variable_get('uc_tablequote_type','weight')); // 6.x
	  db_query("INSERT INTO {uc_tablequote} (min, max, rate, type) VALUES (:min, :max, :rate, :type)", 
	  			array(':min' => $form_state['values']['uc_tablequote_minimum'], 
	  				  ':max' => $form_state['values']['uc_tablequote_maximum'], 
	  	  			  ':rate' => $form_state['values']['uc_tablequote_rate'], 
	  				  ':type' => variable_get('uc_tablequote_type','weight')));    
	}
  }

  drupal_set_message(t('The configuration options have been saved.'));
}

/**
 * Implementation of hook_theme().
 */
function uc_tablequote_theme($form){
  return array(
    'uc_tablequote_admin_settings' => array(
      'arguments' => array('form' => NULL),
    ),
  );
}

function theme_uc_tablequote_admin_settings($form){
  $header = array(t('Delete'), t('Minimum value'), t('Maximum value'), t('Shipping Rate'));
  $result = db_query("SELECT * FROM {uc_tablequote} ORDER BY min");
  $output = '';

  // while ($r = db_fetch_object($result)) { // 6.x
  foreach ($result as $r) {
    $row = array();
	$rows = array(); 
    $qid = $r->qid;
    $r->rate > 0 ? $rate = $r->rate : $rate = 'Free';
    $row[] = drupal_render($form['rates'][$qid]['delete']);
    $row[] = $r->min;
    $row[] = $r->max;
    $row[] = $rate;
    $rows[] = $row;
  }
    
  if (count($rows) == 0) {
    $rows[] = array(array('data' => t('No rates found.'), 'colspan' => 4));
  }
  else {
    $rows[] = array(array('data' => t('Shipping quotes based on: ').variable_get('uc_tablequote_type','weight'), 'colspan' => 4));
  }
  $output .= theme('table', $header, $rows);
  
  $output .= drupal_render($form);
  return $output;
}

/******************************************************************************
 * Module Functions                                                           *
 ******************************************************************************/
function uc_tablequote_quote($products, $details) {
  $total = 0;
  $type = variable_get('uc_tablequote_type', 'weight');
  
  foreach ($products as $product) {
    if ($product->shippable) {
      switch($type) {
        case 'order':
          $total += $product->price * $product->qty;
          break;
        case 'weight':
        default:
          $total += $product->qty * $product->weight * uc_weight_conversion($product->weight_units, variable_get('uc_weight_unit', 'lb'));
      }
    }
  }

  $rate = 0;
  $found = FALSE;
  $result = db_query("SELECT * FROM {uc_tablequote}");
  // while ($r = db_fetch_object($result)) { // 6.x
  foreach ($result as $r) {
    if (($total >= $r->min) && ($total < $r->max)) {
      $rate = $r->rate;
      $found = TRUE;
      break;
    }
  }

  $method = uc_tablequote_shipping_method();
  $quotes = array();
  if ($found) {
    $quotes[] = array('rate' => $rate, 'format' => uc_currency_format($rate), 'option_label' => $method['tablequote']['quote']['accessorials'][0]);
  }

  return $quotes;
}
CommentFileSizeAuthor
#5 uc_tablequote-7.x.tar_.gz10.07 KBpandroid

Comments

anrikun’s picture

Please ask fivepoints to become a co-maintainer of this module.

AlexanderPop’s picture

any progress?

ñull’s picture

What about it?

anrikun’s picture

I'm still waiting for someone to become a co-maintainer :-(

pandroid’s picture

StatusFileSize
new10.07 KB

We use this module, so we've had to port it to D7. Its attached here for those who are interested. Its works for us, but no guarantees.

Note that this module is patched according to #461884 and includes the ability to create multiple tables. I've included the D6 install file which creates the tables, but I've not tested whether this works under D7 (our tables already exist and there are no changes). You might need to do the install under D6 then upgrade.

We use this module for Royal Mail in the UK. We create three tables, one for Royal Mail UK, one for Royal Mail EU and one for Rest of the World. Each table contains the respective Royal Mail price/weight price list for that sector. Then we set up a rule (conditions) for each table, which checks the orders shipping country. When the module runs, only the rule matching the order's country fires, and the right postage gets calculated.

geefin’s picture

I had used this module in D6 form and it worked perfectly, but hit a lot of issues trying to use/sort the above D7 port. Then I found this module - http://drupal.org/project/uc_global_quote which seems to do everything I needed/wanted the D7 table quote module to do.