1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Controller\Adminhtml\Order\Invoice;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Model\Order\Email\Sender\InvoiceCommentSender;
use Magento\Sales\Model\Order\Invoice;
use Magento\Backend\App\Action;
use Magento\Framework\Registry;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Controller\Result\RawFactory;
class AddComment extends \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\View
{
/**
* @var InvoiceCommentSender
*/
protected $invoiceCommentSender;
/**
* @var JsonFactory
*/
protected $resultJsonFactory;
/**
* @var PageFactory
*/
protected $resultPageFactory;
/**
* @var RawFactory
*/
protected $resultRawFactory;
/**
* @param Context $context
* @param Registry $registry
* @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
* @param InvoiceCommentSender $invoiceCommentSender
* @param JsonFactory $resultJsonFactory
* @param PageFactory $resultPageFactory
* @param RawFactory $resultRawFactory
*/
public function __construct(
Context $context,
Registry $registry,
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
InvoiceCommentSender $invoiceCommentSender,
JsonFactory $resultJsonFactory,
PageFactory $resultPageFactory,
RawFactory $resultRawFactory
) {
$this->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;
}
}
}