|
38 | 38 | $is_fiscale = $fattura->isFiscale(); |
39 | 39 | } |
40 | 40 |
|
41 | | - $record = $dbo->fetchOne('SELECT |
| 41 | + $record = $dbo->fetchOne('SELECT |
42 | 42 | `co_documenti`.*, |
43 | 43 | `co_tipidocumento`.`reversed` AS is_reversed, |
44 | 44 | `co_documenti`.`idagente` AS idagente_fattura, |
|
65 | 65 | LEFT JOIN `dt_causalet` ON `co_documenti`.`idcausalet`=`dt_causalet`.`id` |
66 | 66 | LEFT JOIN `dt_causalet_lang` ON (`dt_causalet_lang`.`id_record` = `dt_causalet`.`id` AND `dt_causalet_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).') |
67 | 67 | INNER JOIN `zz_segments` ON `co_documenti`.`id_segment` = `zz_segments`.`id` |
68 | | - WHERE |
| 68 | + WHERE |
69 | 69 | `co_tipidocumento`.`dir` = '.prepare($dir).' AND `co_documenti`.`id`='.prepare($id_record)); |
70 | 70 |
|
71 | 71 | // Note di credito collegate |
|
82 | 82 | $fattura_acquisto_originale = null; |
83 | 83 |
|
84 | 84 | if (!empty($fattura)) { |
85 | | - $reverse_charge = $fattura->getRighe()->first(fn ($item, $key) => $item->aliquota != null && $item->aliquota->codice_natura_fe !== null && str_starts_with($item->aliquota->codice_natura_fe, 'N6'))->id; |
| 85 | + $riga_reverse_charge = null; |
| 86 | + foreach ($fattura->getRighe() as $riga) { |
| 87 | + if ($riga->aliquota != null && $riga->aliquota->codice_natura_fe !== null && str_starts_with($riga->aliquota->codice_natura_fe, 'N6')) { |
| 88 | + $riga_reverse_charge = $riga; |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + $reverse_charge = $riga_reverse_charge?->id; |
86 | 93 | $autofattura_vendita = Fattura::find($fattura->id_autofattura); |
87 | 94 |
|
88 | | - $abilita_autofattura = (($fattura->anagrafica->nazione->iso2 != 'IT' && !empty($fattura->anagrafica->nazione->iso2)) || $reverse_charge) && $dir == 'uscita' && $fattura->id_autofattura == null; |
| 95 | + $is_fornitore_estero = !empty($fattura->anagrafica->nazione) && !empty($fattura->anagrafica->nazione->iso2) && $fattura->anagrafica->nazione->iso2 != 'IT'; |
| 96 | + |
| 97 | + $abilita_autofattura = $is_fornitore_estero && $dir == 'uscita' && $fattura->id_autofattura == null; |
89 | 98 |
|
90 | 99 | $fattura_acquisto_originale = Fattura::where('id_autofattura', '=', $fattura->id)->first(); |
91 | 100 | $autofattura_collegata = Fattura::where('id_autofattura', '=', $fattura->id)->where('id', '!=', $fattura_acquisto_originale->id)->orderBy('id', 'DESC')->first(); |
|
0 commit comments