forked from invoiceninja/invoiceninja
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
488 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
app/Http/Requests/EInvoice/Peppol/AddTaxIdentifierRequest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
/** | ||
* Invoice Ninja (https://invoiceninja.com). | ||
* | ||
* @link https://github.com/invoiceninja/invoiceninja source repository | ||
* | ||
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com) | ||
* | ||
* @license https://www.elastic.co/licensing/elastic-license | ||
*/ | ||
|
||
namespace App\Http\Requests\EInvoice\Peppol; | ||
|
||
use App\Models\Country; | ||
use Illuminate\Validation\Rule; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Auth\Access\AuthorizationException; | ||
use App\Rules\EInvoice\Peppol\SupportsReceiverIdentifier; | ||
use App\Services\EDocument\Standards\Peppol\ReceiverIdentifier; | ||
|
||
class AddTaxIdentifierRequest extends FormRequest | ||
{ | ||
private array $vat_regex_patterns = [ | ||
'DE' => '/^DE\d{9}$/', | ||
'AT' => '/^ATU\d{8}$/', | ||
'BE' => '/^BE0\d{9}$/', | ||
'BG' => '/^BG\d{9,10}$/', | ||
'CY' => '/^CY\d{8}L$/', | ||
'HR' => '/^HR\d{11}$/', | ||
'DK' => '/^DK\d{8}$/', | ||
'ES' => '/^ES[A-Z0-9]\d{7}[A-Z0-9]$/', | ||
'EE' => '/^EE\d{9}$/', | ||
'FI' => '/^FI\d{8}$/', | ||
'FR' => '/^FR\d{2}\d{9}$/', | ||
'EL' => '/^EL\d{9}$/', | ||
'HU' => '/^HU\d{8}$/', | ||
'IE' => '/^IE\d{7}[A-Z]{1,2}$/', | ||
'IT' => '/^IT\d{11}$/', | ||
'LV' => '/^LV\d{11}$/', | ||
'LT' => '/^LT(\d{9}|\d{12})$/', | ||
'LU' => '/^LU\d{8}$/', | ||
'MT' => '/^MT\d{8}$/', | ||
'NL' => '/^NL\d{9}B\d{2}$/', | ||
'PL' => '/^PL\d{10}$/', | ||
'PT' => '/^PT\d{9}$/', | ||
'CZ' => '/^CZ\d{8,10}$/', | ||
'RO' => '/^RO\d{2,10}$/', | ||
'SK' => '/^SK\d{10}$/', | ||
'SI' => '/^SI\d{8}$/', | ||
'SE' => '/^SE\d{12}$/', | ||
]; | ||
|
||
public function authorize(): bool | ||
{ | ||
/** | ||
* @var \App\Models\User | ||
*/ | ||
$user = auth()->user(); | ||
|
||
if (app()->isLocal()) { | ||
return true; | ||
} | ||
|
||
return $user->account->isPaid() && $user->isAdmin() && $user->company()->legal_entity_id != null; | ||
} | ||
|
||
/** | ||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'country' => ['required', 'bail', Rule::in(array_keys($this->vat_regex_patterns))], | ||
'vat_number' => [ | ||
'required', | ||
'string', | ||
'bail', | ||
function ($attribute, $value, $fail) { | ||
if ($this->country && isset($this->vat_regex_patterns[$this->country])) { | ||
if (!preg_match($this->vat_regex_patterns[$this->country], $value)) { | ||
$fail(ctrans('texts.invalid_vat_number')); | ||
} | ||
} | ||
}, | ||
] | ||
]; | ||
} | ||
|
||
public function prepareForValidation() | ||
{ | ||
$input = $this->all(); | ||
|
||
if(isset($input['country'])) { | ||
$country = $this->country(); | ||
$input['country'] = $country->iso_3166_2; | ||
} | ||
|
||
$this->replace($input); | ||
|
||
} | ||
|
||
public function country(): Country | ||
{ | ||
|
||
/** @var \Illuminate\Support\Collection<\App\Models\Country> */ | ||
$countries = app('countries'); | ||
|
||
return $countries->first(function ($c){ | ||
return $this->country == $c->id; | ||
}); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.