Problem/Motivation

For a store configured with "Prices are entered with taxes included.", and a tax type configured to NOT display taxes of this type inclusive in product prices, the tax amount is removed twice for tax-exempt customer.

When a customer is tax-exempt (no matching zones, and so no matching rates in LocalTaxTypeBase) and prices include tax, the LocalTaxTypeBase Plugin provides the rates corresponding to the store zone (and of course the product), and set the flag $negate to TRUE (if no positive adjustments already added by an another OrderProcessor are found)

if (empty($positive_tax_adjustments)) {
          $store_profile = $this->buildStoreProfile($store);
          $rates = $this->resolveRates($order_item, $store_profile);
          $negate = TRUE;
}

Then the Plugin remove the tax amount from the unit price

if ($prices_include_tax && !$this->isDisplayInclusive()) {
          $unit_price = $unit_price->subtract($tax_amount);
          $order_item->setUnitPrice($unit_price);
}

And add a negative adjustment, never included in this case (because $negate = TRUE).

$order_item->addAdjustment(new Adjustment([
          'type' => 'tax',
          'label' => $zone->getDisplayLabel(),
          'amount' => $negate ? $tax_amount->multiply('-1') : $tax_amount,
          'percentage' => $percentage->getNumber(),
          'source_id' => $this->entityId . '|' . $zone->getId() . '|' . $rate->getId(),
          'included' => !$negate && $this->isDisplayInclusive(),
]));

Then the TaxOrderProcessor (after called the tax type plugin) continue and substract each negative adjustment from the unit price again.

if ($order->getStore()->get('prices_include_tax')->value) {
      foreach ($order->getItems() as $order_item) {
        $adjustments = $order_item->getAdjustments();
        $negative_tax_adjustments = array_filter($adjustments, function ($adjustment) {
          /** @var \Drupal\commerce_order\Adjustment $adjustment */
          return $adjustment->getType() == 'tax' && $adjustment->isNegative();
        });
        $adjustments = array_diff_key($adjustments, $negative_tax_adjustments);
        $unit_price = $order_item->getUnitPrice();
        foreach ($negative_tax_adjustments as $adjustment) {
          $unit_price = $unit_price->add($adjustment->getAmount());
        }
        $order_item->setUnitPrice($unit_price);
        $order_item->setAdjustments($adjustments);
}

And finally, tax-exempt customer (without matching zone for the store) got a price with the VAT removed twice.

Proposed resolution

Add a condition in the LocalTaxTypeBase plugin with the flag $negate before add/substract the tax amount from the unit price.

If the flag $negate is set to TRUE, then the plugin add a negative adjustment, negative adjustment which is subtract from the unit price in the TaxOrderProcessor.

 if ($prices_include_tax && !$this->isDisplayInclusive() && !$negate) {
          $unit_price = $unit_price->subtract($tax_amount);
          $order_item->setUnitPrice($unit_price);
        }
elseif (!$prices_include_tax && $this->isDisplayInclusive() && !$negate) {
          $unit_price = $unit_price->add($tax_amount);
          $order_item->setUnitPrice($unit_price);
}

Remaining tasks

Is this fix cover all the use cases. I believe but I could miss another use case ?

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Comments

flocondetoile created an issue. See original summary.

flocondetoile’s picture

Issue summary: View changes
flocondetoile’s picture

Status: Active » Needs review
StatusFileSize
new1 KB
flocondetoile’s picture

Title: Tax amount removed twice for tax-exempt customer when price is inclusive » Tax amount removed twice for tax-exempt customer when price include tax
Issue summary: View changes
flocondetoile’s picture

Added a test without the fix => should failed.
And added the test with the fix => should pass.

The last submitted patch, 5: 2934538-5_test_should_failed.patch, failed testing. View results

bojanz’s picture

Status: Needs review » Needs work

The first fix makes sense.

-        elseif (!$prices_include_tax && $this->isDisplayInclusive()) {
+        elseif (!$prices_include_tax && $this->isDisplayInclusive() && !$negate) {

This second one is unneeded because if $prices_include_tax is FALSE, then $negate can never be TRUE.

The test needs work. We already have a test case in CustomTest::testTaxExemptPrices(), we need to expand it.

  • bojanz committed d910579 on 8.x-2.x authored by flocondetoile
    Issue #2934538 by flocondetoile, bojanz: Tax amount removed twice for...
bojanz’s picture

Status: Needs work » Fixed

Addressed my feedback, and committed. Thank you for tracking this down.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.