sale_order.py 1.47 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 33 34 35 36 37 38
# -*- coding: utf-8 -*-
# © 2014-2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openerp import models, fields, api


class SaleOrder(models.Model):
    _inherit = "sale.order"

    # This field commercial_partner_id should be moved
    # in an OCA base module named for example sale_commercial_partner
    commercial_partner_id = fields.Many2one(
        related='partner_id.commercial_partner_id', string='Commercial Entity',
        store=True, readonly=True)
    mandate_id = fields.Many2one(
        'account.banking.mandate', string='Direct Debit Mandate',
        ondelete='restrict', readonly=True,
        states={'draft': [('readonly', False)], 'sent': [('readonly', False)]})

    @api.multi
    def _prepare_invoice(self):
        """Copy mandate from sale order to invoice"""
        vals = super(SaleOrder, self)._prepare_invoice()
        vals['mandate_id'] = self.mandate_id.id
        return vals

    @api.onchange('payment_mode_id')
    def payment_mode_change(self):
        """Select by default the first valid mandate of the partner"""
        if (
                self.payment_mode_id.payment_method_id.mandate_required and
                self.partner_id):
            mandates = self.env['account.banking.mandate'].search([
                ('state', '=', 'valid'),
                ('partner_id', '=', self.commercial_partner_id.id),
                ])
            self.mandate_id = mandates[0]