Updates for logic with Verifactu

This commit is contained in:
David Bomba 2025-10-15 11:08:10 +11:00
parent 00db79a611
commit 9fc9ab933d
5 changed files with 26 additions and 23 deletions

View File

@ -76,7 +76,8 @@ class VerifactuAmountCheck implements ValidationRule
$fail("Invoice already credited in full"); $fail("Invoice already credited in full");
} }
\DB::connection(config('database.default'))->beginTransaction(); // \DB::connection(config('database.default'))->beginTransaction();
$array_data = request()->all(); $array_data = request()->all();
unset($array_data['client_id']); unset($array_data['client_id']);
@ -84,7 +85,7 @@ class VerifactuAmountCheck implements ValidationRule
$invoice->fill($array_data); $invoice->fill($array_data);
/** Calculate the gross total of the cancellation invoice. */ /** Calculate the gross total of the cancellation invoice. */
$total = $invoice->calc()->getTotal(); // $total = $invoice->calc()->getTotal();
// $items = $array_data['line_items']; // $items = $array_data['line_items'];
@ -106,10 +107,12 @@ class VerifactuAmountCheck implements ValidationRule
// $invoice->line_items = $items; // $invoice->line_items = $items;
// /** Total WITHOUT IRPF */ /** Total WITHOUT IRPF */
// $ex_iprf_total = $invoice->calc()->getTotal(); $total = $invoice->calc()->getTotal();
\DB::connection(config('database.default'))->rollBack(); $invoice->refresh();
// \DB::connection(config('database.default'))->rollBack();
if($total > 0) { if($total > 0) {
$fail("Only negative invoices can rectify a invoice."); $fail("Only negative invoices can rectify a invoice.");
@ -119,18 +122,18 @@ class VerifactuAmountCheck implements ValidationRule
$adjustable_amount = $invoice->backup->adjustable_amount - $child_invoices_sum; $adjustable_amount = $invoice->backup->adjustable_amount - $child_invoices_sum;
/** The client facing amount that can be cancelled This is the amount that will NOT contain IRPF amounts */ /** The client facing amount that can be cancelled This is the amount that will NOT contain IRPF amounts */
$client_facing_adjustable_amount = ($child_invoices_sum / $invoice->backup->adjustable_amount) * $total; $client_facing_adjustable_amount = ($invoice->amount / $invoice->backup->adjustable_amount) * $adjustable_amount;
nlog("total: " . $total); nlog("total: " . $total);
nlog("invoice->amount: " . $invoice->amount);
nlog("adjustable_amount: " . $adjustable_amount); nlog("adjustable_amount: " . $adjustable_amount);
nlog("child_invoices_sum: " . $child_invoices_sum); nlog("child_invoices_sum: " . $child_invoices_sum);
nlog("invoice->backup->adjustable_amount: " . $invoice->backup->adjustable_amount); nlog("invoice->backup->adjustable_amount: " . $invoice->backup->adjustable_amount);
nlog("client_facing_adjustable_amount: " . $client_facing_adjustable_amount); nlog("client_facing_adjustable_amount: " . $client_facing_adjustable_amount);
if(abs($total) > $adjustable_amount) { if(abs($total) > $client_facing_adjustable_amount) {
$total = abs($total); // The rectification invoice amount cannot be greater than the original invoice amount $fail("Total de ajuste {$total} no puede exceder el saldo de la factura {$client_facing_adjustable_amount}");
$fail("Total de ajuste {$client_facing_adjustable_amount} no puede exceder el saldo de la factura {$total}");
} }
} }

View File

@ -139,8 +139,8 @@ class InvoiceValidator
$errors = []; $errors = [];
// Check for required fields based on invoice type // Check for required fields based on invoice type
if ($invoice->getTipoFactura() === 'R2' && !$invoice->getTipoRectificativa()) { if (in_array($invoice->getTipoFactura(), ['R1','R2']) && !$invoice->getTipoRectificativa()) {
$errors[] = "Rectification invoices (R2) must specify TipoRectificativa"; $errors[] = "Rectification invoices (R1/R2) must specify TipoRectificativa";
} }
// Check for simplified invoice requirements // Check for simplified invoice requirements

