invoiceCommentSender = $invoiceCommentSender; $this->resultJsonFactory = $resultJsonFactory; $this->resultPageFactory = $resultPageFactory; $this->resultRawFactory = $resultRawFactory; parent::__construct($context, $registry, $resultForwardFactory); } /** * Add comment to invoice action * * @return \Magento\Framework\Controller\ResultInterface */ public function execute() { try { $this->getRequest()->setParam('invoice_id', $this->getRequest()->getParam('id')); $data = $this->getRequest()->getPost('comment'); if (empty($data['comment'])) { throw new LocalizedException(__('The comment is missing. Enter and try again.')); } $invoice = $this->getInvoice(); if (!$invoice) { /** @var \Magento\Backend\Model\View\Result\Forward $resultForward */ $resultForward = $this->resultForwardFactory->create(); return $resultForward->forward('noroute'); } $invoice->addComment( $data['comment'], isset($data['is_customer_notified']), isset($data['is_visible_on_front']) ); $this->invoiceCommentSender->send($invoice, !empty($data['is_customer_notified']), $data['comment']); $invoice->save(); /** @var \Magento\Backend\Model\View\Result\Page $resultPage */ $resultPage = $this->resultPageFactory->create(); $resultPage->getConfig()->getTitle()->prepend(__('Invoices')); $response = $resultPage->getLayout()->getBlock('invoice_comments')->toHtml(); } catch (LocalizedException $e) { $response = ['error' => true, 'message' => $e->getMessage()]; } catch (\Exception $e) { $response = ['error' => true, 'message' => __('Cannot add new comment.')]; } if (is_array($response)) { /** @var \Magento\Framework\Controller\Result\Json $resultJson */ $resultJson = $this->resultJsonFactory->create(); $resultJson->setData($response); return $resultJson; } else { /** @var \Magento\Framework\Controller\Result\Raw $resultRaw */ $resultRaw = $this->resultRawFactory->create(); $resultRaw->setContents($response); return $resultRaw; } } }