Esempio n. 1
0
 /**
  * @param \Ess\M2ePro\Model\Order          $order
  * @param \Magento\Sales\Model\Order\Shipment $shipment
  *
  * @throws \LogicException
  *
  * @return array
  */
 private function getItemsToShip(\Ess\M2ePro\Model\Order $order, \Magento\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;
 }
Esempio n. 2
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));
 }
Esempio n. 3
0
 private function performAction($action, $newState)
 {
     $productsAffectedCount = 0;
     $productsDeletedCount = 0;
     $productsExistCount = 0;
     $stockItems = array();
     foreach ($this->order->getItemsCollection()->getItems() as $item) {
         if ($action == self::ACTION_SUB) {
             $qty = $item->getChildObject()->getQtyPurchased();
             $item->setData('qty_reserved', $qty);
         } else {
             $qty = $item->getQtyReserved();
         }
         $products = $this->getItemProductsByAction($item, $action);
         if (count($products) == 0) {
             continue;
         }
         foreach ($products as $key => $productId) {
             /** @var $magentoProduct \Ess\M2ePro\Model\Magento\Product */
             $magentoProduct = $this->modelFactory->getObject('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];
             $this->stockItem->setStockItem($stockItem);
             if (!$this->changeProductQty($magentoProduct, $this->stockItem, $action, $qty)) {
                 if ($action == self::ACTION_SUB) {
                     unset($products[$key]);
                 }
                 continue;
             }
             if ($action == self::ACTION_ADD) {
                 unset($products[$key]);
             }
             $productsAffectedCount++;
             $this->transaction->addObject($this->stockItem->getStockItem());
         }
         $item->setReservedProducts($products);
         $this->transaction->addObject($item);
     }
     unset($stockItems);
     if ($productsExistCount == 0 && $productsDeletedCount == 0) {
         $this->order->setData('reservation_state', self::STATE_UNKNOWN)->save();
         throw new \Ess\M2ePro\Model\Exception\Logic('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 \Ess\M2ePro\Model\Exception\Logic('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));
     }
     if ($productsAffectedCount <= 0) {
         return;
     }
     $this->order->setData('reservation_state', $newState);
     if ($newState == self::STATE_PLACED && !$this->getFlag('order_reservation')) {
         $this->order->setData('reservation_start_date', $this->getHelper('Data')->getCurrentGmtDate());
     }
     $this->transaction->addObject($this->order);
     $this->transaction->save();
 }