Validation tests

This commit is contained in:
David Bomba 2024-10-22 11:17:43 +11:00
parent 29a372b65b
commit 1d8b00f55a
13 changed files with 490 additions and 109 deletions

View File

@ -11,6 +11,7 @@
namespace App\Http\Controllers;
use App\Http\Requests\EInvoice\Peppol\AddTaxIdentifierRequest;
use App\Http\Requests\EInvoice\Peppol\CreateRequest;
use App\Http\Requests\EInvoice\Peppol\DisconnectRequest;
use App\Services\EDocument\Gateway\Storecove\Storecove;
@ -25,17 +26,14 @@ class EInvoicePeppolController extends BaseController
*/
$company = auth()->user()->company();
$data = [
...$request->validated(),
'country' => $request->country()->iso_3166_2,
];
$legal_entity_response = $storecove->createLegalEntity($request->validated(), $company);
$legal_entity_response = $storecove->createLegalEntity($data, $company);
$scheme = $storecove->router->resolveRouting($request->country, $company->settings->classification);
$add_identifier_response = $storecove->addIdentifier(
legal_entity_id: $legal_entity_response['id'],
identifier: $company->settings->vat_number,
scheme: $request->receiverIdentifier(),
scheme: $scheme,
);
if ($add_identifier_response) {
@ -50,6 +48,18 @@ class EInvoicePeppolController extends BaseController
return response()->noContent(status: 422);
}
public function addAdditionalTaxIdentifier(AddTaxIdentifierRequest $request, Storecove $storecove): Response
{
$company = auth()->user()->company();
$scheme = $storecove->router->resolveRouting($request->country, $company->settings->classification);
$storecove->addAdditionalTaxIdentifier($company->legal_entity_id, $request->identifier, $scheme);
return response()->json(['message' => 'ok'], 200);
}
public function disconnect(DisconnectRequest $request, Storecove $storecove): Response
{
/**

View File

@ -32,6 +32,36 @@ class UpdateCompanyRequest extends Request
'portal_custom_css',
'portal_custom_head'
];
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}$/',
];
/**
* Determine if the user is authorized to make this request.
@ -86,6 +116,24 @@ class UpdateCompanyRequest extends Request
$rules['inbound_mailbox_whitelist'] = ['sometimes', 'string', 'nullable', 'regex:/^[\w\-\.\+]+@([\w-]+\.)+[\w-]{2,4}(,[\w\-\.\+]+@([\w-]+\.)+[\w-]{2,4})*$/'];
$rules['inbound_mailbox_blacklist'] = ['sometimes', 'string', 'nullable', 'regex:/^[\w\-\.\+]+@([\w-]+\.)+[\w-]{2,4}(,[\w\-\.\+]+@([\w-]+\.)+[\w-]{2,4})*$/'];
$rules['settings.vat_number'] = [
'nullable',
'string',
'bail',
'sometimes',
Rule::requiredIf(function () {
return $this->input('settings.e_invoice_type') === 'PEPPOL';
}),
function ($attribute, $value, $fail) {
$country_code = $this->getCountryCode();
if ($country_code && isset($this->vat_regex_patterns[$country_code]) && $this->input('settings.e_invoice_type') === 'PEPPOL') {
if (!preg_match($this->vat_regex_patterns[$country_code], $value)) {
$fail(ctrans('texts.invalid_vat_number'));
}
}
},
];
return $rules;
}
@ -139,6 +187,12 @@ class UpdateCompanyRequest extends Request
$this->replace($input);
}
private function getCountryCode()
{
return auth()->user()->company()->country()->iso_3166_2;
}
/**
* For the hosted platform, we restrict the feature settings.
*

View File

@ -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;
});
}
}

View File

@ -17,9 +17,42 @@ use App\Rules\EInvoice\Peppol\SupportsReceiverIdentifier;
use App\Services\EDocument\Standards\Peppol\ReceiverIdentifier;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class CreateRequest 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
{
/**
@ -31,7 +64,7 @@ class CreateRequest extends FormRequest
return true;
}
return $user->account->isPaid() &&
return $user->account->isPaid() && $user->isAdmin() &&
$user->company()->legal_entity_id === null;
}
@ -45,7 +78,7 @@ class CreateRequest extends FormRequest
'line1' => ['required', 'string'],
'line2' => ['nullable', 'string'],
'city' => ['required', 'string'],
'country' => ['required', 'integer', 'exists:countries,id', new SupportsReceiverIdentifier()],
'country' => ['required', 'bail', Rule::in(array_keys($this->vat_regex_patterns))],
'zip' => ['required', 'string'],
'county' => ['required', 'string'],
];
@ -58,15 +91,28 @@ class CreateRequest extends FormRequest
);
}
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
{
return Country::find($this->country);
/** @var \Illuminate\Support\Collection<\App\Models\Country> */
$countries = app('countries');
return $countries->first(function ($c){
return $this->country == $c->id;
});
}
public function receiverIdentifier(): string
{
$identifier = new ReceiverIdentifier($this->country()->iso_3166_2);
return $identifier->get();
}
}

