--- uc_tablequote.module	2009-03-31 20:42:02.000000000 +0200
+++ uc_tablequote.module.patched	2010-04-12 16:51:48.640625000 +0200
@@ -19,9 +19,9 @@ function uc_tablequote_menu(){
 }
 
 /**
- * Implementation of Übercart's hook_shipping_method().
+ * Implementation of Ubercart's hook_shipping_method().
  */
-function uc_tablequote_shipping_method(){
+function uc_tablequote_shipping_method() {
   $methods = array();
 
   //$enabled = variable_get('uc_quote_enabled', array('tablequote' => true));
@@ -30,7 +30,7 @@ function uc_tablequote_shipping_method()
   $methods['tablequote'] = array(
     'id' => 'tablequote',
     'module' => 'uc_tablequote',
-    'title' => t('Wieght/order quote shipping'),
+    'title' => t('Weight/order quote shipping'),
     'enabled' => $enabled['tablequote'],
     'quote' => array(
       'type' => 'order',
@@ -93,52 +93,52 @@ function uc_tablequote_admin_settings(){
     );
   }
   $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,
+    '#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',
+    '#type' => 'textfield',
     '#title' => t('Minimum value'),
     '#default_value' => 0,
     '#size' => 12,
   );
   $form['uc_tablequote']['uc_tablequote_maximum'] = array(
-		'#type' => 'textfield',
+    '#type' => 'textfield',
     '#title' => t('Maximum value'),
     '#default_value' => 0,
     '#size' => 12,
   );
   $form['uc_tablequote']['uc_tablequote_rate'] = array(
-		'#type' => 'textfield',
+    '#type' => 'textfield',
     '#title' => t('Shipping Rate'),
     '#default_value' => variable_get('uc_tablequote_rate', 0),
     '#size' => 12,
   );
-	$result = db_query("SELECT type FROM {uc_tablequote} tq");
-	$num_rows = db_result(db_query('SELECT COUNT(type) FROM {uc_tablequote} tq'));
-	if ($num_rows == 0) {
-		$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 {
-		while ($row = db_fetch_object($result)) {
-			variable_set('uc_tablequote_type',$row->type);
-		}
-		$form['uc_tablequote']['uc_tablequote_type'] = array(
-			'#value' => t('<p><strong>Type of calculation:</strong> ').variable_get('uc_tablequote_type','weight'),
-		);
-	}
+  $result = db_query("SELECT type FROM {uc_tablequote} tq");
+  $num_rows = db_result(db_query('SELECT COUNT(type) FROM {uc_tablequote} tq'));
+  if ($num_rows == 0) {
+    $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 {
+    while ($row = db_fetch_object($result)) {
+      variable_set('uc_tablequote_type',$row->type);
+    }
+    $form['uc_tablequote']['uc_tablequote_type'] = array(
+      '#value' => t('<p><strong>Type of calculation:</strong> ').variable_get('uc_tablequote_type','weight'),
+    );
+  }
   $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Submit Changes') );
   
   return $form;
@@ -148,30 +148,30 @@ function uc_tablequote_admin_settings_va
   // 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']))) {
+      (!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_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)) {
-      if($form_state['values']['uc_tablequote_minimum'] <= $r->max && $form_state['values']['uc_tablequote_minimum'] >= $r->min) {
+      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) {
+      }
+      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.'));
-			}
+      }
     }
   }
 }
@@ -188,15 +188,15 @@ function uc_tablequote_admin_settings_su
   // 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_result(db_query('SELECT COUNT(type) FROM {uc_tablequote} tq'));
-		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']);
-			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'));
-		}
+    $result = db_query("SELECT type FROM {uc_tablequote} tq");
+    $num_rows = db_result(db_query('SELECT COUNT(type) FROM {uc_tablequote} tq'));
+    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']);
+      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'));
+    }
   }
 
   drupal_set_message(t('The configuration options have been saved.'));
@@ -216,23 +216,23 @@ function uc_tablequote_theme($form){
 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");
-	$num_rows = db_result(db_query('SELECT COUNT(*) FROM {uc_tablequote} ORDER BY min'));
+  $num_rows = db_result(db_query('SELECT COUNT(*) FROM {uc_tablequote} ORDER BY min'));
   if ($num_rows == 0) {
-		$rows[] = array(array('data' => t('No rates found.'), 'colspan' => 4));
-	}
-	else {
-	  while ($r = db_fetch_object($result)) {
-	    $row = 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;
-	  }
-		$rows[] = array(array('data' => t('Shipping quotes based on: ').variable_get('uc_tablequote_type','weight'), 'colspan' => 4));
-	}
+    $rows[] = array(array('data' => t('No rates found.'), 'colspan' => 4));
+  }
+  else {
+    while ($r = db_fetch_object($result)) {
+      $row = 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;
+    }
+    $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);
@@ -242,29 +242,30 @@ function theme_uc_tablequote_admin_setti
 /******************************************************************************
  * Module Functions                                                           *
  ******************************************************************************/
-function uc_tablequote_quote(&$form_state, $products, $details){
-  $rate = 0;
-  $items = uc_cart_get_contents();
-  foreach ($items as $item) {
-    if (variable_get('uc_tablequote_type','weight') == 'weight') {
-			$total += $item->weight * $item->qty;
-		}
-    elseif (variable_get('uc_tablequote_type','weight') == 'order') {
-      $total += $item->price * $item->qty;
+function uc_tablequote_quote($products, $details) {
+  $total = 0;
+  foreach ($products as $product) {
+    if (variable_get('uc_tablequote_type', 'weight') == 'weight') {
+      $total += $product->qty * $product->weight * uc_weight_conversion($product->weight_units, variable_get('uc_weight_unit', 'lb'));
+    }
+    elseif (variable_get('uc_tablequote_type', 'weight') == 'order') {
+      $total += $product->price * $product->qty;
     }
   }
 
+  $rate = 0;
   $result = db_query("SELECT * FROM {uc_tablequote}");
   while ($r = db_fetch_object($result)) {
-    if($total <= $r->max && $total >= $r->min) {
-       $rate = $r->rate;
+    if (($total >= $r->min) && ($total < $r->max)) {
+      $rate = $r->rate;
     }
   }
+
   $method = uc_tablequote_shipping_method();
+  $quotes = array();
   if ($rate > 0) {
     $quotes[] = array('rate' => $rate, 'format' => uc_currency_format($rate), 'option_label' => $method['tablequote']['quote']['accessorials'][0]);
-  } else {
-    $quotes[] = array('rate' => $rate, 'format' => uc_currency_format($rate), 'option_label' => $method['tablequote']['quote']['accessorials'][0]);
   }
+
   return $quotes;
-}
\ No newline at end of file
+}
