@@ -258,33 +258,14 @@ def _compute_suitable_journal_ids(self):
258258 move .suitable_journal_ids = self .env ['account.journal' ].search (domain )
259259
260260 def button_draft (self ):
261- # Get report name before reset to draft because 'attachment' field of report
262- # is False when state != 'posted'
263- report_filenames = self ._get_invoice_attachment_name ()
264- super ().button_draft ()
265261 # Delete attached pdf invoice
266- if report_filenames :
267- for move in self .filtered (lambda x : x .move_type in ('out_invoice' , 'out_refund' )):
268- attachments = self .env ['ir.attachment' ].search ([
269- ('name' , 'in' , report_filenames [move .id ]),
270- ('res_id' , '=' , move .id ),
271- ('res_model' , '=' , self ._name ),
272- ('type' , '=' , 'binary' ),
273- ])
274- if attachments :
275- attachments .unlink ()
276-
277- def _get_invoice_attachment_name (self ):
278- report_filenames = defaultdict (list )
279- for report_name in ('account.report_invoice' , 'account.report_invoice_with_payments' ):
280- try :
281- report_invoice = self .env ['ir.actions.report' ]._get_report_from_name (report_name )
282- except IndexError :
283- report_invoice = False
284- if report_invoice and report_invoice .attachment :
285- for move in self .filtered (lambda x : x .move_type in ('out_invoice' , 'out_refund' )):
286- report_filenames [move .id ].append (safe_eval (report_invoice .attachment , {'object' : self , 'time' : time }))
287- return report_filenames
262+ for move in self .filtered (lambda x : x .move_type in ('out_invoice' , 'out_refund' )):
263+ for report_xmlid in ('account.account_invoices' , 'account.account_invoices_without_payment' ):
264+ report = self .env .ref (report_xmlid )
265+ attach = report .retrieve_attachment (move )
266+ if attach :
267+ attach .unlink ()
268+ super ().button_draft ()
288269
289270 def _get_accounting_date (self , invoice_date , has_tax ):
290271 # On vendor bills/refunds, we want date = invoice_date unless
0 commit comments