diff --git a/contract/models/abstract_contract.py b/contract/models/abstract_contract.py index 70210cec12..48a5f96d3d 100644 --- a/contract/models/abstract_contract.py +++ b/contract/models/abstract_contract.py @@ -6,7 +6,8 @@ # Copyright 2018 ACSONE SA/NV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import api, fields, models +from odoo import _, api, fields, models +from odoo.exceptions import UserError class ContractAbstractContract(models.AbstractModel): @@ -23,7 +24,13 @@ class ContractAbstractContract(models.AbstractModel): partner_id = fields.Many2one( comodel_name="res.partner", string="Partner", index=True ) - pricelist_id = fields.Many2one(comodel_name="product.pricelist", string="Pricelist") + pricelist_id_domain = fields.Binary( + compute="_compute_pricelist_id_domain", readonly=True, store=False + ) + pricelist_id = fields.Many2one( + comodel_name="product.pricelist", + string="Pricelist", + ) contract_type = fields.Selection( selection=[("sale", "Customer"), ("purchase", "Supplier")], default="sale", @@ -56,6 +63,17 @@ class ContractAbstractContract(models.AbstractModel): help="Choose the document that will be automatically generated by cron.", ) + @api.constrains("pricelist_id") + def check_currency(self): + for rec in self: + if rec.pricelist_id and ( + rec.journal_id.currency_id + and rec.journal_id.currency_id != rec.pricelist_id.currency_id + ): + raise UserError( + _("Curreny should be same for Journal and Pricelist in a Contract") + ) + @api.model def _default_generation_type(self): return "invoice" @@ -80,3 +98,21 @@ def _compute_journal_id(self): contract.journal_id = journal.id else: contract.journal_id = None + + @api.depends("journal_id") + def _compute_pricelist_id_domain(self): + for rec in self: + rec.pricelist_id_domain = ( + [("currency_id", "=", rec.journal_id.currency_id.id)] + if rec.journal_id.currency_id + else [] + ) + + @api.onchange("journal_id") + def _onchange_journal_id(self): + self.ensure_one() + if ( + self.journal_id.currency_id + and self.journal_id.currency_id != self.pricelist_id.currency_id + ): + self.pricelist_id = None diff --git a/contract/tests/test_contract.py b/contract/tests/test_contract.py index 51dfddaa49..da4a32f1bf 100644 --- a/contract/tests/test_contract.py +++ b/contract/tests/test_contract.py @@ -2385,13 +2385,16 @@ def test_currency(self): ) self.contract2.journal_id = journal.id self.assertEqual(self.contract2.currency_id, currency_cad) - # Get currency from contract pricelist + # Currency should match for Journal and Pricelist pricelist = self.env["product.pricelist"].create( {"name": "Test pricelist", "currency_id": currency_eur.id} ) - self.contract2.pricelist_id = pricelist.id + error_msg = "Curreny should be same for Journal and Pricelist in a Contract" + with self.assertRaisesRegex(UserError, error_msg): + self.contract2.pricelist_id = pricelist.id + pricelist.currency_id = currency_cad self.contract2.contract_line_ids.automatic_price = True - self.assertEqual(self.contract2.currency_id, currency_eur) + self.assertEqual(self.contract2.currency_id, currency_cad) # Get currency from partner pricelist self.contract2.pricelist_id = False self.contract2.partner_id.property_product_pricelist = pricelist.id diff --git a/contract/views/contract.xml b/contract/views/contract.xml index c9d894094c..036b018462 100644 --- a/contract/views/contract.xml +++ b/contract/views/contract.xml @@ -105,6 +105,7 @@ +