This question has been flagged
3 Replies
4649 Views

Hi,

I am trying to write a module to delete invoices.

I have seen this post https://www.odoo.com/forum/help-1/question/how-to-delete-an-invoice-after-validation-20831 which explains what to do.

I have therefore created a new module delete_invoices with the following code:

/usr/lib/python2.7/dist-packages/openerp/addons/custom/delete_invoices# cat delete_invoices.py
# -*- coding: utf-8 -*-
from openerp import models, fields, api

# DESCRIPTION:
# Class DeleteInvoices allows invoice deletion
# NOTES:
# account_cancel / diary allow cancel entries required

class DeleteInvoices ( models.Model ):
_inherit = 'account.invoice'

@api.multi
def unlink(self):
for invoice in self:
if invoice.state not in ('draft', 'cancel'):
raise Warning(_('You cannot delete an invoice which is not draft or cancelled. You should refund it instead.'))
# elif invoice.internal_number:
# raise Warning(_('You cannot delete an invoice after it has been validated (and received a number). You can set it back to "Draft" state and modify its content, then re-confirm it.'))
return super(account_invoice, self).unlink()


Unfortunately when I try to delete a cancelled invoice I get the following message:

  File "/usr/lib/python2.7/dist-packages/openerp/addons/custom/delete_invoices/delete_invoices.py", line 20, in unlink
return super(account_invoice, self).unlink()
NameError: global name 'account_invoice' is not defined

Any tips or ideas? Thanks a lot.


For completition I also include __openerp__ and __init__ files:

# cat __init__.py
from . import delete_invoices
# cat __openerp__.py
{
'name': 'Allow delete invoice',
'description': 'This module allows to delete invoices once they have been cancelled',
'author': 'E.M.',
'depends': ['account_cancel'],
'data': [],
}

Thanks

Avatar
Discard

In the __openerp__.py you have to add account depends too. Account_cancel it's not enough

Author

Thanks Mirco. 'account_cancel' depends on 'account' so at the end you will need to have installed both modules so 'account' might be redundant here. In any case issue is located on how I am replacing the method and not in dependencies.

Best Answer

The account_invoice error refers to the class name used on the super call. Just change to DeleteInvoices and you will be ok

Avatar
Discard
Author

Still same error: File "/usr/lib/python2.7/dist-packages/openerp/addons/custom/delete_invoices/delete_invoices.py", line 20, in unlink return super(DeleteInvoices, self).unlink() NameError: global name 'account_invoice' is not defined

That was an error you havd. You should have more. Post all the code because in that line I only saw one account_invoice variable usage on super call. It must be something that you are not posting