Skip to content

Commit 42254a8

Browse files
committed
Merge remote-tracking branch 'origin/AC-12733' into cia-2.4.8-develop-bugfix-03052025
2 parents f5c3fd2 + 38514c7 commit 42254a8

File tree

48 files changed

+1586
-170
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1586
-170
lines changed

app/code/Magento/Sales/Controller/Adminhtml/Order/Creditmemo/AddComment.php

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,34 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Sales\Controller\Adminhtml\Order\Creditmemo;
89

910
use Magento\Backend\App\Action;
11+
use Magento\Backend\App\Action\Context;
12+
use Magento\Framework\App\Action\HttpPostActionInterface;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Framework\Controller\Result\JsonFactory;
15+
use Magento\Framework\Controller\Result\RawFactory;
16+
use Magento\Framework\Exception\LocalizedException;
17+
use Magento\Framework\View\Result\PageFactory;
18+
use Magento\Sales\Controller\Adminhtml\Order\CreditmemoLoader;
1019
use Magento\Sales\Model\Order\Email\Sender\CreditmemoCommentSender;
20+
use Magento\Sales\Model\Order\Creditmemo\Comment as CreditmemoComment;
21+
use Magento\Sales\Model\ResourceModel\Order\Creditmemo\Comment as CreditmemoCommentResource;
1122

