Skip to content

Commit

Permalink
*
Browse files Browse the repository at this point in the history
  • Loading branch information
len-foss authored and carlosdauden committed Jan 29, 2024
1 parent 5a89fda commit c5e4603
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions base_bank_from_iban/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"depends": ["base_iban"],
"development_status": "Mature",
"data": ["views/res_bank_view.xml"],
"external_dependencies": {"python": ["schwifty"]},
"installable": True,
}
45 changes: 45 additions & 0 deletions base_bank_from_iban/models/res_partner_bank.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Copyright 2017 Tecnativa - Carlos Dauden <carlos.dauden@tecnativa.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3).

import logging

import schwifty

from odoo import api, models

from odoo.addons.base_iban.models.res_partner_bank import (
Expand All @@ -9,10 +13,51 @@
pretty_iban,
)

_logger = logging.getLogger(__name__)


class ResPartnerBank(models.Model):
_inherit = "res.partner.bank"

@api.model_create_multi
def create(self, vals_list):
vals_list = [self._add_bank_vals(vals) for vals in vals_list]
return super().create(vals_list)

def write(self, vals):
self._add_bank_vals(vals)
return super().write(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)
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)
return bank

@api.onchange("acc_number", "acc_type")
def _onchange_acc_number_base_bank_from_iban(self):
if self.acc_type != "iban":
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# generated from manifests external_dependencies
pycountry
schwifty

0 comments on commit c5e4603

Please sign in to comment.