View File

@ -417,7 +417,7 @@ class NinjaMailerJob implements ShouldQueue
$company = $this->company;
$smtp_host = $company->smtp_host ?? '';
$smtp_port = (int)$company->smtp_port;
$smtp_port = $company->smtp_port ?? 0;
$smtp_username = $company->smtp_username ?? '';
$smtp_password = $company->smtp_password ?? '';
$smtp_encryption = $company->smtp_encryption ?? 'tls';
@ -437,7 +437,7 @@ class NinjaMailerJob implements ShouldQueue
'mail.mailers.smtp' => [
'transport' => 'smtp',
'host' => $smtp_host,
'port' => $smtp_port,
'port' => (int)$smtp_port,
'username' => $smtp_username,
'password' => $smtp_password,
'encryption' => $smtp_encryption,

View File

@ -240,30 +240,6 @@ class Company extends BaseModel
use AppSetup;
use \Awobaz\Compoships\Compoships;
// const ENTITY_RECURRING_INVOICE = 'recurring_invoice';
// const ENTITY_CREDIT = 'credit';
// const ENTITY_QUOTE = 'quote';
// const ENTITY_TASK = 'task';
// const ENTITY_EXPENSE = 'expense';
// const ENTITY_PROJECT = 'project';
// const ENTITY_VENDOR = 'vendor';
// const ENTITY_TICKET = 'ticket';
// const ENTITY_PROPOSAL = 'proposal';
// const ENTITY_RECURRING_EXPENSE = 'recurring_expense';
// const ENTITY_RECURRING_TASK = 'task';
// const ENTITY_RECURRING_QUOTE = 'recurring_quote';
/** @var CompanyPresenter */
protected $presenter = CompanyPresenter::class;

View File

@ -1,31 +0,0 @@
<?php
namespace App\Rules\EInvoice\Peppol;
use App\Models\Country;
use App\Services\EDocument\Standards\Peppol\ReceiverIdentifier;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class SupportsReceiverIdentifier implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$country = Country::find($value);
if ($country === null) {
$fail(ctrans('texts.peppol_country_not_supported'));
}
$checker = new ReceiverIdentifier($country->iso_3166_2);
if ($checker->get() === null) {
$fail(ctrans('texts.peppol_country_not_supported'));
}
}
}

View File

