Ejemplo n.º 1
0
 private function processOrder(Ess_M2ePro_Model_Order $order, Mage_Sales_Model_Order_Shipment $shipment, array $trackingDetails = array())
 {
     if ($order->isSingle() || !$order->isComponentModeEbay() || count($shipment->getAllItems()) != 1) {
         return $order->getChildObject()->updateShippingStatus($trackingDetails);
     }
     // combined eBay orders only
     $shipmentItems = $shipment->getAllItems();
     $shipmentItem = reset($shipmentItems);
     /** @var $shipmentItem Mage_Sales_Model_Order_Shipment_Item */
     $additionalData = $shipmentItem->getOrderItem()->getAdditionalData();
     $additionalData = is_string($additionalData) ? @unserialize($additionalData) : array();
     if (empty($additionalData['ebay_item_id']) || empty($additionalData['ebay_transaction_id'])) {
         return $order->getChildObject()->updateShippingStatus($trackingDetails);
     }
     $itemToShip = null;
     foreach ($order->getItemsCollection()->getItems() as $item) {
         /** @var $item Ess_M2ePro_Model_Order_Item */
         if ($item->getChildObject()->getItemId() == $additionalData['ebay_item_id'] || $item->getChildObject()->getTransactionId() == $additionalData['ebay_transaction_id']) {
             $itemToShip = $item;
             break;
         }
     }
     if (is_null($itemToShip)) {
         return $order->getChildObject()->updateShippingStatus($trackingDetails);
     }
     $params = array();
     if (count($trackingDetails) > 0) {
         $params['tracking_number'] = $trackingDetails['tracking_number'];
         $params['carrier_code'] = Mage::helper('M2ePro/Component_Ebay')->getCarrierTitle($trackingDetails['carrier_code'], $trackingDetails['carrier_title']);
     }
     /** @var $dispatcher Ess_M2ePro_Model_Connector_Server_Ebay_OrderItem_Dispatcher */
     $dispatcher = Mage::getModel('M2ePro/Connector_Server_Ebay_OrderItem_Dispatcher');
     $action = Ess_M2ePro_Model_Connector_Server_Ebay_OrderItem_Dispatcher::ACTION_UPDATE_STATUS;
     return $dispatcher->process($action, $itemToShip, $params);
 }
Ejemplo n.º 2
0
 /**
  * @param Ess_M2ePro_Model_Order          $order
  * @param Mage_Sales_Model_Order_Shipment $shipment
  *
  * @return Ess_M2ePro_Model_Order_Item[]
  */
 private function getItemsToShip(Ess_M2ePro_Model_Order $order, Mage_Sales_Model_Order_Shipment $shipment)
 {
     $shipmentItems = $shipment->getAllItems();
     $orderItemDataIdentifier = Ess_M2ePro_Helper_Data::CUSTOM_IDENTIFIER;
     $items = array();
     foreach ($shipmentItems as $shipmentItem) {
         $additionalData = $shipmentItem->getOrderItem()->getAdditionalData();
         $additionalData = is_string($additionalData) ? @unserialize($additionalData) : array();
         if (!isset($additionalData[$orderItemDataIdentifier]['items'][0]['order_item_id'])) {
             continue;
         }
         $id = $additionalData[$orderItemDataIdentifier]['items'][0]['order_item_id'];
         $item = $order->getItemsCollection()->getItemByColumnValue('play_order_item_id', $id);
         if (is_null($item)) {
             continue;
         }
         $items[] = $item;
     }
     return $items;
 }
Ejemplo n.º 3
0
 public function getOrdersContainingItemsFromOrder(Ess_M2ePro_Model_Order $order)
 {
     // Prepare item_id-transaction_id pairs for sql
     // -------------
     $readConnection = $this->getReadConnection();
     $whereSql = array();
     foreach ($order->getItemsCollection()->getItems() as $orderItem) {
         $itemIdSql = $readConnection->quoteInto('?', $orderItem->getData('item_id'));
         $transactionIdSql = $readConnection->quoteInto('?', $orderItem->getData('transaction_id'));
         $whereSql[] = "(item_id = {$itemIdSql} AND transaction_id = {$transactionIdSql})";
     }
     $whereSql = implode(' OR ', $whereSql);
     // -------------
     // Find orders which contains at least one order item from current order
     // -------------
     /** @var $collection Mage_Core_Model_Mysql4_Collection_Abstract */
     $collection = Mage::helper('M2ePro/Component_Ebay')->getCollection('Order');
     $collection->getSelect()->distinct(true)->join(array('moi' => Mage::getResourceModel('M2ePro/Order_Item')->getMainTable()), '(`moi`.`order_id` = `main_table`.`id`)', array())->join(array('meoi' => Mage::getResourceModel('M2ePro/Ebay_Order_Item')->getMainTable()), '(`meoi`.`order_item_id` = `moi`.`id`)', array())->where($whereSql)->where('`main_table`.`id` != ?', $order->getId())->where('`main_table`.`account_id` = ?', $order->getAccountId())->order(array('main_table.id ASC'));
     // -------------
     return $collection->getItems();
 }
