Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
VoidAction.php
Go to the documentation of this file.
1 <?php
8 
10 {
16  public function execute()
17  {
18  $invoice = $this->getInvoice();
19  if (!$invoice) {
21  $resultForward = $this->resultForwardFactory->create();
22  return $resultForward->forward('noroute');
23  }
24  try {
26  $invoiceManagement = $this->_objectManager->get(\Magento\Sales\Api\InvoiceManagementInterface::class);
27  $invoiceManagement->setVoid($invoice->getEntityId());
28 
29  $invoice->getOrder()->setIsInProcess(true);
30  $this->_objectManager->create(
31  \Magento\Framework\DB\Transaction::class
32  )->addObject(
33  $invoice
34  )->addObject(
35  $invoice->getOrder()
36  )->save();
37  $this->messageManager->addSuccessMessage(__('The invoice has been voided.'));
38  } catch (\Magento\Framework\Exception\LocalizedException $e) {
39  $this->messageManager->addErrorMessage($e->getMessage());
40  } catch (\Exception $e) {
41  $this->messageManager->addErrorMessage(__('Invoice voiding error'));
42  }
44  $resultRedirect = $this->resultRedirectFactory->create();
45  $resultRedirect->setPath('sales/*/view', ['invoice_id' => $invoice->getId()]);
46  return $resultRedirect;
47  }
48 }
__()
Definition: __.php:13
$invoice