Skip to content

Commit 9deb916

Browse files
committed
[16.0][IMP] contract: Added domain,constraint for pricelist_id
Using domain for field "pricelist_id" which lists pricelist whose currency is matched with journal_id currency. This domain helps to avoid using different currency within same contract. Similary, api constraint check added to check currency consistency between journal and pricelist. Updating pricelist_id when pricelist or journal currency is changed.
1 parent 69b97e0 commit 9deb916

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

contract/models/abstract_contract.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
# Copyright 2018 ACSONE SA/NV
77
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
88

9-
from odoo import api, fields, models
9+
from odoo import _, api, fields, models
10+
from odoo.exceptions import UserError
1011

1112

1213
class ContractAbstractContract(models.AbstractModel):
@@ -23,7 +24,15 @@ class ContractAbstractContract(models.AbstractModel):
2324
partner_id = fields.Many2one(
2425
comodel_name="res.partner", string="Partner", index=True
2526
)
26-
pricelist_id = fields.Many2one(comodel_name="product.pricelist", string="Pricelist")
27+
pricelist_id_domain = fields.Binary(
28+
compute="_compute_pricelist_id_domain", readonly=True, store=False
29+
)
30+
pricelist_id = fields.Many2one(
31+
comodel_name="product.pricelist",
32+
string="Pricelist",
33+
compute="_compute_pricelist_id",
34+
store=True,
35+
)
2736
contract_type = fields.Selection(
2837
selection=[("sale", "Customer"), ("purchase", "Supplier")],
2938
default="sale",
@@ -56,6 +65,17 @@ class ContractAbstractContract(models.AbstractModel):
5665
help="Choose the document that will be automatically generated by cron.",
5766
)
5867

68+
@api.constrains("pricelist_id")
69+
def check_currency(self):
70+
for rec in self:
71+
if rec.pricelist_id and (
72+
rec.journal_id.currency_id
73+
and rec.journal_id.currency_id != rec.pricelist_id.currency_id
74+
):
75+
raise UserError(
76+
_("Curreny should be same for Journal and Pricelist in a Contract")
77+
)
78+
5979
@api.model
6080
def _default_generation_type(self):
6181
return "invoice"
@@ -80,3 +100,27 @@ def _compute_journal_id(self):
80100
contract.journal_id = journal.id
81101
else:
82102
contract.journal_id = None
103+
104+
@api.depends("journal_id")
105+
def _compute_pricelist_id_domain(self):
106+
for rec in self:
107+
rec.pricelist_id_domain = (
108+
[("currency_id", "=", rec.journal_id.currency_id.id)]
109+
if rec.journal_id.currency_id
110+
else []
111+
)
112+
113+
@api.depends("journal_id.currency_id", "pricelist_id.currency_id")
114+
def _compute_pricelist_id(self):
115+
for rec in self:
116+
if rec.pricelist_id and (rec.journal_id.currency_id != rec.pricelist_id.currency_id):
117+
rec.pricelist_id = None
118+
119+
@api.onchange("journal_id")
120+
def _onchange_journal_id(self):
121+
self.ensure_one()
122+
if (
123+
self.journal_id.currency_id
124+
and self.journal_id.currency_id != self.pricelist_id.currency_id
125+
):
126+
self.pricelist_id = None

contract/tests/test_contract.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,6 +2366,35 @@ def test_recurrency_propagation(self):
23662366
for field in vals:
23672367
self.assertEqual(vals[field], contract2.contract_line_ids[field])
23682368

2369+
def test_pricelist_journal_currency_match(self):
2370+
currency_eur = self.env.ref("base.EUR")
2371+
currency_cad = self.env.ref("base.CAD")
2372+
journal = self.env["account.journal"].create(
2373+
{
2374+
"name": "Test journal CAD",
2375+
"code": "TCAD",
2376+
"type": "sale",
2377+
"currency_id": currency_cad.id,
2378+
}
2379+
)
2380+
pricelist = self.env["product.pricelist"].create(
2381+
{"name": "Test pricelist", "currency_id": currency_cad.id}
2382+
)
2383+
self.contract2.journal_id = journal.id
2384+
self.contract2.pricelist_id = pricelist.id
2385+
self.assertEqual(self.contract2.journal_id.currency_id, self.contract2.pricelist_id.currency_id)
2386+
# Check pricelist currency_id change
2387+
pricelist.currency_id = currency_eur.id
2388+
self.assertEqual(self.contract2.pricelist_id.ids, [])
2389+
self.assertEqual(self.contract2.currency_id, self.contract2.journal_id.currency_id)
2390+
# Check journal currency_id change
2391+
pricelist.currency_id = currency_cad.id
2392+
self.contract2.partner_id.property_product_pricelist = pricelist.id
2393+
self.contract2.contract_line_ids.automatic_price = True
2394+
journal.currency_id = currency_eur.id
2395+
self.assertEqual(self.contract2.pricelist_id.ids, [])
2396+
self.assertEqual(self.contract2.currency_id, currency_cad)
2397+
23692398
def test_currency(self):
23702399
currency_eur = self.env.ref("base.EUR")
23712400
currency_cad = self.env.ref("base.CAD")
@@ -2385,13 +2414,16 @@ def test_currency(self):
23852414
)
23862415
self.contract2.journal_id = journal.id
23872416
self.assertEqual(self.contract2.currency_id, currency_cad)
2388-
# Get currency from contract pricelist
2417+
# Currency should match for Journal and Pricelist
23892418
pricelist = self.env["product.pricelist"].create(
23902419
{"name": "Test pricelist", "currency_id": currency_eur.id}
23912420
)
2392-
self.contract2.pricelist_id = pricelist.id
2421+
error_msg = "Curreny should be same for Journal and Pricelist in a Contract"
2422+
with self.assertRaisesRegex(UserError, error_msg):
2423+
self.contract2.pricelist_id = pricelist.id
2424+
pricelist.currency_id = currency_cad
23932425
self.contract2.contract_line_ids.automatic_price = True
2394-
self.assertEqual(self.contract2.currency_id, currency_eur)
2426+
self.assertEqual(self.contract2.currency_id, currency_cad)
23952427
# Get currency from partner pricelist
23962428
self.contract2.pricelist_id = False
23972429
self.contract2.partner_id.property_product_pricelist = pricelist.id

contract/views/contract.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
<group name="main">
106106
<group>
107107
<field name="commercial_partner_id" invisible="1" />
108+
<field name="pricelist_id_domain" invisible="1" />
108109
<field
109110
name="partner_id"
110111
required="1"
@@ -113,6 +114,7 @@
113114
<field
114115
name="pricelist_id"
115116
attrs="{'readonly': [('is_terminated','=',True)]}"
117+
domain="pricelist_id_domain"
116118
/>
117119
<field
118120
name="payment_term_id"

0 commit comments

Comments
 (0)