Skip to content

Commit

Permalink
[IMP] new field only_nominative on account.wallet.type model to preve…
Browse files Browse the repository at this point in the history
…nt wallet without partner defined
  • Loading branch information
legalsylvain committed Aug 2, 2022
1 parent 1c69e03 commit e8c4fa3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
13 changes: 13 additions & 0 deletions account_wallet/models/account_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
27 changes: 26 additions & 1 deletion account_wallet/models/account_wallet_type.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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",
Expand All @@ -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."
)
)
12 changes: 12 additions & 0 deletions account_wallet/tests/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion account_wallet/views/account_wallet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@
</h1>
</div>
<group>
<field name="partner_id" />
<field name="wallet_type_id" />
<field name="only_nominative" invisible="1" />
<field
name="partner_id"
attrs="{'required': [('only_nominative', '=', True)]}"
/>
<field name="company_currency_id" invisible="1" />
<field
name="balance"
Expand Down
1 change: 1 addition & 0 deletions account_wallet/views/account_wallet_type.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
name="company_id"
groups="base.group_multi_company"
/>
<field name="only_nominative" />
<field name="automatic_nominative_creation" />
</group>
</group>
Expand Down

0 comments on commit e8c4fa3

Please sign in to comment.