Skip to content

Instantly share code, notes, and snippets.

@JanGalek
Last active May 18, 2018 07:58
Show Gist options
  • Select an option

  • Save JanGalek/9443ff6b9c3024e9877eff4e4a2a2f6c to your computer and use it in GitHub Desktop.

Select an option

Save JanGalek/9443ff6b9c3024e9877eff4e4a2a2f6c to your computer and use it in GitHub Desktop.
SK Ares :)
<?php
$vies = new VIES();
if (($data = $vies->getInfo('SK ' . $values->faktura_dic)) !== false) {
$values->faktura_dic = $data->vatNumber;
$values->faktura_mesto = $data->address['city'];
$values->faktura_firma = $data->traderName;
$values->faktura_ulice = $data->address['street'];
$values->faktura_psc = $data->address['zip'];
} else {
$message = 'Podle zadaného DIČ nebyl nalezen žádný záznam. Zkontrolujte prosím zadané údaje';
$this->flashMessage($message, 'warning');
}
<?php
declare(strict_types=1);
namespace App\Galek\Helper;
class VIES
{
protected static $_viesUrl = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl';
protected $_client;
public function __construct()
{
$this->_client = new \SoapClient(self::$_viesUrl);
}
public function checkVAT($vatNumber)
{
$vatNumber = str_replace('SK SK', 'SK', $vatNumber);
$vatNumber = $this->sanitize($vatNumber);
if ($vatNumber) {
return $this->_client->checkVat($vatNumber)->valid;
}
return false;
}
/**
* Get more information about a given VAT number
* @param string $vatNumber A European VAT number
* @return array|bool Returns false if the VAT number is not valid, or an array containing more information about the VAT number.
* [PHP]
* (object) array(
* 'countryCode' => 'BE',
* 'vatNumber' => '0883923584',
* 'requestDate' => '2011-12-14+01:00',
* 'valid' => true
* 'traderName' => 'GCV TSE-WebDesign',
* 'traderAddress' => 'Ezenhoek 53
* 2590 Berlaar',
* 'requestIdentifier' => ''
* 'address' => [
* 'street' => 'Ezenhoek 53',
* 'city' => 'BerLaar'
* 'zip' => 2590
* 'post' => 'BerLaar'
* ]
* );
* [/PHP]
*/
public function getInfo($vatNumber)
{
$vatNumber = str_replace('SK SK', 'SK', $vatNumber);
if ($this->checkVAT($vatNumber)) {
$data = $this->_client->checkVatApprox($this->sanitize($vatNumber));
$data->address = $this->cutAddress($data->traderAddress);
return $data;
}
return false;
}
/**
* Sanitize the VAT number and split the country code from the VAT number
* @param string $vatNumber A European VAT number
* @return array|bool Returns false if the VAT number does not meet the required standards to make a call to the SOAP client,
* or an array containing the country code and the VAT number.
* [PHP]
* (object) array(
* 'countryCode' => 'BE',
* 'vatNumber' => '0883923584'
* );
* [/PHP]
*/
protected function sanitize($vatNumber)
{
$vatNumber = preg_replace('/[^a-z0-9]+/i', '', $vatNumber);
if (strlen($vatNumber) <= 2) {
return false;
}
return [
'countryCode' => substr($vatNumber, 0, 2),
'vatNumber' => substr($vatNumber, 2),
];
}
protected function cutAddress($address)
{
$matches = null;
$data = explode("\n", $address);
if ($data[0] != 'N/A') {
$street = $data[0];
if (preg_match('/(\d{3})\s?(\d{2})/', $data[1])) {
preg_match('/(\d{3})\s?(\d{2})\s(.*)/', $data[1], $i);
if ($data[2] == 'Slovensko') {
$post = $i[3];
} else {
$post = $data[2];
}
} else {
preg_match('/(\d{3})\s?(\d{2})\s(.*)/', $data[2], $i);
if ($data[2] == 'Slovensko') {
$post = $i[3];
} else {
$post = $data[1];
}
}
$zip = $i[1] . '' . $i[2];
$city = $i[3];
} else {
$street = '';
$city = '';
$zip = '';
$post = '';
}
return [
'street' => $street,
'city' => $city,
'zip' => $zip,
'post' => $post,
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment