Skip to content

Instantly share code, notes, and snippets.

@PoeHaH
Created November 8, 2016 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PoeHaH/c467d86118557ad17c464a482769f657 to your computer and use it in GitHub Desktop.
Save PoeHaH/c467d86118557ad17c464a482769f657 to your computer and use it in GitHub Desktop.
PHP code to determine VAT rate for your Belgian services-business
<?php
class VatHelper{
private $countrycodes;
private $vatrates;
public function __construct(){
$this->countrycodes = array('Austria'=>'AT','Belgium'=>'BE','Bulgaria'=>'BG','Croatia'=>'HR','Cyprus'=>'CY','Czech Republic'=>'CZ','Denmark'=>'DK','Estonia'=>'EE','Finland'=>'FI','France'=>'FR','Germany'=>'DE','Greece'=>'EL','Hungary'=>'HU','Ireland'=>'IE','Italy'=>'IT','Latvia'=>'LV','Lithuania'=>'LT','Luxembourg'=>'LU','Malta'=>'MT','Netherlands'=>'NL','Poland'=>'PL','Portugal'=>'PT','Romania'=>'RO','Slovakia'=>'SK','Slovenia'=>'SI','Spain'=>'ES','Sweden'=>'SE','United Kingdom'=>'GB');
$this->vatrates = array('AT' => 20,'BE' => 21,'BG' => 20,'HR' => 25,'CY' => 19,'CZ' => 21,'DK' => 25,'EE' => 20,'FI' => 24,'FR' => 20,'DE' => 19,'EL' => 24,'HU' => 27,'IE' => 23,'IT' => 22,'LV' => 21,'LT' => 21,'LU' => 17,'MT' => 18,'NL' => 21,'PL' => 23,'PT' => 23,'RO' => 20,'SK' => 20,'SI' => 22,'ES' => 21,'SE' => 25,'GB' => 20);
}
public function getTotalPrice($licenseamount,$selectedcountry,$vatid){
$vatrate = $this->getVatRate($selectedcountry,$vatid);
return $licenseamount + (($licenseamount*$vatrate)/100);
}
public function getVatRate($country,$vatid){
$countrycode = $this->countrycodes[$country];
$isEU = array_key_exists($country,$this->countrycodes);
// Not a EU country, so no VAT needs to be applied.
if(!$isEU) return 0;
// It's a EU country and it's our own (Belgium)
if($countrycode === 'BE') return $this->vatrates['BE'];
// It's a EU country, check if we need to apply VAT.
$vatid = str_replace(array(' ', '.', '-', ',', ', '), '', trim($vatid));
// No VAT ID entered, It's a EU private customer, apply VAT from their country.
if(empty($vatid)) return $this->vatrates[$countrycode];
// A VAT ID was entered, it's a EU business. Validate the VAT ID.
$client = new SoapClient("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl");
// Something went wrong creating the SoapClient, assume it's a private EU customer.
// This case shouldn't really happen (maybe when EU servers are down?).
if(!$client) return $this->vatrates[$countrycode];
// Fire off the VAT check
$params = array('countryCode' => $countrycode, 'vatNumber' => $vatid);
try{
$r = $client->checkVat($params);
// Valid EU Business, so we don't need to take VAT into account.
if($r->valid == true){
return 0;
} else { // Invalid VAT ID. We'll assume a private customer then.
return $this->vatrates[$countrycode];
}
}
catch(Exception $e){ // Something went wrong, assume it's a private customer.
return $this->vatrates[$countrycode];
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment