Fixes for VAT number checks

This commit is contained in:
David Bomba 2023-05-25 16:41:29 +10:00
parent 05accdf1c5
commit 6485e48896
4 changed files with 118 additions and 5 deletions

View File

@ -0,0 +1,65 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Client;
use App\Models\Client;
use App\Models\Company;
use App\Libraries\MultiDB;
use Illuminate\Bus\Queueable;
use App\DataProviders\USStates;
use App\Utils\Traits\MakesHash;
use App\Services\Tax\TaxService;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\Middleware\WithoutOverlapping;
class CheckVat implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
use MakesHash;
public $tries = 1;
/**
* Create a new job instance.
*
* @param Client $client
* @param Company $company
*/
public function __construct(public Client $client, protected Company $company)
{
}
/**
* Execute the job.
*
*/
public function handle()
{
MultiDB::setDb($this->company->db);
$tax_service = new TaxService($this->client);
$tax_service->validateVat();
}
public function middleware()
{
return [new WithoutOverlapping($this->client->id)];
}
}

View File

@ -11,8 +11,10 @@
namespace App\Observers;
use App\Utils\Ninja;
use App\Models\Client;
use App\Models\Webhook;
use App\Jobs\Client\CheckVat;
use App\Jobs\Util\WebhookHandler;
use App\Jobs\Client\UpdateTaxData;
@ -20,6 +22,36 @@ class ClientObserver
{
public $afterCommit = true;
private $eu_country_codes = [
'AT' => '40',
'BE' => '56',
'BG' => '100',
'CY' => '196',
'CZ' => '203',
'DE' => '276',
'DK' => '208',
'EE' => '233',
'ES' => '724',
'FI' => '246',
'FR' => '250',
'GR' => '300',
'HR' => '191',
'HU' => '348',
'IE' => '372',
'IT' => '380',
'LT' => '440',
'LU' => '442',
'LV' => '428',
'MT' => '470',
'NL' => '528',
'PL' => '616',
'PT' => '620',
'RO' => '642',
'SE' => '752',
'SI' => '705',
'SK' => '703',
];
/**
* Handle the client "created" event.
*
@ -33,6 +65,10 @@ class ClientObserver
UpdateTaxData::dispatch($client, $client->company);
}
if(in_array($client->country_id, $this->eu_country_codes) && $client->company->calculate_taxes) {
CheckVat::dispatch($client, $client->company);
}
$subscriptions = Webhook::where('company_id', $client->company_id)
->where('event_id', Webhook::EVENT_CREATE_CLIENT)
->exists();
@ -50,11 +86,17 @@ class ClientObserver
*/
public function updated(Client $client)
{
if($client->getOriginal('postal_code') != $client->postal_code && $client->country_id == 840 && $client->company->calculate_taxes)
{
/** Monitor postal code changes for US based clients for tax calculations */
if(Ninja::isHosted() && $client->getOriginal('postal_code') != $client->postal_code && $client->country_id == 840 && $client->company->calculate_taxes) {
UpdateTaxData::dispatch($client, $client->company);
}
/** Monitor vat numbers for EU based clients for tax calculations */
if($client->getOriginal('vat_number') != $client->vat_number && in_array($client->country_id, $this->eu_country_codes) && $client->company->calculate_taxes) {
CheckVat::dispatch($client, $client->company);
}
$event = Webhook::EVENT_UPDATE_CLIENT;
if ($client->getOriginal('deleted_at') && !$client->deleted_at) {

View File

@ -117,9 +117,12 @@ class ClientRepository extends BaseRepository
*/
public function create($client): ?Client
{
/** @var \App\Models\User $user */
$user = auth()->user();
return $this->save(
$client,
ClientFactory::create(auth()->user()->company()->id, auth()->user()->id)
ClientFactory::create($user->company()->id, $user->id)
);
}

View File

@ -26,10 +26,13 @@ class TaxService
$vat_check = (new VatNumberCheck($this->client->vat_number, $client_country_code))->run();
$this->client->has_valid_vat_number = $vat_check->isValid();
$this->client->saveQuietly();
if($vat_check->isValid()) {
$this->client->has_valid_vat_number = true;
$this->client->saveQuietly();
}
return $this;
}
public function initTaxProvider()