Skip to content

Commit af32ac1

Browse files
author
Tang, Yu(ytang1)
committed
Merge pull request #36 from magento-fearless-kiwis/develop-PR
[FearlessKiwis] Bug Fixes
2 parents 093dd63 + 228f844 commit af32ac1

File tree

33 files changed

+434
-219
lines changed

33 files changed

+434
-219
lines changed

app/code/Magento/Catalog/Block/Product/View/Options.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ protected function _getPriceConfiguration($option)
162162
$data = [
163163
'prices' => [
164164
'oldPrice' => [
165-
'amount' => $this->pricingHelper->currency($option->getPrice(false), false, false),
165+
'amount' => $optionPrice,
166166
'adjustments' => [],
167167
],
168168
'basePrice' => [

app/code/Magento/Tax/CustomerData/CheckoutTotalsJsLayoutDataProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function getData()
6161
protected function getTotalsConfig()
6262
{
6363
return [
64-
'display_subtotal_incl_tax' => (int)$this->taxConfig->displayCartSubtotalInclTax(),
64+
'display_cart_subtotal_incl_tax' => (int)$this->taxConfig->displayCartSubtotalInclTax(),
6565
'display_cart_subtotal_excl_tax' => (int)$this->taxConfig->displayCartSubtotalExclTax(),
6666
];
6767
}

app/code/Magento/Tax/Model/Calculation/AbstractAggregateCalculator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function calculateWithTaxInPrice(QuoteDetailsItemInterface $item, $qua
2828
$priceInclTax = $this->calculationTool->round($item->getUnitPrice());
2929
$rowTotalInclTax = $priceInclTax * $quantity;
3030
if (!$this->isSameRateAsStore($rate, $storeRate)) {
31-
$priceInclTax = $this->calculatePriceInclTax($priceInclTax, $storeRate, $rate);
31+
$priceInclTax = $this->calculatePriceInclTax($priceInclTax, $storeRate, $rate, $round);
3232
$rowTotalInclTax = $priceInclTax * $quantity;
3333
}
3434
$rowTaxExact = $this->calculationTool->calcTaxAmount($rowTotalInclTax, $rate, true, false);

app/code/Magento/Tax/Model/Calculation/AbstractCalculator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,14 +433,18 @@ protected function deltaRound($price, $rate, $direction, $type = self::KEY_REGUL
433433
* @param float $storePriceInclTax
434434
* @param float $storeRate
435435
* @param float $customerRate
436+
* @param boolean $round
436437
* @return float
437438
*/
438-
protected function calculatePriceInclTax($storePriceInclTax, $storeRate, $customerRate)
439+
protected function calculatePriceInclTax($storePriceInclTax, $storeRate, $customerRate, $round = true)
439440
{
440441
$storeTax = $this->calculationTool->calcTaxAmount($storePriceInclTax, $storeRate, true, false);
441442
$priceExclTax = $storePriceInclTax - $storeTax;
442443
$customerTax = $this->calculationTool->calcTaxAmount($priceExclTax, $customerRate, false, false);
443-
$customerPriceInclTax = $this->calculationTool->round($priceExclTax + $customerTax);
444+
$customerPriceInclTax = $priceExclTax + $customerTax;
445+
if ($round) {
446+
$customerPriceInclTax = $this->calculationTool->round($customerPriceInclTax);
447+
}
444448
return $customerPriceInclTax;
445449
}
446450
}

app/code/Magento/Tax/Model/Calculation/UnitBaseCalculator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected function calculateWithTaxInPrice(QuoteDetailsItemInterface $item, $qua
4545
$applyTaxAfterDiscount = $this->config->applyTaxAfterDiscount($this->storeId);
4646
$priceInclTax = $this->calculationTool->round($item->getUnitPrice());
4747
if (!$this->isSameRateAsStore($rate, $storeRate)) {
48-
$priceInclTax = $this->calculatePriceInclTax($priceInclTax, $storeRate, $rate);
48+
$priceInclTax = $this->calculatePriceInclTax($priceInclTax, $storeRate, $rate, $round);
4949
}
5050
$uniTax = $this->calculationTool->calcTaxAmount($priceInclTax, $rate, true, false);
5151
$deltaRoundingType = self::KEY_REGULAR_DELTA_ROUNDING;

app/code/Magento/Tax/Model/Sales/Total/Quote/Tax.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public function __construct(
8888
* @param ShippingAssignmentInterface $shippingAssignment
8989
* @param Address\Total $total
9090
* @return $this
91+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
9192
*/
9293
public function collect(
9394
\Magento\Quote\Model\Quote $quote,
@@ -291,13 +292,18 @@ protected function processExtraTaxables(Address\Total $total, Array $itemsByType
291292
* @param Address\Total $total
292293
* @return array|null
293294
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
295+
* @SuppressWarnings(PHPMD.NPathComplexity)
294296
*/
295297
public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
296298
{
297299
$totals = [];
298300
$store = $quote->getStore();
299301
$applied = $total->getAppliedTaxes();
300302
$amount = $total->getTaxAmount();
303+
if ($amount == null) {
304+
$this->enhanceTotalData($quote, $total);
305+
$amount = $total->getTaxAmount();
306+
}
301307
$taxAmount = $amount + $total->getTotalAmount('discount_tax_compensation');
302308

303309
$area = null;
@@ -340,6 +346,44 @@ public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Qu
340346
return $totals;
341347
}
342348

349+
/**
350+
* Adds minimal tax information to the "total" data structure
351+
*
352+
* @param \Magento\Quote\Model\Quote $quote
353+
* @param Address\Total $total
354+
* @return null
355+
*/
356+
protected function enhanceTotalData(
357+
\Magento\Quote\Model\Quote $quote,
358+
\Magento\Quote\Model\Quote\Address\Total $total
359+
) {
360+
$taxAmount = 0;
361+
$shippingTaxAmount = 0;
362+
$discountTaxCompensation = 0;
363+
364+
$subtotalInclTax = $total->getSubtotalInclTax();
365+
$computeSubtotalInclTax = true;
366+
if ($total->getSubtotalInclTax() > 0) {
367+
$computeSubtotalInclTax = false;
368+
}
369+
370+
/** @var \Magento\Quote\Model\Quote\Address $address */
371+
foreach ($quote->getAllAddresses() as $address) {
372+
$taxAmount += $address->getTaxAmount();
373+
$shippingTaxAmount += $address->getShippingTaxAmount();
374+
$discountTaxCompensation += $address->getDiscountTaxCompensationAmount();
375+
if ($computeSubtotalInclTax) {
376+
$subtotalInclTax += $address->getSubtotalInclTax();
377+
}
378+
}
379+
380+
$total->setTaxAmount($taxAmount);
381+
$total->setShippingTaxAmount($shippingTaxAmount);
382+
$total->setDiscountTaxCompensationAmount($discountTaxCompensation); // accessed via 'discount_tax_compensation'
383+
$total->setSubtotalInclTax($subtotalInclTax);
384+
return;
385+
}
386+
343387
/**
344388
* Process model configuration array.
345389
* This method can be used for changing totals collect sort order

app/code/Magento/Tax/Test/Unit/Model/Calculation/RowBaseAndTotalBaseCalculatorTestCase.php

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class RowBaseAndTotalBaseCalculatorTestCase extends \PHPUnit_Framework_TestCase
2020
const RATE = 10;
2121
const STORE_RATE = 11;
2222

23+
const UNIT_PRICE_INCL_TAX = 495.49549549545;
24+
const UNIT_PRICE_INCL_TAX_ROUNDED = 495.5;
25+
2326
const CODE = 'CODE';
2427
const TYPE = 'TYPE';
2528

@@ -71,7 +74,7 @@ public function initMocks($isTaxIncluded)
7174
{
7275
$this->initMockItem($isTaxIncluded);
7376
$this->initMockConfig();
74-
$this->initMockCalculationTool();
77+
$this->initMockCalculationTool($isTaxIncluded);
7578
$this->initMockAppliedTaxDataObjectFactory();
7679
}
7780

@@ -93,7 +96,7 @@ public function setUp()
9396
$this->mockCalculationTool = $this->getMockBuilder('\Magento\Tax\Model\Calculation')
9497
->disableOriginalConstructor()
9598
->setMethods(
96-
['__wakeup', 'round', 'getRate', 'getStoreRate', 'getRateRequest', 'getAppliedRates', 'calcTaxAmount']
99+
['__wakeup', 'round', 'getRate', 'getStoreRate', 'getRateRequest', 'getAppliedRates']
97100
)
98101
->getMock();
99102
$this->mockConfig = $this->getMockBuilder('\Magento\Tax\Model\Config')
@@ -129,11 +132,12 @@ public function setUp()
129132

130133
/**
131134
* @param $calculator RowBaseCalculator|TotalBaseCalculator
135+
* @param boolean $round
132136
* @return \Magento\Tax\Api\Data\TaxDetailsItemInterface
133137
*/
134-
public function calculate($calculator)
138+
public function calculate($calculator, $round = true)
135139
{
136-
return $calculator->calculate($this->mockItem, 1);
140+
return $calculator->calculate($this->mockItem, 1, $round);
137141
}
138142

139143
/**
@@ -147,7 +151,7 @@ protected function initMockItem($isTaxIncluded)
147151
$this->mockItem,
148152
[
149153
[
150-
self::ONCE => true,
154+
self::ONCE => false,
151155
self::MOCK_METHOD_NAME => 'getDiscountAmount',
152156
self::MOCK_VALUE => 1,
153157
],
@@ -157,17 +161,17 @@ protected function initMockItem($isTaxIncluded)
157161
self::MOCK_VALUE => self::CODE
158162
],
159163
[
160-
self::ONCE => true,
164+
self::ONCE => false,
161165
self::MOCK_METHOD_NAME => 'getType',
162166
self::MOCK_VALUE => self::TYPE
163167
],
164168
[
165-
self::ONCE => true,
169+
self::ONCE => false,
166170
self::MOCK_METHOD_NAME => 'getUnitPrice',
167171
self::MOCK_VALUE => self::UNIT_PRICE
168172
],
169173
[
170-
self::ONCE => true,
174+
self::ONCE => false,
171175
self::MOCK_METHOD_NAME => 'getIsTaxIncluded',
172176
self::MOCK_VALUE => $isTaxIncluded
173177
]
@@ -185,7 +189,7 @@ protected function initMockConfig()
185189
$this->mockConfig,
186190
[
187191
[
188-
self::ONCE => true,
192+
self::ONCE => false,
189193
self::MOCK_METHOD_NAME => 'applyTaxAfterDiscount',
190194
self::MOCK_VALUE => true,
191195
]
@@ -196,47 +200,55 @@ protected function initMockConfig()
196200
/**
197201
* init mock calculation model
198202
*
203+
* @param boolean $isTaxIncluded
199204
*/
200205

201-
protected function initMockCalculationTool()
206+
protected function initMockCalculationTool($isTaxIncluded)
202207
{
203-
$this->mockReturnValues(
204-
$this->mockCalculationTool,
208+
$mockValues = [
205209
[
206-
[
207-
self::ONCE => false,
208-
self::MOCK_METHOD_NAME => 'calcTaxAmount',
209-
self::MOCK_VALUE => 1.5,
210-
],
211-
[
212-
self::ONCE => true,
213-
self::MOCK_METHOD_NAME => 'getRate',
214-
self::MOCK_VALUE => self::RATE
215-
],
216-
[
217-
self::ONCE => true,
218-
self::MOCK_METHOD_NAME => 'getAppliedRates',
219-
self::MOCK_VALUE => [
220-
[
221-
'id' => 0,
222-
'percent' => 1.4,
223-
'rates' => [
224-
[
225-
'code' => 'sku_1',
226-
'title' => 'title1',
227-
'percent' => 1.1,
228-
],
210+
self::ONCE => false,
211+
self::MOCK_METHOD_NAME => 'getRate',
212+
self::MOCK_VALUE => self::RATE
213+
],
214+
[
215+
self::ONCE => false,
216+
self::MOCK_METHOD_NAME => 'getAppliedRates',
217+
self::MOCK_VALUE => [
218+
[
219+
'id' => 0,
220+
'percent' => 1.4,
221+
'rates' => [
222+
[
223+
'code' => 'sku_1',
224+
'title' => 'title1',
225+
'percent' => 1.1,
229226
],
230227
],
231-
]
232-
],
233-
[
234-
self::ONCE => false,
235-
self::MOCK_METHOD_NAME => 'round',
236-
self::MOCK_VALUE => 1.3
228+
],
237229
]
238-
]
230+
],
231+
];
232+
233+
if ($isTaxIncluded) {
234+
$mockValues[] = [
235+
self::ONCE => false,
236+
self::MOCK_METHOD_NAME => 'getStoreRate',
237+
self::MOCK_VALUE => self::STORE_RATE
238+
];
239+
}
240+
241+
$this->mockReturnValues(
242+
$this->mockCalculationTool,
243+
$mockValues
239244
);
245+
$this->mockCalculationTool->expects($this->atLeastOnce())
246+
->method('round')
247+
->willReturnCallback(
248+
function ($price) {
249+
return round($price, 2);
250+
}
251+
);
240252
}
241253

242254
/**
@@ -249,7 +261,7 @@ protected function initMockAppliedTaxDataObjectFactory()
249261
$this->appliedTaxDataObjectFactory,
250262
[
251263
[
252-
self::ONCE => true,
264+
self::ONCE => false,
253265
self::MOCK_METHOD_NAME => 'create',
254266
self::MOCK_VALUE => $this->mockAppliedTax,
255267
]

app/code/Magento/Tax/Test/Unit/Model/Calculation/RowBaseCalculatorTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@ public function testCalculateWithTaxInPrice()
2626

2727
$this->assertSame(
2828
$this->taxDetailsItem,
29-
$this->calculate($this->rowBaseCalculator)
29+
$this->calculate($this->rowBaseCalculator, true)
30+
);
31+
$this->assertEquals(self::UNIT_PRICE_INCL_TAX_ROUNDED, $this->taxDetailsItem->getPriceInclTax());
32+
33+
$this->assertSame(
34+
$this->taxDetailsItem,
35+
$this->calculate($this->rowBaseCalculator, false)
3036
);
37+
$this->assertEquals(self::UNIT_PRICE_INCL_TAX, $this->taxDetailsItem->getPriceInclTax());
3138
}
3239

3340
public function testCalculateWithTaxNotInPrice()

app/code/Magento/Tax/Test/Unit/Model/Calculation/TotalBaseCalculatorTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ public function testCalculateWithTaxInPrice()
2222
$this->taxDetailsItem,
2323
$this->calculate($this->totalBaseCalculator)
2424
);
25+
$this->assertEquals(self::UNIT_PRICE_INCL_TAX_ROUNDED, $this->taxDetailsItem->getPriceInclTax());
26+
}
27+
28+
public function testCalculateWithTaxInPriceNoRounding()
29+
{
30+
$this->initTotalBaseCalculator();
31+
$this->totalBaseCalculator->expects($this->exactly(3))
32+
->method('deltaRound')->will($this->returnValue(0));
33+
$this->initMocks(true);
34+
35+
$this->assertSame(
36+
$this->taxDetailsItem,
37+
$this->calculate($this->totalBaseCalculator, false)
38+
);
39+
$this->assertEquals(self::UNIT_PRICE_INCL_TAX, $this->taxDetailsItem->getPriceInclTax());
2540
}
2641

2742
public function testCalculateWithTaxNotInPrice()

0 commit comments

Comments
 (0)