@ -222,7 +222,7 @@ class Storecove
}
$company_defaults = [
'acts_as_receiver' => false,
'acts_as_receiver' => true,
'acts_as_sender' => true,
'advertisements' => ['invoice'],
];
@ -316,6 +316,41 @@ class Storecove
}
/**
* addAdditionalTaxIdentifier
*
* Adds an additional TAX identifier to the legal entity, where they are selling cross border
* and are required to be registered in the destination country.
*
* @param int $legal_entity_id
* @param string $identifier
* @param string $scheme
* @return mixed
*/
public function addAdditionalTaxIdentifier(int $legal_entity_id, string $identifier, string $scheme)
{
$uri = "legal_entities/{$legal_entity_id}/additional_tax_identifiers";
$data = [
"identifier" => $identifier,
"scheme" => $scheme,
"superscheme" => "iso6523-actorid-upis",
];
$r = $this->httpClient($uri, (HttpVerb::POST)->value, $data);
if ($r->successful()) {
$data = $r->json();
return $data;
}
return $r;
}
/**
* deleteIdentifier
*

View File

@ -1,34 +0,0 @@
<?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\Services\EDocument\Standards\Peppol;
// https://www.storecove.com/docs/#_receiver_identifiers_list
class ReceiverIdentifier
{
public array $mappings = [
'DE' => 'DE:VAT',
// @todo: Check with Dave what other countries we support.
];
public function __construct(
public string $country,
) {
}
public function get(): ?string
{
return $this->mappings[$this->country] ?? null;
}
}

View File

@ -622,7 +622,7 @@ class Email implements ShouldQueue
$company = $this->company;
$smtp_host = $company->smtp_host ?? '';
$smtp_port = (int)$company->smtp_port;
$smtp_port = $company->smtp_port ?? 0;
$smtp_username = $company->smtp_username ?? '';
$smtp_password = $company->smtp_password ?? '';
$smtp_encryption = $company->smtp_encryption ?? 'tls';
@ -641,7 +641,7 @@ class Email implements ShouldQueue
'mail.mailers.smtp' => [
'transport' => 'smtp',
'host' => $smtp_host,
'port' => $smtp_port,
'port' => (int)$smtp_port,
'username' => $smtp_username,
'password' => $smtp_password,
'encryption' => $smtp_encryption,

View File

@ -5402,6 +5402,7 @@ $lang = array(
'credit_updated' => 'Credit Updated',
'payment_updated' => 'Payment Updated',
'search_placeholder' => 'Find invoices, clients, and more',
'invalid_vat_number' => "The VAT number is not valid for the selected country. Format should be Country Code followed by number only ie, DE123456789",
);
return $lang;

View File

@ -0,0 +1,118 @@
<?php
namespace Tests\Unit\Requests\EInvoice\Peppol;
use Tests\TestCase;
use Illuminate\Support\Facades\Validator;
use App\Http\Requests\EInvoice\Peppol\AddTaxIdentifierRequest;
class AddTaxIdentifierRequestTest extends TestCase
{
protected AddTaxIdentifierRequest $request;
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}$/',
];
protected function setUp(): void
{
parent::setUp();
$this->request = new AddTaxIdentifierRequest();
}
public function testValidInput()
{
$validator = Validator::make([
'country' => 'DE',
'vat_number' => 'DE123456789',
], $this->request->rules());
$this->assertTrue($validator->passes());
}
public function testInvalidCountry()
{
$validator = Validator::make([
'country' => 'US',
'vat_number' => 'DE123456789',
], $this->request->rules());
$this->assertFalse($validator->passes());
$this->assertArrayHasKey('country', $validator->errors()->toArray());
}
public function testInvalidVatNumber()
{
$data = [
'country' => 'DE',
'vat_number' => 'DE12345', // Too short
];
$rules = [
'country' => ['required', 'bail'],
'vat_number' => [
'required',
'string',
'bail',
function ($attribute, $value, $fail) use ($data){
if ( isset($this->vat_regex_patterns[$data['country']])) {
if (!preg_match($this->vat_regex_patterns[$data['country']], $value)) {
$fail(ctrans('texts.invalid_vat_number'));
}
}
},
]
];
$validator = Validator::make($data, $rules);
$this->assertFalse($validator->passes());
$this->assertArrayHasKey('vat_number', $validator->errors()->toArray());
}
public function testMissingCountry()
{
$validator = Validator::make([
'vat_number' => 'DE123456789',
], $this->request->rules());
$this->assertFalse($validator->passes());
$this->assertArrayHasKey('country', $validator->errors()->toArray());
}
public function testMissingVatNumber()
{
$validator = Validator::make([
'country' => 'DE',
], $this->request->rules());
$this->assertFalse($validator->passes());
$this->assertArrayHasKey('vat_number', $validator->errors()->toArray());
}
}

View File

@ -0,0 +1,91 @@
<?php
namespace Tests\Feature\EInvoice\Validation;
use Tests\TestCase;
use Illuminate\Support\Facades\Validator;
use App\Http\Requests\EInvoice\Peppol\CreateRequest;
use App\Models\Country;
use Illuminate\Support\Collection;
class CreateRequestTest extends TestCase
{
protected CreateRequest $request;
protected function setUp(): void
{
parent::setUp();
$this->request = new CreateRequest();
}
public function testValidInput()
{
$validator = Validator::make([
'party_name' => 'Test Company',
'line1' => '123 Test St',
'city' => 'Test City',
'country' => 'DE', // Assuming 1 is the ID for Germany
'zip' => '12345',
'county' => 'Test County',
], $this->request->rules());
$this->assertTrue($validator->passes());
}
public function testInvalidCountry()
{
$validator = Validator::make([
'party_name' => 'Test Company',
'line1' => '123 Test St',
'city' => 'Test City',
'country' => 999, // Invalid country ID
'zip' => '12345',
'county' => 'Test County',
], $this->request->rules());
$this->assertFalse($validator->passes());
$this->assertArrayHasKey('country', $validator->errors()->toArray());
}
public function testMissingRequiredFields()
{
$validator = Validator::make([
'line2' => 'Optional line',
], $this->request->rules());
$this->assertFalse($validator->passes());
$errors = $validator->errors()->toArray();
$this->assertArrayHasKey('party_name', $errors);
$this->assertArrayHasKey('line1', $errors);
$this->assertArrayHasKey('city', $errors);
$this->assertArrayHasKey('country', $errors);
$this->assertArrayHasKey('zip', $errors);
$this->assertArrayHasKey('county', $errors);
}
public function testOptionalLine2()
{
$validator = Validator::make([
'party_name' => 'Test Company',
'line1' => '123 Test St',
'line2' => 'Optional line',
'city' => 'Test City',
'country' => 'AT',
'zip' => '12345',
'county' => 'Test County',
], $this->request->rules());
$this->assertTrue($validator->passes());
}
public function testCountryPreparation()
{
$request = new CreateRequest([
'country' => '276', // Assuming 1 is the ID for Germany
]);
$request->prepareForValidation();
$this->assertEquals('DE', $request->input('country'));
}
}