AddComment.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. use Magento\Backend\App\Action\Context;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Sales\Model\Order\Email\Sender\InvoiceCommentSender;
  11. use Magento\Sales\Model\Order\Invoice;
  12. use Magento\Backend\App\Action;
  13. use Magento\Framework\Registry;
  14. use Magento\Framework\Controller\Result\JsonFactory;
  15. use Magento\Framework\View\Result\PageFactory;
  16. use Magento\Framework\Controller\Result\RawFactory;
  17. class AddComment extends \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\View
  18. {
  19. /**
  20. * @var InvoiceCommentSender
  21. */
  22. protected $invoiceCommentSender;
  23. /**
  24. * @var JsonFactory
  25. */
  26. protected $resultJsonFactory;
  27. /**
  28. * @var PageFactory
  29. */
  30. protected $resultPageFactory;
  31. /**
  32. * @var RawFactory
  33. */
  34. protected $resultRawFactory;
  35. /**
  36. * @param Context $context
  37. * @param Registry $registry
  38. * @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
  39. * @param InvoiceCommentSender $invoiceCommentSender
  40. * @param JsonFactory $resultJsonFactory
  41. * @param PageFactory $resultPageFactory
  42. * @param RawFactory $resultRawFactory
  43. */
  44. public function __construct(
  45. Context $context,
  46. Registry $registry,
  47. \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
  48. InvoiceCommentSender $invoiceCommentSender,
  49. JsonFactory $resultJsonFactory,
  50. PageFactory $resultPageFactory,
  51. RawFactory $resultRawFactory
  52. ) {
  53. $this->invoiceCommentSender = $invoiceCommentSender;
  54. $this->resultJsonFactory = $resultJsonFactory;
  55. $this->resultPageFactory = $resultPageFactory;
  56. $this->resultRawFactory = $resultRawFactory;
  57. parent::__construct($context, $registry, $resultForwardFactory);
  58. }
  59. /**
  60. * Add comment to invoice action
  61. *
  62. * @return \Magento\Framework\Controller\ResultInterface
  63. */
  64. public function execute()
  65. {
  66. try {
  67. $this->getRequest()->setParam('invoice_id', $this->getRequest()->getParam('id'));
  68. $data = $this->getRequest()->getPost('comment');
  69. if (empty($data['comment'])) {
  70. throw new LocalizedException(__('The comment is missing. Enter and try again.'));
  71. }
  72. $invoice = $this->getInvoice();
  73. if (!$invoice) {
  74. /** @var \Magento\Backend\Model\View\Result\Forward $resultForward */
  75. $resultForward = $this->resultForwardFactory->create();
  76. return $resultForward->forward('noroute');
  77. }
  78. $invoice->addComment(
  79. $data['comment'],
  80. isset($data['is_customer_notified']),
  81. isset($data['is_visible_on_front'])
  82. );
  83. $this->invoiceCommentSender->send($invoice, !empty($data['is_customer_notified']), $data['comment']);
  84. $invoice->save();
  85. /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
  86. $resultPage = $this->resultPageFactory->create();
  87. $resultPage->getConfig()->getTitle()->prepend(__('Invoices'));
  88. $response = $resultPage->getLayout()->getBlock('invoice_comments')->toHtml();
  89. } catch (LocalizedException $e) {
  90. $response = ['error' => true, 'message' => $e->getMessage()];
  91. } catch (\Exception $e) {
  92. $response = ['error' => true, 'message' => __('Cannot add new comment.')];
  93. }
  94. if (is_array($response)) {
  95. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  96. $resultJson = $this->resultJsonFactory->create();
  97. $resultJson->setData($response);
  98. return $resultJson;
  99. } else {
  100. /** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
  101. $resultRaw = $this->resultRawFactory->create();
  102. $resultRaw->setContents($response);
  103. return $resultRaw;
  104. }
  105. }
  106. }