View File

@ -779,12 +779,12 @@ class Invoice extends BaseXmlModel implements XmlModelInterface
$root->appendChild($this->createElement($doc, 'TipoFactura', $this->tipoFactura)); $root->appendChild($this->createElement($doc, 'TipoFactura', $this->tipoFactura));
// 5. TipoRectificativa (only for R1 invoices) // 5. TipoRectificativa (only for R1 invoices)
if (in_array($this->tipoFactura, [self::TIPO_FACTURA_SUSTITUIDA, self::TIPO_FACTURA_RECTIFICATIVA]) && $this->tipoRectificativa !== null) { if (in_array($this->tipoFactura, [self::TIPO_FACTURA_SUSTITUIDA, self::TIPO_FACTURA_RECTIFICATIVA, self::TIPO_FACTURA_RECTIFICATIVA_PARTIAL]) && $this->tipoRectificativa !== null) {
$root->appendChild($this->createElement($doc, 'TipoRectificativa', $this->tipoRectificativa)); $root->appendChild($this->createElement($doc, 'TipoRectificativa', $this->tipoRectificativa));
} }
// 6. FacturasRectificadas (only for R1 invoices) // 6. FacturasRectificadas (only for R1 invoices)
if ($this->tipoFactura === self::TIPO_FACTURA_RECTIFICATIVA && $this->facturasRectificadas !== null) { if (in_array($this->tipoFactura, [self::TIPO_FACTURA_RECTIFICATIVA_PARTIAL, self::TIPO_FACTURA_RECTIFICATIVA]) && $this->facturasRectificadas !== null) {
$facturasRectificadasElement = $this->createElement($doc, 'FacturasRectificadas'); $facturasRectificadasElement = $this->createElement($doc, 'FacturasRectificadas');
foreach ($this->facturasRectificadas as $facturaRectificada) { foreach ($this->facturasRectificadas as $facturaRectificada) {

View File

@ -83,19 +83,19 @@ class HandleCancellation extends AbstractService
$this->invoice->service()->workFlow()->save(); $this->invoice->service()->workFlow()->save();
// R2 Cancellation - do not create a separate document // R2 Cancellation - do not create a separate document
if($this->invoice->backup->document_type === 'R2'){ // You cannot cancel a cancellation!!!!! if(in_array($this->invoice->backup->document_type, ['R1','R2'])){ // You cannot cancel a cancellation!!!!!
$parent = Invoice::withTrashed()->find($this->decodePrimaryKey($this->invoice->backup->parent_invoice_id)); // empty catch
// $parent = Invoice::withTrashed()->find($this->decodePrimaryKey($this->invoice->backup->parent_invoice_id));
if(!$parent) { // if(!$parent) {
return $this->invoice; // return $this->invoice;
} // }
$parent->backup->adjustable_amount -= $this->invoice->amount; // $parent->backup->child_invoice_ids->reject(fn($id) => $id === $this->invoice->hashed_id);
$parent->backup->child_invoice_ids->reject(fn($id) => $id === $this->invoice->hashed_id); // $parent->save();
$parent->save();
$this->invoice->service()->cancelVerifactu(); // $this->invoice->service()->cancelVerifactu();
} }
else { else {
$replicated_invoice = $this->invoice->replicate(); $replicated_invoice = $this->invoice->replicate();

View File

@ -753,7 +753,7 @@ class InvoiceService
->get() ->get()
->sum('backup.adjustable_amount'); ->sum('backup.adjustable_amount');
//@modified->amount may not have the correct totals due to IRPF. //@todo verifactu - this won't be accurate as the invoice->amount will be the ex IPRF amount. modified->amount may not have the correct totals due to IRPF.
if(\App\Utils\BcMath::greaterThan(abs($child_invoice_amounts), $modified_invoice->amount)) { if(\App\Utils\BcMath::greaterThan(abs($child_invoice_amounts), $modified_invoice->amount)) {
$modified_invoice->status_id = Invoice::STATUS_CANCELLED; $modified_invoice->status_id = Invoice::STATUS_CANCELLED;
$modified_invoice->saveQuietly(); $modified_invoice->saveQuietly();