Skip to content

Commit e6dae1a

Browse files
committed
feat: generazione integrazione IVA per reverse charge
1 parent 35a625f commit e6dae1a

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

modules/fatture/src/Gestori/Movimenti.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
namespace Modules\Fatture\Gestori;
2222

2323
use Modules\Fatture\Fattura;
24+
use Modules\Iva\Aliquota;
2425
use Modules\PrimaNota\Mastrino;
2526
use Modules\PrimaNota\Movimento;
2627

@@ -122,7 +123,7 @@ public function registra()
122123
$righe = $this->fattura->getRighe();
123124
foreach ($righe as $riga) {
124125
// Retro-compatibilità per versioni <= 2.4
125-
$id_conto = $riga->id_conto ?: $this->fattura->idconto;
126+
$id_conto = $riga->idconto ?: $this->fattura->idconto;
126127

127128
$indetraibile = $riga->iva_indetraibile;
128129
$imponibile = $riga->totale_imponibile + $indetraibile;
@@ -157,7 +158,7 @@ public function registra()
157158

158159
// Aggiunta dell'IVA al conto di costo per fatture di acquisto con split payment
159160
foreach ($righe as $riga) {
160-
$id_conto = $riga->id_conto ?: $this->fattura->idconto;
161+
$id_conto = $riga->idconto ?: $this->fattura->idconto;
161162
$iva_riga = $riga->iva;
162163

163164
if (!empty($iva_riga)) {
@@ -240,8 +241,6 @@ public function registra()
240241
}
241242

242243
// Registrazione dei singoli Movimenti nel relativo Mastrino
243-
$i = 0;
244-
245244
$totale_dare = 0;
246245
$totale_avere = 0;
247246

@@ -259,6 +258,37 @@ public function registra()
259258
$movimento->save();
260259
}
261260

261+
if ($is_acquisto && !$is_nota) {
262+
$is_fornitore_italiano = $this->fattura->anagrafica->nazione && $this->fattura->anagrafica->nazione->iso2 == 'IT';
263+
264+
$righe_reverse_charge = [];
265+
$totale_imponibile_reverse = 0;
266+
$aliquota_iva = null;
267+
268+
foreach ($this->fattura->getRighe() as $riga) {
269+
if ($riga->aliquota != null && $riga->aliquota->codice_natura_fe !== null && str_starts_with($riga->aliquota->codice_natura_fe, 'N6')) {
270+
$righe_reverse_charge[] = $riga;
271+
$totale_imponibile_reverse += $riga->totale_imponibile;
272+
273+
if ($aliquota_iva === null) {
274+
$aliquota_iva = Aliquota::find(setting('Iva predefinita'));
275+
}
276+
}
277+
}
278+
279+
if ($is_fornitore_italiano && !empty($righe_reverse_charge) && $aliquota_iva) {
280+
$importo_iva = $totale_imponibile_reverse * $aliquota_iva->percentuale / 100;
281+
282+
$movimento = Movimento::build($mastrino, setting('Conto per Iva su vendite'), $this->fattura);
283+
$movimento->setTotale(0, $importo_iva);
284+
$movimento->save();
285+
286+
$movimento = Movimento::build($mastrino, setting('Conto per Iva su acquisti'), $this->fattura);
287+
$movimento->setTotale($importo_iva, 0);
288+
$movimento->save();
289+
}
290+
}
291+
262292
// Nel penultimo conto del mastrino inserisco l'eventuale differenza per evitare sbilanci nel totale,
263293
// evitando di mettere differenze nell'iva
264294
$diff = round($totale_avere - $totale_dare, 4);

0 commit comments

Comments
 (0)