Skip to content

Commit ff2c965

Browse files
author
Yu Tang
committed
MAGETWO-43291: Incl tax prices on Product and catalog pages include tax on FPT
1 parent 560085a commit ff2c965

File tree

13 files changed

+84
-197
lines changed

13 files changed

+84
-197
lines changed

app/code/Magento/Weee/Helper/Data.php

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public function displayTotalsInclTax($store = null)
196196
* @param mixed $website
197197
* @return float
198198
*/
199-
public function getAmount($product, $website = null)
199+
public function getAmountExclTax($product, $website = null)
200200
{
201201
if (!$product->hasData($this->cacheProductWeeeAmount)) {
202202
/** @var \Magento\Store\Model\Store $store */
@@ -208,7 +208,7 @@ public function getAmount($product, $website = null)
208208

209209
$amount = 0;
210210
if ($this->isEnabled($store)) {
211-
$amount = $this->_weeeTax->getWeeeAmount($product, null, null, $website);
211+
$amount = $this->_weeeTax->getWeeeAmountExclTax($product, null, null, $website);
212212
}
213213

214214
$product->setData($this->cacheProductWeeeAmount, $amount);
@@ -402,7 +402,7 @@ public function getProductWeeeAttributesForDisplay($product)
402402

403403
if ($this->isEnabled($store)) {
404404
$calculateTax = ($this->typeOfDisplay(1) || $this->typeOfDisplay(2)) ? 1 : 0;
405-
return $this->getProductWeeeAttributes($product, null, null, null, $calculateTax);
405+
return $this->getProductWeeeAttributes($product, null, null, null, $calculateTax, false);
406406
}
407407
return [];
408408
}
@@ -443,45 +443,6 @@ public function getProductWeeeAttributesForRenderer(
443443
return [];
444444
}
445445

446-
/**
447-
* Returns amount to display
448-
*
449-
* @param \Magento\Catalog\Model\Product $product
450-
* @return int
451-
*/
452-
public function getAmountForDisplay($product)
453-
{
454-
/** @var \Magento\Store\Model\Store $store */
455-
$store = $product->getStore();
456-
457-
if ($this->isEnabled($store)) {
458-
return $this->_weeeTax->getWeeeAmount($product, null, null, null, $this->typeOfDisplay(1));
459-
}
460-
return 0;
461-
}
462-
463-
/**
464-
* Returns all summed WEEE taxes with all local taxes applied
465-
*
466-
* @param \Magento\Framework\DataObject[] $attributes Result from getProductWeeeAttributes()
467-
* @return float
468-
* @throws \Magento\Framework\Exception\LocalizedException
469-
*/
470-
public function getAmountInclTaxes($attributes)
471-
{
472-
if (!is_array($attributes)) {
473-
throw new \Magento\Framework\Exception\LocalizedException(__('$attributes must be an array'));
474-
}
475-
476-
$amount = 0;
477-
foreach ($attributes as $attribute) {
478-
/* @var $attribute \Magento\Framework\DataObject */
479-
$amount += $attribute->getAmountInclTax();
480-
}
481-
482-
return (float) $amount;
483-
}
484-
485446
/**
486447
* Get the weee tax including tax
487448
*

app/code/Magento/Weee/Model/Tax.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ protected function _construct()
143143
* @param null|false|\Magento\Framework\DataObject $billing
144144
* @param Website $website
145145
* @param bool $calculateTax
146-
* @return int
146+
* @return float
147147
*/
148148
public function getWeeeAmount(
149149
$product,
@@ -166,6 +166,34 @@ public function getWeeeAmount(
166166
return $amount;
167167
}
168168

169+
/**
170+
* @param Product $product
171+
* @param null|false|\Magento\Framework\DataObject $shipping
172+
* @param null|false|\Magento\Framework\DataObject $billing
173+
* @param Website $website
174+
* @return float
175+
*/
176+
public function getWeeeAmountExclTax(
177+
$product,
178+
$shipping = null,
179+
$billing = null,
180+
$website = null
181+
) {
182+
$amountExclTax = 0;
183+
$attributes = $this->getProductWeeeAttributes(
184+
$product,
185+
$shipping,
186+
$billing,
187+
$website,
188+
true,
189+
false
190+
);
191+
foreach ($attributes as $attribute) {
192+
$amountExclTax += $attribute->getAmountExclTax();
193+
}
194+
return $amountExclTax;
195+
}
196+
169197
/**
170198
* @param bool $forceEnabled
171199
* @return array
@@ -272,6 +300,7 @@ public function getProductWeeeAttributes(
272300
if ($value) {
273301
$taxAmount = $amount = 0;
274302
$amount = $value;
303+
$amountExclTax = $value;
275304
if ($calculateTax && $this->weeeConfig->isTaxable($store)) {
276305
/** @var \Magento\Tax\Model\Calculation $calculator */
277306
$defaultPercent = $calculator->getRate(
@@ -282,12 +311,14 @@ public function getProductWeeeAttributes(
282311
);
283312
if ($this->_taxData->priceIncludesTax($store)) {
284313
$amountInclTax = $value / (100 + $defaultPercent) * (100 + $currentPercent);
285-
//round the "golden price"
286-
$amountInclTax = $this->priceCurrency->round($amountInclTax);
314+
if ($round) {
315+
$amountInclTax = $this->priceCurrency->round($amountInclTax);
316+
}
287317
$taxAmount = $amountInclTax - $amountInclTax / (100 + $currentPercent) * 100;
288318
if ($round) {
289319
$taxAmount = $this->priceCurrency->round($taxAmount);
290320
}
321+
$amountExclTax = $amountInclTax - $taxAmount;
291322
} else {
292323
$appliedRates = $this->_calculationFactory->create()->getAppliedRates($rateRequest);
293324
if (count($appliedRates) > 1) {
@@ -316,6 +347,7 @@ public function getProductWeeeAttributes(
316347
$one->setName(__($attribute['label_value'] ? $attribute['label_value'] : $attribute['frontend_label']))
317348
->setAmount($amount)
318349
->setTaxAmount($taxAmount)
350+
->setAmountExclTax($amountExclTax)
319351
->setCode($attribute['attribute_code']);
320352

321353
$result[] = $one;

app/code/Magento/Weee/Observer/GetPriceConfigurationObserver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ private function insertWeeePrice($holder, $key, $weeeAttributes)
121121
$weeeSum = 0;
122122
foreach ($weeeAttributes[$holder['optionId']] as $weeeAttribute) {
123123
$holder[$key]['weeePrice' . $weeeAttribute->getCode()] =
124-
['amount' => (float)$weeeAttribute->getAmount()];
125-
$weeeSum += (float)$weeeAttribute->getAmount();
124+
['amount' => (float)$weeeAttribute->getAmountExclTax()];
125+
$weeeSum += (float)$weeeAttribute->getAmountExclTax();
126126
}
127127
$holder[$key]['weeePrice']['amount'] += (float)$weeeSum;
128128
} else {

app/code/Magento/Weee/Pricing/Adjustment.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ public function isIncludedInDisplayPrice()
102102
*/
103103
public function extractAdjustment($amount, SaleableInterface $saleableItem, $context = [])
104104
{
105-
if (isset($context[CustomOptionPriceInterface::CONFIGURATION_OPTION_FLAG])) {
106-
return 0;
107-
}
108-
return $this->getAmount($saleableItem);
105+
return 0;
109106
}
110107

111108
/**
@@ -144,8 +141,8 @@ public function isExcludedWith($adjustmentCode)
144141
*/
145142
protected function getAmount(SaleableInterface $saleableItem)
146143
{
147-
$weeeAmount = $this->weeeHelper->getAmount($saleableItem);
148-
$weeeAmount = $this->priceCurrency->convertAndRound($weeeAmount);
144+
$weeeAmount = $this->weeeHelper->getAmountExclTax($saleableItem);
145+
$weeeAmount = $this->priceCurrency->convert($weeeAmount);
149146
return $weeeAmount;
150147
}
151148

app/code/Magento/Weee/Pricing/Render/Adjustment.php

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -51,58 +51,16 @@ public function __construct(
5151
*/
5252
protected function apply()
5353
{
54-
// Does catalog price include tax? (true, false)
55-
$isPriceIncludesTax = $this->isPriceIncludesTax();
56-
// Price display configurations (DISPLAY_TYPE_EXCLUDING_TAX, DISPLAY_TYPE_INCLUDING_TAX, DISPLAY_TYPE_BOTH)
57-
$priceDisplayConfig = $this->getTaxDisplayConfig();
58-
59-
// NOTE: By default, weee_tax is automatically included in the display price even when certain configurations
60-
// should not allow it. We must remove the weee_tax adjustments for these configurations in the display and/or
61-
// final prices.
6254
$weeeAmount = $this->amountRender->getAmount()->getAdjustmentAmount($this->getAdjustmentCode());
6355
$weeeTaxAmount =
6456
$this->amountRender->getAmount()->getAdjustmentAmount(\Magento\Weee\Pricing\TaxAdjustment::ADJUSTMENT_CODE);
6557

66-
$this->amountRender->setDisplayValue(
67-
$this->amountRender->getDisplayValue() - $weeeTaxAmount
68-
);
6958
$this->finalAmount = $this->amountRender->getDisplayValue();
7059

71-
if ($isPriceIncludesTax == false) {
72-
if ($priceDisplayConfig == \Magento\Tax\Model\Config::DISPLAY_TYPE_INCLUDING_TAX
73-
|| $priceDisplayConfig == \Magento\Tax\Model\Config::DISPLAY_TYPE_BOTH
74-
) {
75-
$this->finalAmount += $weeeTaxAmount;
76-
if ($this->typeOfDisplay([Tax::DISPLAY_EXCL_DESCR_INCL]) == false) {
77-
$this->amountRender->setDisplayValue(
78-
$this->amountRender->getDisplayValue() + $weeeTaxAmount
79-
);
80-
}
81-
}
82-
if ($this->typeOfDisplay([Tax::DISPLAY_EXCL_DESCR_INCL])) {
83-
$this->amountRender->setDisplayValue(
84-
$this->amountRender->getDisplayValue() - $weeeAmount
85-
);
86-
}
87-
} else {
88-
// If catalog prices already include tax, we need to once again back out weee_tax
89-
if ($priceDisplayConfig == \Magento\Tax\Model\Config::DISPLAY_TYPE_EXCLUDING_TAX) {
90-
$this->amountRender->setDisplayValue(
91-
$this->amountRender->getDisplayValue() - $weeeTaxAmount
92-
);
93-
$this->finalAmount -= $weeeTaxAmount;
94-
if ($this->typeOfDisplay([Tax::DISPLAY_EXCL_DESCR_INCL])) {
95-
$this->amountRender->setDisplayValue(
96-
$this->amountRender->getDisplayValue() - $weeeAmount + $weeeTaxAmount
97-
);
98-
}
99-
} else {
100-
if ($this->typeOfDisplay([Tax::DISPLAY_EXCL_DESCR_INCL])) {
101-
$this->amountRender->setDisplayValue(
102-
$this->amountRender->getDisplayValue() - $weeeAmount
103-
);
104-
}
105-
}
60+
if ($this->typeOfDisplay([Tax::DISPLAY_EXCL_DESCR_INCL])) {
61+
$this->amountRender->setDisplayValue(
62+
$this->amountRender->getDisplayValue() - $weeeAmount - $weeeTaxAmount
63+
);
10664
}
10765
return $this->toHtml();
10866
}
@@ -138,10 +96,10 @@ public function getFinalAmount()
13896
*
13997
* @return float
14098
*/
141-
protected function getWeeeTaxAmount()
99+
protected function getWeeeAmount()
142100
{
143101
$product = $this->getSaleableItem();
144-
return $this->weeeHelper->getAmount($product);
102+
return $this->weeeHelper->getAmountExclTax($product);
145103
}
146104

147105
/**
@@ -151,7 +109,7 @@ protected function getWeeeTaxAmount()
151109
*/
152110
public function showInclDescr()
153111
{
154-
return $this->isWeeeShown() && $this->getWeeeTaxAmount() && $this->typeOfDisplay(Tax::DISPLAY_INCL_DESCR);
112+
return $this->isWeeeShown() && $this->getWeeeAmount() && $this->typeOfDisplay(Tax::DISPLAY_INCL_DESCR);
155113
}
156114

157115
/**
@@ -161,7 +119,7 @@ public function showInclDescr()
161119
*/
162120
public function showExclDescrIncl()
163121
{
164-
return $this->isWeeeShown() && $this->getWeeeTaxAmount() && $this->typeOfDisplay(Tax::DISPLAY_EXCL_DESCR_INCL);
122+
return $this->isWeeeShown() && $this->getWeeeAmount() && $this->typeOfDisplay(Tax::DISPLAY_EXCL_DESCR_INCL);
165123
}
166124

167125
/**
@@ -204,7 +162,7 @@ public function renderWeeeTaxAttribute(\Magento\Framework\DataObject $attribute)
204162
*/
205163
public function renderWeeeTaxAttributeWithTax(\Magento\Framework\DataObject $attribute)
206164
{
207-
return $this->convertAndFormatCurrency($attribute->getData('amount') + $attribute->getData('tax_amount'));
165+
return $this->convertAndFormatCurrency($attribute->getData('amount_excl_tax') + $attribute->getData('tax_amount'));
208166
}
209167

210168
/**
@@ -215,7 +173,7 @@ public function renderWeeeTaxAttributeWithTax(\Magento\Framework\DataObject $att
215173
*/
216174
public function renderWeeeTaxAttributeWithoutTax(\Magento\Framework\DataObject $attribute)
217175
{
218-
$price = $attribute->getData('amount') - $attribute->getData('tax_amount');
176+
$price = $attribute->getData('amount_excl_tax');
219177
return ($price > 0) ? $this->convertAndFormatCurrency($price): $this->convertAndFormatCurrency(0);
220178
}
221179

app/code/Magento/Weee/Pricing/Render/TaxAdjustment.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,13 @@ public function __construct(
4848
public function getDefaultExclusions()
4949
{
5050
$exclusions = parent::getDefaultExclusions();
51+
$exclusions[] = \Magento\Weee\Pricing\TaxAdjustment::ADJUSTMENT_CODE;
5152

5253
// Determine if the Weee amount should be excluded from the price
5354
if ($this->typeOfDisplay([Tax::DISPLAY_EXCL_DESCR_INCL, Tax::DISPLAY_EXCL])) {
5455
$exclusions[] = \Magento\Weee\Pricing\Adjustment::ADJUSTMENT_CODE;
5556
}
5657

57-
// Determine if the Weee tax amount should be excluded from the price (Excl Tax. Price)
58-
// NOTE: By default, weee_tax amount is included in the Excl Price. Therefore, we will add weee_tax to the
59-
// list of exclusions at least once
60-
if ($this->weeeHelper->isTaxable() == true) {
61-
$exclusions[] = \Magento\Weee\Pricing\TaxAdjustment::ADJUSTMENT_CODE;
62-
63-
if ($this->weeeHelper->displayTotalsInclTax() &&
64-
$this->weeeHelper->getTaxDisplayConfig() == \Magento\Tax\Model\Config::DISPLAY_TYPE_BOTH &&
65-
$this->typeOfDisplay([Tax::DISPLAY_INCL, TAX::DISPLAY_INCL_DESCR])) {
66-
$exclusions[] = \Magento\Weee\Pricing\TaxAdjustment::ADJUSTMENT_CODE;
67-
}
68-
}
69-
7058
return $exclusions;
7159
}
7260

app/code/Magento/Weee/Pricing/TaxAdjustment.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,7 @@ public function isIncludedInDisplayPrice()
103103
*/
104104
public function extractAdjustment($amount, SaleableInterface $saleableItem, $context = [])
105105
{
106-
if (isset($context[CustomOptionPriceInterface::CONFIGURATION_OPTION_FLAG])) {
107-
return 0;
108-
}
109-
return $this->getAmount($saleableItem);
106+
return 0;
110107
}
111108

112109
/**

app/code/Magento/Weee/Test/Unit/Helper/DataTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testGetAmount()
5757
$this->product->expects($this->any())->method('hasData')->will($this->returnValue(false));
5858
$this->product->expects($this->any())->method('getData')->will($this->returnValue(11.26));
5959

60-
$this->assertEquals('11.26', $this->helperData->getAmount($this->product));
60+
$this->assertEquals('11.26', $this->helperData->getAmountExclTax($this->product));
6161
}
6262

6363
/**

app/code/Magento/Weee/Test/Unit/Observer/GetPriceConfigurationObserverTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ public function testGetPriceConfiguration($testArray, $expectedArray)
2727
$weeeObject1 = new \Magento\Framework\DataObject(
2828
[
2929
'code' => 'fpt1',
30-
'amount' => '15.0000',
30+
'amount_excl_tax' => '15.0000',
3131
]
3232
);
3333

3434
$weeeObject2 = new \Magento\Framework\DataObject(
3535
[
3636
'code' => 'fpt2',
37-
'amount' => '16.0000',
37+
'amount_excl_tax' => '16.0000',
3838
]
3939
);
4040

0 commit comments

Comments
 (0)