Skip to content

How to overwrite MagentoTaxModelCalculationAbstractAggregateCalculator?

I was facing an issue in tax calculation after discount for that i need to do code change from

//TODO: handle originalDiscountAmount
$taxableAmount = max($rowTotalInclTax - $discountAmount, 0);
$rowTaxAfterDiscount = $this->calculationTool->calcTaxAmount(
    $taxableAmount,
    $rate,
    true,
    false
);
$rowTaxAfterDiscount = $this->roundAmount(
    $rowTaxAfterDiscount,
    $rate,
    true,
    self::KEY_REGULAR_DELTA_ROUNDING,
    $round,
    $item
);
// Set discount tax compensation
$discountTaxCompensationAmount = $rowTax - $rowTaxAfterDiscount;
$rowTax = $rowTaxAfterDiscount;

to

$taxableAmount = max($rowTotal - $discountAmount, 0);
$discountTaxCompensationAmount = 0;
$rowTax = $taxableAmount * ($rate / 100);

in vendor/magento/module-tax/Model/Calculation/AbstractAggregateCalculator.php:49

For that change i use preference in my custom module

  <preference for="MagentoTaxModelCalculationAbstractAggregateCalculator" type="CustomTaxModelCalculationAbstractAggregateCalculator" />

and overwritten the file

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace CustomTaxModelCalculation;

use MagentoTaxApiDataQuoteDetailsItemInterface;

/**
 * Abstract aggregate calculator.
 */
abstract class AbstractAggregateCalculator extends MagentoTaxModelCalculationAbstractAggregateCalculator
{
    /**
     * @inheritdoc
     */
    protected function calculateWithTaxInPrice(QuoteDetailsItemInterface $item, $quantity, $round = true)
    {
        $taxRateRequest = $this->getAddressRateRequest()->setProductClassId(
            $this->taxClassManagement->getTaxClassId($item->getTaxClassKey())
        );
        $rate = $this->calculationTool->getRate($taxRateRequest);
        $storeRate = $this->calculationTool->getStoreRate($taxRateRequest, $this->storeId);

        $discountTaxCompensationAmount = 0;
        $applyTaxAfterDiscount = $this->config->applyTaxAfterDiscount($this->storeId);
        $discountAmount = $item->getDiscountAmount();

        // Calculate $rowTotalInclTax
        $priceInclTax = $this->calculationTool->round($item->getUnitPrice());
        $rowTotalInclTax = $priceInclTax * $quantity;
        if (!$this->isSameRateAsStore($rate, $storeRate)) {
            $priceInclTax = $this->calculatePriceInclTax($priceInclTax, $storeRate, $rate, $round);
            $rowTotalInclTax = $priceInclTax * $quantity;
        }
        $rowTaxExact = $this->calculationTool->calcTaxAmount($rowTotalInclTax, $rate, true, false);
        $deltaRoundingType = self::KEY_REGULAR_DELTA_ROUNDING;
        if ($applyTaxAfterDiscount) {
            $deltaRoundingType = self::KEY_TAX_BEFORE_DISCOUNT_DELTA_ROUNDING;
        }
        $rowTax = $this->roundAmount($rowTaxExact, $rate, true, $deltaRoundingType, $round, $item);
        $rowTotal = $rowTotalInclTax - $rowTax;
        $price = $rowTotal / $quantity;
        if ($round) {
            $price = $this->calculationTool->round($price);
        }

        //Handle discount
        if ($applyTaxAfterDiscount) {
            //TODO: handle originalDiscountAmount
            $taxableAmount = max($rowTotal - $discountAmount, 0);
            $discountTaxCompensationAmount = 0;
            $rowTax = $taxableAmount * ($rate / 100);
        }

        // Calculate applied taxes
        /** @var  MagentoTaxApiDataAppliedTaxInterface[] $appliedTaxes */
        $appliedRates = $this->calculationTool->getAppliedRates($taxRateRequest);
        $appliedTaxes = $this->getAppliedTaxes($rowTax, $rate, $appliedRates);

        return $this->taxDetailsItemDataObjectFactory->create()
            ->setCode($item->getCode())
            ->setType($item->getType())
            ->setRowTax($rowTax)
            ->setPrice($price)
            ->setPriceInclTax($priceInclTax)
            ->setRowTotal($rowTotal)
            ->setRowTotalInclTax($rowTotalInclTax)
            ->setDiscountTaxCompensationAmount($discountTaxCompensationAmount)
            ->setAssociatedItemCode($item->getAssociatedItemCode())
            ->setTaxPercent($rate)
            ->setAppliedTaxes($appliedTaxes);
    }
    /**
     * Round amount
     *
     * @param float $amount
     * @param null|float $rate
     * @param null|bool $direction
     * @param string $type
     * @param bool $round
     * @param QuoteDetailsItemInterface $item
     * @return float
     */
    abstract protected function roundAmount(
        $amount,
        $rate = null,
        $direction = null,
        $type = self::KEY_REGULAR_DELTA_ROUNDING,
        $round = true,
        $item = null
    );
}

But the changes are not reflecting, i think abstract class cannot be overwritten, can some one suggest a solution or fix?