Code:
class smc {
var $code, $title, $description, $enabled, $num_zones;
// class constructor
function smc() {
$this->code = 'smc';
$this->title = MODULE_SHIPPING_SMC_TEXT_TITLE;
$this->description = MODULE_SHIPPING_SMC_TEXT_DESCRIPTION;
$this->sort_order = MODULE_SHIPPING_SMC_SORT_ORDER;
$this->icon = '';
$this->tax_class = MODULE_SHIPPING_SMC_TAX_CLASS;
$this->enabled = ((MODULE_SHIPPING_SMC_STATUS == 'True') ? true : false);
// CUSTOMIZE THIS SETTING FOR THE NUMBER OF ZONES NEEDED
$this->num_zones = 10;
$this->num_zone_start = 2;
}
// class methods
function quote($method = '') {
global $order, $cart, $shipping_weight, $shipping_num_boxes;
$dest_country = $order->delivery['country']['iso_code_2'];
$dest_zone = 0;
$error = false;
$zipcode_prefix = substr($order->delivery['postcode'],0,3);
global $db;
$check = $db->Execute("select shipzonecode from " . TABLE_SHIPZONECODE . " where '" . $zipcode_prefix . "' BETWEEN shipzonestartzip and shipzoneendzip");
while (!$check->EOF)) {
$dest_zone = $check['shipzonecode'];
}
$check->MoveNext();
// echo $dest_zone;
if ($dest_zone == 0) {
$error = true;
} else {
/* This is the Shipping Calculation Area */
$shipping = -1;
$zones_cost = constant('MODULE_SHIPPING_SMC_COST_' . $dest_zone);
$order_total = $cart->show_total();
$zones_table = split("[:,]" , $zones_cost);
$size = sizeof($zones_table);
for ($i=0; $i<$size; $i+=2) {
if ($order_total <= $zones_table[$i]) {
$shipping = $zones_table[$i+1];
$shipping_method = MODULE_SHIPPING_SMC_TEXT_WAY . ' ' . $order->delivery['postcode'];
break;
}
}
/*End of Shipping Calculation Area */
if ($shipping == -1) {
$shipping_cost = 0;
$shipping_method = MODULE_SHIPPING_SMC_UNDEFINED_RATE;
} else {
if (strpos($shipping, "%") === false) {
$shipping_cost = ($shipping) + constant('MODULE_SHIPPING_SMC_HANDLING_' . $dest_zone);
} else {
$shipcost = (str_replace("%","",$shipping) / 100);
$shipping_cost = ($order_total * $shipcost) + constant('MODULE_SHIPPING_SMC_HANDLING_' . $dest_zone);
}
}
}
$this->quotes = array('id' => $this->code,
'module' => MODULE_SHIPPING_SMC_TEXT_TITLE,
'methods' => array(array('id' => $this->code,
'title' => $shipping_method,
'cost' => $shipping_cost)));
if ($this->tax_class > 0) {
$this->quotes['tax'] = zen_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
}
if (zen_not_null($this->icon)) $this->quotes['icon'] = zen_image($this->icon, $this->title);
if ($error == true) $this->quotes['error'] = MODULE_SHIPPING_SMC_INVALID_ZONE;
return $this->quotes;
}
function check() {
global $db;
if (!isset($this->_check)) {
$check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_SMC_STATUS'");
$this->_check = $check_query->RecordCount();
}
return $this->_check;
}
function install() {
global $db;
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) VALUES ('Enable Zones Method', 'MODULE_SHIPPING_SMC_STATUS', 'True', 'Do you want to offer zone rate shipping?', '6', '0', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_SHIPPING_SMC_TAX_CLASS', '0', 'Use the following tax class on the shipping fee.', '6', '0', 'zen_get_tax_class_title', 'zen_cfg_pull_down_tax_classes(', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_SHIPPING_SMC_SORT_ORDER', '0', 'Sort order of display.', '6', '0', now())");
for ($i = $this->num_zone_start; $i <= $this->num_zones + ($this->num_zone_start - 1); $i++) {
$default_countries = 'US';
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Zone " . $i ." Countries', 'MODULE_SHIPPING_SMC_COUNTRIES_" . $i ."', '" . $default_countries . "', 'Comma separated list of two character ISO country codes that are part of Zone " . $i . ".', '6', '0', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Zone " . $i ." Shipping Table', 'MODULE_SHIPPING_SMC_COST_" . $i ."', '25:6.50,500:13%,999999:5%', 'Shipping rates to Zone " . $i . " destinations based on a group of maximum order amounts. Example: 25:6.50,500:13%,999999:5%,... Amounts less than or equal to 25 would cost 6.50 for Zone " . $i . " destinations.', '6', '0', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Zone " . $i ." Handling Fee', 'MODULE_SHIPPING_SMC_HANDLING_" . $i."', '0', 'Handling Fee for this shipping zone', '6', '0', now())");
}
}
function remove() {
global $db;
$db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
}
function keys() {
$keys = array('MODULE_SHIPPING_SMC_STATUS', 'MODULE_SHIPPING_SMC_TAX_CLASS', 'MODULE_SHIPPING_SMC_SORT_ORDER');
for ($i = $this->num_zone_start; $i <= $this->num_zones + ($this->num_zone_start - 1); $i++) {
$keys[] = 'MODULE_SHIPPING_SMC_COUNTRIES_' . $i;
$keys[] = 'MODULE_SHIPPING_SMC_COST_' . $i;
$keys[] = 'MODULE_SHIPPING_SMC_HANDLING_' . $i;
}
return $keys;
}
}
?>
Thanks in advance for any input