12-
class AddComment extends \Magento\Backend\App\Action
23+
class AddComment extends Action implements HttpPostActionInterface
1324
{
1425
/**
1526
* Authorization level of a basic admin session
1627
*
1728
* @see _isAllowed()
1829
*/
19-
const ADMIN_RESOURCE = 'Magento_Sales::sales_creditmemo';
30+
public const ADMIN_RESOURCE = 'Magento_Sales::sales_creditmemo';
2031

2132
/**
22-
* @var \Magento\Sales\Controller\Adminhtml\Order\CreditmemoLoader
33+
* @var CreditmemoLoader
2334
*/
2435
protected $creditmemoLoader;
2536

@@ -29,41 +40,59 @@ class AddComment extends \Magento\Backend\App\Action
2940
protected $creditmemoCommentSender;
3041

3142
/**
32-
* @var \Magento\Framework\View\Result\PageFactory
43+
* @var PageFactory
3344
*/
3445
protected $resultPageFactory;
3546

3647
/**
37-
* @var \Magento\Framework\Controller\Result\JsonFactory
48+
* @var JsonFactory
3849
*/
3950
protected $resultJsonFactory;
4051

4152
/**
42-
* @var \Magento\Framework\Controller\Result\RawFactory
53+
* @var RawFactory
4354
*/
4455
protected $resultRawFactory;
4556

4657
/**
47-
* @param Action\Context $context
48-
* @param \Magento\Sales\Controller\Adminhtml\Order\CreditmemoLoader $creditmemoLoader
58+
* @var CreditmemoComment
59+
*/
60+
private $creditmemoComment;
61+
62+
/**
63+
* @var CreditmemoCommentResource
64+
*/
65+
private $creditmemoCommentResource;
66+
67+
/**
68+
* @param Context $context
69+
* @param CreditmemoLoader $creditmemoLoader
4970
* @param CreditmemoCommentSender $creditmemoCommentSender
50-
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
51-
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
52-
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
71+
* @param PageFactory $resultPageFactory
72+
* @param JsonFactory $resultJsonFactory
73+
* @param RawFactory $resultRawFactory
74+
* @param CreditmemoComment|null $creditmemoComment
75+
* @param CreditmemoCommentResource|null $creditmemoCommentResource
5376
*/
5477
public function __construct(
5578
Action\Context $context,
56-
\Magento\Sales\Controller\Adminhtml\Order\CreditmemoLoader $creditmemoLoader,
79+
CreditmemoLoader $creditmemoLoader,
5780
CreditmemoCommentSender $creditmemoCommentSender,
58-
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
59-
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
60-
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory
81+
PageFactory $resultPageFactory,
82+
JsonFactory $resultJsonFactory,
83+
RawFactory $resultRawFactory,
84+
?CreditmemoComment $creditmemoComment = null,
85+
?CreditmemoCommentResource $creditmemoCommentResource = null
6186
) {
6287
$this->creditmemoLoader = $creditmemoLoader;
6388
$this->creditmemoCommentSender = $creditmemoCommentSender;
6489
$this->resultPageFactory = $resultPageFactory;
6590
$this->resultJsonFactory = $resultJsonFactory;
6691
$this->resultRawFactory = $resultRawFactory;
92+
$this->creditmemoComment = $creditmemoComment ??
93+
ObjectManager::getInstance()->get(CreditmemoComment::class);
94+
$this->creditmemoCommentResource = $creditmemoCommentResource ??
95+
ObjectManager::getInstance()->get(CreditmemoCommentResource::class);
6796
parent::__construct($context);
6897
}
6998

@@ -78,7 +107,7 @@ public function execute()
78107
$this->getRequest()->setParam('creditmemo_id', $this->getRequest()->getParam('id'));
79108
$data = $this->getRequest()->getPost('comment');
80109
if (empty($data['comment'])) {
81-
throw new \Magento\Framework\Exception\LocalizedException(
110+
throw new LocalizedException(
82111
__('The comment is missing. Enter and try again.')
83112
);
84113
}
@@ -87,17 +116,28 @@ public function execute()
87116
$this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
88117
$this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
89118
$creditmemo = $this->creditmemoLoader->load();
90-
$comment = $creditmemo->addComment(
91-
$data['comment'],
92-
isset($data['is_customer_notified']),
93-
isset($data['is_visible_on_front'])
94-
);
95-
$comment->save();
96119

97-
$this->creditmemoCommentSender->send($creditmemo, !empty($data['is_customer_notified']), $data['comment']);
120+
if (empty($data['comment_id'])) {
121+
$comment = $creditmemo->addComment(
122+
$data['comment'],
123+
isset($data['is_customer_notified']),
124+
isset($data['is_visible_on_front'])
125+
);
126+
$this->creditmemoCommentSender->send(
127+
$creditmemo,
128+
!empty($data['is_customer_notified']),
129+
$data['comment']
130+
);
131+
$comment->save();
132+
} else {
133+
$comment = $this->creditmemoComment->setComment($data['comment'])->setId($data['comment_id']);
134+
$comment->setCreditmemo($creditmemo);
135+
$this->creditmemoCommentResource->save($comment);
136+
}
137+
98138
$resultPage = $this->resultPageFactory->create();
99139
$response = $resultPage->getLayout()->getBlock('creditmemo_comments')->toHtml();
100-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
140+
} catch (LocalizedException $e) {
101141
$response = ['error' => true, 'message' => $e->getMessage()];
102142
} catch (\Exception $e) {
103143
$response = ['error' => true, 'message' => __('Cannot add new comment.')];
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Sales\Controller\Adminhtml\Order\Creditmemo;
10+
11+
use Magento\Backend\App\Action\Context;
12+
use Magento\Backend\App\Action;
13+
use Magento\Backend\Model\View\Result\ForwardFactory;
14+
use Magento\Framework\App\Action\HttpPostActionInterface;
15+
use Magento\Framework\Controller\ResultInterface;
16+
17+
/**
18+
* Edit creditmemo comments
19+
*
20+
*/
21+
class EditComment extends Action implements HttpPostActionInterface
22+
{
23+
/**
24+
* Authorization level of a basic admin session
25+
*
26+
* @see _isAllowed()
27+
*/
28+
public const ADMIN_RESOURCE = 'Magento_Sales::sales_creditmemo';
29+
30+
/**
31+
* @var ForwardFactory
32+
*/
33+
private ForwardFactory $resultForwardFactory;
34+
35+
/**
36+
* @param Context $context
37+
* @param ForwardFactory $resultForwardFactory
38+
*/
39+
public function __construct(
40+
Context $context,
41+
ForwardFactory $resultForwardFactory
42+
) {
43+
$this->resultForwardFactory = $resultForwardFactory;
44+
parent::__construct($context);
45+
}
46+
47+
/**
48+
* Edit creditmemo comment
49+
*
50+
* @return ResultInterface
51+
*/
52+
public function execute(): ResultInterface
53+
{
54+
$resultForward = $this->resultForwardFactory->create();
55+
56+
return $resultForward->forward('addComment');
57+
}
58+
}

app/code/Magento/Sales/Controller/Adminhtml/Order/Invoice/AddComment.php

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
declare(strict_types=1);
87

98
namespace Magento\Sales\Controller\Adminhtml\Order\Invoice;
@@ -19,9 +18,13 @@
1918
use Magento\Framework\Controller\Result\JsonFactory;
2019
use Magento\Framework\View\Result\PageFactory;
2120
use Magento\Framework\Controller\Result\RawFactory;
21+
use Magento\Sales\Model\Order\Invoice\Comment as InvoiceComment;
22+
use Magento\Sales\Model\ResourceModel\Order\Invoice\Comment as InvoiceCommentResource;
2223

2324
/**
24-
* Class AddComment
25+
* Add invoice comment
26+
*
27+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2528
*/
2629
class AddComment extends \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\View implements
2730
HttpPostActionInterface
@@ -51,6 +54,16 @@ class AddComment extends \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInv
5154
*/
5255
protected $invoiceRepository;
5356

57+
/**
58+
* @var InvoiceComment
59+
*/
60+
private $invoiceComment;
61+
62+
/**
63+
* @var InvoiceCommentResource
64+
*/
65+
private $invoiceCommentResource;
66+
5467
/**
5568
* @param Context $context
5669
* @param Registry $registry
@@ -59,7 +72,10 @@ class AddComment extends \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInv
5972
* @param JsonFactory $resultJsonFactory
6073
* @param PageFactory $resultPageFactory
6174
* @param RawFactory $resultRawFactory
62-
* @param InvoiceRepositoryInterface $invoiceRepository
75+
* @param InvoiceRepositoryInterface|null $invoiceRepository
76+
* @param InvoiceComment|null $invoiceComment
77+
* @param InvoiceCommentResource|null $invoiceCommentResource
78+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6379
*/
6480
public function __construct(
6581
Context $context,
@@ -69,14 +85,20 @@ public function __construct(
6985
JsonFactory $resultJsonFactory,
7086
PageFactory $resultPageFactory,
7187
RawFactory $resultRawFactory,
72-
?InvoiceRepositoryInterface $invoiceRepository = null
88+
?InvoiceRepositoryInterface $invoiceRepository = null,
89+
?InvoiceComment $invoiceComment = null,
90+
?InvoiceCommentResource $invoiceCommentResource = null
7391
) {
7492
$this->invoiceCommentSender = $invoiceCommentSender;
7593
$this->resultJsonFactory = $resultJsonFactory;
7694
$this->resultPageFactory = $resultPageFactory;
7795
$this->resultRawFactory = $resultRawFactory;
7896
$this->invoiceRepository = $invoiceRepository ?:
7997
ObjectManager::getInstance()->get(InvoiceRepositoryInterface::class);
98+
$this->invoiceComment = $invoiceComment ?? ObjectManager::getInstance()->get(InvoiceComment::class);
99+
$this->invoiceCommentResource = $invoiceCommentResource ??
100+
ObjectManager::getInstance()->get(InvoiceCommentResource::class);
101+
80102
parent::__construct($context, $registry, $resultForwardFactory, $invoiceRepository);
81103
}
82104

@@ -94,19 +116,27 @@ public function execute()
94116
throw new LocalizedException(__('The comment is missing. Enter and try again.'));
95117
}
96118
$invoice = $this->getInvoice();
119+
97120
if (!$invoice) {
98121
/** @var \Magento\Backend\Model\View\Result\Forward $resultForward */
99122
$resultForward = $this->resultForwardFactory->create();
100123
return $resultForward->forward('noroute');
101124
}
102-
$invoice->addComment(
103-
$data['comment'],
104-
isset($data['is_customer_notified']),
105-
isset($data['is_visible_on_front'])
106-
);
107-
108-
$this->invoiceCommentSender->send($invoice, !empty($data['is_customer_notified']), $data['comment']);
109-
$this->invoiceRepository->save($invoice);
125+
126+
if (empty($data['comment_id'])) {
127+
$invoice->addComment(
128+
$data['comment'],
129+
isset($data['is_customer_notified']),
130+
isset($data['is_visible_on_front'])
131+
);
132+
133+
$this->invoiceCommentSender->send($invoice, !empty($data['is_customer_notified']), $data['comment']);
134+
$this->invoiceRepository->save($invoice);
135+
} else {
136+
$comment = $this->invoiceComment->setComment($data['comment'])->setId($data['comment_id']);
137+
$comment->setInvoice($invoice);
138+
$this->invoiceCommentResource->save($comment);
139+
}
110140

111141
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
112142
$resultPage = $this->resultPageFactory->create();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Sales\Controller\Adminhtml\Order\Invoice;
10+
11+
use Magento\Backend\App\Action\Context;
12+
use Magento\Backend\Model\View\Result\ForwardFactory;
13+
use Magento\Framework\App\Action\HttpPostActionInterface;
14+
use Magento\Framework\Controller\ResultInterface;
15+
use Magento\Backend\App\Action;
16+
17+
/**
18+
* Edit invoice comments
19+
*
20+
*/
21+
class EditComment extends Action implements HttpPostActionInterface
22+
{
23+
/**
24+
* Authorization level of a basic admin session
25+
*
26+
* @see _isAllowed()
27+
*/
28+
public const ADMIN_RESOURCE = 'Magento_Sales::sales_invoice';
29+
30+
/**
31+
* @var ForwardFactory
32+
*/
33+
private ForwardFactory $resultForwardFactory;
34+
35+
/**
36+
* @param Context $context
37+
* @param ForwardFactory $resultForwardFactory
38+
*/
39+
public function __construct(
40+
Context $context,
41+
ForwardFactory $resultForwardFactory
42+
) {
43+
$this->resultForwardFactory = $resultForwardFactory;
44+
parent::__construct($context);
45+
}
46+
47+
/**
48+
* Edit invoice comment
49+
*
50+
* @return ResultInterface
51+
*/
52+
public function execute(): ResultInterface
53+
{
54+
$resultForward = $this->resultForwardFactory->create();
55+
return $resultForward->forward('addComment');
56+
}
57+
}

0 commit comments

Comments
 (0)