From e8c4fa380283197ede73bdac8202882cfd6eeb88 Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Tue, 2 Aug 2022 17:08:45 +0200 Subject: [PATCH] [IMP] new field only_nominative on account.wallet.type model to prevent wallet without partner defined --- account_wallet/models/account_wallet.py | 13 ++++++++++ account_wallet/models/account_wallet_type.py | 27 +++++++++++++++++++- account_wallet/tests/test_wallet.py | 12 +++++++++ account_wallet/views/account_wallet.xml | 6 ++++- account_wallet/views/account_wallet_type.xml | 1 + 5 files changed, 57 insertions(+), 2 deletions(-) diff --git a/account_wallet/models/account_wallet.py b/account_wallet/models/account_wallet.py index f713a05..457fbc5 100644 --- a/account_wallet/models/account_wallet.py +++ b/account_wallet/models/account_wallet.py @@ -19,6 +19,7 @@ class AccountWallet(models.Model): wallet_type_id = fields.Many2one( "account.wallet.type", "Wallet Type", required=True, ondelete="restrict" ) + only_nominative = fields.Boolean(related="wallet_type_id.only_nominative") partner_id = fields.Many2one( comodel_name="res.partner", string="Partner", @@ -64,6 +65,18 @@ def _check_partner(self): ) return True + @api.constrains("partner_id", "wallet_type_id") + def _check_only_nominative(self): + wallets = self.filtered(lambda x: not x.partner_id and x.only_nominative) + if wallets: + raise ValidationError( + _( + "You have to set a partner on the wallets %s" + "because the wallet type allow only nominative wallets." + % ",".join(wallets.mapped("name")) + ) + ) + def _get_name(self): """ Get a composed display name from different properties diff --git a/account_wallet/models/account_wallet_type.py b/account_wallet/models/account_wallet_type.py index 28033e4..43b6e52 100644 --- a/account_wallet/models/account_wallet_type.py +++ b/account_wallet/models/account_wallet_type.py @@ -1,7 +1,8 @@ # © 2015 Laetitia Gangloff, Acsone SA/NV (http://www.acsone.eu) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import api, fields, models +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError class AccountWalletType(models.Model): @@ -53,6 +54,12 @@ class AccountWalletType(models.Model): " (related to a partner) when selling products related to a wallet type." ) + only_nominative = fields.Boolean( + help="Check this box if you want to ensure all the wallets have" + " a partner defined. Note that enable this feature will prevent to use" + " wallet for 'Gifts' (Customer pay a wallet to make a gift to another customer)" + ) + _sql_constraints = [ ( "product_wallet_type_uniq", @@ -70,3 +77,21 @@ class AccountWalletType(models.Model): def onchange_product_id(self): if self.product_id and not self.account_id: self.account_id = self.product_id._get_product_accounts()["income"] + + @api.onchange("only_nominative") + def onchange_only_nominative(self): + if self.only_nominative: + self.automatic_nominative_creation = True + + @api.constrains("only_nominative", "automatic_nominative_creation") + def _check_only_nominative_automatic_nominative_creation(self): + types = self.filtered( + lambda x: x.only_nominative and not x.automatic_nominative_creation + ) + if types: + raise ValidationError( + _( + "You have to check 'Automatic Nominative Creation'" + "if 'Only Nominative' is checked." + ) + ) diff --git a/account_wallet/tests/test_wallet.py b/account_wallet/tests/test_wallet.py index 45d1eac..4ec94b5 100644 --- a/account_wallet/tests/test_wallet.py +++ b/account_wallet/tests/test_wallet.py @@ -2,6 +2,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from psycopg2 import IntegrityError +from odoo.exceptions import ValidationError from odoo.tools import mute_logger from .common import WalletCommon @@ -73,6 +74,17 @@ def test_wallet_unique(self): self.wallet.write({"active": False}) + def test_wallet_partner_required(self): + self.wallet.partner_id = self.partner + self.wallet_type.write( + { + "only_nominative": True, + "automatic_nominative_creation": True, + } + ) + with self.assertRaises(ValidationError): + self.wallet.partner_id = False + def test_wallet_credit_note(self): partner = self.env["res.partner"].create({"name": "Test Wallet credit_notes"}) product = self.env["product.product"].search([], limit=1) diff --git a/account_wallet/views/account_wallet.xml b/account_wallet/views/account_wallet.xml index 9e4ca1e..e22e8e2 100644 --- a/account_wallet/views/account_wallet.xml +++ b/account_wallet/views/account_wallet.xml @@ -85,8 +85,12 @@ - + + +