Skip to content

Commit

Permalink
Merge pull request #36 from magento-fearless-kiwis/develop-PR
Browse files Browse the repository at this point in the history
[FearlessKiwis] Bug Fixes
  • Loading branch information
Tang, Yu(ytang1) committed Oct 24, 2015
2 parents 093dd63 + 228f844 commit af32ac1
Show file tree
Hide file tree
Showing 33 changed files with 434 additions and 219 deletions.
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Block/Product/View/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected function _getPriceConfiguration($option)
$data = [
'prices' => [
'oldPrice' => [
'amount' => $this->pricingHelper->currency($option->getPrice(false), false, false),
'amount' => $optionPrice,
'adjustments' => [],
],
'basePrice' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getData()
protected function getTotalsConfig()
{
return [
'display_subtotal_incl_tax' => (int)$this->taxConfig->displayCartSubtotalInclTax(),
'display_cart_subtotal_incl_tax' => (int)$this->taxConfig->displayCartSubtotalInclTax(),
'display_cart_subtotal_excl_tax' => (int)$this->taxConfig->displayCartSubtotalExclTax(),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function calculateWithTaxInPrice(QuoteDetailsItemInterface $item, $qua
$priceInclTax = $this->calculationTool->round($item->getUnitPrice());
$rowTotalInclTax = $priceInclTax * $quantity;
if (!$this->isSameRateAsStore($rate, $storeRate)) {
$priceInclTax = $this->calculatePriceInclTax($priceInclTax, $storeRate, $rate);
$priceInclTax = $this->calculatePriceInclTax($priceInclTax, $storeRate, $rate, $round);
$rowTotalInclTax = $priceInclTax * $quantity;
}
$rowTaxExact = $this->calculationTool->calcTaxAmount($rowTotalInclTax, $rate, true, false);
Expand Down
8 changes: 6 additions & 2 deletions app/code/Magento/Tax/Model/Calculation/AbstractCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,14 +433,18 @@ protected function deltaRound($price, $rate, $direction, $type = self::KEY_REGUL
* @param float $storePriceInclTax
* @param float $storeRate
* @param float $customerRate
* @param boolean $round
* @return float
*/
protected function calculatePriceInclTax($storePriceInclTax, $storeRate, $customerRate)
protected function calculatePriceInclTax($storePriceInclTax, $storeRate, $customerRate, $round = true)
{
$storeTax = $this->calculationTool->calcTaxAmount($storePriceInclTax, $storeRate, true, false);
$priceExclTax = $storePriceInclTax - $storeTax;
$customerTax = $this->calculationTool->calcTaxAmount($priceExclTax, $customerRate, false, false);
$customerPriceInclTax = $this->calculationTool->round($priceExclTax + $customerTax);
$customerPriceInclTax = $priceExclTax + $customerTax;
if ($round) {
$customerPriceInclTax = $this->calculationTool->round($customerPriceInclTax);
}
return $customerPriceInclTax;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function calculateWithTaxInPrice(QuoteDetailsItemInterface $item, $qua
$applyTaxAfterDiscount = $this->config->applyTaxAfterDiscount($this->storeId);
$priceInclTax = $this->calculationTool->round($item->getUnitPrice());
if (!$this->isSameRateAsStore($rate, $storeRate)) {
$priceInclTax = $this->calculatePriceInclTax($priceInclTax, $storeRate, $rate);
$priceInclTax = $this->calculatePriceInclTax($priceInclTax, $storeRate, $rate, $round);
}
$uniTax = $this->calculationTool->calcTaxAmount($priceInclTax, $rate, true, false);
$deltaRoundingType = self::KEY_REGULAR_DELTA_ROUNDING;
Expand Down
44 changes: 44 additions & 0 deletions app/code/Magento/Tax/Model/Sales/Total/Quote/Tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function __construct(
* @param ShippingAssignmentInterface $shippingAssignment
* @param Address\Total $total
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function collect(
\Magento\Quote\Model\Quote $quote,
Expand Down Expand Up @@ -291,13 +292,18 @@ protected function processExtraTaxables(Address\Total $total, Array $itemsByType
* @param Address\Total $total
* @return array|null
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
{
$totals = [];
$store = $quote->getStore();
$applied = $total->getAppliedTaxes();
$amount = $total->getTaxAmount();
if ($amount == null) {
$this->enhanceTotalData($quote, $total);
$amount = $total->getTaxAmount();
}
$taxAmount = $amount + $total->getTotalAmount('discount_tax_compensation');

$area = null;
Expand Down Expand Up @@ -340,6 +346,44 @@ public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Qu
return $totals;
}

/**
* Adds minimal tax information to the "total" data structure
*
* @param \Magento\Quote\Model\Quote $quote
* @param Address\Total $total
* @return null
*/
protected function enhanceTotalData(
\Magento\Quote\Model\Quote $quote,
\Magento\Quote\Model\Quote\Address\Total $total
) {
$taxAmount = 0;
$shippingTaxAmount = 0;
$discountTaxCompensation = 0;

$subtotalInclTax = $total->getSubtotalInclTax();
$computeSubtotalInclTax = true;
if ($total->getSubtotalInclTax() > 0) {
$computeSubtotalInclTax = false;
}

/** @var \Magento\Quote\Model\Quote\Address $address */
foreach ($quote->getAllAddresses() as $address) {
$taxAmount += $address->getTaxAmount();
$shippingTaxAmount += $address->getShippingTaxAmount();
$discountTaxCompensation += $address->getDiscountTaxCompensationAmount();
if ($computeSubtotalInclTax) {
$subtotalInclTax += $address->getSubtotalInclTax();
}
}

$total->setTaxAmount($taxAmount);
$total->setShippingTaxAmount($shippingTaxAmount);
$total->setDiscountTaxCompensationAmount($discountTaxCompensation); // accessed via 'discount_tax_compensation'
$total->setSubtotalInclTax($subtotalInclTax);
return;
}

/**
* Process model configuration array.
* This method can be used for changing totals collect sort order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class RowBaseAndTotalBaseCalculatorTestCase extends \PHPUnit_Framework_TestCase
const RATE = 10;
const STORE_RATE = 11;

const UNIT_PRICE_INCL_TAX = 495.49549549545;
const UNIT_PRICE_INCL_TAX_ROUNDED = 495.5;

const CODE = 'CODE';
const TYPE = 'TYPE';

Expand Down Expand Up @@ -71,7 +74,7 @@ public function initMocks($isTaxIncluded)
{
$this->initMockItem($isTaxIncluded);
$this->initMockConfig();
$this->initMockCalculationTool();
$this->initMockCalculationTool($isTaxIncluded);
$this->initMockAppliedTaxDataObjectFactory();
}

Expand All @@ -93,7 +96,7 @@ public function setUp()
$this->mockCalculationTool = $this->getMockBuilder('\Magento\Tax\Model\Calculation')
->disableOriginalConstructor()
->setMethods(
['__wakeup', 'round', 'getRate', 'getStoreRate', 'getRateRequest', 'getAppliedRates', 'calcTaxAmount']
['__wakeup', 'round', 'getRate', 'getStoreRate', 'getRateRequest', 'getAppliedRates']
)
->getMock();
$this->mockConfig = $this->getMockBuilder('\Magento\Tax\Model\Config')
Expand Down Expand Up @@ -129,11 +132,12 @@ public function setUp()

/**
* @param $calculator RowBaseCalculator|TotalBaseCalculator
* @param boolean $round
* @return \Magento\Tax\Api\Data\TaxDetailsItemInterface
*/
public function calculate($calculator)
public function calculate($calculator, $round = true)
{
return $calculator->calculate($this->mockItem, 1);
return $calculator->calculate($this->mockItem, 1, $round);
}

/**
Expand All @@ -147,7 +151,7 @@ protected function initMockItem($isTaxIncluded)
$this->mockItem,
[
[
self::ONCE => true,
self::ONCE => false,
self::MOCK_METHOD_NAME => 'getDiscountAmount',
self::MOCK_VALUE => 1,
],
Expand All @@ -157,17 +161,17 @@ protected function initMockItem($isTaxIncluded)
self::MOCK_VALUE => self::CODE
],
[
self::ONCE => true,
self::ONCE => false,
self::MOCK_METHOD_NAME => 'getType',
self::MOCK_VALUE => self::TYPE
],
[
self::ONCE => true,
self::ONCE => false,
self::MOCK_METHOD_NAME => 'getUnitPrice',
self::MOCK_VALUE => self::UNIT_PRICE
],
[
self::ONCE => true,
self::ONCE => false,
self::MOCK_METHOD_NAME => 'getIsTaxIncluded',
self::MOCK_VALUE => $isTaxIncluded
]
Expand All @@ -185,7 +189,7 @@ protected function initMockConfig()
$this->mockConfig,
[
[
self::ONCE => true,
self::ONCE => false,
self::MOCK_METHOD_NAME => 'applyTaxAfterDiscount',
self::MOCK_VALUE => true,
]
Expand All @@ -196,47 +200,55 @@ protected function initMockConfig()
/**
* init mock calculation model
*
* @param boolean $isTaxIncluded
*/

protected function initMockCalculationTool()
protected function initMockCalculationTool($isTaxIncluded)
{
$this->mockReturnValues(
$this->mockCalculationTool,
$mockValues = [
[
[
self::ONCE => false,
self::MOCK_METHOD_NAME => 'calcTaxAmount',
self::MOCK_VALUE => 1.5,
],
[
self::ONCE => true,
self::MOCK_METHOD_NAME => 'getRate',
self::MOCK_VALUE => self::RATE
],
[
self::ONCE => true,
self::MOCK_METHOD_NAME => 'getAppliedRates',
self::MOCK_VALUE => [
[
'id' => 0,
'percent' => 1.4,
'rates' => [
[
'code' => 'sku_1',
'title' => 'title1',
'percent' => 1.1,
],
self::ONCE => false,
self::MOCK_METHOD_NAME => 'getRate',
self::MOCK_VALUE => self::RATE
],
[
self::ONCE => false,
self::MOCK_METHOD_NAME => 'getAppliedRates',
self::MOCK_VALUE => [
[
'id' => 0,
'percent' => 1.4,
'rates' => [
[
'code' => 'sku_1',
'title' => 'title1',
'percent' => 1.1,
],
],
]
],
[
self::ONCE => false,
self::MOCK_METHOD_NAME => 'round',
self::MOCK_VALUE => 1.3
],
]
]
],
];

if ($isTaxIncluded) {
$mockValues[] = [
self::ONCE => false,
self::MOCK_METHOD_NAME => 'getStoreRate',
self::MOCK_VALUE => self::STORE_RATE
];
}

$this->mockReturnValues(
$this->mockCalculationTool,
$mockValues
);
$this->mockCalculationTool->expects($this->atLeastOnce())
->method('round')
->willReturnCallback(
function ($price) {
return round($price, 2);
}
);
}

/**
Expand All @@ -249,7 +261,7 @@ protected function initMockAppliedTaxDataObjectFactory()
$this->appliedTaxDataObjectFactory,
[
[
self::ONCE => true,
self::ONCE => false,
self::MOCK_METHOD_NAME => 'create',
self::MOCK_VALUE => $this->mockAppliedTax,
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ public function testCalculateWithTaxInPrice()

$this->assertSame(
$this->taxDetailsItem,
$this->calculate($this->rowBaseCalculator)
$this->calculate($this->rowBaseCalculator, true)
);
$this->assertEquals(self::UNIT_PRICE_INCL_TAX_ROUNDED, $this->taxDetailsItem->getPriceInclTax());

$this->assertSame(
$this->taxDetailsItem,
$this->calculate($this->rowBaseCalculator, false)
);
$this->assertEquals(self::UNIT_PRICE_INCL_TAX, $this->taxDetailsItem->getPriceInclTax());
}

public function testCalculateWithTaxNotInPrice()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ public function testCalculateWithTaxInPrice()
$this->taxDetailsItem,
$this->calculate($this->totalBaseCalculator)
);
$this->assertEquals(self::UNIT_PRICE_INCL_TAX_ROUNDED, $this->taxDetailsItem->getPriceInclTax());
}

public function testCalculateWithTaxInPriceNoRounding()
{
$this->initTotalBaseCalculator();
$this->totalBaseCalculator->expects($this->exactly(3))
->method('deltaRound')->will($this->returnValue(0));
$this->initMocks(true);

$this->assertSame(
$this->taxDetailsItem,
$this->calculate($this->totalBaseCalculator, false)
);
$this->assertEquals(self::UNIT_PRICE_INCL_TAX, $this->taxDetailsItem->getPriceInclTax());
}

public function testCalculateWithTaxNotInPrice()
Expand Down
Loading

0 comments on commit af32ac1

Please sign in to comment.