@@ -109,6 +109,18 @@ def _import_fattura_pa(self, tree, invoice):
109
109
invoice = self .env ['account.move' ]
110
110
first_run = False
111
111
112
+ # Type must be present in the context to get the right behavior of the _default_journal method (account.move).
113
+ # journal_id must be present in the context to get the right behavior of the _default_account method (account.move.line).
114
+ elements = tree .xpath ('//CessionarioCommittente//IdCodice' )
115
+ company = elements and self .env ['res.company' ].search ([('vat' , 'ilike' , elements [0 ].text )], limit = 1 )
116
+ if not company :
117
+ elements = tree .xpath ('//CessionarioCommittente//CodiceFiscale' )
118
+ company = elements and self .env ['res.company' ].search ([('l10n_it_codice_fiscale' , 'ilike' , elements [0 ].text )], limit = 1 )
119
+ if not company :
120
+ # Only invoices with a correct VAT or Codice Fiscale can be imported
121
+ _logger .warning ('No company found with VAT or Codice Fiscale like %r.' , elements [0 ].text )
122
+ continue
123
+
112
124
# Refund type.
113
125
# TD01 == invoice
114
126
# TD02 == advance/down payment on invoice
@@ -123,32 +135,14 @@ def _import_fattura_pa(self, tree, invoice):
123
135
move_type = 'in_refund'
124
136
elif elements and elements [0 ].text and elements [0 ].text != 'TD01' :
125
137
_logger .info ('Document type not managed: %s. Invoice type is set by default.' , elements [0 ].text )
126
- invoice_ctx = invoice .with_context (default_move_type = move_type )
127
-
128
- # type must be present in the context to get the right behavior of the _default_journal method (account.move).
129
- # journal_id must be present in the context to get the right behavior of the _default_account method (account.move.line).
130
-
131
- elements = tree .xpath ('//CessionarioCommittente//IdCodice' )
132
- company = elements and self .env ['res.company' ].search ([('vat' , 'ilike' , elements [0 ].text )], limit = 1 )
133
- if not company :
134
- elements = tree .xpath ('//CessionarioCommittente//CodiceFiscale' )
135
- company = elements and self .env ['res.company' ].search ([('l10n_it_codice_fiscale' , 'ilike' , elements [0 ].text )], limit = 1 )
136
-
137
- if company :
138
- invoice_ctx = invoice_ctx .with_context (company_id = company .id )
139
- else :
140
- company = self .env .company
141
- if elements :
142
- _logger .info ('No company found with codice fiscale: %s. The user\' s company is set by default.' , elements [0 ].text )
143
- else :
144
- _logger .info ('Company not found. The user\' s company is set by default.' )
145
138
146
- if not self .env .is_superuser ():
147
- if self .env .company != company :
148
- raise UserError (_ ("You can only import invoice concern your current company: %s" , self .env .company .display_name ))
139
+ # Setup the context for the Invoice Form
140
+ invoice_ctx = invoice .with_company (company ) \
141
+ .with_context (default_move_type = move_type ,
142
+ account_predictive_bills_disable_prediction = True )
149
143
150
144
# move could be a single record (editing) or be empty (new).
151
- with Form (invoice_ctx . with_context ( account_predictive_bills_disable_prediction = True ) ) as invoice_form :
145
+ with Form (invoice_ctx ) as invoice_form :
152
146
message_to_log = []
153
147
154
148
# Partner (first step to avoid warning 'Warning! You must first select a partner.'). <1.2>
0 commit comments