Code:
<?php
/*
This module is for use with Con Way Freight.
Released under the GNU General Public License
*/
class conway extends base {
var $code, $title, $description, $icon, $enabled;
// class constructor
function conway() {
global $total_weight, $order, $db, $template;
$this->code = 'conway';
$this->title = MODULE_SHIPPING_CONWAY_TEXT_TITLE;
$this->description = MODULE_SHIPPING_CONWAY_TEXT_DESCRIPTION;
$this->sort_order = MODULE_SHIPPING_CONWAY_SORT_ORDER;
$this->icon = DIR_WS_IMAGES . 'conway-images/conway.gif';
$this->tax_class = MODULE_SHIPPING_CONWAY_TAX_CLASS;
$this->enabled = ((MODULE_SHIPPING_CONWAY_STATUS == 'True') ? true : false);
if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_CONWAY_ZONE > 0) ) {
$check_flag = false;
$check = $db->Execute("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_SHIPPING_CONWAY_ZONE . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
while (!$check->EOF) {
if ($check->fields['zone_id'] < 1) {
$check_flag = true;
break;
} elseif ($check->fields['zone_id'] == $order->delivery['zone_id']) {
$check_flag = true;
break;
}
$check->MoveNext();
}
if ($check_flag == false) {
$this->enabled = false;
}
}
if($this->enabled == true && $order->delivery['country']['iso_code_2']!='US' && $order->delivery['country']['iso_code_2']!='CA') {
$this->enabled = false;
}
$this->shipping_weight = 0;
$this->shipping_num_boxes = 1;
if ($this->enabled == true && (is_numeric($total_weight) || $_SESSION['cart']->attributes_1216ft()==true) ) {
$this->shipping_weight = $total_weight;
if($this->shipping_weight <= 150 && $_SESSION['cart']->attributes_1216ft()==false) {
$this->enabled = false;
return false;
}
}
}
// class methods
function quote($method = '') {
global $total_weight, $zc_large_percent, $zc_large_weight, $shipping_weight, $shipping_num_boxes, $order;
$this->shipping_weight = $total_weight;// + ($total_weight*($zc_large_percent/100)) + $zc_large_weight;
if($this->shipping_weight <= 150 && $_SESSION['cart']->attributes_1216ft()==false) {
$this->enabled = false;
return false;
} else {
$error_msg = '';
//First, we get the customer's zipcode and country in the right format.
$dest_country = $order->delivery['country']['iso_code_2'];
if ($dest_country == 'US') {
$dest_zip = preg_replace('/[^0-9]/i', '', strtoupper($order->delivery['postcode']));
$dest_zip = substr($dest_zip, 0, 5);
} elseif ($dest_country == 'CA') {
$dest_zip = preg_replace('/[^0-9A-Z]/i', '', strtoupper($order->delivery['postcode']));
$dest_zip = substr($dest_zip, 0, 6);
$dest_country = 'CN';
} else {
$error_msg = '<br>' . MODULE_SHIPPING_CONWAY_TEXT_ERROR_BAD_COUNTRY;
}
//Format the shipping zip code as well. class xx insert class
if (MODULE_SHIPPING_CONWAY_SHIP_COUNTRY == 'US') {
$ship_zip = preg_replace('/[^0-9]/i', '', MODULE_SHIPPING_CONWAY_SHIP_ZIP);
$ship_zip = substr($ship_zip, 0, 5);
$ship_country = 'US';
} elseif (MODULE_SHIPPING_CONWAY_SHIP_COUNTRY == 'CA') {
$ship_zip = preg_replace('/[^0-9A-Z]/i', '', strtoupper(MODULE_SHIPPING_CONWAY_SHIP_ZIP));
$ship_zip = substr($ship_zip, 0, 6);
$ship_country = 'CN';
}
if (!zen_not_null($error_msg)) {
$title = "Rating";
$requestType = "RateRequest";
$requestUrl = "https://www.Con-way.com/XMLj/X-Rate";
$today = date("m/d/y");
$username = MODULE_SHIPPING_CONWAY_USERNAME;
$password = MODULE_SHIPPING_CONWAY_PASSWORD;
$custNmbr = MODULE_SHIPPING_CONWAY_CUSTOMER_NUMBER;
$shipterm = MODULE_SHIPPING_CONWAY_SHIP_TERMS;
$myElements = array('TotalCharge','NetCharge');
//$myElements = array('TotalCharge', 'Discount', 'TotalAccessorialCharges', 'NetCharge', 'TransitTime');
$itemArray = array(array('class'=>'XX', 'weight'=>ceil($this->shipping_weight)));
$accArray = array(); // Your accessorial services - add as needed
$accArray[] = "SSC";
if (MODULE_SHIPPING_CONWAY_RES_DELIVERY == 'True' && !zen_not_null($order->delivery['company'])) {
$accArray[] = "RSD";
}
if (MODULE_SHIPPING_CONWAY_INS_PICKUP == 'True') {
$accArray[] = "OIP";
}
if (MODULE_SHIPPING_CONWAY_INS_DELIVERY == 'True') {
$accArray[] = "DID";
}
if (MODULE_SHIPPING_CONWAY_DNC == 'True') {
$accArray[] = "DNC";
}
if (MODULE_SHIPPING_CONWAY_LIFT_GATE_DELIVERY == 'True') {
$accArray[] = "DLG";
}
if (MODULE_SHIPPING_CONWAY_RATE_GUARANTEED == 'True') {
$accArray[] = "GUR";
}
$xmlRequest="<RateRequest>" .
"<OriginZip country=\"$ship_country\">$ship_zip</OriginZip>" .
"<DestinationZip country=\"$dest_country\">$dest_zip</DestinationZip>";
if(zen_not_null($custNmbr)) {
$xmlRequest .= "<CustNmbr shipcode=\"S\">$custNmbr</CustNmbr>";
}
$xmlRequest .= "<ChargeCode>$shipterm</ChargeCode>" .
"<DiscountRate>100</DiscountRate>" .
"<EffectiveDate>$today</EffectiveDate>";
foreach ($itemArray as $item) { // Add commodity items to the XML Request
extract($item);
$xmlRequest .= "<Item>" .
"<CmdtyClass>$class</CmdtyClass>" .
"<Weight unit=\"lbs\">$weight</Weight>" .
"</Item>";
}
foreach ($accArray as $acc) { // Add accessorials to the XML Request
$xmlRequest .= "<Accessorial>$acc</Accessorial>";
}
$xmlRequest .= "</RateRequest>";
//echo '<pre>'.htmlspecialchars($xmlRequest).'</pre>';
$xmlRequest = urlencode($xmlRequest);
// open synchronous connection to Con-way servlet and set options
$urlConn = curl_init ($requestUrl);
curl_setopt ($urlConn, CURLOPT_POST, 1);
curl_setopt ($urlConn, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($urlConn, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($urlConn, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
curl_setopt ($urlConn, CURLOPT_USERPWD, $username.":".$password);
curl_setopt ($urlConn, CURLOPT_POSTFIELDS, "$requestType=$xmlRequest");
ob_start();
curl_exec($urlConn);
$xmlResponse = ob_get_contents();
ob_end_clean();
if (curl_errno($urlConn)) {
curl_close($urlConn);
$error_msg = sprintf('Unable to connect to Con-way server. Error [%d]: %s', curl_errno($ch), curl_error($ch));
} else {
curl_close($urlConn);
$total_shipping_price = 0;
$parser= xml_parser_create();
xml_parse_into_struct($parser,$xmlResponse,$xmlVals);
xml_parser_free($parser);
// print_r($xmlVals);
$myVals = $this->getElementValue($xmlVals, 'NetCharge');
$ship_price = $myVals[0];
if (!is_numeric($ship_price)) {
$error_msg = MODULE_SHIPPING_CONWAY_TEXT_ERROR_BAD_RESPONSE;
} else {
$handling_fee = MODULE_SHIPPING_CONWAY_HANDLING;
if(!zen_not_null($handling_fee)) {
$handling_fee = 0;
} elseif(substr($handling_fee, -1) == '%') {
$handling_fee = ($handling_fee / 100) * $ship_price;
} elseif($handling_fee > 0 && $handling_fee < 1) {
$ship_price = $ship_price/$handling_fee;
$handling_fee = 0;
}
$total_shipping_price = $ship_price + $handling_fee;
}
}
}//if (!zen_not_null($error_msg)) {
if (!zen_not_null($error_msg)) {
$items_1216ft_fee = MODULE_SHIPPING_CONWAY_ITEMS1216FT_FEE;
$items_1216ft_fee = trim($items_1216ft_fee);
if(is_numeric($items_1216ft_fee)) {
if($_SESSION['cart']->attributes_1216ft()==true) $total_shipping_price = $total_shipping_price + MODULE_SHIPPING_CONWAY_ITEMS1216FT_FEE;
}
$this->quotes = array('id' => $this->code,
'module' => '',//MODULE_SHIPPING_CONWAY_TEXT_TITLE,
'methods' => array(array('id' => $this->code,
'title' => MODULE_SHIPPING_CONWAY_TEXT_WAY,
'cost' => $total_shipping_price)));
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);
} else {
$this->quotes = array('module' => $this->title,
'error' => MODULE_SHIPPING_CONWAY_TEXT_ERROR_DESCRIPTION . ' ' .$error_msg);
}
return $this->quotes;
}
}
function getElementValue($xmlVals, $elName) {
$elValue = null;
foreach ($xmlVals as $arrkey => $arrvalue) {
foreach ($arrvalue as $key => $value) {
if ($value==strtoupper($elName)){
$elValue[] = $arrvalue['value'];
}
}
}
return $elValue;
}
function check() {
global $db;
if (!isset($this->_check)) {
$check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_CONWAY_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 Conway Freight Shipping', 'MODULE_SHIPPING_CONWAY_STATUS', 'True', 'Do you want to offer Conway Freight shipping?', '6', '10', '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, set_function, date_added) values ('Shipping Terms', 'MODULE_SHIPPING_CONWAY_SHIP_TERMS', 'P', 'Will these shipments be prepaid (P) or collect (C)? (This is here for future dev. No collect support right now)', '6', '20', 'zen_cfg_select_option(array(\'P\', \'C\'), ', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Shipper\'s Zip Code', 'MODULE_SHIPPING_CONWAY_SHIP_ZIP', 'ZIPCODE', 'Enter the zip code of where these shipments will be sent from. (Required)', '6', '30', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Shipper\'s Country', 'MODULE_SHIPPING_CONWAY_SHIP_COUNTRY', 'US', 'Select the country where these shipments will be sent from.', '6', '40', 'zen_cfg_select_option(array(\'US\', \'CA\'), ', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Username', 'MODULE_SHIPPING_CONWAY_USERNAME', 'USERNAME', 'Your Conway account username.', '6', '50', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Password', 'MODULE_SHIPPING_CONWAY_PASSWORD', 'PASSWORD', 'Your Conway account password.', '6', '60', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Customer Number', 'MODULE_SHIPPING_CONWAY_CUSTOMER_NUMBER', 'Your Account # Here', 'Your Conway customer number to get a customer specific discount.', '6', '70', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Inside Pickup', 'MODULE_SHIPPING_CONWAY_INS_PICKUP', 'False', 'Indicates if this is an inside pickup.', '6', '70', '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, set_function, date_added) values ('Inside Delivery', 'MODULE_SHIPPING_CONWAY_INS_DELIVERY', 'False', 'Indicates if this is an inside delivery.', '6', '80', '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, set_function, date_added) values ('Residential Delivery', 'MODULE_SHIPPING_CONWAY_RES_DELIVERY', 'False', 'Enable residential delivery if delivery address has no company name?', '6', '90', '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, set_function, date_added) values ('Destination Notify', 'MODULE_SHIPPING_CONWAY_DNC', 'False', 'Indicates if Destination Notify.', '6', '100', '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, set_function, date_added) values ('Lift Gate Delivery', 'MODULE_SHIPPING_CONWAY_LIFT_GATE_DELIVERY', 'False', 'Will Conway need a lift gate on the delivery address?', '6', '110', '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, set_function, date_added) values ('Guaranteed Rate', 'MODULE_SHIPPING_CONWAY_RATE_GUARANTEED', 'False', 'Display Guarantted delivery rate instead of normal rate?', '6', '115', '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, date_added) values ('Handling Fee', 'MODULE_SHIPPING_CONWAY_HANDLING', '.72', 'Handling fee for this shipping method.', '6', '120', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Items 12/16 FT Fee', 'MODULE_SHIPPING_CONWAY_ITEMS1216FT_FEE', '40.00', 'Handling fee on items 12/16 FT lengths for this shipping method.', '6', '125', 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_CONWAY_TAX_CLASS', '1', 'Use the following tax class on the shipping fee.', '6', '130', '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, use_function, set_function, date_added) values ('Shipping Zone', 'MODULE_SHIPPING_CONWAY_ZONE', '0', 'If a zone is selected, only enable this shipping method for that zone.', '6', '140', 'zen_get_zone_class_title', 'zen_cfg_pull_down_zone_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 of display.', 'MODULE_SHIPPING_CONWAY_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '150', now())");
}
function remove() {
global $db;
$db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
}
function keys() {
return array('MODULE_SHIPPING_CONWAY_STATUS',
'MODULE_SHIPPING_CONWAY_SHIP_TERMS',
'MODULE_SHIPPING_CONWAY_SHIP_ZIP',
'MODULE_SHIPPING_CONWAY_SHIP_COUNTRY',
'MODULE_SHIPPING_CONWAY_USERNAME',
'MODULE_SHIPPING_CONWAY_PASSWORD',
'MODULE_SHIPPING_CONWAY_CUSTOMER_NUMBER',
'MODULE_SHIPPING_CONWAY_INS_PICKUP',
'MODULE_SHIPPING_CONWAY_INS_DELIVERY',
'MODULE_SHIPPING_CONWAY_RES_DELIVERY',
'MODULE_SHIPPING_CONWAY_DNC',
'MODULE_SHIPPING_CONWAY_RATE_GUARANTEED',
'MODULE_SHIPPING_CONWAY_LIFT_GATE_DELIVERY',
'MODULE_SHIPPING_CONWAY_HANDLING',
'MODULE_SHIPPING_CONWAY_ITEMS1216FT_FEE',
'MODULE_SHIPPING_CONWAY_TAX_CLASS',
'MODULE_SHIPPING_CONWAY_ZONE',
'MODULE_SHIPPING_CONWAY_SORT_ORDER');
}
}
?>