Working on verifactu document mutations

This commit is contained in:
David Bomba 2025-08-13 11:05:02 +10:00
parent 8bc1513591
commit bf8041ab7c
9 changed files with 23 additions and 15 deletions

View File

@ -48,7 +48,7 @@ class InvoiceBackupCast implements CastsAttributes
] : [],
'parent_invoice_id' => $value->parent_invoice_id,
'parent_invoice_number' => $value->parent_invoice_number,
'reason' => $value->reason,
'document_type' => $value->document_type,
'child_invoice_ids' => $value->child_invoice_ids->toArray(),
'redirect' => $value->redirect,
])

View File

@ -27,7 +27,7 @@ class InvoiceBackup implements Castable
public Cancellation $cancellation = new Cancellation(0,0),
public ?string $parent_invoice_id = null, // The id of the invoice that was cancelled
public ?string $parent_invoice_number = null, // The number of the invoice that was cancelled
public ?string $reason = null, // The reason for the cancellation
public ?string $document_type = null, // The reason for the cancellation
public Collection $child_invoice_ids = new Collection(), // Collection of child invoice IDs
public ?string $redirect = null, // The redirect url for the invoice
) {}
@ -49,7 +49,7 @@ class InvoiceBackup implements Castable
cancellation: Cancellation::fromArray($data['cancellation'] ?? []),
parent_invoice_id: $data['parent_invoice_id'] ?? null,
parent_invoice_number: $data['parent_invoice_number'] ?? null,
reason: $data['reason'] ?? null,
document_type: $data['document_type'] ?? null,
child_invoice_ids: isset($data['child_invoice_ids']) ? collect($data['child_invoice_ids']) : new Collection(),
redirect: $data['redirect'] ?? null
);

View File

@ -43,7 +43,7 @@ class RestoreDisabledRule implements ValidationRule
$delete_query = clone $base_query;
$mutated_query = $delete_query->where(function ($q){
$q->whereNotNull('backup->parent_invoice_id')->orWhere('backup->child_invoice_ids', '!=', '[]');
$q->where('backup->document_type', '!=', 'R2')->orWhere('backup->child_invoice_ids', '!=', '[]');
});
/** For verifactu, we do not allow restores of deleted invoices */

View File

@ -71,6 +71,9 @@ class Verifactu extends AbstractService
if($this->invoice->amount < 0) {
$document = $document->setRectification();
}
elseif($this->invoice->backup->document_type === 'F3') {
$document = $document->setModification();
}
$document = $document->getInvoice();
// }

View File

@ -236,14 +236,8 @@ class RegistroAlta
$this->v_invoice->setFacturasRectificadas($facturasRectificadas);
// // Set up rectification amounts
// $importeRectificacion = [
// 'BaseRectificada' => $this->calc->getNetSubtotal(),
// 'CuotaRectificada' => $this->invoice->total_taxes,
// 'CuotaRecargoRectificado' => 0.00
// ];
// $this->v_invoice->setRectificationAmounts($importeRectificacion);
$this->invoice->backup->document_type = 'R2';
$this->invoice->save();
return $this;
}

View File

@ -144,6 +144,16 @@ class SendToAeat implements ShouldQueue
$response = $verifactu->send($soapXml);
nlog($response);
if($response['success']) {
//if successful, we need to pop this invoice from the child array of the parent invoice!
$parent = Invoice::withTrashed()->find($invoice->backup->parent_invoice_id);
if($parent) {
$parent->backup->child_invoice_ids = $parent->backup->child_invoice_ids->reject(fn($id) => $id === $invoice->hashed_id);
$parent->saveQuietly();
}
}
//@todo - verifactu logging
}
public function middleware()

View File

@ -100,7 +100,7 @@ class HandleCancellation extends AbstractService
$replicated_invoice->line_items = $items;
$replicated_invoice->backup->parent_invoice_id = $this->invoice->hashed_id;
$replicated_invoice->backup->parent_invoice_number = $this->invoice->number;
$replicated_invoice->backup->reason = $this->reason ?? 'R3';
$replicated_invoice->backup->document_type = 'R2'; // Full Credit Note Generated for the invoice
$invoice_repository = new InvoiceRepository();
$replicated_invoice = $invoice_repository->save([], $replicated_invoice);

View File

@ -733,6 +733,7 @@ class InvoiceService
$this->markSent();
//Update the client balance by the delta amount from the previous invoice to this one.
$this->invoice->backup->parent_invoice_id = $modified_invoice->hashed_id;
$this->invoice->backup->document_type = 'F3';
$this->invoice->saveQuietly();
$this->invoice->client->service()->updateBalance(round(($this->invoice->amount - $modified_invoice->amount), 2));

View File

@ -474,7 +474,7 @@ class VerifactuFeatureTest extends TestCase
$invoice2->number = 'TEST0033343460-C10';
$invoice2->status_id = Invoice::STATUS_DRAFT;
$invoice2->backup->parent_invoice_id = $invoice->hashed_id;
$invoice2->backup->document_type = 'R2';
$items = $invoice2->line_items;
foreach($items as &$item) {