Skip to content

Commit

Permalink
Merge PR #127 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by huguesdk
  • Loading branch information
OCA-git-bot committed May 6, 2024
2 parents f2efcdb + 22e48d3 commit b3213f1
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 11 deletions.
4 changes: 4 additions & 0 deletions l10n_be_cooperator_national_number/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# SPDX-FileCopyrightText: 2023 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from . import models
4 changes: 4 additions & 0 deletions l10n_be_cooperator_national_number/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# SPDX-FileCopyrightText: 2023 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from . import company
from . import subscription_request
from . import res_partner
7 changes: 3 additions & 4 deletions l10n_be_cooperator_national_number/models/company.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright 2019 Coop IT Easy SCRL fs
# Houssine Bakkali <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

# SPDX-FileCopyrightText: 2023 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
Expand Down
33 changes: 29 additions & 4 deletions l10n_be_cooperator_national_number/models/subscription_request.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# SPDX-FileCopyrightText: 2023 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from collections import namedtuple

from odoo import _, api, fields, models
from odoo.exceptions import UserError

Expand Down Expand Up @@ -25,19 +31,38 @@ def _compute_require_national_number(self):
self.company_id.require_national_number and not self.is_company
)

def get_national_number_from_partner(self, partner):
national_number_id_category = self.env.ref(
@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"
).id
)

@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
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()
)
# this function checks the value of id_number.name, not id_number
# directly.
id_number = namedtuple("id_number", ("name"))(national_number)
national_number_id_category.validate_id_number(id_number)

def validate_subscription_request(self):
self.ensure_one()
if self.require_national_number and not self.national_number:
raise UserError(_("National Number is required."))
if self.national_number:
self.check_be_national_register_number(self.national_number)
invoice = super().validate_subscription_request()
if not self.is_company:
partner = invoice.partner_id
Expand Down
4 changes: 4 additions & 0 deletions l10n_be_cooperator_national_number/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# SPDX-FileCopyrightText: 2023 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from . import test_cooperator_national_number
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright 2019 Coop IT Easy SCRL fs
# Robin Keunen <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
# SPDX-FileCopyrightText: 2023 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from unittest import mock

from odoo.exceptions import UserError, ValidationError
from odoo.tests.common import TransactionCase
Expand All @@ -9,6 +11,10 @@

NATIONAL_NUMBER = 90010100123

account_move_action_post = (
"odoo.addons.account.models.account_move.AccountMove.action_post"
)


class TestCooperatorNationalNumber(TransactionCase, CooperatorTestMixin):
@classmethod
Expand Down Expand Up @@ -73,6 +79,27 @@ def test_no_national_number_provided(self):
with self.assertRaises(UserError):
subscription_request.validate_subscription_request()

@mock.patch(account_move_action_post)
def test_invalid_national_number_provided(self, account_move_action_post_mock):
"""
Providing an invalid national number should raise a validation error.
"""
self.set_national_number_required()
vals = self.get_dummy_subscription_requests_vals()
subscription_request = self.env["subscription.request"].create(vals)
subscription_request.national_number = "42"
with self.assertRaises(ValidationError):
subscription_request.validate_subscription_request()
# no capital release requests should be created or posted
capital_release_requests = self.env["account.move"].search(
[("subscription_request", "=", subscription_request.id)]
)
self.assertFalse(capital_release_requests)
# mocking account.move.create() would be better, but if it is called,
# the error is confusing: psycopg2.ProgrammingError: can't adapt type
# 'MagicMock'
account_move_action_post_mock.assert_not_called()

def test_national_number_provided_not_required(self):
"""Expect no error when a number is given but not required."""
vals = self.get_dummy_subscription_requests_vals()
Expand Down

0 comments on commit b3213f1

Please sign in to comment.