-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by pedrobaeza
- Loading branch information
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