PageRenderTime 28ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/community/Fontis/Australia/Model/Shipping/Carrier/Australiapost.php

https://bitbucket.org/dnejedly/eaparts
PHP | 370 lines | 238 code | 61 blank | 71 comment | 41 complexity | 34980a325b6e0b90b67139d97ca0e18e MD5 | raw file
  1. <?php
  2. /**
  3. * Fontis Australia Extension
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * @category Fontis
  16. * @package Fontis_Australia
  17. * @author Chris Norton
  18. * @copyright Copyright (c) 2008 Fontis Pty. Ltd. (http://www.fontis.com.au)
  19. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  20. */
  21. /**
  22. * Australia Post shipping model
  23. *
  24. * @category Fontis
  25. * @package Fontis_Australia
  26. */
  27. class Fontis_Australia_Model_Shipping_Carrier_Australiapost
  28. extends Mage_Shipping_Model_Carrier_Abstract
  29. implements Mage_Shipping_Model_Carrier_Interface
  30. {
  31. protected $_code = 'australiapost';
  32. /**
  33. * Collects the shipping rates for Australia Post from the DRC API.
  34. *
  35. * @param Mage_Shipping_Model_Rate_Request $data
  36. * @return Mage_Shipping_Model_Rate_Result
  37. */
  38. public function collectRates(Mage_Shipping_Model_Rate_Request $request)
  39. {
  40. // Check if this method is active
  41. if (!$this->getConfigFlag('active'))
  42. {
  43. return false;
  44. }
  45. // Check if this method is even applicable (shipping from Australia)
  46. $origCountry = Mage::getStoreConfig('shipping/origin/country_id', $this->getStore());
  47. if ($origCountry != "AU")
  48. {
  49. return false;
  50. }
  51. $result = Mage::getModel('shipping/rate_result');
  52. // TODO: Add some more validations
  53. $frompcode = Mage::getStoreConfig('shipping/origin/postcode', $this->getStore());
  54. $topcode = $request->getDestPostcode();
  55. if ($request->getDestCountryId())
  56. {
  57. $destCountry = $request->getDestCountryId();
  58. }
  59. else
  60. {
  61. $destCountry = "AU";
  62. }
  63. // Here we get the weight (and convert it to grams) and set some
  64. // sensible defaults for other shipping parameters.
  65. $sweight = (int)((float)$request->getPackageWeight() * (float)$this->getConfigData('weight_units'));
  66. $sheight = $swidth = $slength = 100;
  67. $shipping_num_boxes = 1;
  68. // Switch between domestic and international shipping methods based
  69. // on destination country.
  70. if($destCountry == "AU")
  71. {
  72. //============= Domestic Shipping ==================
  73. $shipping_methods = array('STANDARD', 'EXPRESS');
  74. foreach($shipping_methods as $shipping_method)
  75. {
  76. $drc = $this->_drcRequest($shipping_method, $frompcode, $topcode, $destCountry, $sweight, $slength, $swidth, $sheight, $shipping_num_boxes);
  77. if($drc['err_msg'] == 'OK')
  78. {
  79. // Check for registered post activation. If so, add extra options
  80. if($this->getConfigData('registered_post'))
  81. {
  82. $title_days = ($drc['days'] == 1) ? ' (1 day)' : ' (' . $drc['days'] . ' days)';
  83. $title = $this->getConfigData('name') . " " .
  84. ucfirst(strtolower($shipping_method)) .
  85. $title_days;
  86. $charge = $drc['charge'];
  87. $charge += $this->getConfigData('registered_post_charge');
  88. if($this->getConfigData('person_to_person'))
  89. {
  90. $charge += 5.50;
  91. }
  92. elseif($this->getConfigData('delivery_confirmation'))
  93. {
  94. $charge += 1.85;
  95. }
  96. $method = $this->_createMethod($request, $shipping_method, $title, $charge, $charge);
  97. $result->append($method);
  98. // Insurance only covers up to $5000 worth of goods.
  99. $packageValue = ($request->getPackageValue() > 5000) ? 5000 : $request->getPackageValue();
  100. // Insurance cost is $1.25 per $100 or part thereof. First $100 is
  101. // included in normal registered post costs.
  102. $insurance = (ceil($packageValue / 100) - 1) * 1.25;
  103. // Only add a new method if the insurance is different
  104. if($insurance > 0) {
  105. $charge += $insurance;
  106. $title = $this->getConfigData('name') . " " . ucfirst(strtolower($shipping_method)) . ' with Extra Cover';
  107. $method = $this->_createMethod($request, $shipping_method . '_EC', $title, $charge, $charge);
  108. $result->append($method);
  109. }
  110. }
  111. else
  112. {
  113. $title_days = ($drc['days'] == 1) ? ' (1 day)' : ' (' . $drc['days'] . ' days)';
  114. $title = $this->getConfigData('name') . " " .
  115. ucfirst(strtolower($shipping_method)) .
  116. $title_days;
  117. $method = $this->_createMethod($request, $shipping_method, $title, $drc['charge'], $drc['charge']);
  118. $result->append($method);
  119. }
  120. }
  121. }
  122. }
  123. else
  124. {
  125. //============= International Shipping ==================
  126. // International shipping options are highly dependent upon whether
  127. // or not you are using registered post.
  128. if($this->getConfigData('registered_post'))
  129. {
  130. //============= Registered Post ================
  131. // Registered Post International
  132. // Same price as Air Mail, plus $5. Extra cover is not available.
  133. // A weight limit of 2kg applies.
  134. if($sweight <= 2000)
  135. {
  136. $drc = $this->_drcRequest('AIR', $frompcode, $topcode, $destCountry, $sweight, $slength, $swidth, $sheight, $shipping_num_boxes);
  137. if($drc['err_msg'] == 'OK')
  138. {
  139. $title_days = ($drc['days'] == 1) ? ' (1 day)' : ' (' . $drc['days'] . ' days)';
  140. $title = $this->getConfigData('name') . ' Registered Post International' . $title_days;
  141. // RPI is another 5 dollars.
  142. $charge = $drc['charge'] + 5;
  143. if($this->getConfigData('delivery_confirmation'))
  144. {
  145. $charge += 3.30;
  146. }
  147. $charge += $this->getConfigData('registered_post_charge');
  148. $method = $this->_createMethod($request, 'AIR', $title, $charge, $charge);
  149. $result->append($method);
  150. }
  151. }
  152. // Express Post International
  153. $drc = $this->_drcRequest('EPI', $frompcode, $topcode, $destCountry, $sweight, $slength, $swidth, $sheight, $shipping_num_boxes);
  154. if($drc['err_msg'] == 'OK')
  155. {
  156. $title_days = ($drc['days'] == 1) ? ' (1 day)' : ' (' . $drc['days'] . ' days)';
  157. $title = $this->getConfigData('name') . ' Express Post International' . $title_days;
  158. $charge = $drc['charge'];
  159. if($this->getConfigData('delivery_confirmation'))
  160. {
  161. $charge += 3.30;
  162. }
  163. $charge += $this->getConfigData('registered_post_charge');
  164. $method = $this->_createMethod($request, 'EPI', $title, $charge, $charge);
  165. $result->append($method);
  166. // Insurance only covers up to $5000 worth of goods.
  167. $packageValue = ($request->getPackageValue() > 5000) ? 5000 : $request->getPackageValue();
  168. // Insurance cost is $2.25 per $100 or part thereof. First $100 is $8.45.
  169. $insurance = 8.45 + (ceil($packageValue / 100) - 1) * 1.25;
  170. $charge += $insurance;
  171. $title = $this->getConfigData('name') . ' Express Post International with Extra Cover';
  172. $method = $this->_createMethod($request, 'EPI-EC', $title, $charge, $charge);
  173. $result->append($method);
  174. }
  175. // Express Courier International
  176. // TODO: Create a table for this method.
  177. }
  178. else
  179. {
  180. //============= Standard Post ================
  181. // Sea Shipping
  182. $drc = $this->_drcRequest('SEA', $frompcode, $topcode, $destCountry, $sweight, $slength, $swidth, $sheight, $shipping_num_boxes);
  183. if($drc['err_msg'] == 'OK')
  184. {
  185. $title_days = ($drc['days'] == 1) ? ' (1 day)' : ' (' . $drc['days'] . ' days)';
  186. $title = $this->getConfigData('name') . ' Sea Mail' .
  187. $title_days;
  188. $method = $this->_createMethod($request, 'SEA', $title, $drc['charge'], $drc['charge']);
  189. $result->append($method);
  190. }
  191. // Air Mail
  192. $drc = $this->_drcRequest('AIR', $frompcode, $topcode, $destCountry, $sweight, $slength, $swidth, $sheight, $shipping_num_boxes);
  193. if($drc['err_msg'] == 'OK')
  194. {
  195. $title_days = ($drc['days'] == 1) ? ' (1 day)' : ' (' . $drc['days'] . ' days)';
  196. $title = $this->getConfigData('name') . ' Air Mail' .
  197. $title_days;
  198. $method = $this->_createMethod($request, 'AIR', $title, $drc['charge'], $drc['charge']);
  199. $result->append($method);
  200. }
  201. // Express Post International
  202. $drc = $this->_drcRequest('EPI', $frompcode, $topcode, $destCountry, $sweight, $slength, $swidth, $sheight, $shipping_num_boxes);
  203. if($drc['err_msg'] == 'OK')
  204. {
  205. $title_days = ($drc['days'] == 1) ? ' (1 day)' : ' (' . $drc['days'] . ' days)';
  206. $title = $this->getConfigData('name') . ' Express Post International' .
  207. $title_days;
  208. $method = $this->_createMethod($request, 'EPI', $title, $drc['charge'], $drc['charge']);
  209. $result->append($method);
  210. }
  211. // Express Courier International
  212. // TODO: Create a table for this method.
  213. }
  214. }
  215. Mage::log(print_r($result->asArray(), true), null, 'rates.log');
  216. return $result;
  217. }
  218. protected function _createMethod($request, $method_code, $title, $price, $cost)
  219. {
  220. $method = Mage::getModel('shipping/rate_result_method');
  221. $method->setCarrier('australiapost');
  222. $method->setCarrierTitle($this->getConfigData('title'));
  223. $method->setMethod($method_code);
  224. $method->setMethodTitle($title);
  225. $method->setPrice($this->getFinalPriceWithHandlingFee($price));
  226. $method->setCost($cost);
  227. return $method;
  228. }
  229. protected function _curl_get_file_contents($url)
  230. {
  231. $c = curl_init();
  232. curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  233. curl_setopt($c, CURLOPT_URL, $url);
  234. $contents = curl_exec($c);
  235. curl_close($c);
  236. if ($contents)
  237. {
  238. return $contents;
  239. }
  240. else
  241. {
  242. return false;
  243. }
  244. }
  245. protected function _drcRequest($service, $fromPostCode, $toPostCode, $destCountry, $weight, $length, $width, $height, $num_boxes)
  246. {
  247. // Construct the appropriate URL and send all the information
  248. // to the Australia Post DRC.
  249. $url = "http://drc.edeliver.com.au/ratecalc.asp?" .
  250. "Pickup_Postcode=" . rawurlencode($fromPostCode) .
  251. "&Destination_Postcode=" . rawurlencode($toPostCode) .
  252. "&Country=" . rawurlencode($destCountry) .
  253. "&Weight=" . rawurlencode($weight) .
  254. "&Service_Type=" . rawurlencode($service) .
  255. "&Height=" . rawurlencode($height) .
  256. "&Width=" . rawurlencode($width) .
  257. "&Length=" . rawurlencode($length) .
  258. "&Quantity=" . rawurlencode($num_boxes);
  259. if(ini_get('allow_url_fopen'))
  260. {
  261. Mage::log('Using fopen URL wrappers', null, 'fontis_australia.log');
  262. $drc_result = file($url);
  263. }
  264. else if(extension_loaded('curl'))
  265. {
  266. Mage::log('fopen URL wrappers unavailable, using curl', null, 'fontis_australia.log');
  267. try {
  268. $drc_result = $this->_curl_get_file_contents($url);
  269. } catch(Exception $e) {
  270. Mage::log($e);
  271. $drc_result = array();
  272. $drc_result['err_msg'] = 'FAIL';
  273. }
  274. $drc_result = explode("\n",$drc_result);
  275. //clean up array
  276. foreach($drc_result as $k => $vals) {
  277. if($vals == '') unset($drc_result[$k]);
  278. }
  279. }
  280. else
  281. {
  282. Mage::log('No download method available, could not contact DRC!', null, 'fontis_australia.log');
  283. $a = array();
  284. $a['err_msg'] = 'FAIL';
  285. return $a;
  286. }
  287. Mage::log("DRC result: " . $drc_result, null, 'fontis_australia.log');
  288. $result = array();
  289. foreach($drc_result as $vals)
  290. {
  291. $tokens = split("=", $vals);
  292. if(isset($tokens[1])) {
  293. $result[$tokens[0]] = trim($tokens[1]);
  294. } else {
  295. return array('err_msg' => 'Parsing error on Australia Post results');
  296. }
  297. }
  298. return $result;
  299. }
  300. /**
  301. * Get allowed shipping methods
  302. *
  303. * @return array
  304. */
  305. public function getAllowedMethods()
  306. {
  307. return array('australiapost' => $this->getConfigData('name'));
  308. }
  309. }