-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] base_bank_from_iban: Handle correctly non IBAN accounts
If any non IBAN account is provided, there's an ugly log with traceback each time, polluting tests and system logs: ``` INFO prod odoo.addons.base_bank_from_iban.models.res_partner_bank: Could not find bank from IBAN Traceback (most recent call last): File ".../addons/base_bank_from_iban/models/res_partner_bank.py", line 34, in _add_bank_vals bank = self._get_bank_from_iban(vals["acc_number"]) File ".../addons/base_bank_from_iban/models/res_partner_bank.py", line 42, in _get_bank_from_iban iban = schwifty.IBAN(acc_number) File ".../python/site-packages/schwifty/iban.py", line 77, in __init__ self.validate(validate_bban) File ".../python/site-packages/schwifty/iban.py", line 175, in validate self._validate_characters() File ".../python/site-packages/schwifty/iban.py", line 185, in _validate_characters raise exceptions.InvalidStructure(f"Invalid characters in IBAN {self!s}") schwifty.exceptions.InvalidStructure: Invalid characters in IBAN XXXXXX ``` This commit removes that traceback catching the proper exception, and handling it accordingly, and also removing an extra INFO log that was not adding value.
- Loading branch information
1 parent
207d18d
commit 1da90fe
Showing
2 changed files
with
67 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
# Copyright 2017 Tecnativa - Carlos Dauden <[email protected]> | ||
# Copyright 2017 Tecnativa - Carlos Dauden | ||
# Copyright 2024 Tecnativa - Pedro M. Baeza | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3). | ||
|
||
import logging | ||
|
||
import schwifty | ||
|
||
from odoo import api, models | ||
|
@@ -13,8 +12,6 @@ | |
pretty_iban, | ||
) | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
class ResPartnerBank(models.Model): | ||
_inherit = "res.partner.bank" | ||
|
@@ -30,32 +27,37 @@ def write(self, vals): | |
|
||
def _add_bank_vals(self, vals): | ||
if vals.get("acc_number") and not vals.get("bank_id"): | ||
try: | ||
bank = self._get_bank_from_iban(vals["acc_number"]) | ||
vals["bank_id"] = bank.id | ||
except Exception: | ||
_logger.info("Could not find bank from IBAN", exc_info=True) | ||
vals["bank_id"] = self._get_bank_from_iban(vals["acc_number"]).id | ||
return vals | ||
|
||
@api.model | ||
def _get_bank_from_iban(self, acc_number): | ||
iban = schwifty.IBAN(acc_number) | ||
country_code = iban.country_code.lower() | ||
country = self.env.ref("base.%s" % country_code, raise_if_not_found=False) | ||
vals = { | ||
"name": iban.bank["name"], | ||
"bic": iban.bank["bic"], | ||
"code": iban.bank["bank_code"], | ||
"country": country.id, | ||
} | ||
domain = [("code", "=", iban.bank["bank_code"])] | ||
bank = self.env["res.bank"].search(domain) | ||
if bank and len(bank) == 1: | ||
for field in vals: | ||
if not bank[field]: | ||
bank[field] = vals[field] | ||
else: | ||
bank = self.env["res.bank"].create(vals) | ||
try: | ||
iban = schwifty.IBAN(acc_number) | ||
country_code = iban.country_code.lower() | ||
country = self.env.ref("base.%s" % country_code, raise_if_not_found=False) | ||
if iban.bank: | ||
vals = { | ||
"name": iban.bank["name"], | ||
"bic": iban.bank["bic"], | ||
"code": iban.bank["bank_code"], | ||
"country": country.id, | ||
} | ||
domain = [ | ||
("code", "=", iban.bank["bank_code"]), | ||
("country", "=", country.id), | ||
] | ||
bank = self.env["res.bank"].search(domain, limit=1) | ||
if bank: | ||
for field in vals: | ||
if not bank[field]: | ||
bank[field] = vals[field] | ||
else: | ||
bank = self.env["res.bank"].create(vals) | ||
else: | ||
bank = self.env["res.bank"] | ||
except schwifty.exceptions.InvalidStructure: | ||
bank = self.env["res.bank"] | ||
return bank | ||
|
||
@api.onchange("acc_number", "acc_type") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters