private function processOrder(\Ess\M2ePro\Model\Order $order) { if ($order->getChildObject()->isPaymentCompleted()) { // unpaid order became paid // immediately created magento order should be canceled // and new magento order should be created instead if ($order->canCancelMagentoOrder()) { $message = 'Payment Status was updated to Paid on eBay. ' . 'As Magento Order #%order_id% can have wrong data, it have to be cancelled.'; $order->addWarningLog($message, array('!order_id' => $order->getMagentoOrder()->getRealOrderId())); try { $order->cancelMagentoOrder(); } catch (\Exception $e) { // magento order was not cancelled // do not create new magento order to prevent oversell return; } } $this->clearOrder($order); $this->createMagentoOrder($order); } else { // unpaid order did not become paid // immediately created magento order should be canceled // and unpaid item process should be opened for each order item if ($order->canCancelMagentoOrder()) { $message = 'Payment Status was not updated to Paid. Magento Order #%order_id% ' . 'have to be cancelled according to Account\'s Automatic Cancellation Setting.'; $order->addWarningLog($message, array('!order_id' => $order->getMagentoOrder()->getRealOrderId())); try { $order->cancelMagentoOrder(); } catch (\Exception $e) { } } $this->openUnpaidItemProcess($order); } }
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(); }