Add AEAT response to logs

This commit is contained in:
David Bomba 2025-08-14 12:08:17 +10:00
parent 9b98d45d3b
commit 256555c952
5 changed files with 89 additions and 18 deletions

View File

@ -11,17 +11,18 @@
namespace App\Http\Controllers;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use App\Http\Requests\EInvoice\Peppol\StoreEntityRequest;
use Illuminate\Http\JsonResponse;
use App\Services\EDocument\Jobs\SendEDocument;
use App\Http\Requests\EInvoice\Peppol\RetrySendRequest;
use App\Services\EDocument\Gateway\Storecove\Storecove;
use App\Http\Requests\EInvoice\Peppol\DisconnectRequest;
use App\Http\Requests\EInvoice\Peppol\ShowEntityRequest;
use App\Http\Requests\EInvoice\Peppol\StoreEntityRequest;
use App\Http\Requests\EInvoice\Peppol\UpdateEntityRequest;
use App\Services\EDocument\Standards\Verifactu\SendToAeat;
use App\Http\Requests\EInvoice\Peppol\AddTaxIdentifierRequest;
use App\Http\Requests\EInvoice\Peppol\RemoveTaxIdentifierRequest;
use App\Http\Requests\EInvoice\Peppol\RetrySendRequest;
use App\Http\Requests\EInvoice\Peppol\ShowEntityRequest;
use App\Http\Requests\EInvoice\Peppol\UpdateEntityRequest;
use App\Services\EDocument\Jobs\SendEDocument;
class EInvoicePeppolController extends BaseController
{
@ -264,8 +265,12 @@ class EInvoicePeppolController extends BaseController
public function retrySend(RetrySendRequest $request)
{
if(auth()->user()->company()->verifactuEnabled()) {
SendToAeat::dispatch($request->entity_id, auth()->user()->company(), 'create');
}
else {
SendEDocument::dispatch($request->entity, $request->entity_id, auth()->user()->company()->db);
}
return response()->json(['message' => 'trying....'], 200);
}

View File

@ -11,11 +11,15 @@
namespace App\Services\EDocument\Standards\Validation\Verifactu;
use App\Services\EDocument\Standards\Validation\EntityLevelInterface;
use App\Models\Quote;
use App\Models\Client;
use App\Models\Credit;
use App\Models\Vendor;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\PurchaseOrder;
use Illuminate\Support\Facades\App;
use App\Services\EDocument\Standards\Validation\EntityLevelInterface;
//@todo - need to implement a rule set for verifactu for validation
class EntityLevel implements EntityLevelInterface
@ -168,10 +172,60 @@ class EntityLevel implements EntityLevelInterface
$errors[] = ['field' => 'email', 'label' => ctrans("texts.email")];
}
$delivery_network_supported = $client->checkDeliveryNetwork();
return $errors;
if (is_string($delivery_network_supported)) {
$errors[] = ['field' => ctrans("texts.country"), 'label' => $delivery_network_supported];
}
private function testCompanyState(mixed $entity): array
{
$client = false;
$vendor = false;
$settings_object = false;
$company = false;
if ($entity instanceof Client) {
$client = $entity;
$company = $entity->company;
$settings_object = $client;
} elseif ($entity instanceof Company) {
$company = $entity;
$settings_object = $company;
} elseif ($entity instanceof Vendor) {
$vendor = $entity;
$company = $entity->company;
$settings_object = $company;
} elseif ($entity instanceof Invoice || $entity instanceof Credit || $entity instanceof Quote) {
$client = $entity->client;
$company = $entity->company;
$settings_object = $entity->client;
} elseif ($entity instanceof PurchaseOrder) {
$vendor = $entity->vendor;
$company = $entity->company;
$settings_object = $company;
}
$errors = [];
foreach ($this->company_settings_fields as $field) {
if ($this->validString($settings_object->getSetting($field))) {
continue;
}
$errors[] = ['field' => $field, 'label' => ctrans("texts.{$field}")];
}
//If not an individual, you MUST have a VAT number
if ($company->getSetting('classification') != 'individual' && !$this->validString($company->getSetting('vat_number'))) {
$errors[] = ['field' => 'vat_number', 'label' => ctrans("texts.vat_number")];
} elseif ($company->getSetting('classification') == 'individual' && !$this->validString($company->getSetting('id_number'))) {
$errors[] = ['field' => 'id_number', 'label' => ctrans("texts.id_number")];
}
if(!$this->isValidSpanishVAT($company->getSetting('vat_number'))) {
$errors[] = ['field' => 'vat_number', 'label' => ctrans("texts.vat_number")];
}
return $errors;

View File

@ -154,6 +154,7 @@ class Verifactu extends AbstractService
'previous_hash' => $this->_previous_huella,
'state' => $this->_document->serialize(),
'response' => $response,
'status' => $response['guid'],
]);
}
/**

View File

@ -26,6 +26,8 @@ class ResponseProcessor
try {
$this->loadXml($xmlResponse);
nlog($this->dom->saveXML());
return [
'success' => $this->isSuccessful(),
'status' => $this->getStatus(),
@ -33,6 +35,7 @@ class ResponseProcessor
'warnings' => $this->getWarnings(),
'data' => $this->getResponseData(),
'metadata' => $this->getMetadata(),
'guid' => $this->getGuid(),
'raw_response' => $xmlResponse
];
} catch (Exception $e) {
@ -66,6 +69,11 @@ class ResponseProcessor
$this->root = $this->dom->documentElement;
}
private function getGuid(): ?string
{
return $this->getElementText('.//tikR:CSV') ?? null;
}
/**
* Check if response indicates success
*/

View File

@ -72,12 +72,6 @@ class SendToAeat implements ShouldQueue
$invoice = Invoice::withTrashed()->find($this->invoice_id);
// if($invoice->client->country->iso_3166_2 != 'ES') {
// $invoice->backup->guid = 'NOT_ES';
// $invoice->saveQuietly();
// return;
// }
switch($this->action) {
case 'create':
$this->createInvoice($invoice);
@ -115,6 +109,11 @@ class SendToAeat implements ShouldQueue
$message = $response['errors'][0]['message'];
}
if($response['success']) {
$invoice->backup->guid = $response['guid'];
$invoice->saveQuietly();
}
$this->writeActivity($invoice, $response['success'] ? Activity::VERIFACTU_INVOICE_SENT : Activity::VERIFACTU_INVOICE_SENT_FAILURE, $message);
$this->systemLog($invoice, $response, $response['success'] ? SystemLog::EVENT_VERIFACTU_SUCCESS : SystemLog::EVENT_VERIFACTU_FAILURE, SystemLog::TYPE_VERIFACTU_INVOICE);
@ -154,6 +153,10 @@ class SendToAeat implements ShouldQueue
$parent->backup->child_invoice_ids = $parent->backup->child_invoice_ids->reject(fn($id) => $id === $invoice->hashed_id);
$parent->saveQuietly();
}
$invoice->backup->guid = $response['guid'];
$invoice->saveQuietly();
}
if(isset($response['errors'][0]['message'])){