PageRenderTime 58ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/catalog/catalog/includes/modules/shipping/ups.php

https://github.com/osCommerce/oscommerce_cvs
PHP | 306 lines | 246 code | 46 blank | 14 comment | 36 complexity | ba4adbd6a45ab7afc32bfc239199ff26 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /*
  3. $Id: ups.php,v 1.57 2005/02/24 00:38:03 hpdl Exp $
  4. osCommerce, Open Source E-Commerce Solutions
  5. http://www.oscommerce.com
  6. Copyright (c) 2005 osCommerce
  7. Released under the GNU General Public License
  8. */
  9. class ups {
  10. var $code, $title, $descrption, $icon, $enabled, $types;
  11. // class constructor
  12. function ups() {
  13. global $osC_Database, $order;
  14. $this->code = 'ups';
  15. $this->title = MODULE_SHIPPING_UPS_TEXT_TITLE;
  16. $this->description = MODULE_SHIPPING_UPS_TEXT_DESCRIPTION;
  17. $this->sort_order = MODULE_SHIPPING_UPS_SORT_ORDER;
  18. $this->icon = DIR_WS_ICONS . 'shipping_ups.gif';
  19. $this->tax_class = MODULE_SHIPPING_UPS_TAX_CLASS;
  20. $this->enabled = ((MODULE_SHIPPING_UPS_STATUS == 'True') ? true : false);
  21. if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_UPS_ZONE > 0) ) {
  22. $check_flag = false;
  23. $Qcheck = $osC_Database->query('select zone_id from :table_zones_to_geo_zones where geo_zone_id = :geo_zone_id and zone_country_id = :zone_country_id order by zone_id');
  24. $Qcheck->bindTable(':table_zones_to_geo_zones', TABLE_ZONES_TO_GEO_ZONES);
  25. $Qcheck->bindInt(':geo_zone_id', MODULE_SHIPPING_UPS_ZONE);
  26. $Qcheck->bindInt(':zone_country_id', $order->delivery['country']['id']);
  27. $Qcheck->execute();
  28. while ($Qcheck->next()) {
  29. if ($Qcheck->valueInt('zone_id') < 1) {
  30. $check_flag = true;
  31. break;
  32. } elseif ($Qcheck->valueInt('zone_id') == $order->delivery['zone_id']) {
  33. $check_flag = true;
  34. break;
  35. }
  36. }
  37. if ($check_flag == false) {
  38. $this->enabled = false;
  39. }
  40. }
  41. $this->types = array('1DM' => 'Next Day Air Early AM',
  42. '1DML' => 'Next Day Air Early AM Letter',
  43. '1DA' => 'Next Day Air',
  44. '1DAL' => 'Next Day Air Letter',
  45. '1DAPI' => 'Next Day Air Intra (Puerto Rico)',
  46. '1DP' => 'Next Day Air Saver',
  47. '1DPL' => 'Next Day Air Saver Letter',
  48. '2DM' => '2nd Day Air AM',
  49. '2DML' => '2nd Day Air AM Letter',
  50. '2DA' => '2nd Day Air',
  51. '2DAL' => '2nd Day Air Letter',
  52. '3DS' => '3 Day Select',
  53. 'GND' => 'Ground',
  54. 'GNDCOM' => 'Ground Commercial',
  55. 'GNDRES' => 'Ground Residential',
  56. 'STD' => 'Canada Standard',
  57. 'XPR' => 'Worldwide Express',
  58. 'XPRL' => 'worldwide Express Letter',
  59. 'XDM' => 'Worldwide Express Plus',
  60. 'XDML' => 'Worldwide Express Plus Letter',
  61. 'XPD' => 'Worldwide Expedited');
  62. }
  63. // class methods
  64. function quote($method = '') {
  65. global $osC_Tax, $order, $shipping_weight, $shipping_num_boxes, $osC_Weight;
  66. if (tep_not_null($method) && isset($this->types[$method])) {
  67. $prod = $method;
  68. } else {
  69. $prod = 'GNDRES';
  70. }
  71. if ($method) $this->_upsAction('3'); // return a single quote
  72. $this->_upsProduct($prod);
  73. $shipping_weight = $osC_Weight->convert($shipping_weight, SHIPPING_WEIGHT_UNIT, MODULE_SHIPPING_UPS_WEIGHT_UNIT);
  74. $country_name = tep_get_countries(SHIPPING_ORIGIN_COUNTRY, true);
  75. $this->_upsOrigin(SHIPPING_ORIGIN_ZIP, $country_name['countries_iso_code_2']);
  76. $this->_upsDest($order->delivery['postcode'], $order->delivery['country']['iso_code_2']);
  77. $this->_upsRate(MODULE_SHIPPING_UPS_PICKUP);
  78. $this->_upsContainer(MODULE_SHIPPING_UPS_PACKAGE);
  79. $this->_upsWeight($shipping_weight);
  80. $this->_upsRescom(MODULE_SHIPPING_UPS_RES);
  81. $upsQuote = $this->_upsGetQuote();
  82. if ( (is_array($upsQuote)) && (sizeof($upsQuote) > 0) ) {
  83. $this->quotes = array('id' => $this->code,
  84. 'module' => $this->title . ' (' . $shipping_num_boxes . ' x ' . $shipping_weight . 'lbs)',
  85. 'tax' => 0);
  86. $methods = array();
  87. $qsize = sizeof($upsQuote);
  88. for ($i=0; $i<$qsize; $i++) {
  89. list($type, $cost) = each($upsQuote[$i]);
  90. $methods[] = array('id' => $type,
  91. 'title' => $this->types[$type],
  92. 'cost' => ($cost + MODULE_SHIPPING_UPS_HANDLING) * $shipping_num_boxes);
  93. }
  94. $this->quotes['methods'] = $methods;
  95. if ($this->tax_class > 0) {
  96. $this->quotes['tax'] = $osC_Tax->getTaxRate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
  97. }
  98. } else {
  99. $this->quotes = array('module' => $this->title,
  100. 'error' => 'An error occured with the UPS shipping calculations.<br>' . $upsQuote . '<br>If you prefer to use UPS as your shipping method, please contact the store owner.');
  101. }
  102. if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->title);
  103. return $this->quotes;
  104. }
  105. function check() {
  106. if (!isset($this->_check)) {
  107. $this->_check = defined('MODULE_SHIPPING_UPS_STATUS');
  108. }
  109. return $this->_check;
  110. }
  111. function install() {
  112. global $osC_Database;
  113. $osC_Database->simpleQuery("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())");
  114. $osC_Database->simpleQuery("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())");
  115. $osC_Database->simpleQuery("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())");
  116. $osC_Database->simpleQuery("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())");
  117. $osC_Database->simpleQuery("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())");
  118. $osC_Database->simpleQuery("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())");
  119. $osC_Database->simpleQuery("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())");
  120. $osC_Database->simpleQuery("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())");
  121. $osC_Database->simpleQuery("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Module weight Unit', 'MODULE_SHIPPING_UPS_WEIGHT_UNIT', '2', 'What unit of weight does this shipping module use?.', '6', '0', 'tep_get_weight_class_title', 'tep_cfg_pull_down_weight_classes(', now())");
  122. }
  123. function remove() {
  124. global $osC_Database;
  125. $Qdel = $osC_Database->query('delete from :table_configuration where configuration_key in (":configuration_key")');
  126. $Qdel->bindTable(':table_configuration', TABLE_CONFIGURATION);
  127. $Qdel->bindRaw(':configuration_key', implode('", "', $this->keys()));
  128. $Qdel->execute();
  129. }
  130. function keys() {
  131. 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_WEIGHT_UNIT');
  132. }
  133. function _upsProduct($prod){
  134. $this->_upsProductCode = $prod;
  135. }
  136. function _upsOrigin($postal, $country){
  137. $this->_upsOriginPostalCode = $postal;
  138. $this->_upsOriginCountryCode = $country;
  139. }
  140. function _upsDest($postal, $country){
  141. $postal = str_replace(' ', '', $postal);
  142. if ($country == 'US') {
  143. $this->_upsDestPostalCode = substr($postal, 0, 5);
  144. } else {
  145. $this->_upsDestPostalCode = $postal;
  146. }
  147. $this->_upsDestCountryCode = $country;
  148. }
  149. function _upsRate($foo) {
  150. switch ($foo) {
  151. case 'RDP':
  152. $this->_upsRateCode = 'Regular+Daily+Pickup';
  153. break;
  154. case 'OCA':
  155. $this->_upsRateCode = 'On+Call+Air';
  156. break;
  157. case 'OTP':
  158. $this->_upsRateCode = 'One+Time+Pickup';
  159. break;
  160. case 'LC':
  161. $this->_upsRateCode = 'Letter+Center';
  162. break;
  163. case 'CC':
  164. $this->_upsRateCode = 'Customer+Counter';
  165. break;
  166. }
  167. }
  168. function _upsContainer($foo) {
  169. switch ($foo) {
  170. case 'CP': // Customer Packaging
  171. $this->_upsContainerCode = '00';
  172. break;
  173. case 'ULE': // UPS Letter Envelope
  174. $this->_upsContainerCode = '01';
  175. break;
  176. case 'UT': // UPS Tube
  177. $this->_upsContainerCode = '03';
  178. break;
  179. case 'UEB': // UPS Express Box
  180. $this->_upsContainerCode = '21';
  181. break;
  182. case 'UW25': // UPS Worldwide 25 kilo
  183. $this->_upsContainerCode = '24';
  184. break;
  185. case 'UW10': // UPS Worldwide 10 kilo
  186. $this->_upsContainerCode = '25';
  187. break;
  188. }
  189. }
  190. function _upsWeight($foo) {
  191. $this->_upsPackageWeight = $foo;
  192. }
  193. function _upsRescom($foo) {
  194. switch ($foo) {
  195. case 'RES': // Residential Address
  196. $this->_upsResComCode = '1';
  197. break;
  198. case 'COM': // Commercial Address
  199. $this->_upsResComCode = '2';
  200. break;
  201. }
  202. }
  203. function _upsAction($action) {
  204. /* 3 - Single Quote
  205. 4 - All Available Quotes */
  206. $this->_upsActionCode = $action;
  207. }
  208. function _upsGetQuote() {
  209. if (!isset($this->_upsActionCode)) $this->_upsActionCode = '4';
  210. $request = join('&', array('accept_UPS_license_agreement=yes',
  211. '10_action=' . $this->_upsActionCode,
  212. '13_product=' . $this->_upsProductCode,
  213. '14_origCountry=' . $this->_upsOriginCountryCode,
  214. '15_origPostal=' . $this->_upsOriginPostalCode,
  215. '19_destPostal=' . $this->_upsDestPostalCode,
  216. '22_destCountry=' . $this->_upsDestCountryCode,
  217. '23_weight=' . $this->_upsPackageWeight,
  218. '47_rate_chart=' . $this->_upsRateCode,
  219. '48_container=' . $this->_upsContainerCode,
  220. '49_residential=' . $this->_upsResComCode));
  221. $http = new httpClient();
  222. if ($http->Connect('www.ups.com', 80)) {
  223. $http->addHeader('Host', 'www.ups.com');
  224. $http->addHeader('User-Agent', 'osCommerce');
  225. $http->addHeader('Connection', 'Close');
  226. if ($http->Get('/using/services/rave/qcostcgi.cgi?' . $request)) $body = $http->getBody();
  227. $http->Disconnect();
  228. } else {
  229. return 'error';
  230. }
  231. $body_array = explode("\n", $body);
  232. $returnval = array();
  233. $errorret = 'error'; // only return error if NO rates returned
  234. $n = sizeof($body_array);
  235. for ($i=0; $i<$n; $i++) {
  236. $result = explode('%', $body_array[$i]);
  237. $errcode = substr($result[0], -1);
  238. switch ($errcode) {
  239. case 3:
  240. if (is_array($returnval)) $returnval[] = array($result[1] => $result[8]);
  241. break;
  242. case 4:
  243. if (is_array($returnval)) $returnval[] = array($result[1] => $result[8]);
  244. break;
  245. case 5:
  246. $errorret = $result[1];
  247. break;
  248. case 6:
  249. if (is_array($returnval)) $returnval[] = array($result[3] => $result[10]);
  250. break;
  251. }
  252. }
  253. if (empty($returnval)) $returnval = $errorret;
  254. return $returnval;
  255. }
  256. }
  257. ?>