PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/dnejedly/eaparts
PHP | 354 lines | 224 code | 65 blank | 65 comment | 21 complexity | a15e51b13aed10e676adf617cad8e050 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. * Originally based on Magento Tablerate Shipping code and Auctionmaid Matrixrate.
  16. * @copyright Copyright (c) 2008 Auction Maid (http://www.auctionmaid.com)
  17. * @author Karen Baker <enquiries@auctionmaid.com>
  18. *
  19. * @category Fontis
  20. * @package Fontis_Australia
  21. * @author Chris Norton
  22. * @copyright Copyright (c) 2008 Fontis Pty. Ltd. (http://www.fontis.com.au)
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. */
  25. /**
  26. * Australia Post eParcel shipping model
  27. *
  28. * @category Fontis
  29. * @package Fontis_Australia
  30. */
  31. class Fontis_Australia_Model_Shipping_Carrier_Eparcel
  32. extends Mage_Shipping_Model_Carrier_Abstract
  33. implements Mage_Shipping_Model_Carrier_Interface
  34. {
  35. protected $_code = 'eparcel';
  36. protected $_default_condition_name = 'package_weight';
  37. protected $_conditionNames = array();
  38. public function __construct()
  39. {
  40. parent::__construct();
  41. foreach ($this->getCode('condition_name') as $k=>$v) {
  42. $this->_conditionNames[] = $k;
  43. }
  44. }
  45. public function collectRates(Mage_Shipping_Model_Rate_Request $request)
  46. {
  47. if (!$this->getConfigFlag('active')) {
  48. return false;
  49. }
  50. if (!$request->getConditionName()) {
  51. $request->setConditionName($this->getConfigData('condition_name') ? $this->getConfigData('condition_name') : $this->_default_condition_name);
  52. }
  53. $result = Mage::getModel('shipping/rate_result');
  54. $rates = $this->getRate($request);
  55. if(is_array($rates))
  56. {
  57. foreach ($rates as $rate)
  58. {
  59. if (!empty($rate) && $rate['price'] >= 0) {
  60. $method = Mage::getModel('shipping/rate_result_method');
  61. $method->setCarrier('eparcel');
  62. $method->setCarrierTitle($this->getConfigData('title'));
  63. $method->setMethod($rate['delivery_type']);
  64. $method->setMethodTitle($rate['delivery_type']);
  65. $shippingPrice = $this->getFinalPriceWithHandlingFee($rate['price']);
  66. $method->setPrice($shippingPrice);
  67. $method->setCost($rate['cost']);
  68. $method->setDeliveryType($rate['delivery_type']);
  69. $result->append($method);
  70. }
  71. }
  72. }
  73. else
  74. {
  75. if (!empty($rates) && $rates['price'] >= 0) {
  76. $method = Mage::getModel('shipping/rate_result_method');
  77. $method->setCarrier('eparcel');
  78. $method->setCarrierTitle($this->getConfigData('title'));
  79. $method->setMethod('bestway');
  80. $method->setMethodTitle($this->getConfigData('name'));
  81. $shippingPrice = $this->getFinalPriceWithHandlingFee($rates['price']);
  82. $method->setPrice($shippingPrice);
  83. $method->setCost($rates['cost']);
  84. $method->setDeliveryType($rates['delivery_type']);
  85. $result->append($method);
  86. }
  87. }
  88. return $result;
  89. }
  90. public function getRate(Mage_Shipping_Model_Rate_Request $request)
  91. {
  92. return Mage::getResourceModel('australia/shipping_carrier_eparcel')->getRate($request);
  93. }
  94. public function getCode($type, $code='')
  95. {
  96. $codes = array(
  97. 'condition_name'=>array(
  98. 'package_weight' => Mage::helper('shipping')->__('Weight vs. Destination'),
  99. 'package_value' => Mage::helper('shipping')->__('Price vs. Destination'),
  100. 'package_qty' => Mage::helper('shipping')->__('# of Items vs. Destination'),
  101. ),
  102. 'condition_name_short'=>array(
  103. 'package_weight' => Mage::helper('shipping')->__('Weight (and above)'),
  104. 'package_value' => Mage::helper('shipping')->__('Order Subtotal (and above)'),
  105. 'package_qty' => Mage::helper('shipping')->__('# of Items (and above)'),
  106. ),
  107. );
  108. if (!isset($codes[$type])) {
  109. throw Mage::exception('Mage_Shipping', Mage::helper('shipping')->__('Invalid Table Rate code type: %s', $type));
  110. }
  111. if (''===$code) {
  112. return $codes[$type];
  113. }
  114. if (!isset($codes[$type][$code])) {
  115. throw Mage::exception('Mage_Shipping', Mage::helper('shipping')->__('Invalid Table Rate code for type %s: %s', $type, $code));
  116. }
  117. return $codes[$type][$code];
  118. }
  119. /**
  120. * Get allowed shipping methods
  121. *
  122. * @return array
  123. */
  124. public function getAllowedMethods()
  125. {
  126. return array('bestway'=>$this->getConfigData('name'));
  127. }
  128. /*
  129. * Tracking code
  130. */
  131. public function isTrackingAvailable()
  132. {
  133. return true;
  134. }
  135. public function getTrackingInfo($tracking)
  136. {
  137. $info = array();
  138. $result = $this->getTracking($tracking);
  139. if($result instanceof Mage_Shipping_Model_Tracking_Result){
  140. if ($trackings = $result->getAllTrackings()) {
  141. return $trackings[0];
  142. }
  143. }
  144. elseif (is_string($result) && !empty($result)) {
  145. return $result;
  146. }
  147. return false;
  148. }
  149. public function getTracking($trackings)
  150. {
  151. if (!is_array($trackings)) {
  152. $trackings = array($trackings);
  153. }
  154. return $this->_getTracking($trackings);
  155. }
  156. protected function _getTracking($trackings)
  157. {
  158. $result = Mage::getModel('shipping/tracking_result');
  159. foreach($trackings as $t) {
  160. $tracking = Mage::getModel('shipping/tracking_result_status');
  161. $tracking->setCarrier($this->_code);
  162. $tracking->setCarrierTitle($this->getConfigData('title'));
  163. $tracking->setTracking($t);
  164. $tracking->setUrl('http://www.eparcel.com.au/');
  165. $result->append($tracking);
  166. }
  167. return $result;
  168. }
  169. /**
  170. * Event Observer. Triggered when a shipment is created.
  171. */
  172. public function sendManifest($observer)
  173. {
  174. //Mage::log('=========================================================================');
  175. $order = $observer->getEvent()->getShipment()->getOrder();
  176. if(!($order->getShippingCarrier() instanceof $this)) {
  177. return $this;
  178. }
  179. // The current timestamp is used several times
  180. $timestamp = date('c');
  181. // Save the consignment number as it will be used more than once
  182. $consignmentNumber = $order->getIncrementId();
  183. $doc = new SimpleXMLElement('<PCMS xmlns="http://www.auspost.com.au/xml/pcms"></PCMS>');
  184. $pcms = $doc->addChild('SendPCMSManifest');
  185. $head = $pcms->addChild('header');
  186. $body = $pcms->addChild('body');
  187. $head->addChild('TransactionDateTime', $timestamp);
  188. $head->addChild('TransactionId', $consignmentNumber);
  189. $head->addChild('TransactionSequence', '0'); // Used to identify a sequence of transactions, N/A
  190. $head->addChild('ApplicationId', 'MERCHANT');
  191. $manifest = $body->addChild('PCMSManifest');
  192. $manifest->addChild('MerchantLocationId', $this->getConfigData('merchant_location_id')); // Testing = AWV
  193. $manifest->addChild('ManifestNumber', $consignmentNumber);
  194. $manifest->addChild('DateSubmitted', $timestamp);
  195. $manifest->addChild('DateLodged', $timestamp);
  196. // There may be multiple consignments per manifest.
  197. $consignment = $manifest->addChild('PCMSConsignment');
  198. // Get shipping address info
  199. $shippingAddress = $order->getShippingAddress();
  200. $name = $shippingAddress->getFirstname().' '.$shippingAddress->getLastname();
  201. $street = $shippingAddress->getStreet();
  202. // TODO: Revert back to using the Magento directory lookup, once they have
  203. // fixed the code that does loadByName.
  204. $stateCodes = array(
  205. 'Victoria' => 'VIC',
  206. 'New South Wales' => 'NSW',
  207. 'Australian Capital Territory' => 'ACT',
  208. 'Northern Territory' => 'NT',
  209. 'Queensland' => 'QLD',
  210. 'South Australia' => 'SA',
  211. 'Tasmania' => 'TAS',
  212. 'Western Australia' => 'WA'
  213. );
  214. $consignment->addChild('ConsignmentNumber', $consignmentNumber);
  215. $consignment->addChild('ChargeCode', $this->getConfigData('charge_code')); // Testing = S2
  216. $consignment->addChild('DeliveryName', $name);
  217. if($shippingAddress->getCompany()) { $consignment->addChild('DeliveryCompanyName', $shippingAddress->getCompany()); } // Optional
  218. if(is_array($street)) {
  219. $consignment->addChild('DeliveryAddressLine1', $street[0]);
  220. if(count($street) >= 2) { $consignment->addChild('DeliveryAddressLine2', $street[1]); } // Optional
  221. if(count($street) >= 3) { $consignment->addChild('DeliveryAddressLine3', $street[2]); } // Optional
  222. if(count($street) >= 4) { $consignment->addChild('DeliveryAddressLine4', $street[3]); } // Optional
  223. }
  224. else {
  225. $consignment->addChild('DeliveryAddressLine1', $street);
  226. }
  227. $consignment->addChild('DeliveryPhoneNumber', $shippingAddress->getTelephone());
  228. $consignment->addChild('DeliveryEmailAddress', $order->getCustomerEmail());
  229. $consignment->addChild('DeliverySuburb', $shippingAddress->getCity());
  230. //$consignment->addChild('DeliveryStateCode', Mage::getModel('directory/region')->loadByName($shippingAddress->getRegion(), 'AU')->getCode());
  231. $consignment->addChild('DeliveryStateCode', $stateCodes[$shippingAddress->getRegion()]);
  232. $consignment->addChild('DeliveryPostcode', $shippingAddress->getPostcode());
  233. $consignment->addChild('DeliveryCountryCode', 'AU'); // International deliveries not currently accepted
  234. $consignment->addChild('IsInternationalDelivery', 'false'); // International deliveries not currently accepted
  235. $consignment->addChild('ReturnName', $this->getConfigData('return_name')); // Optional
  236. $consignment->addChild('ReturnAddressLine1', $this->getConfigData('return_address_1'));
  237. $consignment->addChild('ReturnAddressLine2', $this->getConfigData('return_address_2')); // Optional
  238. $consignment->addChild('ReturnAddressLine3', $this->getConfigData('return_address_3')); // Optional
  239. $consignment->addChild('ReturnAddressLine4', $this->getConfigData('return_address_4')); // Optional
  240. $consignment->addChild('ReturnSuburb', $this->getConfigData('return_suburb'));
  241. $consignment->addChild('ReturnStateCode', $this->getConfigData('return_state'));
  242. $consignment->addChild('ReturnPostcode', $this->getConfigData('return_postcode'));
  243. $consignment->addChild('ReturnCountryCode', 'AU');
  244. $consignment->addChild('CreatedDateTime', $timestamp);
  245. $consignment->addChild('PostChargeToAccount', $this->getConfigData('post_charge_account')); // For Testing = 8830728
  246. $consignment->addChild('IsSignatureRequired', $this->getConfigData('signature_required') ? 'Y' : 'N'); // Y/N
  247. $consignment->addChild('DeliverPartConsignment', 'N'); // Y/N
  248. $consignment->addChild('ContainsDangerousGoods', 'false'); // true/false
  249. foreach($order->getAllVisibleItems() as $item) {
  250. Mage::log('Item: ' . print_r($item->getData(), true));
  251. // Consignments have one article per product
  252. $article = $consignment->addChild('PCMSDomesticArticle'); // International deliveries not currently accepted
  253. $article->addChild('ArticleNumber', $item->getSku());
  254. $article->addChild('BarcodeArticleNumber', '');
  255. //$article->addChild('Length', ''); // Optional
  256. //$article->addChild('Width', ''); // Optional
  257. //$article->addChild('Height', ''); // Optional
  258. $article->addChild('ActualWeight', $item->getRowWeight());
  259. //$article->addChild('CubicWeight', ''); // Optional
  260. $article->addChild('ArticleDescription', $item->getShortDescription());
  261. $article->addChild('IsTransitCoverRequired', 'N');
  262. //$article->addChild('TransitCoverAmount', '');
  263. // All contents are optional
  264. $contents = $article->addChild('ContentsItem');
  265. //$contents->addChild('ProductType', '');
  266. //$contents->addChild('GoodsDescription', '');
  267. //$contents->addChild('CountryOriginCode', '');
  268. //$contents->addChild('Weight', $item->getWeight());
  269. $contents->addChild('Quantity', $item->getQtyShipped());
  270. $contents->addChild('UnitValue', $item->getPrice());
  271. $contents->addChild('Value', $item->getRowTotal());
  272. //$contents->addChild('HSTariff', '');
  273. //$contents->addChild('ProductClassification', '');
  274. //*/
  275. }
  276. $data = $doc->asXML();
  277. //Mage::log($data);
  278. $soap = new SoapClient('https://test603a.auspost.com.au/despatchManifest/DespatchManifestWS?WSDL', array('login' => 'soaptest', 'password' => 'password'));
  279. $soap->submitManifestForDespatch($data);
  280. // Automatically add tracking information
  281. $track = Mage::getModel('sales/order_shipment_track');
  282. $track->setCarrierCode('eparcel');
  283. $track->setNumber($consignmentNumber);
  284. $track->setTitle($this->getConfigData('title'));
  285. $observer->getEvent()->getShipment()->addTrack($track);
  286. //Mage::log(print_r($track->getData(), true));
  287. return $this;
  288. }
  289. }