From 57ff6a17b7053fd535c4ea7b34fd7d428573e338 Mon Sep 17 00:00:00 2001 From: mreficent Date: Fri, 9 Mar 2018 15:24:06 +0100 Subject: [PATCH] [FIX] account: fix bank_account_id field in account.journal * In account.journal model, the domain of bank_account_id field was incorrectly comparing the partner_id with a res.company object * When modifying the company of a bank journal, the company of the related res.partner.bank was changed without checking if it had a company and also its partner (account holder) was not changed. Was PR #23480. Courtesy of Miquel Raich (Eficent) --- addons/account/models/account.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/addons/account/models/account.py b/addons/account/models/account.py index 2178341a3bf..a39497d7216 100644 --- a/addons/account/models/account.py +++ b/addons/account/models/account.py @@ -378,7 +378,7 @@ class AccountJournal(models.Model): belongs_to_company = fields.Boolean('Belong to the user\'s current company', compute="_belong_to_company", search="_search_company_journals",) # Bank journals fields - bank_account_id = fields.Many2one('res.partner.bank', string="Bank Account", ondelete='restrict', copy=False, domain="[('partner_id','=', company_id)]") + bank_account_id = fields.Many2one('res.partner.bank', string="Bank Account", ondelete='restrict', copy=False) bank_statements_source = fields.Selection([('undefined', 'Undefined Yet'),('manual', 'Record Manually')], string='Bank Feeds', default='undefined') bank_acc_number = fields.Char(related='bank_account_id.acc_number') bank_id = fields.Many2one('res.bank', related='bank_account_id.bank_id') @@ -488,11 +488,16 @@ class AccountJournal(models.Model): @api.multi def write(self, vals): for journal in self: + company = journal.company_id if ('company_id' in vals and journal.company_id.id != vals['company_id']): if self.env['account.move'].search([('journal_id', 'in', self.ids)], limit=1): raise UserError(_('This journal already contains items, therefore you cannot modify its company.')) - if self.bank_account_id: - self.bank_account_id.company_id = vals['company_id'] + company = self.env['res.company'].browse(vals['company_id']) + if self.bank_account_id.company_id and self.bank_account_id.company_id != company: + self.bank_account_id.write({ + 'company_id': company.id, + 'partner_id': company.partner_id.id, + }) if ('code' in vals and journal.code != vals['code']): if self.env['account.move'].search([('journal_id', 'in', self.ids)], limit=1): raise UserError(_('This journal already contains items, therefore you cannot modify its short name.')) @@ -508,8 +513,14 @@ class AccountJournal(models.Model): self.default_credit_account_id.currency_id = vals['currency_id'] if self.bank_account_id: self.bank_account_id.currency_id = vals['currency_id'] - if 'bank_account_id' in vals and not vals.get('bank_account_id'): - raise UserError(_('You cannot empty the bank account once set.')) + if 'bank_account_id' in vals: + if not vals.get('bank_account_id'): + raise UserError(_('You cannot empty the bank account once set.')) + else: + bank_account = self.env['res.partner.bank'].browse(vals['bank_account_id']) + if bank_account.partner_id != company.partner_id: + raise UserError(_("The partners of the journal's company and the related bank account mismatch.")) + result = super(AccountJournal, self).write(vals) # Create the bank_account_id if necessary