VoidAction.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Sales\Controller\Adminhtml\Order\Invoice;
  8. class VoidAction extends \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\View
  9. {
  10. /**
  11. * Void invoice action
  12. *
  13. * @return \Magento\Framework\Controller\ResultInterface
  14. */
  15. public function execute()
  16. {
  17. $invoice = $this->getInvoice();
  18. if (!$invoice) {
  19. /** @var \Magento\Backend\Model\View\Result\Forward $resultForward */
  20. $resultForward = $this->resultForwardFactory->create();
  21. return $resultForward->forward('noroute');
  22. }
  23. try {
  24. /** @var \Magento\Sales\Api\InvoiceManagementInterface $invoiceManagement */
  25. $invoiceManagement = $this->_objectManager->get(\Magento\Sales\Api\InvoiceManagementInterface::class);
  26. $invoiceManagement->setVoid($invoice->getEntityId());
  27. $invoice->getOrder()->setIsInProcess(true);
  28. $this->_objectManager->create(
  29. \Magento\Framework\DB\Transaction::class
  30. )->addObject(
  31. $invoice
  32. )->addObject(
  33. $invoice->getOrder()
  34. )->save();
  35. $this->messageManager->addSuccessMessage(__('The invoice has been voided.'));
  36. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  37. $this->messageManager->addErrorMessage($e->getMessage());
  38. } catch (\Exception $e) {
  39. $this->messageManager->addErrorMessage(__('Invoice voiding error'));
  40. }
  41. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  42. $resultRedirect = $this->resultRedirectFactory->create();
  43. $resultRedirect->setPath('sales/*/view', ['invoice_id' => $invoice->getId()]);
  44. return $resultRedirect;
  45. }
  46. }