Example #1
0
 private static function convertToObject($jsonObject, $jsonType)
 {
     $rootObject = null;
     if (preg_match('/Collection\\(([\\\\\\w]+)\\)$/', $jsonType)) {
         $rootObject = new Tritac_ChannelEngineApiClient_Helpers_Collection($jsonType);
         if (is_array($jsonObject)) {
             foreach ($jsonObject as $jsonChildObject) {
                 $rootObject[] = self::convertToObject($jsonChildObject, $rootObject->getTypeName());
             }
         }
     } else {
         $rootObject = new $jsonType();
         if ($jsonObject != null) {
             foreach (get_object_vars($jsonObject) as $key => $value) {
                 if (array_key_exists(lcfirst($key), $rootObject::$typeMap)) {
                     $value = self::convertToObject($value, $rootObject::$typeMap[lcfirst($key)]);
                 }
                 $action = 'set' . $key;
                 if (method_exists($rootObject, $action)) {
                     $rootObject->{$action}($value);
                 }
             }
         }
     }
     return $rootObject;
 }
Example #2
0
 /**
  * Post new shipment to ChannelEngine. This function is set in extension config file.
  *
  * @param Varien_Event_Observer $observer
  * @return bool
  * @throws Exception
  */
 public function saveShipment(Varien_Event_Observer $observer)
 {
     Mage::log('--------------------------------------');
     $event = $observer->getEvent();
     /** @var $_shipment Mage_Sales_Model_Order_Shipment */
     $_shipment = $event->getShipment();
     /** @var $_order Mage_Sales_Model_Order */
     $_order = $_shipment->getOrder();
     $storeId = $_order->getStoreId();
     $ceOrder = Mage::getModel('channelengine/order')->loadByOrderId($_order->getId());
     $ceOrderId = $ceOrder->getChannelOrderId();
     if (!$ceOrderId) {
         return false;
     }
     // Check if the API client was initialized for this order
     if (!isset($this->_client[$storeId])) {
         return false;
     }
     // Initialize new ChannelEngine shipment object
     $ceShipment = new Tritac_ChannelEngineApiClient_Models_Shipment();
     $ceShipment->setOrderId($ceOrderId);
     $ceShipment->setMerchantShipmentNo($_shipment->getId());
     // Set tracking info if available
     $trackingCode = null;
     $trackingCodes = $_shipment->getAllTracks();
     if (count($trackingCodes) > 0) {
         $trackingCode = $trackingCodes[0];
         $ceShipment->setTrackTraceNo($trackingCode->getNumber());
         $ceShipment->setMethod($trackingCode->getTitle());
     }
     // If the shipment is already known to ChannelEngine we will just update it
     $_channelShipment = Mage::getModel('channelengine/shipment')->loadByShipmentId($_shipment->getId());
     if ($_channelShipment->getId() != null) {
         if ($trackingCode != null) {
             Mage::Log("TrackTrace: {$trackingCode->getNumber()}");
         }
         Mage::log("CE Shipment Id: #{$_channelShipment->getChannelengineShipmentId()}");
         $ceShipment->setId($_channelShipment->getChannelengineShipmentId());
         $this->_client[$storeId]->putShipment($ceShipment);
         return true;
     }
     Mage::log('New shipment, continue');
     // Add the shipment lines
     $ceShipmentLines = new Tritac_ChannelEngineApiClient_Helpers_Collection('Tritac_ChannelEngineApiClient_Models_ShipmentLine');
     foreach ($_shipment->getAllItems() as $_shipmentItem) {
         // Get the quantity for this shipment
         $shippedQty = (int) $_shipmentItem->getQty();
         if ($shippedQty == 0) {
             continue;
         }
         // Get the original order item
         $_orderItem = Mage::getModel('sales/order_item')->load($_shipmentItem->getOrderItemId());
         if ($_orderItem == null) {
             continue;
         }
         $ceShipmentLine = new Tritac_ChannelEngineApiClient_Models_ShipmentLine();
         $ceShipmentLine->setOrderLineId($_orderItem->getChannelengineOrderLineId());
         $ceShipmentLine->setQuantity($shippedQty);
         $ceShipmentLine->setStatus(Tritac_ChannelEngineApiClient_Enums_ShipmentLineStatus::SHIPPED);
         $ceShipmentLines->append($ceShipmentLine);
     }
     // Check if there are any shipment lines
     if (count($ceShipmentLines) == 0) {
         return false;
     }
     $ceShipment->setLines($ceShipmentLines);
     // Post shipment to ChannelEngine
     try {
         $result = $this->_client[$storeId]->postShipment($ceShipment);
         if ($result == null) {
             return false;
         }
         $_channelShipment = Mage::getModel('channelengine/shipment')->setShipmentId($_shipment->getId())->setChannelengineShipmentId($result->getId());
         $_channelShipment->save();
         Mage::log("Shipment #{$_shipment->getId()} (CE #{$result->getId()}) was placed successfully.");
     } catch (Exception $e) {
         Mage::getModel('adminnotification/inbox')->addCritical("A shipment (#{$_shipment->getId()}) could not be exported", "Please contact ChannelEngine support at <a href='mailto:support@channelengine.com'>support@channelengine.com</a> or +31(0)71-5288792");
         Mage::logException($e);
     }
     return true;
 }