Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#11358 Fix full tax summary display breakdown #11446

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions app/code/Magento/Tax/Api/Data/AppliedTaxRateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Tax\Api\Data;

/**
Expand All @@ -21,34 +20,41 @@ interface AppliedTaxRateInterface extends \Magento\Framework\Api\ExtensibleDataI
public function getCode();

/**
* Set code
* Get Title
*
* @param string $code
* @return $this
* @return string|null
*/
public function setCode($code);
public function getTitle();

/**
* Get Title
* Get Tax Percent
*
* @return string|null
* @return float|null
*/
public function getTitle();
public function getPercent();

/**
* Set Title
* Get tax amount value
*
* @param string $title
* @return float|string
*/
public function getAmount();

/**
* Set code
*
* @param string $code
* @return $this
*/
public function setTitle($title);
public function setCode($code);

/**
* Get Tax Percent
* Set Title
*
* @return float|null
* @param string $title
* @return $this
*/
public function getPercent();
public function setTitle($title);

/**
* Set Tax Percent
Expand All @@ -58,6 +64,12 @@ public function getPercent();
*/
public function setPercent($percent);

/**
* @param string|float $amount
* @return $this
*/
public function setAmount($amount);

/**
* Retrieve existing extension attributes object or create a new one.
*
Expand Down
42 changes: 36 additions & 6 deletions app/code/Magento/Tax/Api/Data/GrandTotalRatesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,61 @@
* @api
* @since 100.0.2
*/
interface GrandTotalRatesInterface
interface GrandTotalRatesInterface extends \Magento\Framework\Api\ExtensibleDataInterface
{
/**
* Tax rate title
*
* @return string
*/
public function getTitle();

/**
* Get tax percentage value
*
* @return string
*/
public function getPercent();

/**
* Get tax amount value
*
* @return float|string
*/
public function getAmount();

/**
* @param string $title
* @return $this
*/
public function setTitle($title);

/**
* @param float $percent
* @return $this
*/
public function setPercent($percent);

/**
* Tax rate title
* @param string|float $amount
* @return $this
*/
public function setAmount($amount);

/**
* Retrieve existing extension attributes object or create a new one.
*
* @return string
* @return \Magento\Tax\Api\Data\GrandTotalRatesExtensionInterface|null
*/
public function getTitle();
public function getExtensionAttributes();

/**
* @param string $title
* Set an extension attributes object.
*
* @param \Magento\Tax\Api\Data\GrandTotalRatesExtensionInterface $extensionAttributes
* @return $this
*/
public function setTitle($title);
public function setExtensionAttributes(
\Magento\Tax\Api\Data\GrandTotalRatesExtensionInterface $extensionAttributes
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected function calculateWithTaxInPrice(QuoteDetailsItemInterface $item, $qua
$this->taxClassManagement->getTaxClassId($item->getTaxClassKey())
);
$rate = $this->calculationTool->getRate($taxRateRequest);
$storeRate = $storeRate = $this->calculationTool->getStoreRate($taxRateRequest, $this->storeId);
$storeRate = $this->calculationTool->getStoreRate($taxRateRequest, $this->storeId);

$discountTaxCompensationAmount = 0;
$applyTaxAfterDiscount = $this->config->applyTaxAfterDiscount($this->storeId);
Expand Down Expand Up @@ -70,6 +70,7 @@ protected function calculateWithTaxInPrice(QuoteDetailsItemInterface $item, $qua
/** @var \Magento\Tax\Api\Data\AppliedTaxInterface[] $appliedTaxes */
$appliedRates = $this->calculationTool->getAppliedRates($taxRateRequest);
$appliedTaxes = $this->getAppliedTaxes($rowTax, $rate, $appliedRates);
$appliedTaxes = $this->calculateAppliedTaxesRateAmounts($appliedTaxes);

return $this->taxDetailsItemDataObjectFactory->create()
->setCode($item->getCode())
Expand Down Expand Up @@ -145,6 +146,7 @@ protected function calculateWithTaxNotInPrice(QuoteDetailsItemInterface $item, $
$rowTaxes[] = $rowTaxAfterDiscount;
$rowTaxesBeforeDiscount[] = $rowTaxPerRate;
}
$appliedTaxes = $this->calculateAppliedTaxesRateAmounts($appliedTaxes);
$rowTax = array_sum($rowTaxes);
$rowTaxBeforeDiscount = array_sum($rowTaxesBeforeDiscount);
$rowTotalInclTax = $rowTotal + $rowTaxBeforeDiscount;
Expand Down
33 changes: 33 additions & 0 deletions app/code/Magento/Tax/Model/Calculation/AbstractCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
namespace Magento\Tax\Model\Calculation;

use Magento\Customer\Api\Data\AddressInterface as CustomerAddress;
use Magento\Tax\Api\Data\AppliedTaxInterface;
use Magento\Tax\Api\Data\AppliedTaxInterfaceFactory;
use Magento\Tax\Api\Data\AppliedTaxRateInterface;
use Magento\Tax\Api\Data\AppliedTaxRateInterfaceFactory;
use Magento\Tax\Api\Data\QuoteDetailsItemInterface;
use Magento\Tax\Api\Data\TaxDetailsItemInterface;
Expand Down Expand Up @@ -444,4 +446,35 @@ protected function calculatePriceInclTax($storePriceInclTax, $storeRate, $custom
}
return $customerPriceInclTax;
}

/**
* Calculate applied taxes rates amounts
*
* @param array $appliedTaxes
* @return array
*/
protected function calculateAppliedTaxesRateAmounts($appliedTaxes)
{
/** @var AppliedTaxInterface $appliedTax */
foreach ($appliedTaxes as $appliedTax) {
if ($appliedTax->getRates()) {
$totalNominalPercent = 0;
/** @var AppliedTaxRateInterface $rateObject */
foreach ($appliedTax->getRates() as $rateObject) {
$totalNominalPercent += $rateObject->getPercent();
}

foreach ($appliedTax->getRates() as $rateObject) {
$amount = $this->calculationTool->round(
$appliedTax->getAmount() * (
$rateObject->getPercent() / $totalNominalPercent
)
);
$rateObject->setAmount($amount);
}
}
}

return $appliedTaxes;
}
}
51 changes: 43 additions & 8 deletions app/code/Magento/Tax/Model/Calculation/GrandTotalRates.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,53 @@

namespace Magento\Tax\Model\Calculation;

use Magento\Framework\Model\AbstractExtensibleModel;
use Magento\Tax\Api\Data\GrandTotalRatesInterface;
use Magento\Framework\Api\AbstractSimpleObject;

/**
* Grand Total Tax Details Model
*/
class GrandTotalRates extends AbstractSimpleObject implements GrandTotalRatesInterface
class GrandTotalRates extends AbstractExtensibleModel implements GrandTotalRatesInterface
{

/**#@+
* Constants defined for keys of array, makes typos less likely
*/
const TITLE = 'title';
const PERCENT = 'percent';
const TITLE = 'title';
const AMOUNT = 'amount';
/**#@-*/

/**
* {@inheritdoc}
*/
public function getTitle()
{
return $this->_get(self::TITLE);
return $this->getData(self::TITLE);
}

/**
* {@inheritdoc}
*/
public function setTitle($title)
public function getPercent()
{
return $this->setData(self::TITLE, $title);
return $this->getData(self::PERCENT);
}

/**
* {@inheritdoc}
*/
public function getPercent()
public function getAmount()
{
return $this->_get(self::PERCENT);
return $this->getData(self::AMOUNT);
}

/**
* {@inheritdoc}
*/
public function setTitle($title)
{
return $this->setData(self::TITLE, $title);
}

/**
Expand All @@ -52,4 +62,29 @@ public function setPercent($percent)
{
return $this->setData(self::PERCENT, $percent);
}

/**
* {@inheritdoc}
*/
public function setAmount($amount)
{
return $this->setData(self::AMOUNT, $amount);
}

/**
* {@inheritdoc}
*/
public function getExtensionAttributes()
{
return $this->_getExtensionAttributes();
}

/**
* {@inheritdoc}
*/
public function setExtensionAttributes(
\Magento\Tax\Api\Data\GrandTotalRatesExtensionInterface $extensionAttributes
) {
return $this->_setExtensionAttributes($extensionAttributes);
}
}
2 changes: 2 additions & 0 deletions app/code/Magento/Tax/Model/Calculation/UnitBaseCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ protected function calculateWithTaxInPrice(QuoteDetailsItemInterface $item, $qua
/** @var \Magento\Tax\Api\Data\AppliedTaxInterface[] $appliedTaxes */
$appliedRates = $this->calculationTool->getAppliedRates($taxRateRequest);
$appliedTaxes = $this->getAppliedTaxes($rowTax, $rate, $appliedRates);
$appliedTaxes = $this->calculateAppliedTaxesRateAmounts($appliedTaxes);

return $this->taxDetailsItemDataObjectFactory->create()
->setCode($item->getCode())
Expand Down Expand Up @@ -163,6 +164,7 @@ protected function calculateWithTaxNotInPrice(QuoteDetailsItemInterface $item, $
$unitTaxes[] = $unitTaxAfterDiscount;
$unitTaxesBeforeDiscount[] = $unitTaxPerRate;
}
$appliedTaxes = $this->calculateAppliedTaxesRateAmounts($appliedTaxes);
$unitTax = array_sum($unitTaxes);
$unitTaxBeforeDiscount = array_sum($unitTaxesBeforeDiscount);

Expand Down
6 changes: 3 additions & 3 deletions app/code/Magento/Tax/Model/Quote/GrandTotalDetailsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
*/
namespace Magento\Tax\Model\Quote;

use Magento\Quote\Api\Data\TotalSegmentExtensionFactory;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\App\ObjectManager;
use Magento\Quote\Api\Data\TotalSegmentExtensionFactory;

class GrandTotalDetailsPlugin
{
Expand Down Expand Up @@ -73,9 +72,11 @@ protected function getRatesData($rates)
{
$taxRates = [];
foreach ($rates as $rate) {
/** @var \Magento\Tax\Api\Data\GrandTotalRatesInterface $taxRate */
$taxRate = $this->ratesFactory->create([]);
$taxRate->setPercent($rate['percent']);
$taxRate->setTitle($rate['title']);
$taxRate->setAmount($rate['amount']);
$taxRates[] = $taxRate;
}
return $taxRates;
Expand All @@ -94,7 +95,6 @@ public function afterProcess(
array $totalSegments,
array $addressTotals = []
) {

if (!array_key_exists($this->code, $addressTotals)) {
return $totalSegments;
}
Expand Down
Loading