creditmemoRepository = $creditmemoRepository; $this->commentRepository = $creditmemoCommentRepository; $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->filterBuilder = $filterBuilder; $this->creditmemoNotifier = $creditmemoNotifier; $this->priceCurrency = $priceCurrency; $this->eventManager = $eventManager; } /** * Cancel an existing creditmemo * * @param int $id Credit Memo Id * @return void * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function cancel($id) { throw new \Magento\Framework\Exception\LocalizedException(__('You can not cancel Credit Memo')); } /** * Returns list of comments attached to creditmemo * * @param int $id * @return \Magento\Sales\Api\Data\CreditmemoCommentSearchResultInterface */ public function getCommentsList($id) { $this->searchCriteriaBuilder->addFilters( [$this->filterBuilder->setField('parent_id')->setValue($id)->setConditionType('eq')->create()] ); $searchCriteria = $this->searchCriteriaBuilder->create(); return $this->commentRepository->getList($searchCriteria); } /** * Notify user * * @param int $id * @return bool */ public function notify($id) { $creditmemo = $this->creditmemoRepository->get($id); return $this->creditmemoNotifier->notify($creditmemo); } /** * Prepare creditmemo to refund and save it. * * @param \Magento\Sales\Api\Data\CreditmemoInterface $creditmemo * @param bool $offlineRequested * @return \Magento\Sales\Api\Data\CreditmemoInterface * @throws \Magento\Framework\Exception\LocalizedException */ public function refund( \Magento\Sales\Api\Data\CreditmemoInterface $creditmemo, $offlineRequested = false ) { $this->validateForRefund($creditmemo); $creditmemo->setState(\Magento\Sales\Model\Order\Creditmemo::STATE_REFUNDED); $connection = $this->getResource()->getConnection('sales'); $connection->beginTransaction(); try { $invoice = $creditmemo->getInvoice(); if ($invoice && !$offlineRequested) { $invoice->setIsUsedForRefund(true); $invoice->setBaseTotalRefunded( $invoice->getBaseTotalRefunded() + $creditmemo->getBaseGrandTotal() ); $creditmemo->setInvoiceId($invoice->getId()); $this->getInvoiceRepository()->save($creditmemo->getInvoice()); } $order = $this->getRefundAdapter()->refund( $creditmemo, $creditmemo->getOrder(), !$offlineRequested ); $this->creditmemoRepository->save($creditmemo); $this->getOrderRepository()->save($order); $connection->commit(); } catch (\Exception $e) { $connection->rollBack(); throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage())); } return $creditmemo; } /** * Validates if credit memo is available for refund. * * @param \Magento\Sales\Api\Data\CreditmemoInterface $creditmemo * @return bool * @throws \Magento\Framework\Exception\LocalizedException */ protected function validateForRefund(\Magento\Sales\Api\Data\CreditmemoInterface $creditmemo) { if ($creditmemo->getId() && $creditmemo->getState() != \Magento\Sales\Model\Order\Creditmemo::STATE_OPEN) { throw new \Magento\Framework\Exception\LocalizedException( __('We cannot register an existing credit memo.') ); } $baseOrderRefund = $this->priceCurrency->round( $creditmemo->getOrder()->getBaseTotalRefunded() + $creditmemo->getBaseGrandTotal() ); if ($baseOrderRefund > $this->priceCurrency->round($creditmemo->getOrder()->getBaseTotalPaid())) { $baseAvailableRefund = $creditmemo->getOrder()->getBaseTotalPaid() - $creditmemo->getOrder()->getBaseTotalRefunded(); throw new \Magento\Framework\Exception\LocalizedException( __( 'The most money available to refund is %1.', $creditmemo->getOrder()->formatPriceTxt($baseAvailableRefund) ) ); } return true; } /** * Initializes RefundAdapterInterface dependency. * * @return \Magento\Sales\Model\Order\RefundAdapterInterface * @deprecated 100.1.3 */ private function getRefundAdapter() { if ($this->refundAdapter === null) { $this->refundAdapter = \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\Sales\Model\Order\RefundAdapterInterface::class); } return $this->refundAdapter; } /** * Initializes ResourceConnection dependency. * * @return \Magento\Framework\App\ResourceConnection|mixed * @deprecated 100.1.3 */ private function getResource() { if ($this->resource === null) { $this->resource = \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\Framework\App\ResourceConnection::class); } return $this->resource; } /** * Initializes OrderRepositoryInterface dependency. * * @return \Magento\Sales\Api\OrderRepositoryInterface * @deprecated 100.1.3 */ private function getOrderRepository() { if ($this->orderRepository === null) { $this->orderRepository = \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\Sales\Api\OrderRepositoryInterface::class); } return $this->orderRepository; } /** * Initializes InvoiceRepositoryInterface dependency. * * @return \Magento\Sales\Api\InvoiceRepositoryInterface * @deprecated 100.1.3 */ private function getInvoiceRepository() { if ($this->invoiceRepository === null) { $this->invoiceRepository = \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\Sales\Api\InvoiceRepositoryInterface::class); } return $this->invoiceRepository; } }