From e0b44cfa39b81ea827849659cd3f8f7c34d736d7 Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Mon, 24 Jan 2022 14:35:15 +0100 Subject: [PATCH] [14.0][IMP] contract: Remove deprecated domain in onchange --- contract/models/abstract_contract_line.py | 14 +++-- contract/models/contract.py | 10 +--- contract/tests/test_contract.py | 64 +++++++++++++++-------- contract/views/abstract_contract_line.xml | 1 + contract/views/contract.xml | 1 + 5 files changed, 51 insertions(+), 39 deletions(-) diff --git a/contract/models/abstract_contract_line.py b/contract/models/abstract_contract_line.py index 6ee8e229e..713320586 100644 --- a/contract/models/abstract_contract_line.py +++ b/contract/models/abstract_contract_line.py @@ -20,7 +20,12 @@ class ContractAbstractContractLine(models.AbstractModel): name = fields.Text(string="Description", required=True) quantity = fields.Float(default=1.0, required=True) - uom_id = fields.Many2one("uom.uom", string="Unit of Measure") + allowed_uom_categ_id = fields.Many2one(related="product_id.uom_id.category_id") + uom_id = fields.Many2one( + "uom.uom", + string="Unit of Measure", + domain="[('category_id', '=?', allowed_uom_categ_id)]", + ) automatic_price = fields.Boolean( string="Auto-price?", help="If this is marked, the price will be obtained automatically " @@ -232,13 +237,7 @@ class ContractAbstractContractLine(models.AbstractModel): @api.onchange("product_id") def _onchange_product_id(self): - if not self.product_id: - return {"domain": {"uom_id": []}} - vals = {} - domain = { - "uom_id": [("category_id", "=", self.product_id.uom_id.category_id.id)] - } if not self.uom_id or ( self.product_id.uom_id.category_id.id != self.uom_id.category_id.id ): @@ -257,4 +256,3 @@ class ContractAbstractContractLine(models.AbstractModel): vals["name"] = self.product_id.get_product_multiline_description_sale() vals["price_unit"] = product.price self.update(vals) - return {"domain": domain} diff --git a/contract/models/contract.py b/contract/models/contract.py index 807042cb2..3f0984629 100644 --- a/contract/models/contract.py +++ b/contract/models/contract.py @@ -89,6 +89,7 @@ class ContractContract(models.Model): string="Invoicing contact", comodel_name="res.partner", ondelete="restrict", + domain="['|', ('id', 'parent_of', partner_id), ('id', 'child_of', partner_id)]", ) partner_id = fields.Many2one( comodel_name="res.partner", inverse="_inverse_partner_id", required=True @@ -371,15 +372,6 @@ class ContractContract(models.Model): else: self.payment_term_id = partner.property_payment_term_id self.invoice_partner_id = self.partner_id.address_get(["invoice"])["invoice"] - return { - "domain": { - "invoice_partner_id": [ - "|", - ("id", "parent_of", self.partner_id.id), - ("id", "child_of", self.partner_id.id), - ] - } - } def _convert_contract_lines(self, contract): self.ensure_one() diff --git a/contract/tests/test_contract.py b/contract/tests/test_contract.py index dce80ea27..e356976fb 100644 --- a/contract/tests/test_contract.py +++ b/contract/tests/test_contract.py @@ -21,6 +21,8 @@ class TestContractBase(common.SavepointCase): @classmethod def setUpClass(cls): super().setUpClass() + cls.uom_categ_obj = cls.env["uom.category"] + cls.uom_obj = cls.env["uom.uom"] cls.today = fields.Date.today() cls.pricelist = cls.env["product.pricelist"].create( {"name": "pricelist for contract test"} @@ -32,6 +34,13 @@ class TestContractBase(common.SavepointCase): "email": "demo@demo.com", } ) + cls.partner_2 = cls.env["res.partner"].create( + { + "name": "partner test contract 2", + "property_product_pricelist": cls.pricelist.id, + "email": "demo2@demo.com", + } + ) cls.product_1 = cls.env.ref("product.product_product_1") cls.product_2 = cls.env.ref("product.product_product_2") cls.product_1.taxes_id += cls.env["account.tax"].search( @@ -168,6 +177,19 @@ class TestContractBase(common.SavepointCase): } ) + @classmethod + def _create_uom(cls): + vals = { + "name": "New Uom Categ", + } + categ = cls.uom_categ_obj.create(vals) + vals = { + "name": "New Uom", + "category_id": categ.id, + "factor": 1.0, + } + return cls.uom_obj.create(vals) + class TestContract(TestContractBase): def _add_template_line(self, overrides=None): @@ -252,8 +274,7 @@ class TestContract(TestContractBase): def test_contract(self): self.assertEqual(self.contract.recurring_next_date, to_date("2018-01-15")) self.assertAlmostEqual(self.acct_line.price_subtotal, 50.0) - res = self.acct_line._onchange_product_id() - self.assertIn("uom_id", res["domain"]) + self.acct_line._onchange_product_id() self.acct_line.price_unit = 100.0 self.contract.partner_id = self.partner.id self.contract.recurring_create_invoice() @@ -483,22 +504,36 @@ class TestContract(TestContractBase): self.contract.partner_id.property_product_pricelist, ) + def test_invoice_partner_id_domain(self): + contract_form = self.contract.fields_view_get(False, "form") + invoice_partner_id_field = contract_form["fields"].get("invoice_partner_id") + self.assertEqual( + self.contract._fields["invoice_partner_id"].domain, + invoice_partner_id_field.get("domain"), + ) + def test_uom(self): uom_litre = self.env.ref("uom.product_uom_litre") self.acct_line.uom_id = uom_litre.id self.acct_line._onchange_product_id() self.assertEqual(self.acct_line.uom_id, self.acct_line.product_id.uom_id) - def test_onchange_product_id(self): - line = self.env["contract.line"].new() - res = line._onchange_product_id() - self.assertFalse(res["domain"]["uom_id"]) - def test_no_pricelist(self): self.contract.pricelist_id = False self.acct_line.quantity = 2 self.assertAlmostEqual(self.acct_line.price_subtotal, 100.0) + def test_contract_uom_domain(self): + """Create a new uom. Try to set it on contract line. + The one set should not be that one""" + contract_form = self.contract.fields_view_get(False, "form") + contract_line_ids_field = contract_form["fields"].get("contract_line_ids") + uom_id_field = contract_line_ids_field["views"]["tree"]["fields"].get("uom_id") + self.assertEqual( + self.contract.contract_line_ids._fields["uom_id"].domain, + uom_id_field.get("domain"), + ) + def test_check_journal(self): journal = self.env["account.journal"].search([("type", "=", "sale")]) journal.write({"type": "general"}) @@ -581,21 +616,6 @@ class TestContract(TestContractBase): self.contract._onchange_contract_type() self.assertFalse(any(self.contract.contract_line_ids.mapped("automatic_price"))) - def test_contract_onchange_product_id_domain_blank(self): - """It should return a blank UoM domain when no product.""" - line = self.env["contract.template.line"].new() - res = line._onchange_product_id() - self.assertFalse(res["domain"]["uom_id"]) - - def test_contract_onchange_product_id_domain(self): - """It should return UoM category domain.""" - line = self._add_template_line() - res = line._onchange_product_id() - self.assertEqual( - res["domain"]["uom_id"][0], - ("category_id", "=", self.product_1.uom_id.category_id.id), - ) - def test_contract_onchange_product_id_uom(self): """It should update the UoM for the line.""" line = self._add_template_line( diff --git a/contract/views/abstract_contract_line.xml b/contract/views/abstract_contract_line.xml index 140ffb3fb..bc3ca6aee 100644 --- a/contract/views/abstract_contract_line.xml +++ b/contract/views/abstract_contract_line.xml @@ -24,6 +24,7 @@