@@ -70,14 +70,32 @@ public function __construct($name, $directory = null, $plugin = null)
7070 $ numero = $ dati_generali ['Numero ' ];
7171 $ progressivo_invio = $ this ->getHeader ()['DatiTrasmissione ' ]['ProgressivoInvio ' ];
7272
73- $ fattura = Fattura::where ([
74- 'progressivo_invio ' => $ progressivo_invio ,
75- 'numero_esterno ' => $ numero ,
76- 'data ' => $ data ,
77- ])->first ();
78-
79- if (!empty ($ fattura ) && $ fattura ->tipo ->dir == 'uscita ' ) {
80- throw new \UnexpectedValueException ();
73+ // Estrazione partita IVA e codice fiscale del cedente dal file XML
74+ $ cedente = $ this ->getHeader ()['CedentePrestatore ' ]['DatiAnagrafici ' ];
75+ $ partita_iva = $ cedente ['IdFiscaleIVA ' ]['IdCodice ' ] ?? null ;
76+ $ codice_fiscale = $ cedente ['CodiceFiscale ' ] ?? null ;
77+
78+ // Ricerca anagrafica corrispondente per partita IVA o codice fiscale
79+ $ anagrafica = null ;
80+ if (!empty ($ partita_iva )) {
81+ $ anagrafica = Anagrafica::where ('piva ' , $ partita_iva )->first ();
82+ }
83+ if (empty ($ anagrafica ) && !empty ($ codice_fiscale )) {
84+ $ anagrafica = Anagrafica::where ('codice_fiscale ' , $ codice_fiscale )->first ();
85+ }
86+
87+ // Verifica fattura pre-esistente solo se l'anagrafica corrisponde
88+ if (!empty ($ anagrafica )) {
89+ $ fattura = Fattura::where ([
90+ 'progressivo_invio ' => $ progressivo_invio ,
91+ 'numero_esterno ' => $ numero ,
92+ 'data ' => $ data ,
93+ 'idanagrafica ' => $ anagrafica ->id ,
94+ ])->first ();
95+
96+ if (!empty ($ fattura ) && $ fattura ->tipo ->dir == 'uscita ' ) {
97+ throw new \UnexpectedValueException ();
98+ }
8199 }
82100 }
83101
0 commit comments