Skip to content

Commit 8f618c0

Browse files
committed
Merge remote-tracking branch 'l3/ACP2E-314' into PR_L3_05_04_2022
2 parents 52ac954 + eca6736 commit 8f618c0

File tree

5 files changed

+106
-18
lines changed

5 files changed

+106
-18
lines changed

app/code/Magento/SalesRule/Helper/CartFixedDiscount.php

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77

88
namespace Magento\SalesRule\Helper;
99

10+
use Magento\Framework\App\Config\ScopeConfigInterface;
1011
use Magento\Framework\Pricing\PriceCurrencyInterface;
1112
use Magento\Quote\Api\Data\AddressInterface;
13+
use Magento\Quote\Model\Cart\ShippingMethodConverter;
1214
use Magento\Quote\Model\Quote;
1315
use Magento\SalesRule\Model\DeltaPriceRound;
1416
use Magento\SalesRule\Model\Rule;
17+
use Magento\Store\Model\ScopeInterface;
1518

1619
/**
1720
* Helper for CartFixed Available Discount and Quote Totals
@@ -28,28 +31,45 @@ class CartFixedDiscount
2831
*/
2932
private $priceCurrency;
3033

34+
/**
35+
* @var ShippingMethodConverter
36+
*/
37+
private $shippingMethodConverter = null;
38+
39+
/**
40+
* @var ScopeConfigInterface
41+
*/
42+
private $scopeConfig = null;
43+
3144
/**
3245
* @param DeltaPriceRound $deltaPriceRound
3346
* @param PriceCurrencyInterface $priceCurrency
47+
* @param ShippingMethodConverter $shippingMethodConverter
48+
* @param ScopeConfigInterface $scopeConfig
3449
*/
3550
public function __construct(
3651
DeltaPriceRound $deltaPriceRound,
37-
PriceCurrencyInterface $priceCurrency
52+
PriceCurrencyInterface $priceCurrency,
53+
ShippingMethodConverter $shippingMethodConverter,
54+
ScopeConfigInterface $scopeConfig
3855
) {
3956
$this->deltaPriceRound = $deltaPriceRound;
4057
$this->priceCurrency = $priceCurrency;
58+
$this->shippingMethodConverter = $shippingMethodConverter;
59+
$this->scopeConfig = $scopeConfig;
4160
}
4261

