patch: support for percentage-based tables

spiderman's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 08/17/2007
Juice: 61
patch: support for percentage-based tables

this module has proven extremely useful in working through a fairly complex table-based shipping rates scenario for a soon-to-be-live site i've been working on.

attached is a patch which extends the basic functionality of the module to support percentage-based shipping rates in the table. the feature is very simplistic, merely adding a 'percent' field to the table entries, which gets used if the 'amount' is left blank (0):

<?php
diff
-ur orig/uc_ratequote/uc_ratequote.install uc_ratequote/uc_ratequote.install
--- orig/uc_ratequote/uc_ratequote.install      2007-08-02 12:32:04.000000000 -0400
+++ uc_ratequote/uc_ratequote.install   2007-09-17 01:50:30.000000000 -0400
@@ -9,7 +9,8 @@
         `
qid` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
         `
min` float NOT NULL default '0',
         `
max` float NOT NULL default '0',
-        `
rate` float NOT NULL default '0'
+        `rate` float NOT NULL default '0',
+       `
percent` float NOT NULL default '0'
      
) /*!40100 DEFAULT CHARACTER SET UTF8 */ ;");
     break;
     case 'pgsql':
@@ -19,3 +20,9 @@
function uc_ratequote_uninstall(){
   db_query("
DROP TABLE IF EXISTS {uc_ratequotes}");
}
+
+function uc_ratequote_update_1() {
+  $items = array();
+  $items[] = update_sql("
ALTER TABLE add percent float NOT NULL default '0'");
+  return $items;
+}
diff -ur orig/uc_ratequote/uc_ratequote.module uc_ratequote/uc_ratequote.module
--- orig/uc_ratequote/uc_ratequote.module       2007-08-01 14:49:08.000000000 -0400
+++ uc_ratequote/uc_ratequote.module    2007-09-17 01:55:29.000000000 -0400
@@ -142,6 +142,12 @@
     '#field_prefix' => variable_get('uc_sign_after_amount', FALSE) ? '' : variable_get('uc_currency_sign', '$'),
     '#field_suffix' => variable_get('uc_sign_after_amount', FALSE) ? variable_get('uc_currency_sign', '$') : '',
   );
+  $form['uc_ratequote']['uc_ratequote_order_percent'] = array('#type' => 'textfield',
+    '#title' => t('Shipping Percentage'),
+    '#default_value' => variable_get('uc_ratequote_order_percent', 0),
+    '#size' => 12,
+    '#field_suffix' => variable_get('uc_percentage_sign', '%'),
+  );
   $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Submit Changes') );
  
   return $form;
@@ -153,8 +159,9 @@
     form_set_error('uc_ratequote_order_minimum', t('Order Minimum must be a number not less than 0.'));
   if ($form_values['uc_ratequote_order_maximum'] < 0 || !is_numeric($form_values['uc_ratequote_order_maximum']))
     form_set_error('uc_ratequote_order_maximum', t('Order Maximum must be a number greater than 0.'));
-  if ($form_values['uc_ratequote_order_rate'] < 0 || !is_numeric($form_values['uc_ratequote_order_rate']))
-    form_set_error('uc_ratequote_order_rate', t('Shipping rate must be a number not less than 0.'));
+  if ( ($form_values['uc_ratequote_order_rate'] < 0 && $form_values['uc_ratequote_order_percent'] < 0) ||
+       (!is_numeric($form_values['uc_ratequote_order_rate']) && !is_numeric($form_values['uc_ratequote_order_percent'])) )
+    form_set_error('uc_ratequote_order_rate', t('Shipping rate or percent must be a number not less than 0.'));
  
   // check that max is bigger than min
   if ($form_values['uc_ratequote_order_maximum'] < $form_values['uc_ratequote_order_minimum'])
@@ -182,23 +189,25 @@
  
   // check for and insert new rate quote
   if ($form_values['uc_ratequote_order_maximum'] > $form_values['uc_ratequote_order_minimum'] && $form_values['uc_ratequote_order_maximum'] > 0){
-    db_query("
INSERT INTO {uc_ratequotes} (min, max, rate) VALUES (%f, %f, %f)", $form_values['uc_ratequote_order_minimum'], $form_values['uc_ratequote_order_maximum'], $form_values['uc_ratequote_order_rate']);
+    db_query("
INSERT INTO {uc_ratequotes} (min, max, rate, percent) VALUES (%f, %f, %f, %f)", $form_values['uc_ratequote_order_minimum'], $form_values['uc_ratequote_order_maximum'], $form_values['uc_ratequote_order_rate'], $form_values['uc_ratequote_order_percent']);
   }
  
   drupal_set_message(t('The configuration options have been saved.'));
}

function theme_uc_ratequote_admin_settings($form){
-  $header = array(t('Delete'), t('Order Minimum'), t('Order Maximum'), t('Shipping Rate'));
+  $header = array(t('Delete'), t('Order Minimum'), t('Order Maximum'), t('Shipping Rate'), t('Shipping Percentage'));
   $result = db_query("

SELECT * FROM {uc_ratequotes} ORDER BY min");
   while ($r = db_fetch_object($result)) {
     $row = array();
     $qid = $r->qid;
     $r->rate > 0 ? $rate = uc_currency_format($r->rate) : $rate = 'Free';
+    $r->percent > 0 ? $percent = $r->percent . "
%" : $percent = 'Free';
     $row[] = drupal_render($form['rates'][$qid]['delete']);
     $row[] = uc_currency_format($r->min);
     $row[] = uc_currency_format($r->max);
     $row[] = $rate;
+    $row[] = $percent;
     $rows[] = $row;
   }
   $output .= theme('table', $header, $rows);
@@ -234,22 +243,35 @@
  
   $result = db_query("
SELECT * FROM {uc_ratequotes}");
   while ($r = db_fetch_object($result)) {
-    if($total <= $r->max && $total >= $r->min)
+    if($total <= $r->max && $total >= $r->min) {
       $rate = $r->rate;
+      $percent = $r->percent;
+# debug
+#      ob_start(); print_r($r);
+#      drupal_set_message(ob_get_contents());
+#      ob_end_clean();
+    }
   }
  
   $method = uc_ratequote_shipping_method();
  
-  $quotes[] = array('rate' => $rate, 'format' => uc_currency_format($rate), 'option_label' => $method['ratequote']['quote']['accessorials'][0]);
+  if ($rate > 0) {
+    $quotes[] = array('rate' => $rate, 'format' => uc_currency_format($rate), 'option_label' => $method['ratequote']['quote']['accessorials'][0]);
+  } else {
+    $rate = $total * ($percent / 100);
+    $quotes[] = array('rate' => $rate, 'format' => uc_currency_format($rate), 'option_label' => $method['ratequote']['quote']['accessorials'][0]);
+  }
  
   return $quotes;
}

+# unused? mebbe not..
function uc_ratequote_quote_order($products, $details){
   $method = uc_ratequote_shipping_method();
   $rate = variable_get('uc_ratequote_order_rate', 0);
+  $percent = variable_get('uc_ratequote_order_percent', 0);
  
   $quotes[] = array('rate' => $rate, 'format' => uc_currency_format($rate), 'option_label' => $method['order_rate']['quote']['accessorials'][0]);
  
-  return drupal_to_js($quotes);
+  $quotes;
}
?>

please excuse the debug info, and the (possibly?) crufty bits leftover from the alpha7c updates..

AttachmentSize
uc_ratequote.percentage.diff 5.73 KB
Rate Quote Shipping Module By: Guest (16 replies) Thu, 08/16/2007 - 18:21