account_move_reversal.py 1.09 KB
Newer Older
François C. committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
# -*- coding: utf-8 -*-
# Copyright (C) 2016-Today: La Louve (<http://www.lalouve.fr/>)
# @author: La Louve
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html

from openerp import models, fields, api


class AccountMoveReversal(models.TransientModel):
    _inherit = 'account.move.reversal'

    @api.multi
    def reverse_moves(self):
        payment_id = self._context.get('payment_id', False)
        account_move_ids = self._context.get('active_ids', False)

        # Get move line to unreconcile automaticly
        account_moves = self.env['account.move'].browse(account_move_ids)
        lines_reconciled = account_moves.mapped('line_ids').filtered(
            lambda l: l.reconciled)
        if lines_reconciled:
            lines_reconciled.remove_move_reconcile()

        res = super(AccountMoveReversal, self).reverse_moves()

        # If exist payment, change state to cancelled
        payment = self.env['account.payment'].browse(payment_id)
        if payment:
            payment.state = 'cancelled'
            return {'type': 'ir.actions.act_window_close'}
        return res