Skip to content

Commit 88452b7

Browse files
committed
[IMP] account_usability_akretion: simplify code for invoice attach del upon invoice back to draft
1 parent a6305eb commit 88452b7

File tree

1 file changed

+7
-26
lines changed

1 file changed

+7
-26
lines changed

account_usability/models/account_move.py

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)