Coding to handle VAT, and how to do inclusive pricing.

December 22, 2014 at 8:29 PMPhonicUK

If you're an EU business and you want to sell direct to consumers, you're now required to be able to account for VAT, even if you're below the VAT registration threshold that you'd normally fall under.

Those who are selling exclusively through online marketplaces like the Google Play Store, or the Apple App Store probably don't have to worry about this, as they handle calculating VAT for you as well as collecting and paying it. So you don't really need to do anything.

If you're selling to users directly however, and you have your own billing and/or invoicing system that you've built in house (or have modified a 3rd party one), then this page is probably what you want to read.

I'll be going over how to perform VAT calculations in both directions, that is how to add VAT on to an existing price, or calculate the original price if VAT is taken off.

Just for reference, all code samples will be in C# (or pseudocode) - but should be pretty damn similar in Java. Also in code the VAT percentages will always be in decimal format. (20% becomes 0.20M). The decimal data type must also be used, as its the only one with enough precision for performing financial calculations. (NodeJS users take note, this will bite you in the ass real fast!)

First off, before you can do anything - you need to know the VAT rates for the respective European countries, which can be found at (PDF Warning!) - This changes periodically so you need to keep an eye on it.

For the C# programmers, here's that information as something easier to digest:

        private Dictionary<string, decimal> VATRates = new Dictionary<string, decimal>(){
            {"BE", 0.21M},
            {"BG", 0.20M},
            {"CZ", 0.21M},
            {"DK", 0.25M},
            {"DE", 0.19M},
            {"EE", 0.20M},
            {"EL", 0.23M},
            {"ES", 0.21M},
            {"FR", 0.20M},
            {"HR", 0.25M},
            {"IE", 0.23M},
            {"IT", 0.22M},
            {"CY", 0.19M},
            {"LV", 0.21M},
            {"LT", 0.21M},
            {"LU", 0.15M},
            {"HU", 0.27M},
            {"MT", 0.18M},
            {"NL", 0.21M},
            {"AT", 0.20M},
            {"PL", 0.23M},
            {"PT", 0.23M},
            {"RO", 0.24M},
            {"SI", 0.22M},
            {"SK", 0.20M},
            {"FI", 0.24M},
            {"SE", 0.25M},
            {"UK", 0.20M},

Now if you're just sorting out VAT for the first time, you have two options:

  • Make the consumer pay the VAT by increasing the price by the VAT amount.
  • Decrease the 'actual' price so that after adding VAT, it's the original price consumers were paying.
If you're just making the consumer pay the VAT, then the total price is easy to work out:
    TotalAmount = AmountWithoutVAT x (1 + VATRate)
So if the amount of an item without VAT is £10, and your VAT rate is 20%:
    TotalAmount = £10 x (1 + 0.20) = £12
If you've got a total inclusive of VAT, and need to work out what the price was exclusive of VAT, then its backwards:
    AmountWithoutVAT = TotalAmount / (1 + VATRate)
So if we've got a total price with VAT of £12, and a VAT rate of 20%:
    AmountWithoutVAT = £12 / (1 + 0.20) = £10
This incidentally, is the same way you do VAT inclusive pricing regardless of the VAT rate. You use the total you'd like the customer to pay as the TotalAmount, and the VATRate for that users particular country to get the price you actually 'charge' them for the product or service.
I'll write a separate blog post about generating proper VAT invoices in an automated session at another time. I can tell you however that you don't need to go and buy an expensive PDF authoring library if you want to generate nice PDF invoices for customers to print. Instead you can write yourself a HTML template that you populate, and then use 'wkhtmltopdf' (which uses the webkit engine under the hood) to produce nice PDFs. More on this later, but that's something to play with.

Posted in: C# Development


Comments are closed