Skip to content

Commit 5ccd367

Browse files
committed
MC-20193: Invoice Sales Email not send
1 parent d9d4b82 commit 5ccd367

File tree

4 files changed

+98
-8
lines changed

4 files changed

+98
-8
lines changed

app/code/Magento/Quote/Observer/SubmitObserver.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
*/
66
namespace Magento\Quote\Observer;
77

8+
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
89
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
910
use Magento\Framework\Event\ObserverInterface;
11+
use Magento\Sales\Model\Order\Invoice;
1012

1113
class SubmitObserver implements ObserverInterface
1214
{
@@ -20,16 +22,23 @@ class SubmitObserver implements ObserverInterface
2022
*/
2123
private $orderSender;
2224

25+
/**
26+
* @var InvoiceSender
27+
*/
28+
private $invoiceSender;
29+
2330
/**
2431
* @param \Psr\Log\LoggerInterface $logger
2532
* @param OrderSender $orderSender
2633
*/
2734
public function __construct(
2835
\Psr\Log\LoggerInterface $logger,
29-
OrderSender $orderSender
36+
OrderSender $orderSender,
37+
InvoiceSender $invoiceSender
3038
) {
3139
$this->logger = $logger;
3240
$this->orderSender = $orderSender;
41+
$this->invoiceSender = $invoiceSender;
3342
}
3443

3544
/**
@@ -51,6 +60,11 @@ public function execute(\Magento\Framework\Event\Observer $observer)
5160
if (!$redirectUrl && $order->getCanSendNewEmailFlag()) {
5261
try {
5362
$this->orderSender->send($order);
63+
foreach ($order->getInvoiceCollection()->getItems() as $invoice) {
64+
if ($invoice->getState() === Invoice::STATE_PAID) {
65+
$this->invoiceSender->send($invoice);
66+
}
67+
}
5468
} catch (\Exception $e) {
5569
$this->logger->critical($e);
5670
}

app/code/Magento/Quote/Test/Unit/Observer/SubmitObserverTest.php

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
*/
66
namespace Magento\Quote\Test\Unit\Observer;
77

8+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9+
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
10+
use Magento\Sales\Model\Order\Invoice;
11+
use Magento\Sales\Model\ResourceModel\Order\Invoice\Collection as InvoiceCollection;
12+
813
class SubmitObserverTest extends \PHPUnit\Framework\TestCase
914
{
1015
/**
@@ -42,6 +47,21 @@ class SubmitObserverTest extends \PHPUnit\Framework\TestCase
4247
*/
4348
protected $paymentMock;
4449

50+
/**
51+
* @var InvoiceSender|\PHPUnit_Framework_MockObject_MockObject
52+
*/
53+
private $invoiceSenderMock;
54+
55+
/**
56+
* @var Invoice|\PHPUnit_Framework_MockObject_MockObject
57+
*/
58+
private $invoiceMock;
59+
60+
/**
61+
* @var InvoiceCollection|\PHPUnit_Framework_MockObject_MockObject
62+
*/
63+
private $invoiceCollectionMock;
64+
4565
protected function setUp()
4666
{
4767
$this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
@@ -59,9 +79,18 @@ protected function setUp()
5979
$eventMock->expects($this->once())->method('getQuote')->willReturn($this->quoteMock);
6080
$eventMock->expects($this->once())->method('getOrder')->willReturn($this->orderMock);
6181
$this->quoteMock->expects($this->once())->method('getPayment')->willReturn($this->paymentMock);
62-
$this->model = new \Magento\Quote\Observer\SubmitObserver(
63-
$this->loggerMock,
64-
$this->orderSenderMock
82+
$this->invoiceSenderMock = $this->createMock(InvoiceSender::class);
83+
$this->invoiceMock = $this->createMock(Invoice::class);
84+
$this->invoiceCollectionMock = $this->createMock(InvoiceCollection::class);
85+
$objectManager = new ObjectManager($this);
86+
87+
$this->model = $objectManager->getObject(
88+
\Magento\Quote\Observer\SubmitObserver::class,
89+
[
90+
'logger' => $this->loggerMock,
91+
'orderSender' => $this->orderSenderMock,
92+
'invoiceSender' => $this->invoiceSenderMock,
93+
]
6594
);
6695
}
6796

@@ -70,6 +99,10 @@ public function testSendEmail()
7099
$this->paymentMock->expects($this->once())->method('getOrderPlaceRedirectUrl')->willReturn('');
71100
$this->orderMock->expects($this->once())->method('getCanSendNewEmailFlag')->willReturn(true);
72101
$this->orderSenderMock->expects($this->once())->method('send')->willReturn(true);
102+
$this->orderMock->expects($this->once())
103+
->method('getInvoiceCollection')
104+
->willReturn($this->invoiceCollectionMock);
105+
$this->invoiceCollectionMock->expects($this->once())->method('getItems')->willReturn([]);
73106
$this->loggerMock->expects($this->never())->method('critical');
74107
$this->model->execute($this->observerMock);
75108
}
@@ -93,4 +126,38 @@ public function testSendEmailWhenRedirectUrlExists()
93126
$this->loggerMock->expects($this->never())->method('critical');
94127
$this->model->execute($this->observerMock);
95128
}
129+
130+
public function testSendEmailWithPaidInvoice()
131+
{
132+
$this->prepareDataForSendInvoice();
133+
$this->invoiceMock->expects($this->once())->method('getState')->willReturn(Invoice::STATE_PAID);
134+
$this->invoiceSenderMock->expects($this->once())
135+
->method('send')
136+
->with($this->invoiceMock)
137+
->willReturn(true);
138+
$this->loggerMock->expects($this->never())->method('critical');
139+
140+
$this->model->execute($this->observerMock);
141+
}
142+
143+
public function testSendEmailWithNotPaidInvoice()
144+
{
145+
$this->prepareDataForSendInvoice();
146+
$this->invoiceMock->expects($this->once())->method('getState')->willReturn(Invoice::STATE_OPEN);
147+
$this->invoiceSenderMock->expects($this->never())->method('send');
148+
$this->loggerMock->expects($this->never())->method('critical');
149+
150+
$this->model->execute($this->observerMock);
151+
}
152+
153+
private function prepareDataForSendInvoice()
154+
{
155+
$this->paymentMock->expects($this->once())->method('getOrderPlaceRedirectUrl')->willReturn('');
156+
$this->orderMock->expects($this->once())->method('getCanSendNewEmailFlag')->willReturn(true);
157+
$this->orderSenderMock->expects($this->once())->method('send')->willReturn(true);
158+
$this->orderMock->expects($this->once())
159+
->method('getInvoiceCollection')
160+
->willReturn($this->invoiceCollectionMock);
161+
$this->invoiceCollectionMock->expects($this->once())->method('getItems')->willReturn([$this->invoiceMock]);
162+
}
96163
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\Sales\Model\Order\ShipmentFactory;
1717
use Magento\Sales\Model\Order\Invoice;
1818
use Magento\Sales\Model\Service\InvoiceService;
19+
use Magento\Sales\Helper\Data as SalesData;
1920

2021
/**
2122
* Save invoice controller.
@@ -56,28 +57,36 @@ class Save extends \Magento\Backend\App\Action implements HttpPostActionInterfac
5657
*/
5758
private $invoiceService;
5859

60+
/**
61+
* @var SalesData
62+
*/
63+
private $salesData;
64+
5965
/**
6066
* @param Action\Context $context
6167
* @param Registry $registry
6268
* @param InvoiceSender $invoiceSender
6369
* @param ShipmentSender $shipmentSender
6470
* @param ShipmentFactory $shipmentFactory
6571
* @param InvoiceService $invoiceService
72+
* @param SalesData $salesData
6673
*/
6774
public function __construct(
6875
Action\Context $context,
6976
Registry $registry,
7077
InvoiceSender $invoiceSender,
7178
ShipmentSender $shipmentSender,
7279
ShipmentFactory $shipmentFactory,
73-
InvoiceService $invoiceService
80+
InvoiceService $invoiceService,
81+
SalesData $salesData = null
7482
) {
7583
$this->registry = $registry;
7684
$this->invoiceSender = $invoiceSender;
7785
$this->shipmentSender = $shipmentSender;
7886
$this->shipmentFactory = $shipmentFactory;
7987
$this->invoiceService = $invoiceService;
8088
parent::__construct($context);
89+
$this->salesData = $salesData ?? $this->_objectManager->get(SalesData::class);
8190
}
8291

8392
/**
@@ -199,7 +208,7 @@ public function execute()
199208

200209
// send invoice/shipment emails
201210
try {
202-
if (!empty($data['send_email'])) {
211+
if (!empty($data['send_email']) || $this->salesData->canSendNewInvoiceEmail()) {
203212
$this->invoiceSender->send($invoice);
204213
}
205214
} catch (\Exception $e) {
@@ -208,7 +217,7 @@ public function execute()
208217
}
209218
if ($shipment) {
210219
try {
211-
if (!empty($data['send_email'])) {
220+
if (!empty($data['send_email']) || $this->salesData->canSendNewShipmentEmail()) {
212221
$this->shipmentSender->send($shipment);
213222
}
214223
} catch (\Exception $e) {

dev/tests/integration/testsuite/Magento/Sales/Controller/Adminhtml/Order/Invoice/SaveTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class SaveTest extends AbstractInvoiceControllerTest
2828
*/
2929
public function testSendEmailOnInvoiceSave(): void
3030
{
31-
$order = $this->prepareRequest(['invoice' => ['send_email' => true]]);
31+
$order = $this->prepareRequest();
3232
$this->dispatch('backend/sales/order_invoice/save');
3333

3434
$this->assertSessionMessages(

0 commit comments

Comments
 (0)