Additional routes for legal entities

This commit is contained in:
David Bomba 2024-10-22 13:24:26 +11:00
parent 553e24615c
commit 27850f683a
6 changed files with 328 additions and 21 deletions

View File

@ -21,7 +21,13 @@ class TaxModel
/** @var object $regions */ /** @var object $regions */
public object $regions; public object $regions;
/** @var bool $act_as_sender */
public bool $act_as_sender = false;
/** @var bool $act_as_receiver */
public bool $act_as_receiver = false;
/** /**
* __construct * __construct
* *
@ -34,16 +40,17 @@ class TaxModel
if(!$model) { if(!$model) {
$this->regions = $this->init(); $this->regions = $this->init();
} else { } else {
if(is_null($model->seller_subregion)) {//@phpstan-ignore-line $this->seller_subregion = $model->seller_subregion ?? '';
$this->seller_subregion = ''; $this->act_as_sender = $model->act_as_sender ?? false;
} $this->act_as_receiver = $model->act_as_receiver ?? false;
//@phpstan-ignore-next-line $modelArray = get_object_vars($model);
foreach($model as $key => $value) { foreach ($modelArray as $key => $value) {
$this->{$key} = $value; $this->{$key} = $value;
} }
} }
$this->migrate(); $this->migrate();

View File

@ -17,9 +17,63 @@ use App\Services\EDocument\Gateway\Storecove\Storecove;
use App\Http\Requests\EInvoice\Peppol\DisconnectRequest; use App\Http\Requests\EInvoice\Peppol\DisconnectRequest;
use App\Http\Requests\EInvoice\Peppol\AddTaxIdentifierRequest; use App\Http\Requests\EInvoice\Peppol\AddTaxIdentifierRequest;
use App\Http\Requests\EInvoice\Peppol\ShowEntityRequest; use App\Http\Requests\EInvoice\Peppol\ShowEntityRequest;
use App\Http\Requests\EInvoice\Peppol\UpdateEntityRequest;
class EInvoicePeppolController extends BaseController class EInvoicePeppolController extends BaseController
{ {
/**
* Returns the legal entity ID
*
*
* [
* "id" => 290868,
* "party_name" => "Untitled Company",
* "line1" => "Address 1",
* "line2" => "Address 2",
* "zip" => "Postal Code",
* "city" => "City",
* "county" => "State",
* "country" => "DE",
* "tenant_id" => "EbRYYRWO7oUJE3G3jVa4Xddf6gHGI6kD",
* "public" => true,
* "acts_as_sender" => true,
* "acts_as_receiver" => true,
* "tax_registered" => true,
* "peppol_identifiers" => [
* [
* "superscheme" => "iso6523-actorid-upis",
* "scheme" => "DE:VAT",
* "identifier" => "DE923356489",
* "networks" => [
* "peppol",
* ],
* "corppass_enabled" => false,
* ],
* ],
* "administrations" => [],
* "advertisements" => [
* "invoice",
* ],
* "smart_inbox" => "a4p2q0@receive.storecove.com",
* "api_keys" => [],
* "additional_tax_identifiers" => [
* [
* "id" => 264566,
* "legal_entity_id" => 290868,
* "country" => null,
* "county" => null,
* "identifier" => "ATU73769157",
* "superscheme" => "iso6523-actorid-upis",
* "scheme" => "AT:VAT",
* ],
* ],
* ]
*
*
* @param ShowEntityRequest $request
* @param Storecove $storecove
* @return mixed
*/
public function show(ShowEntityRequest $request, Storecove $storecove) public function show(ShowEntityRequest $request, Storecove $storecove)
{ {
$company = auth()->user()->company(); $company = auth()->user()->company();
@ -30,9 +84,10 @@ class EInvoicePeppolController extends BaseController
} }
/** /**
* Create a legal entity id * Create a legal entity id, response will be
* the same as show()
* *
* @param CreateRequest $request * @param StoreEntityRequest $request
* @param Storecove $storecove * @param Storecove $storecove
* @return Response * @return Response
*/ */
@ -68,6 +123,8 @@ class EInvoicePeppolController extends BaseController
/** /**
* Add an additional tax identifier to * Add an additional tax identifier to
* an existing legal entity id * an existing legal entity id
*
* Response will be the same as show()
* *
* @param AddTaxIdentifierRequest $request * @param AddTaxIdentifierRequest $request
* @param Storecove $storecove * @param Storecove $storecove
@ -96,11 +153,28 @@ class EInvoicePeppolController extends BaseController
return response()->json(['message' => 'ok'], 200); return response()->json(['message' => 'ok'], 200);
} }
public function updateLegalEntity(UpdateEntityRequest $request, Storecove $storecove)
{
$company = auth()->user()->company();
public function disconnect(DisconnectRequest $request, Storecove $storecove): Response $r = $storecove->updateLegalEntity($company->legal_entity_id, $request->validated());
}
/**
* Removed the legal identity from the Peppol network
*
* @param DisconnectRequest $request
* @param Storecove $storecove
* @return \Illuminate\Http\Response
*/
public function disconnect(DisconnectRequest $request, Storecove $storecove): \Illuminate\Http\Response
{ {
/** /**
* @var \App\Models\Company * @var \App\Models\Company $company
*/ */
$company = auth()->user()->company(); $company = auth()->user()->company();
@ -110,6 +184,7 @@ class EInvoicePeppolController extends BaseController
if ($response) { if ($response) {
$company->legal_entity_id = null; $company->legal_entity_id = null;
$company->tax_data = $this->unsetVatNumbers($company->tax_data);
$company->save(); $company->save();
return response()->noContent(); return response()->noContent();
@ -120,4 +195,26 @@ class EInvoicePeppolController extends BaseController
return response()->noContent(status: 422); return response()->noContent(status: 422);
} }
private function unsetVatNumbers(mixed $taxData): mixed
{
if (isset($taxData->regions->EU->subregions)) {
foreach ($taxData->regions->EU->subregions as $country => $data) {
if (isset($data->vat_number)) {
$newData = new \stdClass();
if (is_object($data)) {
$dataArray = get_object_vars($data);
foreach ($dataArray as $key => $value) {
if ($key !== 'vat_number') {
$newData->$key = $value;
}
}
}
$taxData->regions->EU->subregions->$country = $newData;
}
}
}
return $taxData;
}
} }

