/** * Get options * * @return array */ public function toOptionArray() { if ($this->options === null) { $this->options = []; /** @var \Magento\Framework\Phrase $state */ foreach ($this->creditmemoFactory->create()->getStates() as $id => $state) { $this->options[] = ['value' => $id, 'label' => $state->render()]; } } return $this->options; }
public function testLoadByOrder() { $orderId = 1234; $invoiceId = 99; $qty = 1; $data = ['items' => [1 => ['qty' => $qty, 'back_to_stock' => true]]]; $this->loader->setCreditmemoId(0); $this->loader->setOrderId($orderId); $this->loader->setCreditmemo($data); $this->loader->setInvoiceId($invoiceId); $orderMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->setMethods([])->getMock(); $orderMock->expects($this->once())->method('load')->willReturnSelf(); $orderMock->expects($this->once())->method('getId')->willReturn($orderId); $orderMock->expects($this->once())->method('canCreditmemo')->willReturn(true); $this->orderFactoryMock->expects($this->once())->method('create')->willReturn($orderMock); $invoiceMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Invoice')->disableOriginalConstructor()->getMock(); $invoiceMock->expects($this->any())->method('setOrder')->willReturnSelf(); $invoiceMock->expects($this->any())->method('getId')->willReturn(1); $this->invoiceRepositoryMock->expects($this->once())->method('get')->willReturn($invoiceMock); $creditmemoMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Creditmemo')->disableOriginalConstructor()->setMethods([])->getMock(); $orderItemMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Item')->disableOriginalConstructor()->setMethods([])->getMock(); $creditmemoItemMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Creditmemo\\Item')->disableOriginalConstructor()->setMethods([])->getMock(); $creditmemoItemMock->expects($this->any())->method('getOrderItem')->willReturn($orderItemMock); $items = [$creditmemoItemMock, $creditmemoItemMock, $creditmemoItemMock]; $creditmemoMock->expects($this->any())->method('getAllItems')->willReturn($items); $data['qtys'] = [1 => $qty]; $this->creditmemoFactoryMock->expects($this->any())->method('createByInvoice')->with($invoiceMock, $data)->willReturn($creditmemoMock); $this->assertEquals($creditmemoMock, $this->loader->load()); }
/** * Convert order object to creditmemo * * @param \Magento\Sales\Model\Order $order * @return \Magento\Sales\Model\Order\Creditmemo */ public function toCreditmemo(\Magento\Sales\Model\Order $order) { $creditmemo = $this->_creditmemoFactory->create(); $creditmemo->setOrder($order)->setStoreId($order->getStoreId())->setCustomerId($order->getCustomerId())->setBillingAddressId($order->getBillingAddressId())->setShippingAddressId($order->getShippingAddressId()); $this->_objectCopyService->copyFieldsetToTarget('sales_convert_order', 'to_cm', $order, $creditmemo); return $creditmemo; }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testRegisterRefundNotification() { $message = 'Registered notification about refunded amount of . Transaction ID: "' . self::TRANSACTION_ID . '-refund"'; $amount = 50; $grandTotalCreditMemo = 50; $invoiceBaseGrandTotal = 50; $invoiceBaseTotalRefunded = 0; $this->invoiceMock->expects($this->any())->method('getBaseGrandTotal')->willReturn($invoiceBaseGrandTotal); $this->invoiceMock->expects($this->any())->method('getBaseTotalRefunded')->willReturn($invoiceBaseTotalRefunded); $this->creditMemoMock->expects($this->any())->method('getGrandTotal')->willReturn($grandTotalCreditMemo); $this->payment->setParentTransactionId($this->transactionId); $this->mockInvoice($this->transactionId, 1); $this->creditmemoFactoryMock->expects($this->once())->method('createByInvoice')->with($this->invoiceMock, ['adjustment_negative' => $invoiceBaseGrandTotal - $amount])->willReturn($this->creditMemoMock); $this->creditMemoMock->expects($this->once())->method('setPaymentRefundDisallowed')->willReturnSelf(); $this->creditMemoMock->expects($this->once())->method('setAutomaticallyCreated')->willReturnSelf(); $this->creditMemoMock->expects($this->once())->method('register')->willReturnSelf(); $this->creditMemoMock->expects($this->once())->method('addComment')->willReturnSelf(); $this->creditMemoMock->expects($this->once())->method('save')->willReturnSelf(); $this->orderMock->expects($this->once())->method('getBaseCurrency')->willReturn($this->currencyMock); $parentTransaction = $this->getMock('Magento\\Sales\\Model\\Order\\Payment\\Transaction', ['setOrderId', 'setPaymentId', 'loadByTxnId', 'getId', 'getTxnId', 'setTxnId', 'getTxnType'], [], '', false); $newTransactionId = $this->transactionId . '-' . Transaction::TYPE_REFUND; $this->transactionRepositoryMock->expects($this->once())->method('getByTransactionId')->with($this->transactionId)->willReturn($parentTransaction); $this->transactionManagerMock->expects($this->once())->method('isTransactionExists')->with($newTransactionId)->willReturn(false); $this->transactionManagerMock->expects($this->once())->method('generateTransactionId')->with($this->payment, Transaction::TYPE_REFUND, $parentTransaction)->willReturn($newTransactionId); $status = 'status'; $this->mockGetDefaultStatus(Order::STATE_PROCESSING, $status); $this->assertOrderUpdated(Order::STATE_PROCESSING, $status, $message); $additionalInformation = []; $failSafe = false; $transactionType = Transaction::TYPE_REFUND; $this->getTransactionBuilderMock($additionalInformation, $failSafe, $transactionType, $this->transactionId . '-' . Transaction::TYPE_REFUND); $this->assertSame($this->payment, $this->payment->registerRefundNotification($amount)); $this->assertSame($this->creditMemoMock, $this->payment->getData('created_creditmemo')); $this->assertEquals($grandTotalCreditMemo, $this->payment->getData('amount_refunded')); }
/** * Initialize creditmemo model instance * * @return \Magento\Sales\Model\Order\Creditmemo|false */ public function load() { $creditmemo = false; $creditmemoId = $this->getCreditmemoId(); $orderId = $this->getOrderId(); if ($creditmemoId) { $creditmemo = $this->creditmemoFactory->create()->load($creditmemoId); } elseif ($orderId) { $data = $this->getCreditmemo(); $order = $this->orderFactory->create()->load($orderId); $invoice = $this->_initInvoice($order); if (!$this->_canCreditmemo($order)) { return false; } $savedData = $this->_getItemData(); $qtys = array(); $backToStock = array(); foreach ($savedData as $orderItemId => $itemData) { if (isset($itemData['qty'])) { $qtys[$orderItemId] = $itemData['qty']; } if (isset($itemData['back_to_stock'])) { $backToStock[$orderItemId] = true; } } $data['qtys'] = $qtys; $service = $this->orderServiceFactory->create(array('order' => $order)); if ($invoice) { $creditmemo = $service->prepareInvoiceCreditmemo($invoice, $data); } else { $creditmemo = $service->prepareCreditmemo($data); } /** * Process back to stock flags */ foreach ($creditmemo->getAllItems() as $creditmemoItem) { $orderItem = $creditmemoItem->getOrderItem(); $parentId = $orderItem->getParentItemId(); if (isset($backToStock[$orderItem->getId()])) { $creditmemoItem->setBackToStock(true); } elseif ($orderItem->getParentItem() && isset($backToStock[$parentId]) && $backToStock[$parentId]) { $creditmemoItem->setBackToStock(true); } elseif (empty($savedData)) { $creditmemoItem->setBackToStock($this->inventoryHelper->isAutoReturnEnabled()); } else { $creditmemoItem->setBackToStock(false); } } } $this->eventManager->dispatch('adminhtml_sales_order_creditmemo_register_before', array('creditmemo' => $creditmemo, 'input' => $this->getCreditmemo())); $this->registry->register('current_creditmemo', $creditmemo); return $creditmemo; }
/** * Prepare credit memo * * @param $amount * @param $baseGrandTotal * @param false|Invoice $invoice * @return mixed */ protected function prepareCreditMemo($amount, $baseGrandTotal, $invoice) { $entity = $invoice ?: $this->getOrder(); if ($entity->getBaseTotalRefunded() > 0) { $adjustment = ['adjustment_positive' => $amount]; } else { $adjustment = ['adjustment_negative' => $baseGrandTotal - $amount]; } if ($invoice) { $creditMemo = $this->creditmemoFactory->createByInvoice($invoice, $adjustment); } else { $creditMemo = $this->creditmemoFactory->createByOrder($this->getOrder(), $adjustment); } if ($creditMemo) { $totalRefunded = $entity->getBaseTotalRefunded() + $creditMemo->getBaseGrandTotal(); $this->setShouldCloseParentTransaction($entity->getBaseGrandTotal() <= $totalRefunded); } return $creditMemo; }
/** * Return option array * * @return array */ public function toOptionArray() { return $this->creditmemoFactory->create()->getStates(); }
/** * Constructor * * @param ContextInterface $context * @param UiComponentFactory $uiComponentFactory * @param CreditmemoFactory $creditmemoFactory * @param array $components * @param array $data */ public function __construct(ContextInterface $context, UiComponentFactory $uiComponentFactory, CreditmemoFactory $creditmemoFactory, array $components = [], array $data = []) { $this->states = $creditmemoFactory->create()->getStates(); parent::__construct($context, $uiComponentFactory, $components, $data); }