1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Validate e-invoice payload

This commit is contained in:
David Bomba 2024-09-03 16:36:46 +10:00
parent 607f2ff69c
commit f893a75e9b
5 changed files with 175 additions and 4 deletions

View File

@ -0,0 +1,41 @@
<?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\Controllers;
use App\Exceptions\SystemError;
use App\Http\Requests\EInvoice\ValidateEInvoiceRequest;
use App\Services\EDocument\Standards\Validation\Peppol\EntityLevel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
class EInvoiceController extends BaseController
{
public function validateEntity(ValidateEInvoiceRequest $request)
{
$el = new EntityLevel();
$data = [];
match($request->entity){
'invoices' => $data = $el->checkInvoice($request->getEntity()),
'clients' => $data = $el->checkClient($request->getEntity()),
'companies' => $data = $el->checkCompany($request->getEntity()),
default => $data['passes'] = false,
};
return response()->json($data, $data['passes'] ? 200 : 400);
}
}

View File

@ -17,6 +17,7 @@ use Illuminate\Validation\Rule;
class StoreNoteRequest extends Request class StoreNoteRequest extends Request
{ {
/** /**
* Determine if the user is authorized to make this request. * Determine if the user is authorized to make this request.
* *

View File

@ -0,0 +1,93 @@
<?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;
use App\Utils\Ninja;
use App\Models\Client;
use App\Models\Company;
use App\Models\Invoice;
use App\Http\Requests\Request;
use Illuminate\Validation\Rule;
class ValidateEInvoiceRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
/** @var \App\Models\User $user */
$user = auth()->user();
$entity = $this->getEntity();
if($entity instanceof Company)
return $entity->id == $user->company()->id;
return $user->can('view', $entity);
}
public function rules()
{
/** @var \App\Models\User $user */
$user = auth()->user();
return [
'entity' => 'required|bail|in:invoices,clients,companies',
'entity_id' => ['required','bail', Rule::exists($this->entity, 'id')
->when($this->entity != 'companies', function ($q) use($user){
$q->where('company_id', $user->company()->id);
})
],
];
}
public function prepareForValidation()
{
$input = $this->all();
if (isset($input['entity_id']) && $input['entity_id'] != null) {
$input['entity_id'] = $this->decodePrimaryKey($input['entity_id']);
}
$this->replace($input);
}
public function getEntity()
{
if(!$this->entity) {
return false;
}
$class = Invoice::class;
match ($this->entity) {
'invoice' => $class = Invoice::class,
'client' => $class = Client::class,
'companies' => $class = Company::class,
default => $class = Invoice::class,
};
if($this->entity == 'companies')
return auth()->user()->company();
return $class::withTrashed()->find(is_string($this->entity_id) ? $this->decodePrimaryKey($this->entity_id) : $this->entity_id);
}
}

View File

@ -93,6 +93,7 @@ use App\Http\Controllers\BankTransactionRuleController;
use App\Http\Controllers\InAppPurchase\AppleController; use App\Http\Controllers\InAppPurchase\AppleController;
use App\Http\Controllers\Reports\QuoteReportController; use App\Http\Controllers\Reports\QuoteReportController;
use App\Http\Controllers\Auth\PasswordTimeoutController; use App\Http\Controllers\Auth\PasswordTimeoutController;
use App\Http\Controllers\EInvoiceController;
use App\Http\Controllers\PreviewPurchaseOrderController; use App\Http\Controllers\PreviewPurchaseOrderController;
use App\Http\Controllers\Reports\ClientReportController; use App\Http\Controllers\Reports\ClientReportController;
use App\Http\Controllers\Reports\CreditReportController; use App\Http\Controllers\Reports\CreditReportController;
@ -224,6 +225,7 @@ Route::group(['middleware' => ['throttle:api', 'api_db', 'token_auth', 'locale']
Route::get('documents/{document}/download', [DocumentController::class, 'download'])->name('documents.download'); Route::get('documents/{document}/download', [DocumentController::class, 'download'])->name('documents.download');
Route::post('documents/bulk', [DocumentController::class, 'bulk'])->name('documents.bulk'); Route::post('documents/bulk', [DocumentController::class, 'bulk'])->name('documents.bulk');
Route::post('einvoice/validateEntity', [EInvoiceController::class, 'validateEntity'])->name('einvoice.validateEntity');
Route::post('emails', [EmailController::class, 'send'])->name('email.send')->middleware('user_verified'); Route::post('emails', [EmailController::class, 'send'])->name('email.send')->middleware('user_verified');
Route::post('emails/clientHistory/{client}', [EmailHistoryController::class, 'clientHistory'])->name('email.clientHistory'); Route::post('emails/clientHistory/{client}', [EmailHistoryController::class, 'clientHistory'])->name('email.clientHistory');
Route::post('emails/entityHistory', [EmailHistoryController::class, 'entityHistory'])->name('email.entityHistory'); Route::post('emails/entityHistory', [EmailHistoryController::class, 'entityHistory'])->name('email.entityHistory');

View File

@ -41,6 +41,44 @@ class EInvoiceValidationTest extends TestCase
} }
public function testEinvoiceValidationEndpointInvoice()
{
$data =[
'entity' => 'invoices',
'entity_id' => $this->invoice->hashed_id,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson('/api/v1/einvoice/validateEntity', $data);
$response->assertStatus(200);
$arr = $response->json();
}
public function testEinvoiceValidationEndpoint()
{
$data =[
'entity' => 'companies',
'entity_id' => $this->company->hashed_id,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson('/api/v1/einvoice/validateEntity', $data);
$response->assertStatus(200);
$arr = $response->json();
}
public function testInvalidCompanySettings() public function testInvalidCompanySettings()
{ {
@ -171,14 +209,10 @@ class EInvoiceValidationTest extends TestCase
$el = new EntityLevel(); $el = new EntityLevel();
$validation = $el->checkCompany($company); $validation = $el->checkCompany($company);
nlog($validation);
$this->assertFalse($validation['passes']); $this->assertFalse($validation['passes']);
} }
public function testInvalidClientSettings() public function testInvalidClientSettings()
{ {