Example #1
0
 /**
  * Event Observer. Triggered when a shipment is created.
  */
 public function sendManifest($observer)
 {
     //Mage::log('=========================================================================');
     $order = $observer->getEvent()->getShipment()->getOrder();
     if (!$order->getShippingCarrier() instanceof $this) {
         return $this;
     }
     // The current timestamp is used several times
     $timestamp = date('c');
     // Save the consignment number as it will be used more than once
     $consignmentNumber = $order->getIncrementId();
     $doc = new SimpleXMLElement('<PCMS xmlns="http://www.auspost.com.au/xml/pcms"></PCMS>');
     $pcms = $doc->addChild('SendPCMSManifest');
     $head = $pcms->addChild('header');
     $body = $pcms->addChild('body');
     $head->addChild('TransactionDateTime', $timestamp);
     $head->addChild('TransactionId', $consignmentNumber);
     $head->addChild('TransactionSequence', '0');
     // Used to identify a sequence of transactions, N/A
     $head->addChild('ApplicationId', 'MERCHANT');
     $manifest = $body->addChild('PCMSManifest');
     $manifest->addChild('MerchantLocationId', $this->getConfigData('merchant_location_id'));
     // Testing = AWV
     $manifest->addChild('ManifestNumber', $consignmentNumber);
     $manifest->addChild('DateSubmitted', $timestamp);
     $manifest->addChild('DateLodged', $timestamp);
     // There may be multiple consignments per manifest.
     $consignment = $manifest->addChild('PCMSConsignment');
     // Get shipping address info
     $shippingAddress = $order->getShippingAddress();
     $name = $shippingAddress->getFirstname() . ' ' . $shippingAddress->getLastname();
     $street = $shippingAddress->getStreet();
     // TODO: Revert back to using the Magento directory lookup, once they have
     // fixed the code that does loadByName.
     $stateCodes = array('Victoria' => 'VIC', 'New South Wales' => 'NSW', 'Australian Capital Territory' => 'ACT', 'Northern Territory' => 'NT', 'Queensland' => 'QLD', 'South Australia' => 'SA', 'Tasmania' => 'TAS', 'Western Australia' => 'WA');
     $consignment->addChild('ConsignmentNumber', $consignmentNumber);
     $consignment->addChild('ChargeCode', $this->getConfigData('charge_code'));
     // Testing = S2
     $consignment->addChild('DeliveryName', $name);
     if ($shippingAddress->getCompany()) {
         $consignment->addChild('DeliveryCompanyName', $shippingAddress->getCompany());
     }
     // Optional
     if (is_array($street)) {
         $consignment->addChild('DeliveryAddressLine1', $street[0]);
         if (count($street) >= 2) {
             $consignment->addChild('DeliveryAddressLine2', $street[1]);
         }
         // Optional
         if (count($street) >= 3) {
             $consignment->addChild('DeliveryAddressLine3', $street[2]);
         }
         // Optional
         if (count($street) >= 4) {
             $consignment->addChild('DeliveryAddressLine4', $street[3]);
         }
         // Optional
     } else {
         $consignment->addChild('DeliveryAddressLine1', $street);
     }
     $consignment->addChild('DeliveryPhoneNumber', $shippingAddress->getTelephone());
     $consignment->addChild('DeliveryEmailAddress', $order->getCustomerEmail());
     $consignment->addChild('DeliverySuburb', $shippingAddress->getCity());
     //$consignment->addChild('DeliveryStateCode', Mage::getModel('directory/region')->loadByName($shippingAddress->getRegion(), 'AU')->getCode());
     $consignment->addChild('DeliveryStateCode', $stateCodes[$shippingAddress->getRegion()]);
     $consignment->addChild('DeliveryPostcode', $shippingAddress->getPostcode());
     $consignment->addChild('DeliveryCountryCode', 'AU');
     // International deliveries not currently accepted
     $consignment->addChild('IsInternationalDelivery', 'false');
     // International deliveries not currently accepted
     $consignment->addChild('ReturnName', $this->getConfigData('return_name'));
     // Optional
     $consignment->addChild('ReturnAddressLine1', $this->getConfigData('return_address_1'));
     $consignment->addChild('ReturnAddressLine2', $this->getConfigData('return_address_2'));
     // Optional
     $consignment->addChild('ReturnAddressLine3', $this->getConfigData('return_address_3'));
     // Optional
     $consignment->addChild('ReturnAddressLine4', $this->getConfigData('return_address_4'));
     // Optional
     $consignment->addChild('ReturnSuburb', $this->getConfigData('return_suburb'));
     $consignment->addChild('ReturnStateCode', $this->getConfigData('return_state'));
     $consignment->addChild('ReturnPostcode', $this->getConfigData('return_postcode'));
     $consignment->addChild('ReturnCountryCode', 'AU');
     $consignment->addChild('CreatedDateTime', $timestamp);
     $consignment->addChild('PostChargeToAccount', $this->getConfigData('post_charge_account'));
     // For Testing = 8830728
     $consignment->addChild('IsSignatureRequired', $this->getConfigData('signature_required') ? 'Y' : 'N');
     // Y/N
     $consignment->addChild('DeliverPartConsignment', 'N');
     // Y/N
     $consignment->addChild('ContainsDangerousGoods', 'false');
     // true/false
     foreach ($order->getAllVisibleItems() as $item) {
         Mage::log('Item: ' . print_r($item->getData(), true));
         // Consignments have one article per product
         $article = $consignment->addChild('PCMSDomesticArticle');
         // International deliveries not currently accepted
         $article->addChild('ArticleNumber', $item->getSku());
         $article->addChild('BarcodeArticleNumber', '');
         //$article->addChild('Length', ''); // Optional
         //$article->addChild('Width', ''); // Optional
         //$article->addChild('Height', ''); // Optional
         $article->addChild('ActualWeight', $item->getRowWeight());
         //$article->addChild('CubicWeight', ''); // Optional
         $article->addChild('ArticleDescription', $item->getShortDescription());
         $article->addChild('IsTransitCoverRequired', 'N');
         //$article->addChild('TransitCoverAmount', '');
         // All contents are optional
         $contents = $article->addChild('ContentsItem');
         //$contents->addChild('ProductType', '');
         //$contents->addChild('GoodsDescription', '');
         //$contents->addChild('CountryOriginCode', '');
         //$contents->addChild('Weight', $item->getWeight());
         $contents->addChild('Quantity', $item->getQtyShipped());
         $contents->addChild('UnitValue', $item->getPrice());
         $contents->addChild('Value', $item->getRowTotal());
         //$contents->addChild('HSTariff', '');
         //$contents->addChild('ProductClassification', '');
         //*/
     }
     $data = $doc->asXML();
     //Mage::log($data);
     $soap = new SoapClient('https://test603a.auspost.com.au/despatchManifest/DespatchManifestWS?WSDL', array('login' => 'soaptest', 'password' => 'password'));
     $soap->submitManifestForDespatch($data);
     // Automatically add tracking information
     $track = Mage::getModel('sales/order_shipment_track');
     $track->setCarrierCode('eparcel');
     $track->setNumber($consignmentNumber);
     $track->setTitle($this->getConfigData('title'));
     $observer->getEvent()->getShipment()->addTrack($track);
     //Mage::log(print_r($track->getData(), true));
     return $this;
 }