From fa7d3e91695a42f464afdc2de488f2670c418c65 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Tue, 7 Jun 2016 00:58:19 +0200 Subject: [PATCH] Better handling of partner_bank_id and mandate_id on invoice: invisible/required/onchange --- .../models/account_invoice.py | 31 +++++++++++++++---- .../views/account_invoice_view.xml | 6 ++-- .../views/account_payment_method.xml | 2 +- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/account_banking_mandate/models/account_invoice.py b/account_banking_mandate/models/account_invoice.py index 23255c58e..46d884493 100644 --- a/account_banking_mandate/models/account_invoice.py +++ b/account_banking_mandate/models/account_invoice.py @@ -15,6 +15,9 @@ class AccountInvoice(models.Model): 'account.banking.mandate', string='Direct Debit Mandate', ondelete='restrict', readonly=True, states={'draft': [('readonly', False)]}) + mandate_required = fields.Boolean( + related='payment_mode_id.payment_method_id.mandate_required', + readonly=True) @api.model def line_get_convert(self, line, part): @@ -41,16 +44,32 @@ class AccountInvoice(models.Model): vals['mandate_id'] = invoice.mandate_id.id return vals - @api.onchange('payment_mode_id') - def payment_mode_change(self): + @api.onchange('partner_id', 'company_id') + def _onchange_partner_id(self): """Select by default the first valid mandate of the partner""" + super(AccountInvoice, self)._onchange_partner_id() if ( - self.type in ('out_invoice', 'out_refund') and - self.payment_mode_id.payment_type == 'inbound' and - self.payment_mode_id.payment_method_id.mandate_required and - self.partner_id): + self.type == 'out_invoice' and + self.partner_id.customer_payment_mode_id.\ + payment_type == 'inbound' and + self.partner_id.customer_payment_mode_id.payment_method_id.\ + mandate_required and + self.commercial_partner_id): mandates = self.env['account.banking.mandate'].search([ ('state', '=', 'valid'), ('partner_id', '=', self.commercial_partner_id.id), ]) self.mandate_id = mandates[0] + else: + self.mandate_id = False + + @api.onchange('payment_mode_id') + def payment_mode_id_change(self): + super(AccountInvoice, self).payment_mode_id_change() + if ( + self.payment_mode_id and + self.payment_mode_id.payment_type == 'inbound' and + not self.payment_mode_id.payment_method_id.mandate_required): + self.mandate_id = False + elif not self.payment_mode_id: + self.mandate_id = False diff --git a/account_banking_mandate/views/account_invoice_view.xml b/account_banking_mandate/views/account_invoice_view.xml index 41fcc966c..1c1e46647 100644 --- a/account_banking_mandate/views/account_invoice_view.xml +++ b/account_banking_mandate/views/account_invoice_view.xml @@ -12,9 +12,11 @@ account.invoice - + + domain="[('partner_id', '=', commercial_partner_id), ('state', '=', 'valid')]" + attrs="{'required': [('mandate_required', '=', True)], 'invisible': [('mandate_required', '=', False)]}"/> + diff --git a/account_banking_mandate/views/account_payment_method.xml b/account_banking_mandate/views/account_payment_method.xml index 2d4079533..c6067e150 100644 --- a/account_banking_mandate/views/account_payment_method.xml +++ b/account_banking_mandate/views/account_payment_method.xml @@ -6,7 +6,7 @@ account_banking_mandate.account.payment.method.form account.payment.method - +