mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-23 20:00:33 -04:00
Fixes for VAT number checks
This commit is contained in:
parent
05accdf1c5
commit
6485e48896
65
app/Jobs/Client/CheckVat.php
Normal file
65
app/Jobs/Client/CheckVat.php
Normal 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)];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -11,8 +11,10 @@
|
|||||||
|
|
||||||
namespace App\Observers;
|
namespace App\Observers;
|
||||||
|
|
||||||
|
use App\Utils\Ninja;
|
||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
use App\Models\Webhook;
|
use App\Models\Webhook;
|
||||||
|
use App\Jobs\Client\CheckVat;
|
||||||
use App\Jobs\Util\WebhookHandler;
|
use App\Jobs\Util\WebhookHandler;
|
||||||
use App\Jobs\Client\UpdateTaxData;
|
use App\Jobs\Client\UpdateTaxData;
|
||||||
|
|
||||||
@ -20,6 +22,36 @@ class ClientObserver
|
|||||||
{
|
{
|
||||||
public $afterCommit = true;
|
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.
|
* Handle the client "created" event.
|
||||||
*
|
*
|
||||||
@ -33,6 +65,10 @@ class ClientObserver
|
|||||||
UpdateTaxData::dispatch($client, $client->company);
|
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)
|
$subscriptions = Webhook::where('company_id', $client->company_id)
|
||||||
->where('event_id', Webhook::EVENT_CREATE_CLIENT)
|
->where('event_id', Webhook::EVENT_CREATE_CLIENT)
|
||||||
->exists();
|
->exists();
|
||||||
@ -50,11 +86,17 @@ class ClientObserver
|
|||||||
*/
|
*/
|
||||||
public function updated(Client $client)
|
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);
|
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;
|
$event = Webhook::EVENT_UPDATE_CLIENT;
|
||||||
|
|
||||||
if ($client->getOriginal('deleted_at') && !$client->deleted_at) {
|
if ($client->getOriginal('deleted_at') && !$client->deleted_at) {
|
||||||
|
@ -117,9 +117,12 @@ class ClientRepository extends BaseRepository
|
|||||||
*/
|
*/
|
||||||
public function create($client): ?Client
|
public function create($client): ?Client
|
||||||
{
|
{
|
||||||
|
/** @var \App\Models\User $user */
|
||||||
|
$user = auth()->user();
|
||||||
|
|
||||||
return $this->save(
|
return $this->save(
|
||||||
$client,
|
$client,
|
||||||
ClientFactory::create(auth()->user()->company()->id, auth()->user()->id)
|
ClientFactory::create($user->company()->id, $user->id)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,10 +26,13 @@ class TaxService
|
|||||||
|
|
||||||
$vat_check = (new VatNumberCheck($this->client->vat_number, $client_country_code))->run();
|
$vat_check = (new VatNumberCheck($this->client->vat_number, $client_country_code))->run();
|
||||||
|
|
||||||
$this->client->has_valid_vat_number = $vat_check->isValid();
|
if($vat_check->isValid()) {
|
||||||
|
$this->client->has_valid_vat_number = true;
|
||||||
$this->client->saveQuietly();
|
$this->client->saveQuietly();
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function initTaxProvider()
|
public function initTaxProvider()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user