[IMP] account: Verify if res_partner_bank exists in set_bank_account.

When creating or writing on a account.journal model,
it is possible to pass a char representing the bank account number
into bank_acc_number instead of passing directly a res.partner.bank.
In case we pass bank_acc_number the system will try to create automatically
a new res.partner.bank for us, however if a res.partner.bank already exists
with the same account number it will result in an error as we have a unicity
constraint for that. Therefore it is best to first seach for such an existing
res.partner.bank before trying to create it. The search is done on the
sanitized_account_number field as the unicity constraint is on that field.

closes odoo/odoo#49637

Signed-off-by: Cedric Snauwaert (csn) <csn@openerp.com>
This commit is contained in:
Florian Gilbert
2020-04-17 06:45:27 +00:00
parent 7e2971d080
commit 25f157efc0
+14 -8
View File
@@ -2,6 +2,7 @@
from odoo import api, fields, models, _
from odoo.osv import expression
from odoo.exceptions import UserError, ValidationError
from odoo.addons.base.models.res_bank import sanitize_account_number
class AccountJournalGroup(models.Model):
@@ -486,15 +487,20 @@ class AccountJournal(models.Model):
return journal
def set_bank_account(self, acc_number, bank_id=None):
""" Create a res.partner.bank and set it as value of the field bank_account_id """
""" Create a res.partner.bank (if not exists) and set it as value of the field bank_account_id """
self.ensure_one()
self.bank_account_id = self.env['res.partner.bank'].create({
'acc_number': acc_number,
'bank_id': bank_id,
'company_id': self.company_id.id,
'currency_id': self.currency_id.id,
'partner_id': self.company_id.partner_id.id,
}).id
res_partner_bank = self.env['res.partner.bank'].search([('sanitized_acc_number', '=', sanitize_account_number(acc_number)),
('company_id', '=', self.company_id.id)], limit=1)
if res_partner_bank:
self.bank_account_id = res_partner_bank.id
else:
self.bank_account_id = self.env['res.partner.bank'].create({
'acc_number': acc_number,
'bank_id': bank_id,
'company_id': self.company_id.id,
'currency_id': self.currency_id.id,
'partner_id': self.company_id.partner_id.id,
}).id
def name_get(self):
res = []