[ Index ]

PHP Cross Reference of osCMax 2.0.4

title

Body

[close]

/includes/modules/shipping/ -> ups.php (source)

   1  <?php
   2  /*
   3  $Id: ups.php 3 2006-05-27 04:59:07Z user $
   4  
   5    osCommerce, Open Source E-Commerce Solutions
   6    http://www.oscommerce.com
   7  
   8    Copyright 2006 osCMax
   9  
  10    Released under the GNU General Public License
  11  */
  12  
  13  /*
  14  revised by Fritz Clapp as UPS Choice 1.8 2003/08/02
  15    filters service types to those selected in admin and saved in 
  16    configuration table with key MODULE_SHIPPING_UPS_TYPES;
  17    suggests STD service as default for Canada;
  18    modified error message refers to failure to get quote;
  19  
  20  */
  21  
  22    class ups {
  23      var $code, $title, $descrption, $icon, $enabled, $types;
  24  
  25  // class constructor
  26      function ups() {
  27        global $order;
  28  
  29        $this->code = 'ups';
  30        $this->title = MODULE_SHIPPING_UPS_TEXT_TITLE;
  31        $this->description = MODULE_SHIPPING_UPS_TEXT_DESCRIPTION;
  32        $this->sort_order = MODULE_SHIPPING_UPS_SORT_ORDER;
  33        $this->icon = DIR_WS_ICONS . 'shipping_ups.gif';
  34        $this->tax_class = MODULE_SHIPPING_UPS_TAX_CLASS;
  35        $this->enabled = ((MODULE_SHIPPING_UPS_STATUS == 'True') ? true : false);
  36  
  37        if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_UPS_ZONE > 0) ) {
  38          $check_flag = false;
  39          $check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_SHIPPING_UPS_ZONE . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
  40          while ($check = tep_db_fetch_array($check_query)) {
  41            if ($check['zone_id'] < 1) {
  42              $check_flag = true;
  43              break;
  44            } elseif ($check['zone_id'] == $order->delivery['zone_id']) {
  45              $check_flag = true;
  46              break;
  47            }
  48          }
  49  
  50          if ($check_flag == false) {
  51            $this->enabled = false;
  52          }
  53        }
  54  
  55        $this->types = array('1DM' => 'Next Day Air Early AM',
  56                             '1DML' => 'Next Day Air Early AM Letter',
  57                             '1DA' => 'Next Day Air',
  58                             '1DAL' => 'Next Day Air Letter',
  59                             '1DAPI' => 'Next Day Air Intra (Puerto Rico)',
  60                             '1DP' => 'Next Day Air Saver',
  61                             '1DPL' => 'Next Day Air Saver Letter',
  62                             '2DM' => '2nd Day Air AM',
  63                             '2DML' => '2nd Day Air AM Letter',
  64                             '2DA' => '2nd Day Air',
  65                             '2DAL' => '2nd Day Air Letter',
  66                             '3DS' => '3 Day Select',
  67                             'GND' => 'Ground',
  68                             'GNDCOM' => 'Ground Commercial',
  69                             'GNDRES' => 'Ground Residential',
  70                             'STD' => 'Canada Standard',
  71                             'XPR' => 'Worldwide Express',
  72                             'XPRL' => 'worldwide Express Letter',
  73                             'XDM' => 'Worldwide Express Plus',
  74                             'XDML' => 'Worldwide Express Plus Letter',
  75                             'XPD' => 'Worldwide Expedited');
  76      }
  77  
  78  // class methods
  79      function quote($method = '') {
  80        global $HTTP_POST_VARS, $order, $shipping_weight, $shipping_num_boxes;
  81  
  82        if ( (tep_not_null($method)) && (isset($this->types[$method])) ) {
  83          $prod = $method;
  84        } else if ($order->delivery['country']['iso_code_2'] == 'CA') {
  85          $prod = 'STD';
  86        } else {
  87          $prod = 'GNDRES';
  88        }
  89  
  90        if ($method) $this->_upsAction('3'); // return a single quote
  91  
  92        $this->_upsProduct($prod);
  93  
  94        $country_name = tep_get_countries(SHIPPING_ORIGIN_COUNTRY, true);
  95        $this->_upsOrigin(SHIPPING_ORIGIN_ZIP, $country_name['countries_iso_code_2']);
  96        $this->_upsDest($order->delivery['postcode'], $order->delivery['country']['iso_code_2']);
  97        $this->_upsRate(MODULE_SHIPPING_UPS_PICKUP);
  98        $this->_upsContainer(MODULE_SHIPPING_UPS_PACKAGE);
  99        $this->_upsWeight($shipping_weight);
 100        $this->_upsRescom(MODULE_SHIPPING_UPS_RES);
 101        $upsQuote = $this->_upsGetQuote();
 102  
 103        if ( (is_array($upsQuote)) && (sizeof($upsQuote) > 0) ) {
 104          $this->quotes = array('id' => $this->code,
 105                                'module' => $this->title . ' (' . $shipping_num_boxes . ' x ' . $shipping_weight . 'lbs)');
 106  
 107          $methods = array();
 108          $allowed_methods = explode(", ", MODULE_SHIPPING_UPS_TYPES);
 109          $std_rcd = false;
 110          $qsize = sizeof($upsQuote);
 111          for ($i=0; $i<$qsize; $i++) {
 112            list($type, $cost) = each($upsQuote[$i]);
 113            if ($type=='STD') {
 114                if ($std_rcd) continue;
 115                else $std_rcd = true;
 116              };
 117            if (!in_array($type, $allowed_methods)) continue;
 118            $methods[] = array('id' => $type,
 119                               'title' => $this->types[$type],
 120                               'cost' => ($cost + MODULE_SHIPPING_UPS_HANDLING) * $shipping_num_boxes);
 121          }
 122  
 123          $this->quotes['methods'] = $methods;
 124          if ($this->tax_class > 0) {
 125            $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
 126          }
 127        } else {
 128          $this->quotes = array('module' => $this->title,
 129                                'error' => 'We are unable to obtain a rate quote for UPS shipping.<br>Please contact the store if no other alternative is shown.');
 130        }
 131  
 132        if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->title);
 133  
 134        return $this->quotes;
 135      }
 136  
 137      function check() {
 138        if (!isset($this->_check)) {
 139          $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_UPS_STATUS'");
 140          $this->_check = tep_db_num_rows($check_query);
 141        }
 142        return $this->_check;
 143      }
 144  
 145      function install() {
 146        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable UPS Shipping', 'MODULE_SHIPPING_UPS_STATUS', 'True', 'Do you want to offer UPS shipping?', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
 147        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('UPS Pickup Method', 'MODULE_SHIPPING_UPS_PICKUP', 'CC', 'How do you give packages to UPS? CC - Customer Counter, RDP - Daily Pickup, OTP - One Time Pickup, LC - Letter Center, OCA - On Call Air', '6', '0', now())");
 148        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('UPS Packaging?', 'MODULE_SHIPPING_UPS_PACKAGE', 'CP', 'CP - Your Packaging, ULE - UPS Letter, UT - UPS Tube, UBE - UPS Express Box', '6', '0', now())");
 149        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Residential Delivery?', 'MODULE_SHIPPING_UPS_RES', 'RES', 'Quote for Residential (RES) or Commercial Delivery (COM)', '6', '0', now())");
 150        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Handling Fee', 'MODULE_SHIPPING_UPS_HANDLING', '0', 'Handling fee for this shipping method.', '6', '0', now())");
 151        tep_db_query("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_UPS_TAX_CLASS', '0', 'Use the following tax class on the shipping fee.', '6', '0', 'tep_get_tax_class_title', 'tep_cfg_pull_down_tax_classes(', now())");
 152        tep_db_query("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_UPS_ZONE', '0', 'If a zone is selected, only enable this shipping method for that zone.', '6', '0', 'tep_get_zone_class_title', 'tep_cfg_pull_down_zone_classes(', now())");
 153        tep_db_query("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_UPS_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
 154        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ( 'Shipping Methods', 'MODULE_SHIPPING_UPS_TYPES', 'Nxt AM,Nxt AM Ltr,Nxt,Nxt Ltr,Nxt PR,Nxt Save,Nxt Save Ltr,2nd AM,2nd AM Ltr,2nd,2nd Ltr,3 Day Select,Ground,Canada,World Xp,World Xp Ltr, World Xp Plus,World Xp Plus Ltr,World Expedite', 'Select the USPS services to be offered.', '6', '13', 'tep_cfg_select_multioption(array(\'1DM\',\'1DML\', \'1DA\', \'1DAL\', \'1DAPI\', \'1DP\', \'1DPL\', \'2DM\', \'2DML\', \'2DA\', \'2DAL\', \'3DS\',\'GND\', \'STD\', \'XPR\', \'XPRL\', \'XDM\', \'XDML\', \'XPD\'), ', now() )");
 155      }
 156  
 157      function remove() {
 158        tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
 159      }
 160  
 161      function keys() {
 162        return array('MODULE_SHIPPING_UPS_STATUS', 'MODULE_SHIPPING_UPS_PICKUP', 'MODULE_SHIPPING_UPS_PACKAGE', 'MODULE_SHIPPING_UPS_RES', 'MODULE_SHIPPING_UPS_HANDLING', 'MODULE_SHIPPING_UPS_TAX_CLASS', 'MODULE_SHIPPING_UPS_ZONE', 'MODULE_SHIPPING_UPS_SORT_ORDER', 'MODULE_SHIPPING_UPS_TYPES');
 163      }
 164  
 165      function _upsProduct($prod){
 166        $this->_upsProductCode = $prod;
 167      }
 168  
 169      function _upsOrigin($postal, $country){
 170        $this->_upsOriginPostalCode = $postal;
 171        $this->_upsOriginCountryCode = $country;
 172      }
 173  
 174      function _upsDest($postal, $country){
 175        $postal = str_replace(' ', '', $postal);
 176  
 177        if ($country == 'US') {
 178          $this->_upsDestPostalCode = substr($postal, 0, 5);
 179        } else {
 180          $this->_upsDestPostalCode = $postal;
 181        }
 182  
 183        $this->_upsDestCountryCode = $country;
 184      }
 185  
 186      function _upsRate($foo) {
 187        switch ($foo) {
 188          case 'RDP':
 189            $this->_upsRateCode = 'Regular+Daily+Pickup';
 190            break;
 191          case 'OCA':
 192            $this->_upsRateCode = 'On+Call+Air';
 193            break;
 194          case 'OTP':
 195            $this->_upsRateCode = 'One+Time+Pickup';
 196            break;
 197          case 'LC':
 198            $this->_upsRateCode = 'Letter+Center';
 199            break;
 200          case 'CC':
 201            $this->_upsRateCode = 'Customer+Counter';
 202            break;
 203        }
 204      }
 205  
 206      function _upsContainer($foo) {
 207        switch ($foo) {
 208          case 'CP': // Customer Packaging
 209            $this->_upsContainerCode = '00';
 210            break;
 211          case 'ULE': // UPS Letter Envelope
 212            $this->_upsContainerCode = '01';
 213            break;
 214          case 'UT': // UPS Tube
 215            $this->_upsContainerCode = '03';
 216            break;
 217          case 'UEB': // UPS Express Box
 218            $this->_upsContainerCode = '21';
 219            break;
 220          case 'UW25': // UPS Worldwide 25 kilo
 221            $this->_upsContainerCode = '24';
 222            break;
 223          case 'UW10': // UPS Worldwide 10 kilo
 224            $this->_upsContainerCode = '25';
 225            break;
 226        }
 227      }
 228  
 229      function _upsWeight($foo) {
 230        $this->_upsPackageWeight = $foo;
 231      }
 232  
 233      function _upsRescom($foo) {
 234        switch ($foo) {
 235          case 'RES': // Residential Address
 236            $this->_upsResComCode = '1';
 237            break;
 238          case 'COM': // Commercial Address
 239            $this->_upsResComCode = '2';
 240            break;
 241        }
 242      }
 243  
 244      function _upsAction($action) {
 245        /* 3 - Single Quote
 246           4 - All Available Quotes */
 247  
 248        $this->_upsActionCode = $action;
 249      }
 250  
 251      function _upsGetQuote() {
 252        if (!isset($this->_upsActionCode)) $this->_upsActionCode = '4';
 253  
 254        $request = join('&', array('accept_UPS_license_agreement=yes',
 255                                   '10_action=' . $this->_upsActionCode,
 256                                   '13_product=' . $this->_upsProductCode,
 257                                   '14_origCountry=' . $this->_upsOriginCountryCode,
 258                                   '15_origPostal=' . $this->_upsOriginPostalCode,
 259                                   '19_destPostal=' . $this->_upsDestPostalCode,
 260                                   '22_destCountry=' . $this->_upsDestCountryCode,
 261                                   '23_weight=' . $this->_upsPackageWeight,
 262                                   '47_rate_chart=' . $this->_upsRateCode,
 263                                   '48_container=' . $this->_upsContainerCode,
 264                                   '49_residential=' . $this->_upsResComCode));
 265        $http = new httpClient();
 266        if ($http->Connect('www.ups.com', 80)) {
 267          $http->addHeader('Host', 'www.ups.com');
 268          $http->addHeader('User-Agent', 'osCommerce');
 269          $http->addHeader('Connection', 'Close');
 270  
 271          if ($http->Get('/using/services/rave/qcostcgi.cgi?' . $request)) $body = $http->getBody();
 272  
 273          $http->Disconnect();
 274        } else {
 275          return 'error';
 276        }
 277  /*
 278      mail('[email protected]','UPS response',$body,'From: <[email protected]>');
 279  */
 280        $body_array = explode("\n", $body);
 281  
 282        $returnval = array();
 283        $errorret = 'error'; // only return error if NO rates returned
 284  
 285        $n = sizeof($body_array);
 286        for ($i=0; $i<$n; $i++) {
 287          $result = explode('%', $body_array[$i]);
 288          $errcode = substr($result[0], -1);
 289          switch ($errcode) {
 290            case 3:
 291              if (is_array($returnval)) $returnval[] = array($result[1] => $result[8]);
 292              break;
 293            case 4:
 294              if (is_array($returnval)) $returnval[] = array($result[1] => $result[8]);
 295              break;
 296            case 5:
 297              $errorret = $result[1];
 298              break;
 299            case 6:
 300              if (is_array($returnval)) $returnval[] = array($result[3] => $result[10]);
 301              break;
 302          }
 303        }
 304        if (empty($returnval)) $returnval = $errorret;
 305  
 306        return $returnval;
 307      }
 308    }
 309  ?>


Generated: Fri Jan 1 13:43:16 2010 Cross-referenced by PHPXref 0.7