From 310d14fd9cc9e0e26a09764c355f0282acbb5892 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 25 May 2023 17:19:52 +1000 Subject: [PATCH] VAT number checker --- app/Services/Tax/TaxService.php | 19 ++++++++++++++++++- app/Services/Tax/VatNumberCheck.php | 14 ++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/app/Services/Tax/TaxService.php b/app/Services/Tax/TaxService.php index 02d1f21623..d724e175ac 100644 --- a/app/Services/Tax/TaxService.php +++ b/app/Services/Tax/TaxService.php @@ -22,17 +22,34 @@ class TaxService public function validateVat(): self { + if(!extension_loaded('soap')) { + nlog("Install the PHP SOAP extension if you wish to check VAT Numbers. See https://www.php.net/manual/en/soap.installation.php for more information on installing the PHP"); + return $this; + } + $client_country_code = $this->client->shipping_country ? $this->client->shipping_country->iso_3166_2 : $this->client->country->iso_3166_2; $vat_check = (new VatNumberCheck($this->client->vat_number, $client_country_code))->run(); + nlog($vat_check); + if($vat_check->isValid()) { + $this->client->has_valid_vat_number = true; + + if(!$this->client->name && strlen($vat_check->getName()) > 2) { + $this->client->name = $vat_check->getName(); + } + + if(empty($this->client->private_notes) && strlen($vat_check->getAddress()) > 2) { + $this->client->private_notes = $vat_check->getAddress(); + } + $this->client->saveQuietly(); } return $this; - + } public function initTaxProvider() diff --git a/app/Services/Tax/VatNumberCheck.php b/app/Services/Tax/VatNumberCheck.php index e2eea03ada..61c5f2bb50 100644 --- a/app/Services/Tax/VatNumberCheck.php +++ b/app/Services/Tax/VatNumberCheck.php @@ -15,7 +15,7 @@ class VatNumberCheck { private array $response = []; - public function __construct(protected string $vat_number, protected string $country_code) + public function __construct(protected ?string $vat_number, protected string $country_code) { } @@ -32,7 +32,7 @@ class VatNumberCheck $client = new \SoapClient($wsdl); $params = [ 'countryCode' => $this->country_code, - 'vatNumber' => $this->vat_number + 'vatNumber' => $this->vat_number ?? '' ]; $response = $client->checkVat($params); @@ -63,4 +63,14 @@ class VatNumberCheck { return $this->response['valid']; } + + public function getName() + { + return isset($this->response['name']) ? $this->response['name'] : ''; + } + + public function getAddress() + { + return isset($this->response['address']) ? $this->response['address'] : ''; + } }