From b505cabcd973f73255f2699f0e52b67055b439e2 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Sat, 7 May 2016 17:07:15 +0200 Subject: [PATCH] Filter on allowed_journal_ids even if onchange of payment_mode_id is not played (for example when the payment line is pushed from the invoice) --- .../models/account_invoice.py | 9 +--- .../models/account_payment_order.py | 41 +++++++++++-------- .../views/account_payment_order.xml | 4 +- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/account_payment_order/models/account_invoice.py b/account_payment_order/models/account_invoice.py index 7431e12b2..39a57d370 100644 --- a/account_payment_order/models/account_invoice.py +++ b/account_payment_order/models/account_invoice.py @@ -32,14 +32,7 @@ class AccountInvoice(models.Model): @api.multi def _prepare_new_payment_order(self): self.ensure_one() - vals = { - 'payment_mode_id': self.payment_mode_id.id, - 'payment_type': self.payment_mode_id.payment_type, - } - if self.payment_mode_id.bank_account_link == 'fixed': - vals['journal_id'] = self.payment_mode_id.fixed_journal_id.id - # TODO : else: no filter on allowed bank accounts, - # because onchange not played ?? + vals = {'payment_mode_id': self.payment_mode_id.id} return vals @api.multi diff --git a/account_payment_order/models/account_payment_order.py b/account_payment_order/models/account_payment_order.py index 99dd15de2..67bb0c790 100644 --- a/account_payment_order/models/account_payment_order.py +++ b/account_payment_order/models/account_payment_order.py @@ -36,6 +36,9 @@ class AccountPaymentOrder(models.Model): journal_id = fields.Many2one( 'account.journal', string='Bank Journal', readonly=True, states={'draft': [('readonly', False)]}) + allowed_journal_ids = fields.Many2many( + 'account.journal', compute='_compute_allowed_journals', readonly=True, + string='Selectable Bank Journals') # The journal_id field is only required at confirm step, to # allow auto-creation of payment order from invoice company_partner_bank_id = fields.Many2one( @@ -110,34 +113,38 @@ class AccountPaymentOrder(models.Model): for order in self: order.bank_line_count = len(order.bank_line_ids) + @api.multi + @api.depends('payment_mode_id') + def _compute_allowed_journals(self): + for order in self: + allowed_journal_ids = False + if order.payment_mode_id: + if order.payment_mode_id.bank_account_link == 'fixed': + allowed_journal_ids = order.payment_mode_id.fixed_journal_id + else: + allowed_journal_ids = order.payment_mode_id.variable_journal_ids + order.allowed_journal_ids = allowed_journal_ids + @api.model def create(self, vals): if vals.get('name', 'New') == 'New': vals['name'] = self.env['ir.sequence'].next_by_code( 'account.payment.order') or 'New' + if vals.get('payment_mode_id'): + payment_mode = self.env['account.payment.mode'].browse( + vals['payment_mode_id']) + vals['payment_type'] = payment_mode.payment_type + if payment_mode.bank_account_link == 'fixed': + vals['journal_id'] = payment_mode.fixed_journal_id.id return super(AccountPaymentOrder, self).create(vals) @api.onchange('payment_mode_id') def payment_mode_id_change(self): - res = {'domain': {}} + journal_id = False if self.payment_mode_id: if self.payment_mode_id.bank_account_link == 'fixed': - self.journal_id = self.payment_mode_id.fixed_journal_id - # journal_id is a required field, so I can't set it readonly - # so I restrict the domain so that the user cannot change - # the journal - res['domain']['journal_id'] = [( - 'id', - '=', - self.payment_mode_id.fixed_journal_id.id)] - else: - res['domain']['journal_id'] = [( - 'id', - 'in', - self.payment_mode_id.variable_journal_ids.ids)] - else: - self.journal_id = False - return res + journal_id = self.payment_mode_id.fixed_journal_id + self.journal_id = journal_id @api.multi def action_done(self): diff --git a/account_payment_order/views/account_payment_order.xml b/account_payment_order/views/account_payment_order.xml index deab0db81..ae3dc23bf 100644 --- a/account_payment_order/views/account_payment_order.xml +++ b/account_payment_order/views/account_payment_order.xml @@ -34,7 +34,9 @@ - + +