@@ -1850,7 +1850,7 @@ def _get_invoice_partner_id(self, fatt):
1850
1850
partner_id = self .getCedPrest (cedentePrestatore )
1851
1851
return partner_id
1852
1852
1853
- def importFatturaPA (self ):
1853
+ def importFatturaPA (self ): # noqa: C901
1854
1854
self .ensure_one ()
1855
1855
1856
1856
(
@@ -1873,6 +1873,7 @@ def importFatturaPA(self):
1873
1873
1874
1874
new_invoices = []
1875
1875
# convert to dict in order to be able to modify context
1876
+ new_intermediary_data = []
1876
1877
fatturapa_attachments = self ._get_selected_records ()
1877
1878
self .env .context = dict (self .env .context )
1878
1879
for fatturapa_attachment in fatturapa_attachments :
@@ -1924,8 +1925,55 @@ def importFatturaPA(self):
1924
1925
)
1925
1926
invoice .write ({"tax_representative_id" : tax_partner_id })
1926
1927
if Intermediary :
1927
- Intermediary_id = self .getPartnerBase (Intermediary .DatiAnagrafici )
1928
- invoice .write ({"intermediary" : Intermediary_id })
1928
+ vat = False
1929
+ name = False
1930
+ if Intermediary .DatiAnagrafici .Anagrafica .Denominazione :
1931
+ name = Intermediary .DatiAnagrafici .Anagrafica .Denominazione
1932
+ else :
1933
+ nome = Intermediary .DatiAnagrafici .Anagrafica .Nome or ""
1934
+ cognome = Intermediary .DatiAnagrafici .Anagrafica .Cognome or ""
1935
+ name = f"{ nome } { cognome } " .strip ()
1936
+
1937
+ if Intermediary .DatiAnagrafici .IdFiscaleIVA :
1938
+ id_paese = (
1939
+ Intermediary .DatiAnagrafici .IdFiscaleIVA .IdPaese .upper ()
1940
+ )
1941
+ id_codice = re .sub (
1942
+ r"\W+" ,
1943
+ "" ,
1944
+ Intermediary .DatiAnagrafici .IdFiscaleIVA .IdCodice ,
1945
+ ).upper ()
1946
+ # Format Italian VAT ID to always have 11 char
1947
+ # to avoid validation error when creating the given partner
1948
+ if id_paese == "IT" and not id_codice .startswith ("IT" ):
1949
+ vat = "IT{}" .format (id_codice .rjust (11 , "0" )[:11 ])
1950
+ # XXX maybe San Marino needs special formatting too?
1951
+ else :
1952
+ vat = id_codice
1953
+
1954
+ vat_check = self .env ["res.partner" ].search (
1955
+ [("vat" , "=" , vat ), ("name" , "=" , name )]
1956
+ )
1957
+
1958
+ if vat_check :
1959
+ invoice .write ({"intermediary" : vat_check .id })
1960
+ else :
1961
+ new_intermediary_data .append (
1962
+ {
1963
+ "name" : name ,
1964
+ "vat" : vat ,
1965
+ "invoice_id" : invoice .id ,
1966
+ }
1967
+ )
1968
+ else :
1969
+ new_intermediary_data .append (
1970
+ {
1971
+ "name" : name ,
1972
+ "vat" : vat ,
1973
+ "invoice_id" : invoice .id ,
1974
+ }
1975
+ )
1976
+
1929
1977
new_invoices .append (invoice .id )
1930
1978
self .check_invoice_amount (invoice , fattura )
1931
1979
@@ -1949,6 +1997,34 @@ def importFatturaPA(self):
1949
1997
discount_precision , original_discount_precision
1950
1998
)
1951
1999
2000
+ if new_intermediary_data :
2001
+ res = self .env ["wizard.check.intermediary" ].create (
2002
+ {
2003
+ "line_ids" : [
2004
+ (
2005
+ 0 ,
2006
+ 0 ,
2007
+ {
2008
+ "name" : new_data ["name" ],
2009
+ "vat" : new_data ["vat" ],
2010
+ "invoice_id" : new_data ["invoice_id" ],
2011
+ },
2012
+ )
2013
+ for new_data in new_intermediary_data
2014
+ ],
2015
+ "invoice_ids" : [(6 , 0 , new_invoices )],
2016
+ }
2017
+ )
2018
+
2019
+ return {
2020
+ "type" : "ir.actions.act_window" ,
2021
+ "name" : "Confirm action" ,
2022
+ "res_model" : "wizard.check.intermediary" ,
2023
+ "res_id" : res .id ,
2024
+ "view_mode" : "form" ,
2025
+ "target" : "new" ,
2026
+ }
2027
+
1952
2028
return {
1953
2029
"view_type" : "form" ,
1954
2030
"name" : "Electronic Bills" ,
0 commit comments