2017-08-19 6 views
2

私は最後に発生した請求書を削除できるようにaccount.invoiceからunlinkメソッドをオーバーライドしています。Odoo 10 - 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、自己).unlink()が実行され、それは、古いコード形式のaccount_invoice.py reexcutes:エラーが発生します

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

、私はこれが私のリンクを解除書き換えるべきかをこれは起こらないのですか?

+0

'elif invoice.move_id: – Zety

+0

あなたの質問に間違いを付けることができますか? –

答えて

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

しかし、私はまだ元のメソッドの親メソッドを実行します。 –

+0

@ usk70なぜですか?元のメソッドに基づいてエラーをスローし続けます。基本的にカスタムメソッドを役に立たなくするようです。 – travisw

+0

私は言ったことに注意してください:元のメソッドの親メソッド、私たちの優先メソッドの祖父です。 –

2

この作品が試用するかどうかわかりません。

超過請求書はそれを自己と呼んでください。

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

最初に請求書をインポートしないでください。

+0

これは、クラスを拡張するのに推奨されず、必要でもありません。 OPは適切な継承メソッドを使用しています。 – travisw

+0

私は最後にこれを行いました:super(invoice.AccountInvoice、self).unlink()は、継承された元のメソッドからスーパーを使用するよう強制します。私はモデルを保つ。モデル。私はあなたの答えがうまくいくかもしれないと思う。 –

+0

あなたがしたことをするのは私の最初のアイデアでした。私は私の携帯電話を使用しています私はそれを試して自分のPCを持っていなかった。しかし、再び非常に有用な質問 – Cherif