我正在重写account.invoice中的unlink方法,以允许删除最后一次提交的发票。
这是我的代码:
class AccountInvoice(models.Model):
_inherit = "account.invoice"
@api.multi
def unlink(self):
for invoice in self:
if invoice.state not in ('draft', 'cancel'):
raise UserError(('You cannot delete an invoice which is not draft or cancelled. You should refund it instead.'))
elif invoice.move_name:
if invoice.journal_id.sequence_id:
sequence_id = invoice.journal_id.sequence_id
last_assigned_number = sequence_id.next_number_do_not_increase() - 1
last_assigned_number_text = sequence_id.get_next_char(last_assigned_number)
if last_assigned_number_text == invoice.move_name:
invoice.journal_id.sequence_id.write({'number_next': last_assigned_number})
else:
raise UserError(('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(AccountInvoice, self).unlink()到目前一切尚好,
我的具体问题在最后一行,当我运行此代码流时,此例程中没有引发UserErrors,但随后它运行AccountInvoice(AccountInvoice,self).unlink(),并执行旧的代码形式account_invoice.py:
@api.multi
def unlink(self):
for invoice in self:
if invoice.state not in ('draft', 'cancel'):
raise UserError(_('You cannot delete an invoice which is not draft or cancelled. You should refund it instead.'))
elif invoice.move_name:
raise UserError(_('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(AccountInvoice, self).unlink()这会引发一个错误,我应该如何重写这个unlink方法才不会发生这种情况?
发布于 2017-08-20 21:18:46
不知道这项工作是不是可以试试。
称它为发票本身的超级。
super(invoice.AccountInvoice, self).unlink()别忘了先导入发票。
发布于 2017-08-20 13:17:45
如果你用覆盖了原来的方法,而不是扩展(添加)它,那么你只需要避免调用super。
# replace this line to prevent the original method from running
# return super(AccountInvoice, self).unlink()
# this will unlink (delete) all records in the recordset without
# calling the original method (which is what super does)
return self.unlink()发布于 2020-06-08 17:43:09
导入首选的覆盖方法
from odoo.addons.account.models.account_invoice import AccountInvoice as BaseAccountInvoice那就叫它
class AccountInvoice(models.Model):
_inherit = 'account.invoice'
@api.multi
def unlink(self):
return super(BaseAccountInvoice,self).unlink()https://stackoverflow.com/questions/45772787
复制相似问题