Ejemplo n.º 4
0
 /**
  * @param Ess_M2ePro_Model_Order          $order
  * @param Mage_Sales_Model_Order_Shipment $shipment
  *
  * @throws LogicException
  *
  * @return array
  */
 private function getItemsToShip(Ess_M2ePro_Model_Order $order, Mage_Sales_Model_Order_Shipment $shipment)
 {
     $shipmentItems = $shipment->getAllItems();
     $orderItemDataIdentifier = Ess_M2ePro_Helper_Data::CUSTOM_IDENTIFIER;
     $items = array();
     foreach ($shipmentItems as $shipmentItem) {
         $additionalData = $shipmentItem->getOrderItem()->getAdditionalData();
         $additionalData = is_string($additionalData) ? @unserialize($additionalData) : array();
         if (!isset($additionalData[$orderItemDataIdentifier]['items'])) {
             continue;
         }
         if (!is_array($additionalData[$orderItemDataIdentifier]['items'])) {
             continue;
         }
         $qtyAvailable = (int) $shipmentItem->getQty();
         foreach ($additionalData[$orderItemDataIdentifier]['items'] as $data) {
             if ($qtyAvailable <= 0) {
                 continue;
             }
             if (!isset($data['order_item_id'])) {
                 continue;
             }
             $item = $order->getItemsCollection()->getItemByColumnValue('amazon_order_item_id', $data['order_item_id']);
             if (is_null($item)) {
                 continue;
             }
             $qty = $item->getChildObject()->getQtyPurchased();
             if ($qty > $qtyAvailable) {
                 $qty = $qtyAvailable;
             }
             $items[] = array('qty' => $qty, 'amazon_order_item_id' => $data['order_item_id']);
             $qtyAvailable -= $qty;
         }
     }
     return $items;
 }
Ejemplo n.º 5
0
 private function clearOrder(Ess_M2ePro_Model_Order $order)
 {
     $order->setMagentoOrder(null);
     $order->setData('magento_order_id', null);
     $order->save();
     $order->getItemsCollection()->walk('setProduct', array(null));
 }
Ejemplo n.º 6
0
 private function performAction($action, $newState)
 {
     /** @var $transaction Mage_Core_Model_Resource_Transaction */
     $transaction = Mage::getModel('core/resource_transaction');
     $productsAffectedCount = 0;
     $productsDeletedCount = 0;
     $productsExistCount = 0;
     $stockItems = array();
     foreach ($this->order->getItemsCollection()->getItems() as $item) {
         $qty = $item->getChildObject()->getQtyPurchased();
         $products = $this->getItemProductsByAction($item, $action);
         if (count($products) == 0) {
             continue;
         }
         foreach ($products as $key => $productId) {
             /** @var $magentoProduct Ess_M2ePro_Model_Magento_Product */
             $magentoProduct = Mage::getModel('M2ePro/Magento_Product')->setStoreId($this->order->getStoreId())->setProductId($productId);
             if (!$magentoProduct->exists()) {
                 $productsDeletedCount++;
                 unset($products[$key]);
                 continue;
             }
             $productsExistCount++;
             if (!isset($stockItems[$productId])) {
                 $stockItems[$productId] = $magentoProduct->getStockItem();
             }
             $stockItem = $stockItems[$productId];
             /** @var $magentoStockItem Ess_M2ePro_Model_Magento_Product_StockItem */
             $magentoStockItem = Mage::getSingleton('M2ePro/Magento_Product_StockItem');
             $magentoStockItem->setStockItem($stockItem);
             if (!$this->changeProductQty($magentoProduct, $magentoStockItem, $action, $qty)) {
                 continue;
             }
             if ($action == self::ACTION_ADD) {
                 unset($products[$key]);
             }
             $productsAffectedCount++;
             if ($this->getFlag('order_reservation')) {
                 $magentoStockItem->getStockItem()->setData(Ess_M2ePro_Helper_Data::CUSTOM_IDENTIFIER . '_order_reservation', true);
             }
             $transaction->addObject($magentoStockItem->getStockItem());
         }
         $item->setReservedProducts($products);
         $transaction->addObject($item);
     }
     unset($stockItems);
     if ($productsExistCount == 0 && $productsDeletedCount == 0) {
         $this->order->setData('reservation_state', self::STATE_UNKNOWN)->save();
         throw new LogicException('The Order Item(s) was not mapped to Magento Product(s) or mapped incorrect.');
     }
     if ($productsExistCount == 0) {
         $this->order->setData('reservation_state', self::STATE_UNKNOWN)->save();
         throw new LogicException('Product(s) does not exist.');
     }
     if ($productsDeletedCount > 0) {
         $this->order->addWarningLog('QTY for %number% product(s) was not changed. Reason: Product(s) does not exist.', array('!number' => $productsDeletedCount));
     }
     $this->order->setData('reservation_state', $newState);
     if ($newState == self::STATE_PLACED && !$this->getFlag('order_reservation')) {
         $this->order->setData('reservation_start_date', Mage::helper('M2ePro')->getCurrentGmtDate());
     }
     $transaction->addObject($this->order);
     $transaction->save();
 }