Try our conversational search powered by Generative AI!

Problems with VAT calculation when using discount code [11.2.2]

Vote:
 

Hi!

We have a strange problem:

I am on our Norwegian site where we have a tax configuration for the language code NOR set to 25%.

I add a product in the cart, the net price for that item is 215,2 (215,2 * 1,25 = 269)
The shipping cost net price i set to 39,2 (39,2 * 1,25 = 49)
I add a discount code that gives me 10% discount, net amount 21,52. (21,52 *1,25 = 26,90)
The order form has a delivery address to Norway, language code NOR.

So far so good.

Order gross total should be = 269 + 49 - 26,90 = 291,10 but the actual value is 290,12?

A quick look in the CartHelper.Cart

ShippingTotal: 39,2
SubTotal: 215,2
TaxTotal: 57,24
Total: 290,12

What I can see the tax is the thing that is not correct. The tax should be 58,22.

215,2 + 39,2 - 21,52 = 232,88
232,88 * 1,25 = 291,10
Vat: 291,10 - 232,88 = 58,22

The workflow for some strange reason calculates the tax to 57,24 and I can't figure out why? The only thing I found out that the problem only occurs when wew are using discounts.

Has any one had the similiar problem? Or maybe we are missing something?

I found this: https://gist.github.com/jstemerdink/dcee170c14185b64dde09f43f43801ef

Could that help me?

Thanks!

/Kristoffer

#183790
Edited, Oct 23, 2017 15:33
Vote:
 

So it look likes it got something to do with the shipping cost tax calculation:

private static decimal CalculateShippingTax(
            ITaxValue[] taxes,
            decimal shippingTotal,
            decimal shippingCost,
            decimal itemExtendedPrice,
            decimal itemQuantity,
            decimal totalQuantity)
        {
            decimal basePrice = shippingTotal == decimal.Zero
                                    ? itemQuantity / totalQuantity * shippingCost
                                    : itemExtendedPrice / shippingTotal * shippingCost;
            return GetTaxes(taxes, TaxType.ShippingTax, basePrice);
        }

If I modify that function to this:

private static decimal CalculateShippingTax(
            ITaxValue[] taxes,
            decimal shippingTotal,
            decimal shippingCost,
            decimal itemExtendedPrice,
            decimal itemQuantity,
            decimal totalQuantity)
{
        return GetTaxes(taxes, TaxType.ShippingTax, shippingCost);
}

I get the right tax amount for the whole order.

I'm not sure what the original one is doing, why is it using the item price to calculate the tax for the shipping?
And what are the GetPriceExcludingTax do? All prices are excluding tax already?

private static decimal GetPriceExcludingTax(ILineItem item)
{
       return item.PlacedPrice - (item.TryGetDiscountValue(x => x.OrderAmount) + item.TryGetDiscountValue(x => x.EntryAmount)) / item.Quantity;
}

The shipping cost net is 39,2 but this line:

itemExtendedPrice / shippingTotal * shippingCost;

Sets the shipping cost to 35,28. I don't really understands what is happening here?
Then the shipping tax are the calculated on 35,28 and there for we get a diff.

Can someone please enlight me! 

/Kristoffer

#183795
Oct 23, 2017 16:31
Vote:
 

UPDATE

The modified function:

private static decimal CalculateShippingTax(
            ITaxValue[] taxes,
            decimal shippingTotal,
            decimal shippingCost,
            decimal itemExtendedPrice,
            decimal itemQuantity,
            decimal totalQuantity)
{
        return GetTaxes(taxes, TaxType.ShippingTax, shippingCost);
}

only works if I have one item in the cart.

/Kristoffer

#183798
Oct 23, 2017 16:59
Vote:
 

Hi Kristoffer,

This scenario you point out is probably a bug. I guest the discount you set here is order level discount. 

The reason of using item price in the calculate shipping tax to determine the proportion of difference items in the shipment, to get right amount of shipping cost corresponding.

But the implementation is not correct, when the item extended price is minus order level discount while the shipment subtotal is not. That is a reason why you could not get full of the shipping cost for the tax calculation.

So that is this case, I suggest you using item extended price to item discount price from line item calculator.

/Tuan

#183898
Oct 24, 2017 11:57
Vote:
 

Inform, I just file a bug for this case.

/Tuan

#183915
Oct 24, 2017 12:36
Vote:
 
<p>Thanks Tuan!</p>
#183917
Oct 24, 2017 13:17
Vote:
 

Hmm...

I have tried this:

decimal num3 = lineItemCalculator.GetDiscountedPrice(lineItem, currency).Amount * num2;

and this

decimal num3 = lineItemCalculator.GetExtendedPrice(lineItem, currency).Amount * num2;

None of them gives me the right var amount.

Was this what you suggested?

/Kristoffer

#183918
Oct 24, 2017 13:43
Vote:
 

Got it to work. I still needed the extended price to calculate correct lineitem tax so here is the workaround:

protected override Money CalculateTaxTotal(IOrderForm orderForm, IMarket market, Currency currency)
{
    decimal amount1 = new decimal();
    foreach (IShipment shipment in orderForm.Shipments.Where(x => x.ShippingAddress != null))
    {
        decimal amount2 = new decimal();
        decimal amount3 = this.shippingCalculator.GetDiscountedShippingAmount(shipment, market, currency).Amount;
        decimal amount4 = this.shippingCalculator.GetShippingItemsTotal(shipment, currency).Amount;
        foreach (ILineItem lineItem in shipment.LineItems)
        {
            int taxCategoryId;
            ITaxValue[] taxValues;

            if (this.TryGetTaxCategoryId(lineItem, out taxCategoryId)
                && this.TryGetTaxValues(market, shipment, taxCategoryId, out taxValues))
            {                
                decimal num3 = lineItemCalculator.GetExtendedPrice(lineItem, currency).Amount;
                decimal shippingLineItemValue = lineItemCalculator.GetDiscountedPrice(lineItem, currency).Amount;
                decimal totalQuantity = shipment.LineItems.Sum(l => l.Quantity);

                amount2 += CalculateShippingTax(
                    taxValues,
                    amount4,
                    amount3,
                    shippingLineItemValue,
                    lineItem.Quantity,
                    totalQuantity);

                amount1 += currency.Round(GetTaxes(taxValues, TaxType.SalesTax, num3));
            }
        }

        amount1 += currency.Round(amount2);
    }

    return new Money(currency.Round(amount1), currency);
}

Use the shippingLineItemValue to calculate the shipping tax and the num3 to calculate the lineitem tax.

/Kristoffer

#183920
Edited, Oct 24, 2017 14:11
Vote:
 
<p>Yeah, that's correct. The DiscountedPrice for shipping tax calculation and the extended price for sales tax calculation.</p> <p></p> <p>/Tuan</p>
#183921
Oct 24, 2017 14:33
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.