Eric
Eric

Reputation: 1126

Unlink Odoo Journal Entries

I am inheriting the account.move module so that I can delete journal entries. The problem is that when I override the original code, I get a maximum recursion depth error: The original code looks like this:

 def unlink(self):
        for move in self:
            if move.name != '/' and not self._context.get('force_delete'):
                raise UserError(_("You cannot delete an entry which has been posted once."))
            move.line_ids.unlink()
        return super(AccountMove, self).unlink()

while this is my extension

 def unlink(self):
        for move in self:
            move.line_ids.unlink()
        return super(AccountMove, self).unlink()

Thank you!

Upvotes: 1

Views: 1281

Answers (2)

Tejas Tank
Tejas Tank

Reputation: 1216

Odoo 14 version code solution by snippetbucket team. via RPC call as well same context internally pass to remove account.move related records

    move2 = odoo2.env['account.move']
    _moves = move2.search_read([  ('state', '=', 'cancel'), ], ['id'], limit=1000)
    print( len(_moves), "total moves" )
    move2.with_context({'force_delete': True}).unlink([ x['id'] for x in _moves ])

With .with_context({'force_delete': True}) context you can delete journal entries and as well invoices from ODOO 14.x version and future version as well.

Note: account.invoice replaced in odoo 14 version to account.move.

Upvotes: 1

Paxmees
Paxmees

Reputation: 1590

 def unlink(self):
        for move in self:
            move.line_ids.unlink()
        return models.Model.unlink(self)

If you want to bypass the security check then you have to call the base model directly.

Upvotes: 1

Related Questions