Developer forum

Forum » Development » How do i change the VAT on an order

How do i change the VAT on an order

Martin Nielsen
Reply

Hi guys,

 

I'm trying to change the VAT on my orders when a user selects a country within the EU and has entered a VAT number.

 

I've tried different approaches but i have not found a working solution yet, the all have issues.

 

The closest i've come to doing it is the following code:

 

      double calculatedVat = ( (order.Price.PriceWithoutVAT + order.PaymentFee.PriceWithoutVAT) / 100) * newVAT;

      order.AllowOverridePrices = true;
      order.VAT = newVAT;
      order.Price.VAT = calculatedVat; 
      order.Price.VATPercent = newVAT;

 

This correctly changes my VAT, but it prevents the total price from being calculated, making it so all fee's are ignored.

 

I'd like to change the VAT and still have DW do all the math, is that possible?

 

Mvh

 Martin


Replies

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Martin,

 

You should use a PriceProvider to change this. You need to implement the interface ISupportPriceInfo in that PriceProvider. This gives you the possibility to completely control the price including the VAT.

 

You can read more about PriceProviders here: http://developer.dynamicweb-cms.com/documentation/for-developers/ecommerce/extensibility/providers/price-providers.aspx

Especially the section "About the ISupportPriceInfo interface".

 

Hope this helps :)

 

- Jeppe

 
Martin Nielsen
Reply

Are you saying that i cannot achieve this by manipulating the Order object?

 

A PriceProvider seems like an odd place for logic specific to the cart/order.

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

No, that's not what I'm saying. The easiest way is to add the correct price when the product is added to the cart, rather than manipulating the price after.

 

If you cannot perform the correct price calculation until the cart process, then you should change the orderline prices instead of changing the order price.

 

Something like this:

        public void ChangeOrderPrice(Order order, double newVAT)
        {
            if (order == null)
                return;

            double calculatedVat = ((order.Price.PriceWithoutVAT + order.PaymentFee.PriceWithoutVAT) / 100) * newVAT;

            foreach (OrderLine ol in order.OrderLines)
            {
                var currentUnitPrice = ol.UnitPrice;
                currentUnitPrice.VAT = calculatedVat;
                currentUnitPrice.VATPercent = newVAT;

                ol.SetUnitPrice(currentUnitPrice);
            }

            order.ClearCachedPrices();
        }

 

 
Martin Nielsen
Reply

Hi Jeppe,

 

I cannot get you example to work. the VAT amount on my order does not change.

 

How would i handle this like discounts and paymentfee's which also shouldn't have a VAT?

 

 

I tried a combination of yours and my idea and i came up with this:

 

      // Calculate VAT on products in cart
      foreach ( var line in order.OrderLines )
      {        
        double pv = ((line.Price.PriceWithoutVAT) / 100) * newVAT;        
        line.Price.VAT = pv;
        line.AllowOverridePrices = true;
        line.Price.VATPercent = newVAT;
      }
      // Override VAT on prices in cart
      order.AllowOverridePrices = true;

      double shippingFee = ((order.ShippingFee.PriceWithoutVAT) / 100) * newVAT;      
      order.ShippingFee.VATPercent = newVAT;
      order.ShippingFee.VAT = shippingFee;
      
      double calculatedVat = ( (order.Price.PriceWithoutVAT + order.PaymentFee.PriceWithoutVAT) / 100) * newVAT;
      double calculatedVatBeforeFees = (order.PriceBeforeFees.PriceWithoutVAT / 100) * newVAT;
      order.VAT = newVAT;
      order.Price.VAT = calculatedVat; 
      order.Price.VATPercent = newVAT;
      order.PriceBeforeFees.VAT = calculatedVatBeforeFees;
      order.PriceBeforeFees.VATPercent = newVAT;
      
      // Calculate total price
      var totalPrice = order.ShippingFee.PriceWithoutVAT + order.PaymentFee.PriceWithoutVAT + order.PriceBeforeFees.PriceWithoutVAT;

      order.Price.Add( order.ShippingFee );
      order.Price.Add( order.PaymentFee );

 

 

But i cannot get the TotalPrice to contain the Fee's. When i render my templates with the above code running, my shipping and paymentfees are note in the total.

 

// Martin

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

I have some new code for you based on a notification subscriber.

There is, however, an issue; I've not been able to change the VAT for the Payment and Shipping for the presentation. What I mean is, that the order price does contain the Payment and Shipping fees with the new VAT, but the fee tags still contain the old VAT.

 

I will look at it again tomorrow, but I think I need to make a change to the Dynamicweb code base to enable this.

 

        public override void OnNotify(string notification, NotificationArgs args)
        {
            var myArgs = (Dynamicweb.Notifications.eCommerce.Cart.BeforeRenderingNewStepArgs)args;

            var order = myArgs.Order;
            var newVat = 10.0;

            foreach (OrderLine ol in order.OrderLines)
            {
                var olPrice = ol.UnitPrice;
                SetNewVat(olPrice, newVat);

                ol.SetUnitPrice(olPrice, true);
                ol.Type = ((int)OrderLine.OrderLineType.Fixed).ToString();
            }

            order.ClearCachedPrices();

            var payFeeWithoutVat = order.PaymentFee;
            SetNewVat(payFeeWithoutVat, newVat);
            var shipFeeWithoutVat = order.ShippingFee;
            SetNewVat(shipFeeWithoutVat, newVat);

            var newOrderPrice = order.PriceBeforeFees.Add(payFeeWithoutVat).Add(shipFeeWithoutVat);
            
            order.AllowOverridePrices = true;
            order.Price.PriceWithoutVAT = newOrderPrice.PriceWithoutVAT;
            order.Price.PriceWithVAT = newOrderPrice.PriceWithVAT;
            order.Price.VAT = newOrderPrice.VAT;
            order.Price.VATPercent = newOrderPrice.VATPercent;
        }

        private void SetNewVat(PriceInfo price, double newVat)
        {
            price.PriceWithVAT = price.PriceWithoutVAT * (1 + (newVat / 100));
            price.VAT = price.PriceWithVAT - price.PriceWithoutVAT;
            price.VATPercent = newVat;
        }

 

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

So, I've looked some more into this and -- as I feared -- it's not possible to manipulate the fees. Currently every orderline price can be changed and the order total price is also correct. The fee price tags, however, are the only obstacles left. This leads me to conclude that the only workaround is to not use the fee price tags and create your own tags with the correct values.

 

Depending on where and when you do your price manipulation, you may have to create a Template Extender, or a Page OnOutput or some other notification subscriber to set the tag values. You can use Dynamicweb.eCommerce.Frontend.Renderer.RenderPriceInfo to render a price in the same way as eCommerce.

 

I'l look into adding an option similar to AllowOverridePrices for order fees, but I do not have any time frame at this time.

 

By the way, which version of Dynamicweb are you using?

 
Martin Nielsen
Reply

Hi Jeppe, 

I'm on 8.2.1.11 currently.

And i'm trying to change the VAT on the notification "AfterCustomerCountryIsSet" since my logic should run each time the EcomCustomerCountry field is posted.

When would it be possible for you to get this feature assigned and prioritized?
Depending on when it'll be done, we'll decide if we make a workaround or wait around for a fix.

 

 
Martin Nielsen
Reply

I've finally had the time to test your sample, and with my templates and DW settings, everything looks OK.

So i'll do some more testing, and release it to the public if i don't find any more problems.

 

You must be logged in to post in the forum