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
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | 2934538-5_test_with_fix.patch | 3.77 KB | flocondetoile |
| #5 | 2934538-5_test_should_failed.patch | 2.77 KB | flocondetoile |
| #3 | 2934538-3.patch | 1 KB | flocondetoile |
Comments
Comment #2
flocondetoileComment #3
flocondetoileComment #4
flocondetoileComment #5
flocondetoileAdded a test without the fix => should failed.
And added the test with the fix => should pass.
Comment #7
bojanz commentedThe first fix makes sense.
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.
Comment #9
bojanz commentedAddressed my feedback, and committed. Thank you for tracking this down.