Skip to content

Commit

Permalink
[REF] l10n_be_cooperator_national_number: Convenience function for ge…
Browse files Browse the repository at this point in the history
…tting national number

Signed-off-by: Carmen Bianca BAKKER <[email protected]>
  • Loading branch information
carmenbianca committed Jun 11, 2024
1 parent a05eb10 commit b00c055
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
38 changes: 27 additions & 11 deletions l10n_be_cooperator_national_number/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,41 @@
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from odoo import models
from odoo import api, models


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

@api.model
def get_be_national_register_number_id_category(self):
return self.env.ref(
"l10n_be_partner_identification.l10n_be_national_registry_number_category"
)

def get_be_national_register_number_id_number(self):
self.ensure_one()
belgian_cat = self.get_be_national_register_number_id_category()
return self.env["res.partner.id_number"].search(
[
("partner_id", "=", self.id),
("category_id", "=", belgian_cat.id),
],
limit=1,
)

def get_be_national_register_number(self):
self.ensure_one()
id_number = self.get_be_national_register_number_id_number()
if id_number:
return id_number.name
return None

def update_belgian_national_number(self, national_number):
self.ensure_one()
result = None
if national_number:
belgian_cat = self.env.ref(
"l10n_be_partner_identification.l10n_be_national_registry_number_category"
)
existing = self.env["res.partner.id_number"].search(
[
("partner_id", "=", self.id),
("category_id", "=", belgian_cat.id),
]
)
existing = self.get_be_national_register_number_id_number()
# Update
if existing:
existing.name = national_number
Expand All @@ -29,7 +45,7 @@ def update_belgian_national_number(self, national_number):
else:
values = {
"name": national_number,
"category_id": belgian_cat.id,
"category_id": self.get_be_national_register_number_id_category().id,
"partner_id": self.id,
}
result = self.env["res.partner.id_number"].create(values)
Expand Down
24 changes: 4 additions & 20 deletions l10n_be_cooperator_national_number/models/subscription_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,11 @@ def _compute_require_national_number(self):
self.company_id.require_national_number and not self.is_company
)

@api.model
def _get_be_national_register_number_id_category(self):
return self.env.ref(
"l10n_be_partner_identification.l10n_be_national_registry_number_category"
)

@api.model
def get_national_number_from_partner(self, partner):
national_number_id_category = (
self._get_be_national_register_number_id_category()
)
national_number = partner.id_numbers.filtered(
lambda rec: rec.category_id.id == national_number_id_category.id
)
return national_number.name

@api.model
def check_be_national_register_number(self, national_number):
national_number_id_category = (
self._get_be_national_register_number_id_category()
)
national_number_id_category = self.env[
"res.partner"
].get_be_national_register_number_id_category()
# this function checks the value of id_number.name, not id_number
# directly.
id_number = namedtuple("id_number", ("name"))(national_number)
Expand All @@ -71,5 +55,5 @@ def validate_subscription_request(self):

def set_person_info(self, partner):
super().set_person_info(partner)
self.national_number = self.get_national_number_from_partner(partner)
self.national_number = partner.get_be_national_register_number()
return True
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added several convenience functions to get national number values on
``res.partner``.

0 comments on commit b00c055

Please sign in to comment.