From 215cb70cea1059c4c25215a29e83cf6e5ccfbf7b Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Fri, 31 Jul 2015 18:06:06 +0200 Subject: [PATCH] Port module account_invoice_del_attachment_cancel to v8/newAPI --- .../__init__.py | 23 ++++++++ .../__openerp__.py | 44 +++++++++++++++ .../account_invoice.py | 54 +++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 account_invoice_del_attachment_cancel/__init__.py create mode 100644 account_invoice_del_attachment_cancel/__openerp__.py create mode 100644 account_invoice_del_attachment_cancel/account_invoice.py diff --git a/account_invoice_del_attachment_cancel/__init__.py b/account_invoice_del_attachment_cancel/__init__.py new file mode 100644 index 0000000..f391d3e --- /dev/null +++ b/account_invoice_del_attachment_cancel/__init__.py @@ -0,0 +1,23 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# Account Invoice Del Attachment Cancel module for OpenERP +# Copyright (C) 2015 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from . import account_invoice diff --git a/account_invoice_del_attachment_cancel/__openerp__.py b/account_invoice_del_attachment_cancel/__openerp__.py new file mode 100644 index 0000000..9aaff5e --- /dev/null +++ b/account_invoice_del_attachment_cancel/__openerp__.py @@ -0,0 +1,44 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# Account Invoice Del Attachment Cancel module for Odoo +# Copyright (C) 2015 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + + +{ + 'name': 'Account Invoice Delete Attachment on Cancel', + 'version': '0.1', + 'category': 'Accounting & Finance', + 'license': 'AGPL-3', + 'summary': 'Delete the attachment on the invoice when it is set back to draft', + 'description': """ +Account Invoice Delete Attachment on Cancel +=========================================== + +When a customer invoice is validated, on the first generation of the invoice report, a copy of the report is stored as attachment on the invoice. After that, every time a user asks for the Invoice report, it will be taken from the attachment. But, when a customer invoice/refund is cancelled, set back to draft, modified and re-validated, the Invoice report is still the old PDF file, which is often raised as a bug by the users. + +With this module, when a customer invoice/refund is set back to draft, the attachment is deleted. + +This module has been written by Alexis de Lattre from Akretion . + """, + 'author': 'Akretion', + 'website': 'http://www.akretion.com', + 'depends': ['account'], + 'installable': True, +} diff --git a/account_invoice_del_attachment_cancel/account_invoice.py b/account_invoice_del_attachment_cancel/account_invoice.py new file mode 100644 index 0000000..8ff4866 --- /dev/null +++ b/account_invoice_del_attachment_cancel/account_invoice.py @@ -0,0 +1,54 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# Account Invoice Del Attachment Cancel module for Odoo +# Copyright (C) 2015 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from openerp import models, api, _ + + +class AccountInvoice(models.Model): + _inherit = 'account.invoice' + + @api.multi + def invoice_filename_to_match(self): + return 'INV%.pdf' + + @api.multi + def action_cancel_draft(self): + res = super(AccountInvoice, self).action_cancel_draft() + iao = self.env['ir.attachment'] + for invoice in self: + # search for attachments + if 'out' in invoice.type: + filename_to_match = invoice.invoice_filename_to_match() + attachs = iao.search([ + ('res_id', '=', invoice.id), + ('res_model', '=', self._name), + ('type', '=', 'binary'), + ('datas_fname', '=like', filename_to_match), + ]) + if len(attachs) == 1: + # delete attachment + attach = attachs[0] + attach_name = attach.name + attachs.unlink() + invoice.message_post( + _('Attachement %s has been deleted') % attach_name) + return res