account_payment.py 1.17 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 32
# -*- coding: utf-8 -*-

from openerp import api, models, _
from openerp.exceptions import ValidationError


class AccountPayment(models.Model):
    _inherit = "account.payment"

    @api.multi
    def check_post(self):
        new_context = dict(
            self.env.context, **{'account_payment_confirm': True})
        self.with_context(new_context).post()

    @api.multi
    def post(self):
        if self.env.context.get('account_payment_confirm', False):
            for rec in self:
                error_invoices = rec.invoice_ids.filtered(
                    lambda i: i.state != 'open')
                if len(error_invoices):
                    msg = "\n".join([
                        "%s: %s - %s - %s: %s - %s" % (
                            _("invoice"), inv.partner_id.name, inv.number,
                            _("payment"), rec.name, rec.payment_date
                        ) for inv in error_invoices])
                    raise ValidationError(_(
                        """The payment cannot be processed because some """
                        """invoices are not open!\n"""
                        """%s""") % msg)
        return super(AccountPayment, self).post()