Performs a validation check at the entity level

This commit is contained in:
David Bomba 2024-09-03 13:42:22 +10:00
parent bff5b627fd
commit 26d55d112d
3 changed files with 172 additions and 5 deletions

View File

@ -51,6 +51,8 @@ class Storecove
"identifier" => "1200109963131"
];
private ?int $legal_entity_id;
public StorecoveRouter $router;
public function __construct()
@ -129,6 +131,7 @@ class Storecove
*/
public function sendDocument(string $document, int $routing_id, array $override_payload = [])
{
$this->legal_entity_id = $routing_id;
$payload = [
"legalEntityId" => $routing_id,
@ -348,22 +351,32 @@ class Storecove
private function httpClient(string $uri, string $verb, array $data, ?array $headers = [])
{
$r = Http::withToken(config('ninja.storecove_api_key'));
try {
$r->withHeaders($this->getHeaders($headers))
$r = Http::withToken(config('ninja.storecove_api_key'))
->withHeaders($this->getHeaders($headers))
->{$verb}("{$this->base_url}{$uri}", $data)->throw();
}
catch (ClientException $e) {
// 4xx errors
nlog("LEI:: {$this->legal_entity_id}");
nlog("Client error: " . $e->getMessage());
nlog("Response body: " . $e->getResponse()->getBody()->getContents());
} catch (ServerException $e) {
// 5xx errors
nlog("LEI:: {$this->legal_entity_id}");
nlog("Server error: " . $e->getMessage());
nlog("Response body: " . $e->getResponse()->getBody()->getContents());
} catch (RequestException $e) {
} catch (\Illuminate\Http\Client\RequestException $e) {
nlog("LEI:: {$this->legal_entity_id}");
nlog("Request error: {$e->getCode()}: " . $e->getMessage());
$responseBody = $e->response->body();
nlog("Response body: " . $responseBody);
return $e->response;
}
return $r; // @phpstan-ignore-line

View File

@ -305,7 +305,7 @@ class Peppol extends AbstractService
private function setInvoice(): self
{
if($this->invoice->e_invoice) {
if($this->invoice->e_invoice && isset($this->invoice->e_invoice->Invoice)) {
$this->p_invoice = $this->e->decode('Peppol', json_encode($this->invoice->e_invoice->Invoice), 'json');

View File

@ -0,0 +1,154 @@
<?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\Validation\Peppol;
use App\Models\Client;
use App\Models\Invoice;
use Illuminate\Support\Facades\App;
class EntityLevel
{
private array $client_fields = [
'address1',
'city',
'state',
'postal_code',
'country_id',
];
private array $company_settings_fields = [
'address1',
'city',
'state',
'postal_code',
'country_id',
];
private array $company_fields = [
// 'legal_entity_id',
// 'vat_number IF NOT an individual
];
private array $invoice_fields = [
// 'number',
];
private array $errors = [];
public function __invoke($entity): array
{
App::forgetInstance('translator');
$t = app('translator');
App::setLocale($entity->company->locale());
$this->errors['status'] = false;
$this->errors['client'] = $entity->client ? $this->testClientState($entity) : [];
$this->errors['company'] = $this->testCompanyState($entity->company);
$this->errors['invoice'] = $entity instanceof Invoice ? $this->testInvoiceState($entity) : [];
// $this->errors['vendor']= $entity->client ? $this->testClientState($entity) : [];
if(
count($this->errors['client']) == 0 &&
count($this->errors['company']) == 0
){
$this->errors['status'] = true;
}
return $this->errors;
}
private function testClientState($entity): array
{
$errors = [];
foreach($this->client_fields as $field)
{
if($this->validString($entity->client->{$field}))
continue;
$errors[] = ['field' => ctrans("texts.{$field}")];
}
//If not an individual, you MUST have a VAT number
if ($client->classification != 'individual' && !$this->validString($client->vat_number)) {
$errors[] = ['field' => ctrans("texts.vat_number")];
}
return $errors;
}
private function testCompanyState($entity): array
{
$settings_object = $entity->client ? $entity->client : $entity->company;
$errors = [];
foreach($this->company_settings_fields as $field)
{
if($this->validString($settings_object->getSetting($field)))
continue;
$errors[] = ['field' => ctrans("texts.{$field}")];
}
//test legal entity id present
if(!is_int($entity->company->legal_entity_id))
$errors[] = ['field' => "You have not registered a legal entity id as yet."];
//If not an individual, you MUST have a VAT number
if($settings_object->getSetting('classification') != 'individual' && !$this->validString($settings_object->getSetting('vat_number')))
{
$errors[] = ['field' => ctrans("texts.vat_number")];
}
// foreach($this->company_fields as $field)
// {
// }
return $errors;
}
private function testInvoiceState(): array
{
$errors = [];
foreach($this->invoice_fields as $field)
{
}
return $errors;
}
// private function testVendorState(): array
// {
// }
/************************************ helpers ************************************/
private function validString(?string $string)
{
return iconv_strlen($string) > 1;
}
}