4362
/**
4463
* Retrieve shipping amount by quote address and shipping method
4564
*
4665
* @param AddressInterface $address
66+
* @param float $shippingAmount
4767
* @return float
4868
*/
4969
public function calculateShippingAmountWhenAppliedToShipping(
50-
AddressInterface $address
70+
AddressInterface $address,
71+
float $shippingAmount
5172
): float {
52-
$shippingAmount = (float) $address->getShippingAmount();
5373
if ($shippingAmount == 0.0) {
5474
$addressQty = $this->getAddressQty($address);
5575
$address->setItemQty($addressQty);
@@ -59,11 +79,16 @@ public function calculateShippingAmountWhenAppliedToShipping(
5979
foreach ($shippingRates as $shippingRate) {
6080
if ($shippingRate->getCode() === $address->getShippingMethod()
6181
) {
62-
$shippingAmount = (float) $shippingRate->getPrice();
82+
$shippingMethod = $this->shippingMethodConverter
83+
->modelToDataObject($shippingRate, $address->getQuote()->getQuoteCurrencyCode());
84+
$shippingAmount = $this->applyDiscountOnPricesIncludedTax()
85+
? $shippingMethod->getPriceInclTax()
86+
: $shippingMethod->getPriceExclTax();
6387
break;
6488
}
6589
}
6690
}
91+
6792
return $shippingAmount;
6893
}
6994

@@ -180,14 +205,17 @@ public function getQuoteTotalsForMultiShipping(Quote $quote): float
180205
*
181206
* @param Quote\Address $address
182207
* @param float $baseRuleTotals
208+
* @param float $shippingAmount
183209
* @return float
184210
*/
185211
public function getQuoteTotalsForRegularShipping(
186212
Quote\Address $address,
187-
float $baseRuleTotals
213+
float $baseRuleTotals,
214+
float $shippingAmount
188215
): float {
189216
$baseRuleTotals += $this->calculateShippingAmountWhenAppliedToShipping(
190-
$address
217+
$address,
218+
$shippingAmount
191219
);
192220
return $baseRuleTotals;
193221
}
@@ -200,19 +228,21 @@ public function getQuoteTotalsForRegularShipping(
200228
* @param bool $isMultiShipping
201229
* @param Quote\Address $address
202230
* @param float $baseRuleTotals
231+
* @param float $shippingAmount
203232
* @return float
204233
*/
205234
public function getBaseRuleTotals(
206235
int $isAppliedToShipping,
207236
Quote $quote,
208237
bool $isMultiShipping,
209238
Quote\Address $address,
210-
float $baseRuleTotals
239+
float $baseRuleTotals,
240+
float $shippingAmount
211241
): float {
212242
if ($isAppliedToShipping) {
213243
$baseRuleTotals = ($quote->getIsMultiShipping() && $isMultiShipping) ?
214244
$this->getQuoteTotalsForMultiShipping($quote) :
215-
$this->getQuoteTotalsForRegularShipping($address, $baseRuleTotals);
245+
$this->getQuoteTotalsForRegularShipping($address, $baseRuleTotals, $shippingAmount);
216246
}
217247
return (float) $baseRuleTotals;
218248
}
@@ -244,6 +274,19 @@ public function getAvailableDiscountAmount(
244274
return $availableDiscountAmount;
245275
}
246276

277+
/**
278+
* Get configuration setting "Apply Discount On Prices Including Tax" value
279+
*
280+
* @return bool
281+
*/
282+
public function applyDiscountOnPricesIncludedTax(): bool
283+
{
284+
return (bool) $this->scopeConfig->getValue(
285+
'tax/calculation/discount_tax',
286+
ScopeInterface::SCOPE_STORE
287+
) ?? false;
288+
}
289+
247290
/**
248291
* Get address quantity.
249292
*

app/code/Magento/SalesRule/Model/Rule/Action/Discount/CartFixed.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function __construct(
7070
* @return Data
7171
* @throws LocalizedException
7272
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
73+
* @SuppressWarnings(PHPMD.NPathComplexity)
7374
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
7475
*/
7576
public function calculate($rule, $item, $qty)
@@ -104,14 +105,18 @@ public function calculate($rule, $item, $qty)
104105

105106
if ($availableDiscountAmount > 0) {
106107
$store = $quote->getStore();
108+
$shippingPrice = $this->cartFixedDiscountHelper->applyDiscountOnPricesIncludedTax()
109+
? (float) $address->getShippingInclTax()
110+
: (float) $address->getShippingExclTax();
107111
$baseRuleTotals = $shippingMethod ?
108112
$this->cartFixedDiscountHelper
109113
->getBaseRuleTotals(
110114
$isAppliedToShipping,
111115
$quote,
112116
$isMultiShipping,
113117
$address,
114-
$baseRuleTotals
118+
$baseRuleTotals,
119+
$shippingPrice
115120
) : $baseRuleTotals;
116121
if ($isAppliedToShipping) {
117122
$baseDiscountAmount = $this->cartFixedDiscountHelper

app/code/Magento/SalesRule/Model/Validator.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,25 +370,27 @@ public function processShippingAmount(Address $address)
370370
$cartRules[$rule->getId()] = $rule->getDiscountAmount();
371371
}
372372
if ($cartRules[$rule->getId()] > 0) {
373-
$shippingAmount = $address->getShippingAmount() - $address->getShippingDiscountAmount();
373+
$shippingQuoteAmount = (float) $address->getShippingAmount()
374+
- (float) $address->getShippingDiscountAmount();
374375
$quoteBaseSubtotal = (float) $quote->getBaseSubtotal();
375376
$isMultiShipping = $this->cartFixedDiscountHelper->checkMultiShippingQuote($quote);
376377
if ($isAppliedToShipping) {
377378
$quoteBaseSubtotal = ($quote->getIsMultiShipping() && $isMultiShipping) ?
378379
$this->cartFixedDiscountHelper->getQuoteTotalsForMultiShipping($quote) :
379380
$this->cartFixedDiscountHelper->getQuoteTotalsForRegularShipping(
380381
$address,
381-
$quoteBaseSubtotal
382+
$quoteBaseSubtotal,
383+
$shippingQuoteAmount
382384
);
383385
$discountAmount = $this->cartFixedDiscountHelper->
384386
getShippingDiscountAmount(
385387
$rule,
386-
$shippingAmount,
388+
$shippingQuoteAmount,
387389
$quoteBaseSubtotal
388390
);
389391
$baseDiscountAmount = $discountAmount;
390392
} else {
391-
$discountAmount = min($shippingAmount, $quoteAmount);
393+
$discountAmount = min($shippingQuoteAmount, $quoteAmount);
392394
$baseDiscountAmount = min(
393395
$baseShippingAmount - $address->getBaseShippingDiscountAmount(),
394396
$cartRules[$rule->getId()]

app/code/Magento/SalesRule/Test/Unit/Model/Rule/Action/Discount/CartFixedTest.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ protected function setUp(): void
9898
->onlyMethods(['getStore', 'getExtensionAttributes', 'isVirtual'])
9999
->disableOriginalConstructor()
100100
->getMock();
101-
$this->address = $this->createPartialMock(
102-
Address::class,
103-
['getShippingMethod']
104-
);
101+
$this->address = $this->getMockBuilder(Address::class)
102+
->onlyMethods(['getShippingMethod'])
103+
->addMethods(['getShippingInclTax', 'getShippingExclTax'])
104+
->disableOriginalConstructor()
105+
->getMock();
105106
$this->item->expects($this->any())->method('getQuote')->willReturn($this->quote);
106107
$this->item->expects($this->any())->method('getAddress')->willReturn($this->address);
107108

@@ -129,7 +130,8 @@ protected function setUp(): void
129130
'getQuoteTotalsForMultiShipping',
130131
'getQuoteTotalsForRegularShipping',
131132
'getBaseRuleTotals',
132-
'getAvailableDiscountAmount'])
133+
'getAvailableDiscountAmount',
134+
'applyDiscountOnPricesIncludedTax'])
133135
->disableOriginalConstructor()
134136
->getMock();
135137

@@ -177,6 +179,9 @@ public function testCalculate(array $shipping, array $ruleDetails): void
177179
$ruleDetails['discounted_amount']
178180
)
179181
);
182+
$this->cartFixedDiscountHelper->expects($this->any())
183+
->method('applyDiscountOnPricesIncludedTax')
184+
->willReturn(true);
180185
$cartExtensionMock = $this->getMockBuilder(CartExtensionInterface::class)
181186
->disableOriginalConstructor()
182187
->setMethods(['getShippingAssignments'])
@@ -212,6 +217,12 @@ public function testCalculate(array $shipping, array $ruleDetails): void
212217
$shipping['shipping_method']
213218
)
214219
);
220+
$this->address->expects($this->any())
221+
->method('getShippingInclTax')
222+
->willReturn(15.00);
223+
$this->address->expects($this->any())
224+
->method('getShippingExclTax')
225+
->willReturn(10.00);
215226

