For invoicing to other businesses within the EU, it is mandatory to check whether the VAT ID of your customer is correct, in order to invoice using a 0% tax rate. I wrote a script that checks the VAT number that you have from your customer against
the EU database of VAT IDs. The easiest way to use this script is to put it in a function and call that function from a workflow or button.
The function is as follows:
- String checkVAT(String vat_id)
- {
- if(vat_id == "" || vat_id == null)
- {
- return "Enter number in VAT first!";
- }
- else
- {
- vat_id = getAlphaNumeric(vat_id);
- countryCode = vat_id.subString(0,2);
- vatNumber = vat_id.remove(countryCode);
- url = "http://ec.europa.eu/taxation_customs/vies/services/checkVatService";
- //Uncomment next three lines for testing
- // countryCode = "NL";
- // vatNumber = "200";
- // url = "http://ec.europa.eu/taxation_customs/vies/services/checkVatTestService";
- request = "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\"> <Body> <checkVat xmlns=\"urn:ec.europa.eu:taxud:vies:services:checkVat:types\">";
- request = request + "<countryCode>" + countryCode + "</countryCode>";
- request = request + " <vatNumber>" + vatNumber + "</vatNumber>";
- request = request + " </checkVat> </Body></Envelope>";
- headers = Map();
- headers.put("Content-Type","text/xml");
- vat_data = invokeurl
- [
- url :url
- type :POST
- parameters:request
- headers:headers
- ];
- validity = vat_data.getsuffix("valid>");
- validity = validity.getprefix("</");
- if(validity == "true")
- {
- validity = "valid";
- }
- else
- {
- validity = "INVALID";
- }
- name = vat_data.getsuffix("name>");
- name = name.getprefix("</");
- address = vat_data.getsuffix("address>");
- address = address.getprefix("</");
- return "The VAT number is " + validity + if(validity=="valid",". The registered name is " + name + " and the registered address is " + address + ".","");
- }
- }
Here's what the script does:
- It uses the VAT number that was included as parameter vat_id. This can be gathered for instance from a field in your form.
- It returns a string: this is either
- a message that the VAT number was not filled in (first IF statement)
- a message that the number is invalid (row 41)
- a message that the number is valid, including the registered name and address (the IF statement in row 41)
- The service needs the country code and VAT number separately. This is extracted in rows 9-11.
- The request to the service is prepared (row 12-20) and sent (row 21-26). If you want to test without an actual number, uncomment lines 14-16: it will use the test service.
- The returned message is checked for validity of the number (row 27-28) and the name and address are extracted (rows 37-40)
You can extend this script in whichever way you like. For instance, you could write a script that checks whether the name and address match with what is filled in.