Skip to content

Commit f8d3116

Browse files
committed
MAGETWO-72280: [Backport] Tier price saving percentage wrong when VAT is included in price #8833 2.1.x
1 parent 2951e0a commit f8d3116

File tree

4 files changed

+55
-32
lines changed

4 files changed

+55
-32
lines changed

app/code/Magento/Bundle/Pricing/Price/TierPrice.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
namespace Magento\Bundle\Pricing\Price;
88

99
use Magento\Catalog\Pricing\Price\RegularPrice;
10-
use Magento\Framework\Pricing\Amount\AmountInterface;
1110

1211
/**
1312
* Bundle tier prices model
@@ -90,13 +89,4 @@ public function isPercentageDiscount()
9089
{
9190
return true;
9291
}
93-
94-
/**
95-
* @param AmountInterface $amount
96-
* @return float
97-
*/
98-
public function getSavePercent(AmountInterface $amount)
99-
{
100-
return round($amount->getBaseAmount());
101-
}
10292
}

app/code/Magento/Bundle/Test/Unit/Pricing/Price/TierPriceTest.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,27 @@ public function providerForGetterTierPriceList()
181181
/**
182182
* @dataProvider providerForTestGetSavePercent
183183
*/
184-
public function testGetSavePercent($baseAmount, $savePercent)
184+
public function testGetSavePercent($baseAmount, $tierPrice, $savePercent)
185185
{
186+
/** @var \Magento\Framework\Pricing\Amount\AmountInterface|\PHPUnit_Framework_MockObject_MockObject $amount */
186187
$amount = $this->getMockForAbstractClass('Magento\Framework\Pricing\Amount\AmountInterface');
187-
$amount->expects($this->once())->method('getBaseAmount')->willReturn($baseAmount);
188+
$amount->expects($this->any())
189+
->method('getValue')
190+
->will($this->returnValue($tierPrice));
191+
192+
$priceAmount = $this->getMockForAbstractClass('Magento\Framework\Pricing\Amount\AmountInterface');
193+
$priceAmount->expects($this->any())
194+
->method('getValue')
195+
->will($this->returnValue($baseAmount));
196+
197+
$price = $this->getMock('Magento\Framework\Pricing\Price\PriceInterface');
198+
$price->expects($this->any())
199+
->method('getAmount')
200+
->will($this->returnValue($priceAmount));
201+
202+
$this->priceInfo->expects($this->any())
203+
->method('getPrice')
204+
->will($this->returnValue($price));
188205

189206
$this->assertEquals($savePercent, $this->model->getSavePercent($amount));
190207
}
@@ -195,10 +212,8 @@ public function testGetSavePercent($baseAmount, $savePercent)
195212
public function providerForTestGetSavePercent()
196213
{
197214
return [
198-
'no fraction' => [10.0000, 10],
199-
'lower half' => [10.1234, 10],
200-
'half way' => [10.5000, 11],
201-
'upper half' => [10.6789, 11],
215+
'no fraction' => [9.0000, 8.1, 10],
216+
'lower half' => [9.1234, 8.3, 9],
202217
];
203218
}
204219
}

app/code/Magento/Catalog/Pricing/Price/TierPrice.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,20 @@ protected function getBasePrice()
203203
}
204204

205205
/**
206+
* Calculates savings percentage according to the given tier price amount
207+
* and related product price amount.
208+
*
206209
* @param AmountInterface $amount
207210
* @return float
208211
*/
209212
public function getSavePercent(AmountInterface $amount)
210213
{
211-
return ceil(
212-
100 - ((100 / $this->priceInfo->getPrice(FinalPrice::PRICE_CODE)->getValue())
213-
* $amount->getBaseAmount())
214+
$productPriceAmount = $this->priceInfo->getPrice(
215+
FinalPrice::PRICE_CODE
216+
)->getAmount();
217+
218+
return round(
219+
100 - ((100 / $productPriceAmount->getValue()) * $amount->getValue())
214220
);
215221
}
216222

app/code/Magento/Catalog/Test/Unit/Pricing/Price/TierPriceTest.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
use Magento\Catalog\Pricing\Price\TierPrice;
1212
use Magento\Catalog\Pricing\Price\FinalPrice;
13+
use Magento\Framework\Pricing\Amount\AmountInterface;
14+
use Magento\Framework\Pricing\Price\PriceInterface;
1315
use Magento\Customer\Model\Group;
1416
use Magento\Customer\Model\GroupManagement;
1517

@@ -361,27 +363,37 @@ public function providerForGetterTierPriceList()
361363
}
362364

363365
/**
364-
* @covers \Magento\Catalog\Pricing\Price\TierPrice::__construct
365-
* @covers \Magento\Catalog\Pricing\Price\TierPrice::getSavePercent
366-
* @covers \Magento\Catalog\Pricing\Price\TierPrice::getBasePrice
366+
* @param float $basePrice
367+
* @param float $tierPrice
368+
* @param float $savedPercent
369+
*
367370
* @dataProvider dataProviderGetSavePercent
368371
*/
369372
public function testGetSavePercent($basePrice, $tierPrice, $savedPercent)
370373
{
371-
$price = $this->getMock('Magento\Framework\Pricing\Price\PriceInterface');
374+
/** @var AmountInterface|\PHPUnit_Framework_MockObject_MockObject $amount */
375+
$amount = $this->getMockForAbstractClass(AmountInterface::class);
372376

373-
$this->priceInfo->expects(static::atLeastOnce())
374-
->method('getPrice')
375-
->with(FinalPrice::PRICE_CODE)
376-
->willReturn($price);
377-
$price->expects(static::atLeastOnce())
377+
$amount->expects($this->any())
378+
->method('getValue')
379+
->willReturn($tierPrice);
380+
381+
$basePriceAmount = $this->getMockForAbstractClass(AmountInterface::class);
382+
383+
$basePriceAmount->expects($this->any())
378384
->method('getValue')
379385
->willReturn($basePrice);
380386

381-
$amount = $this->getMockForAbstractClass('Magento\Framework\Pricing\Amount\AmountInterface');
382-
$amount->expects($this->atLeastOnce())
383-
->method('getBaseAmount')
384-
->will($this->returnValue($tierPrice));
387+
$price = $this->getMockForAbstractClass(PriceInterface::class);
388+
389+
$price->expects($this->any())
390+
->method('getAmount')
391+
->willReturn($basePriceAmount);
392+
393+
$this->priceInfo->expects($this->any())
394+
->method('getPrice')
395+
->with(FinalPrice::PRICE_CODE)
396+
->willReturn($price);
385397

386398
$this->assertEquals($savedPercent, $this->model->getSavePercent($amount));
387399
}

0 commit comments

Comments
 (0)