View File

@ -79,6 +79,8 @@ class StoreEntityRequest extends FormRequest
'country' => ['required', 'bail', Rule::in(array_keys($this->vat_regex_patterns))], 'country' => ['required', 'bail', Rule::in(array_keys($this->vat_regex_patterns))],
'zip' => ['required', 'string'], 'zip' => ['required', 'string'],
'county' => ['required', 'string'], 'county' => ['required', 'string'],
'acts_as_receiver' => ['required', 'bool'],
'acts_as_sender' => ['required', 'bool'],
]; ];
} }
@ -98,6 +100,9 @@ class StoreEntityRequest extends FormRequest
$input['country'] = $country->iso_3166_2; $input['country'] = $country->iso_3166_2;
} }
$input['acts_as_receiver'] = $input['acts_as_receiver'] ?? true;
$input['acts_as_sender'] = $input['acts_as_sender'] ?? true;
$this->replace($input); $this->replace($input);
} }

View File

@ -0,0 +1,55 @@
<?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\Auth\Access\AuthorizationException;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class UpdateEntityRequest extends FormRequest
{
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 [
'acts_as_receiver' => ['required', 'bool'],
'acts_as_sender' => ['required', 'bool'],
];
}
public function prepareForValidation()
{
$input = $this->all();
$this->replace($input);
}
}

View File