216227
/** validators data */
217228
$this->validator

dev/tests/integration/testsuite/Magento/SalesRule/Model/Rule/Action/Discount/CartFixedTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,33 @@ public function discountByPercentDataProvider()
550550
];
551551
}
552552

553+
/**
554+
* @magentoConfigFixture current_store sales/minimum_order/tax_including 1
555+
* @magentoConfigFixture current_store sales/minimum_order/include_discount_amount 1
556+
* @magentoConfigFixture current_store tax/calculation/price_includes_tax 1
557+
* @magentoConfigFixture current_store tax/calculation/shipping_includes_tax 1
558+
* @magentoConfigFixture current_store tax/calculation/discount_tax 1
559+
* @magentoConfigFixture current_store tax/calculation/apply_after_discount 1
560+
* @magentoDataFixture Magento/SalesRule/_files/cart_rule_with_coupon_5_off_no_condition.php
561+
* @magentoDataFixture Magento/Tax/_files/tax_rule_region_1_al.php
562+
* @magentoDataFixture Magento/Checkout/_files/quote_with_taxable_product_and_customer.php
563+
*/
564+
public function testCartFixedDiscountPriceIncludeTax()
565+
{
566+
$quote = $this->getQuote('test_order_with_taxable_product');
567+
$quote->setCouponCode('CART_FIXED_DISCOUNT_5');
568+
$quote->getShippingAddress()
569+
->setShippingMethod('flatrate_flatrate')
570+
->setCollectShippingRates(true);
571+
$quote->collectTotals();
572+
$this->quoteRepository->save($quote);
573+
574+
$this->assertEquals(0.4, $quote->getShippingAddress()->getTaxAmount());
575+
$this->assertEquals(5, $quote->getShippingAddress()->getShippingAmount());
576+
$this->assertEquals(5, $quote->getShippingAddress()->getSubtotalWithDiscount());
577+
$this->assertEquals(-5, $quote->getShippingAddress()->getDiscountAmount());
578+
}
579+
553580
/**
554581
* Get list of orders by quote id.
555582
*

0 commit comments

Comments
 (0)