'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('Shipping cost'), ), ), '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)) { $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)) { ++$types; variable_set('uc_tablequote_type',$row->type); } if ($types == 0) { // No type has been set 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 { // There's already at least one existing type: $form['uc_tablequote']['uc_tablequote_type'] = array( '#value' => t('
Type of calculation: ').variable_get('uc_tablequote_type','weight').'
', ); // But report error if there's more than one type: if ($types > 1) { 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)) { 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); } } // 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')); } } 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"); 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; } 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')); break; } } } $rate = 0; $found = FALSE; $result = db_query("SELECT * FROM {uc_tablequote}"); while ($r = db_fetch_object($result)) { if (($total >= $r->min) && ($total < $r->max)) { $rate = $r->rate; $found = TRUE; } } $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; }