Skip to content

Commit 8fd0bc5

Browse files
committed
Merge branch 'ACP2E-31' of https://github.com/magento-l3/magento2ce into L3-PR-20211001
2 parents 096522e + af80381 commit 8fd0bc5

File tree

9 files changed

+184
-30
lines changed

9 files changed

+184
-30
lines changed

app/code/Magento/Sales/Block/Adminhtml/Order/Invoice/Totals.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Sales\Block\Adminhtml\Order\Invoice;
77

8+
use Magento\Framework\DataObject;
89
use Magento\Sales\Model\Order\Invoice;
910

1011
/**

app/code/Magento/Sales/Block/Adminhtml/Totals.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,17 @@ protected function _initTotals()
9191
* Add shipping
9292
*/
9393
if (!$order->getIsVirtual()
94-
&& ((double)$order->getShippingAmount()
94+
&& ($order->getShippingAmount() !== null
9595
|| $order->getShippingDescription())
9696
) {
9797
$shippingLabel = __('Shipping & Handling');
9898

99-
if ($order->getCouponCode() && !isset($this->_totals['discount'])) {
100-
$shippingLabel .= " ({$order->getCouponCode()})";
99+
if (!isset($this->_totals['discount'])) {
100+
if ($order->getCouponCode()) {
101+
$shippingLabel .= " ({$order->getCouponCode()})";
102+
} elseif ($order->getDiscountDescription()) {
103+
$shippingLabel .= " ({$order->getDiscountDescription()})";
104+
}
101105
}
102106

103107
$this->_totals['shipping'] = new DataObject(

app/code/Magento/Sales/Block/Order/Invoice/Totals.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function __construct(
3333
protected $_invoice = null;
3434

3535
/**
36+
* Get order
37+
*
3638
* @return Order
3739
*/
3840
public function getInvoice()
@@ -50,6 +52,8 @@ public function getInvoice()
5052
}
5153

5254
/**
55+
* Set Order
56+
*
5357
* @param Order $invoice
5458
* @return $this
5559
*/

app/code/Magento/Sales/Block/Order/Totals.php

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Sales\Block\Order;
77

8+
use Magento\Framework\DataObject;
89
use Magento\Sales\Model\Order;
910

1011
/**
@@ -141,24 +142,7 @@ protected function _initTotals()
141142
);
142143
}
143144

144-
/**
145-
* Add shipping
146-
*/
147-
if (!$source->getIsVirtual() && ((double)$source->getShippingAmount() || $source->getShippingDescription())) {
148-
$label = __('Shipping & Handling');
149-
if ($this->getSource()->getCouponCode() && !isset($this->_totals['discount'])) {
150-
$label = __('Shipping & Handling (%1)', $this->getSource()->getCouponCode());
151-
}
152-
153-
$this->_totals['shipping'] = new \Magento\Framework\DataObject(
154-
[
155-
'code' => 'shipping',
156-
'field' => 'shipping_amount',
157-
'value' => $this->getSource()->getShippingAmount(),
158-
'label' => $label,
159-
]
160-
);
161-
}
145+
$this->addShippingTotal($source);
162146

163147
$this->_totals['grand_total'] = new \Magento\Framework\DataObject(
164148
[
@@ -186,6 +170,38 @@ protected function _initTotals()
186170
return $this;
187171
}
188172

173+
/**
174+
* Add shipping total
175+
*
176+
* @param Order|Order\Invoice $source
177+
* @retrurn void
178+
*/
179+
private function addShippingTotal($source)
180+
{
181+
if (!$source->getIsVirtual()
182+
&& ($source->getShippingAmount() !== null
183+
|| $source->getShippingDescription())
184+
) {
185+
$shippingLabel = __('Shipping & Handling');
186+
187+
if (!isset($this->_totals['discount'])) {
188+
if ($source->getCouponCode()) {
189+
$shippingLabel .= " ({$source->getCouponCode()})";
190+
} elseif ($source->getDiscountDescription()) {
191+
$shippingLabel .= " ({$source->getDiscountDescription()})";
192+
}
193+
}
194+
$this->_totals['shipping'] = new DataObject(
195+
[
196+
'code' => 'shipping',
197+
'field' => 'shipping_amount',
198+
'value' => $source->getShippingAmount(),
199+
'label' => $shippingLabel,
200+
]
201+
);
202+
}
203+
}
204+
189205
/**
190206
* Add new total to totals array after specific total or before last total by default
191207
*

app/code/Magento/Sales/Model/Order/Invoice/Total/Shipping.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
class Shipping extends AbstractTotal
1414
{
1515
/**
16+
* Collect shipping total
17+
*
1618
* @param \Magento\Sales\Model\Order\Invoice $invoice
1719
* @return $this
1820
*/
1921
public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
2022
{
21-
$invoice->setShippingAmount(0);
22-
$invoice->setBaseShippingAmount(0);
2323
$orderShippingAmount = $invoice->getOrder()->getShippingAmount();
2424
$baseOrderShippingAmount = $invoice->getOrder()->getBaseShippingAmount();
2525
$shippingInclTax = $invoice->getOrder()->getShippingInclTax();
@@ -29,7 +29,7 @@ public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
2929
* Check shipping amount in previous invoices
3030
*/
3131
foreach ($invoice->getOrder()->getInvoiceCollection() as $previousInvoice) {
32-
if ((double)$previousInvoice->getShippingAmount() && !$previousInvoice->isCanceled()) {
32+
if ($previousInvoice->getShippingAmount() !== null && !$previousInvoice->isCanceled()) {
3333
return $this;
3434
}
3535
}

app/code/Magento/Sales/Test/Unit/Model/Order/Invoice/Total/ShippingTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ protected function setUp(): void
4444
public function testCollectWithNoOrZeroPrevInvoice(array $prevInvoicesData, $orderShipping, $expectedShipping)
4545
{
4646
$invoice = $this->createInvoiceStub($prevInvoicesData, $orderShipping);
47-
$invoice->expects($this->exactly(2))
47+
$invoice->expects($this->once())
4848
->method('setShippingAmount')
49-
->withConsecutive([0], [$expectedShipping]);
49+
->with($expectedShipping);
5050

5151
$this->total->collect($invoice);
5252
}
@@ -63,7 +63,7 @@ public static function collectWithNoOrZeroPrevInvoiceDataProvider()
6363
'expectedShipping' => 10.00,
6464
],
6565
'zero shipping in previous invoices' => [
66-
'prevInvoicesData' => [['shipping_amount' => '0.0000']],
66+
'prevInvoicesData' => [['shipping_amount' => null]],
6767
'orderShipping' => 10.00,
6868
'expectedShipping' => 10.00,
6969
],
@@ -75,9 +75,8 @@ public function testCollectWithPreviousInvoice()
7575
$orderShipping = 10.00;
7676
$prevInvoicesData = [['shipping_amount' => '10.000']];
7777
$invoice = $this->createInvoiceStub($prevInvoicesData, $orderShipping);
78-
$invoice->expects($this->once())
79-
->method('setShippingAmount')
80-
->with(0);
78+
$invoice->expects($this->never())
79+
->method('setShippingAmount');
8180

8281
$this->total->collect($invoice);
8382
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Block\Adminhtml\Order\Invoice;
9+
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Framework\Registry;
12+
use Magento\Framework\View\LayoutInterface;
13+
use Magento\Sales\Model\OrderFactory;
14+
use Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* @magentoAppArea adminhtml
20+
* @magentoDbIsolation enabled
21+
*/
22+
class TotalsTest extends TestCase
23+
{
24+
/** @var ObjectManagerInterface */
25+
private $objectManager;
26+
27+
/** @var Totals */
28+
private $block;
29+
30+
/** @var OrderFactory */
31+
private $orderFactory;
32+
33+
/** @var Registry */
34+
private $registry;
35+
36+
/** @var CollectionFactory */
37+
private $invoiceCollectionFactory;
38+
39+
/**
40+
* @inheritdoc
41+
*/
42+
protected function setUp(): void
43+
{
44+
parent::setUp();
45+
46+
$this->objectManager = Bootstrap::getObjectManager();
47+
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Totals::class);
48+
$this->orderFactory = $this->objectManager->get(OrderFactory::class);
49+
$this->registry = $this->objectManager->get(Registry::class);
50+
$this->invoiceCollectionFactory = $this->objectManager->get(CollectionFactory::class);
51+
}
52+
53+
/**
54+
* @inheritdoc
55+
*/
56+
protected function tearDown(): void
57+
{
58+
$this->registry->unregister('current_invoice');
59+
60+
parent::tearDown();
61+
}
62+
63+
/**
64+
* @magentoDataFixture Magento/Sales/_files/order_with_free_shipping_by_coupon_and_invoice.php
65+
*
66+
* @return void
67+
*/
68+
public function testCollectTotals(): void
69+
{
70+
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
71+
$invoice = $this->invoiceCollectionFactory->create()->setOrderFilter($order)->setPageSize(1)->getFirstItem();
72+
$this->registry->unregister('current_invoice');
73+
$this->registry->register('current_invoice', $invoice);
74+
75+
$this->block->toHtml();
76+
$totals = $this->block->getTotals();
77+
$this->assertArrayHasKey('shipping', $totals);
78+
$this->assertEquals('0.0000', $totals['shipping']['value']);
79+
$this->assertEquals('Shipping & Handling (1234567890)', $totals['shipping']['label']);
80+
}
81+
}

dev/tests/integration/testsuite/Magento/Sales/Block/Order/Invoice/TotalsTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@ protected function setUp(): void
4444
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
4545
}
4646

47+
/**
48+
* @magentoDataFixture Magento/Sales/_files/order_with_free_shipping_by_coupon_and_invoice.php
49+
*
50+
* @return void
51+
*/
52+
public function testCollectTotals(): void
53+
{
54+
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
55+
$invoice = $order->getInvoiceCollection()->getFirstItem();
56+
57+
$this->block->setOrder($order);
58+
$this->block->setInvoice($invoice);
59+
60+
$this->block->toHtml();
61+
$totals = $this->block->getTotals();
62+
$this->assertArrayHasKey('shipping', $totals);
63+
$this->assertEquals('0.0000', $totals['shipping']['value']);
64+
$this->assertEquals('Shipping & Handling (1234567890)', $totals['shipping']['label']);
65+
}
4766
/**
4867
* @magentoDataFixture Magento/Sales/_files/invoices_for_items.php
4968
*
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\Sales\Api\Data\OrderInterfaceFactory;
10+
use Magento\Sales\Api\OrderRepositoryInterface;
11+
use Magento\Sales\Model\Order;
12+
use Magento\Sales\Model\Order\Item as OrderItem;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
15+
16+
Resolver::getInstance()->requireDataFixture('Magento/Sales/_files/order_with_free_shipping_by_coupon.php');
17+
18+
$objectManager = Bootstrap::getObjectManager();
19+
/** @var \Magento\Sales\Model\Order $order */
20+
$order = $objectManager->get(OrderInterfaceFactory::class)->create()->loadByIncrementId('100000001');
21+
$orderService = $objectManager->create(
22+
\Magento\Sales\Api\InvoiceManagementInterface::class
23+
);
24+
$invoice = $orderService->prepareInvoice($order);
25+
$invoice->register();
26+
$order = $invoice->getOrder();
27+
$order->setIsInProcess(true);
28+
$transactionSave = $objectManager
29+
->create(\Magento\Framework\DB\Transaction::class);
30+
$transactionSave->addObject($invoice)->addObject($order)->save();

0 commit comments

Comments
 (0)