Odoo 10 - 覆盖unlink方法

时间:2017-08-19 14:37:54

标签: odoo odoo-10

我将覆盖account.invoice中的取消链接方法,以允许删除上次提出的发票。

这是我的代码:

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()

到目前为止一切顺利,

我的具体问题是在最后一行,当我运行此代码流时,所以在此ROUTINE中没有引发UserErrors,但它运行super(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()

哪会引发错误,我应该如何重写这种取消链接方法,以免发生这种情况?

3 个答案:

答案 0 :(得分:2)

不知道这项工作是否可以尝试。

自己打电话给超级发票。

           super(invoice.AccountInvoice, self).unlink()

不要忘记先输入发票。

答案 1 :(得分:1)

如果您覆盖原始方法而不是扩展(添加),那么您只需要避免调用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()

答案 2 :(得分:0)

导入首选的替代方法

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()