@ -11,13 +11,13 @@
namespace App\Services\EDocument\Gateway\Storecove; namespace App\Services\EDocument\Gateway\Storecove;
use App\DataMapper\Analytics\LegalEntityCreated;
use App\Models\Company; use App\Models\Company;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Turbo124\Beacon\Facades\LightLogs;
use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException; use GuzzleHttp\Exception\ServerException;
use Illuminate\Http\Client\RequestException; use Illuminate\Http\Client\RequestException;
use Turbo124\Beacon\Facades\LightLogs; use App\DataMapper\Analytics\LegalEntityCreated;
enum HttpVerb: string enum HttpVerb: string
{ {
@ -82,13 +82,47 @@ class Storecove
}; };
$uri = "api/v2/discovery/receives"; $uri = "api/v2/discovery/receives";
$r = $this->httpClient($uri, (HttpVerb::POST)->value, $network_data, $this->getHeaders()); $r = $this->httpClient($uri, (HttpVerb::POST)->value, $network_data, $this->getHeaders());
nlog($network_data);
nlog($r->json());
nlog($r->body());
return ($r->successful() && $r->json()['code'] == 'OK') ? true : false; return ($r->successful() && $r->json()['code'] == 'OK') ? true : false;
} }
/**
* Discovery
*
* @param string $identifier
* @param string $scheme
* @param string $network
* @return bool
*/
public function exists(string $identifier, string $scheme, string $network = 'peppol'): bool
{
$network_data = [];
match ($network) {
'peppol' => $network_data = array_merge($this->peppol_discovery, ['scheme' => $scheme, 'identifier' => $identifier]),
'dbn' => $network_data = array_merge($this->dbn_discovery, ['scheme' => $scheme, 'identifier' => $identifier]),
default => $network_data = array_merge($this->peppol_discovery, ['scheme' => $scheme, 'identifier' => $identifier]),
};
$uri = "api/v2/discovery/exists";
$r = $this->httpClient($uri, (HttpVerb::POST)->value, $network_data, $this->getHeaders());
nlog($network_data);
nlog($r->json());
nlog($r->body());
return ($r->successful() && $r->json()['code'] == 'OK') ? true : false;
}
/** /**
* Unused as yet * Unused as yet
* *
@ -232,6 +266,8 @@ class Storecove
$r = $this->httpClient($uri, (HttpVerb::POST)->value, $payload); $r = $this->httpClient($uri, (HttpVerb::POST)->value, $payload);
if($r->successful()) { if($r->successful()) {
$data = $r->object();
LightLogs::create(new LegalEntityCreated($data->id, $data->tenant_id))->batch();
return $r->json(); return $r->json();
} }
@ -248,8 +284,6 @@ class Storecove
public function getLegalEntity($id) public function getLegalEntity($id)
{ {
// $uri = "legal_entities";
$uri = "legal_entities/{$id}"; $uri = "legal_entities/{$id}";
$r = $this->httpClient($uri, (HttpVerb::GET)->value, []); $r = $this->httpClient($uri, (HttpVerb::GET)->value, []);
@ -351,6 +385,51 @@ class Storecove
} }
/**
* removeAdditionalTaxIdentifier
*
* 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 $tax_identifier
* @return mixed
*/
public function removeAdditionalTaxIdentifier(int $legal_entity_id, string $tax_identifier)
{
$legal_entity = $this->getLegalEntity($legal_entity_id);
if(isset($legal_entity['additional_tax_identifiers']) && is_array($legal_entity['additional_tax_identifiers']))
{
foreach($legal_entity['additional_tax_identifiers'] as $ati)
{
if($ati['identifier'] == $tax_identifier)
{
$uri = "legal_entities/{$legal_entity_id}/additional_tax_identifiers/{$ati['id']}";
$r = $this->httpClient($uri, (HttpVerb::DELETE)->value, []);
if ($r->successful()) {
$data = $r->json();
return $data;
}
return $r;
}
}
}
return false;
}
/** /**
* deleteIdentifier * deleteIdentifier
* *

View File

@ -16,13 +16,14 @@ use App\Models\Client;
use App\Models\Company; use App\Models\Company;
use App\Models\Invoice; use App\Models\Invoice;
use Tests\MockAccountData; use Tests\MockAccountData;
use App\Models\ClientContact;
use App\DataMapper\InvoiceItem; use App\DataMapper\InvoiceItem;
use App\DataMapper\Tax\TaxModel;
use App\DataMapper\ClientSettings; use App\DataMapper\ClientSettings;
use App\DataMapper\CompanySettings; use App\DataMapper\CompanySettings;
use App\Models\ClientContact;
use App\Services\EDocument\Standards\Peppol; use App\Services\EDocument\Standards\Peppol;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use InvoiceNinja\EInvoice\Models\Peppol\PaymentMeans; use InvoiceNinja\EInvoice\Models\Peppol\PaymentMeans;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class StorecoveTest extends TestCase class StorecoveTest extends TestCase
{ {
@ -42,6 +43,69 @@ class StorecoveTest extends TestCase
} }
} }
public function testUnsetOfVatNumers()
{
$settings = CompanySettings::defaults();
$settings->country_id = '276'; // germany
$tax_data = new TaxModel();
$tax_data->seller_subregion = 'DE';
$tax_data->regions->EU->has_sales_above_threshold = false;
$tax_data->regions->EU->tax_all_subregions = true;
$tax_data->regions->US->tax_all_subregions = true;
$tax_data->regions->US->has_sales_above_threshold = true;
$tax_data->regions->EU->subregions->DE->vat_number = 'DE12345';
$tax_data->regions->EU->subregions->RO->vat_number = 'RO12345';
$company = Company::factory()->create([
'account_id' => $this->account->id,
'settings' => $settings,
'tax_data' => $tax_data,
'calculate_taxes' => true,
]);
$this->assertEquals('DE12345', $company->tax_data->regions->EU->subregions->DE->vat_number);
$this->assertEquals('RO12345', $company->tax_data->regions->EU->subregions->RO->vat_number);
$this->assertEquals('DE12345', $company->tax_data->regions->EU->subregions->DE->vat_number);
$this->assertEquals('RO12345', $company->tax_data->regions->EU->subregions->RO->vat_number);
$company->tax_data = $this->unsetVatNumbers($company->tax_data);
$company->save();
$company = $company->fresh();
$this->assertFalse(property_exists($company->tax_data->regions->EU->subregions->DE, 'vat_number'), "DE subregion should not have vat_number property");
$this->assertFalse(property_exists($company->tax_data->regions->EU->subregions->RO, 'vat_number'), "RO subregion should not have vat_number property");
}
private function unsetVatNumbers(mixed $taxData): mixed
{
if (isset($taxData->regions->EU->subregions)) {
foreach ($taxData->regions->EU->subregions as $country => $data) {
if (isset($data->vat_number)) {
$newData = new \stdClass();
if (is_object($data)) {
$dataArray = get_object_vars($data);
foreach ($dataArray as $key => $value) {
if ($key !== 'vat_number') {
$newData->$key = $value;
}
}
}
$taxData->regions->EU->subregions->$country = $newData;
}
}
}
return $taxData;
}
// public function testCreateLegalEntity() // public function testCreateLegalEntity()
// { // {