@@ -1924,8 +1924,78 @@ def importFatturaPA(self):
1924
1924
)
1925
1925
invoice .write ({"tax_representative_id" : tax_partner_id })
1926
1926
if Intermediary :
1927
- Intermediary_id = self .getPartnerBase (Intermediary .DatiAnagrafici )
1928
- invoice .write ({"intermediary" : Intermediary_id })
1927
+ vat = False
1928
+ name = Intermediary .DatiAnagrafici .Anagrafica .Denominazione
1929
+
1930
+ if Intermediary .DatiAnagrafici .IdFiscaleIVA :
1931
+ id_paese = (
1932
+ Intermediary .DatiAnagrafici .IdFiscaleIVA .IdPaese .upper ()
1933
+ )
1934
+ id_codice = re .sub (
1935
+ r"\W+" ,
1936
+ "" ,
1937
+ Intermediary .DatiAnagrafici .IdFiscaleIVA .IdCodice ,
1938
+ ).upper ()
1939
+ # Format Italian VAT ID to always have 11 char
1940
+ # to avoid validation error when creating the given partner
1941
+ if id_paese == "IT" and not id_codice .startswith ("IT" ):
1942
+ vat = "IT{}" .format (id_codice .rjust (11 , "0" )[:11 ])
1943
+ # XXX maybe San Marino needs special formatting too?
1944
+ else :
1945
+ vat = id_codice
1946
+
1947
+ vat_check = self .env ["res.partner" ].search (
1948
+ [("vat" , "=" , vat ), ("name" , "=" , name )]
1949
+ )
1950
+
1951
+ if vat_check :
1952
+ invoice .write ({"intermediary" : vat_check .id })
1953
+ else :
1954
+ res = self .env ["wizard.check.intermediary" ].create (
1955
+ {
1956
+ "name" : name ,
1957
+ "vat" : vat ,
1958
+ "invoice_id" : invoice .id ,
1959
+ "message" : _ (
1960
+ "The contact intermediary %(name)s "
1961
+ "does not exists in the system."
1962
+ "Do you want to create it?"
1963
+ )
1964
+ % {"name" : name },
1965
+ }
1966
+ )
1967
+
1968
+ return {
1969
+ "type" : "ir.actions.act_window" ,
1970
+ "name" : "Confirm action" ,
1971
+ "res_model" : "wizard.check.intermediary" ,
1972
+ "res_id" : res .id ,
1973
+ "view_mode" : "form" ,
1974
+ "target" : "new" ,
1975
+ }
1976
+ else :
1977
+ res = self .env ["wizard.check.intermediary" ].create (
1978
+ {
1979
+ "name" : name ,
1980
+ "vat" : vat ,
1981
+ "invoice_id" : invoice .id ,
1982
+ "message" : _ (
1983
+ "The contact intermediary %(name)s "
1984
+ "does not exists in the system. Do you want to create it?"
1985
+ )
1986
+ % {"name" : name },
1987
+ }
1988
+ )
1989
+
1990
+ return {
1991
+ "type" : "ir.actions.act_window" ,
1992
+ "name" : "Confirm action" ,
1993
+ "res_model" : "wizard.check.intermediary" ,
1994
+ "res_id" : res .id ,
1995
+ "view_mode" : "form" ,
1996
+ "target" : "new" ,
1997
+ }
1998
+
1929
1999
new_invoices .append (invoice .id )
1930
2000
self .check_invoice_amount (invoice , fattura )
1931
2001
0 commit comments