Skip to content

Commit

Permalink
Additional routes for legal entities
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo124 committed Oct 22, 2024
1 parent 553e246 commit 27850f6
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 20 deletions.
23 changes: 15 additions & 8 deletions app/DataMapper/Tax/TaxModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ class TaxModel

/** @var 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
*
Expand All @@ -34,16 +40,17 @@ public function __construct(public mixed $model = null)
if(!$model) {
$this->regions = $this->init();
} else {

if(is_null($model->seller_subregion)) {//@phpstan-ignore-line
$this->seller_subregion = '';
}

//@phpstan-ignore-next-line
foreach($model as $key => $value) {
$this->seller_subregion = $model->seller_subregion ?? '';
$this->act_as_sender = $model->act_as_sender ?? false;
$this->act_as_receiver = $model->act_as_receiver ?? false;
$modelArray = get_object_vars($model);
foreach ($modelArray as $key => $value) {
$this->{$key} = $value;
}


}

$this->migrate();
Expand Down
107 changes: 102 additions & 5 deletions app/Http/Controllers/EInvoicePeppolController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,63 @@
use App\Http\Requests\EInvoice\Peppol\DisconnectRequest;
use App\Http\Requests\EInvoice\Peppol\AddTaxIdentifierRequest;
use App\Http\Requests\EInvoice\Peppol\ShowEntityRequest;
use App\Http\Requests\EInvoice\Peppol\UpdateEntityRequest;

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" => "[email protected]",
* "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)
{
$company = auth()->user()->company();
Expand All @@ -30,9 +84,10 @@ public function show(ShowEntityRequest $request, Storecove $storecove)
}

/**
* 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
* @return Response
*/
Expand Down Expand Up @@ -68,6 +123,8 @@ public function setup(StoreEntityRequest $request, Storecove $storecove): Respon
/**
* Add an additional tax identifier to
* an existing legal entity id
*
* Response will be the same as show()
*
* @param AddTaxIdentifierRequest $request
* @param Storecove $storecove
Expand Down Expand Up @@ -95,12 +152,29 @@ public function addAdditionalTaxIdentifier(AddTaxIdentifierRequest $request, Sto

return response()->json(['message' => 'ok'], 200);

}

public function updateLegalEntity(UpdateEntityRequest $request, Storecove $storecove)
{

$company = auth()->user()->company();

$r = $storecove->updateLegalEntity($company->legal_entity_id, $request->validated());


}

public function disconnect(DisconnectRequest $request, Storecove $storecove): Response
/**
* 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();

Expand All @@ -110,6 +184,7 @@ public function disconnect(DisconnectRequest $request, Storecove $storecove): Re

if ($response) {
$company->legal_entity_id = null;
$company->tax_data = $this->unsetVatNumbers($company->tax_data);
$company->save();

return response()->noContent();
Expand All @@ -120,4 +195,26 @@ public function disconnect(DisconnectRequest $request, Storecove $storecove): Re

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;
}
}
5 changes: 5 additions & 0 deletions app/Http/Requests/EInvoice/Peppol/StoreEntityRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public function rules(): array
'country' => ['required', 'bail', Rule::in(array_keys($this->vat_regex_patterns))],
'zip' => ['required', 'string'],
'county' => ['required', 'string'],
'acts_as_receiver' => ['required', 'bool'],
'acts_as_sender' => ['required', 'bool'],
];
}

Expand All @@ -98,6 +100,9 @@ public function prepareForValidation()
$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);

}
Expand Down
55 changes: 55 additions & 0 deletions app/Http/Requests/EInvoice/Peppol/UpdateEntityRequest.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit 27850f6

Please sign in to comment.