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


Mono - Checking if your application is bundled with mkbundle or similar.

April 4, 2012 at 10:40 PMPhonicUK

'mkbundle' is a utility that ships with Mono that allows you to embed the Mono runtime into your application so you are left with just a single executable file that doesn't require Mono to be installed on the target system. You can either embed just certain parts of Mono or the entire thing.

There are cases where you may want to know at runtime whether you are running as part of a bundle or not, thankfully this is extremely simple:

IsBundled = (typeof(int).Assembly.Location == "mscorlib.dll");

When the application is not bundled, the assembly location for the standard objects will be something like /usr/lib/mono/2.0/mscorlib.dll - but when it is bundled then it's just mscorlib.dll since that file is embedded in the current executable.

Posted in: C# Development | Linux | Mono Development

Tags: , , , ,

Binding outgoing .Net web service calls to a specific IP

March 13, 2012 at 5:51 PMPhonicUK

If you're accessing a remote .Net web service where the remote server checks the IP address of the incoming request, either for authentication or some other reason, you can quickly run into some interesting problems on servers with multiple IP addresses, either on different physical interfaces or multiple addresses routed to the same NIC.

So there are some cases where you need to change which of a machines local IP addresses a request will be made from.

While doing this with .Net web services isn't massively complicated, it is tricky finding out any documentation on how to do it.

In this example, I have a remote .Net web service called StatsReporter. I've added the reference and have the generated code. Now I need to extend it:

using System.Net;
using System;

namespace MyProject.StatsReporter
    public partial class StatsReporter : System.Web.Services.Protocols.SoapHttpClientProtocol
        protected override System.Net.WebRequest GetWebRequest(Uri uri)
            HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(uri);
            Request.ServicePoint.BindIPEndPointDelegate = BindIPEndPointCallback;
            Request.Proxy = null;
            return (Request);

        public IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
            IPAddress IPEndPoint = IPAddress.Any;

                IPEndPoint = IPAddress.Parse(GetBindIPAddress());

            return new IPEndPoint(IPEndPoint, 0);

The original generated code is implemented as a partial class, so the above code can be added to a new file. You'll need to implement GetBindIPAddress() yourself to return the IP that you actually want to use. But aside from that it works.

So what's going on? All you're doing is replacing the WebRequest object that gets created when a service call is made and replacing it with your own. One that has its BindIPEndPointDelegate set to one that will return the IP address to bind to.

In this example I deliberately set the proxy to null to avoid having the service go and 'look' for a proxy server during the first call (which can cause a significant delay) - if your code is going to operate in an environment where non-transparent proxies are being used, you'll want to remove that line.

Posted in: C# Development

Tags: , , , , , , ,