[14.0][IMP] contract: Remove deprecated domain in onchange

This commit is contained in:
Denis Roussel
2022-01-24 14:35:15 +01:00
parent 87f8e2c6c7
commit e0b44cfa39
5 changed files with 51 additions and 39 deletions

View File

@@ -20,7 +20,12 @@ class ContractAbstractContractLine(models.AbstractModel):
name = fields.Text(string="Description", required=True) name = fields.Text(string="Description", required=True)
quantity = fields.Float(default=1.0, 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( automatic_price = fields.Boolean(
string="Auto-price?", string="Auto-price?",
help="If this is marked, the price will be obtained automatically " help="If this is marked, the price will be obtained automatically "
@@ -232,13 +237,7 @@ class ContractAbstractContractLine(models.AbstractModel):
@api.onchange("product_id") @api.onchange("product_id")
def _onchange_product_id(self): def _onchange_product_id(self):
if not self.product_id:
return {"domain": {"uom_id": []}}
vals = {} vals = {}
domain = {
"uom_id": [("category_id", "=", self.product_id.uom_id.category_id.id)]
}
if not self.uom_id or ( if not self.uom_id or (
self.product_id.uom_id.category_id.id != self.uom_id.category_id.id 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["name"] = self.product_id.get_product_multiline_description_sale()
vals["price_unit"] = product.price vals["price_unit"] = product.price
self.update(vals) self.update(vals)
return {"domain": domain}

View File

@@ -89,6 +89,7 @@ class ContractContract(models.Model):
string="Invoicing contact", string="Invoicing contact",
comodel_name="res.partner", comodel_name="res.partner",
ondelete="restrict", ondelete="restrict",
domain="['|', ('id', 'parent_of', partner_id), ('id', 'child_of', partner_id)]",
) )
partner_id = fields.Many2one( partner_id = fields.Many2one(
comodel_name="res.partner", inverse="_inverse_partner_id", required=True comodel_name="res.partner", inverse="_inverse_partner_id", required=True
@@ -371,15 +372,6 @@ class ContractContract(models.Model):
else: else:
self.payment_term_id = partner.property_payment_term_id self.payment_term_id = partner.property_payment_term_id
self.invoice_partner_id = self.partner_id.address_get(["invoice"])["invoice"] 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): def _convert_contract_lines(self, contract):
self.ensure_one() self.ensure_one()

View File

@@ -21,6 +21,8 @@ class TestContractBase(common.SavepointCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
super().setUpClass() super().setUpClass()
cls.uom_categ_obj = cls.env["uom.category"]
cls.uom_obj = cls.env["uom.uom"]
cls.today = fields.Date.today() cls.today = fields.Date.today()
cls.pricelist = cls.env["product.pricelist"].create( cls.pricelist = cls.env["product.pricelist"].create(
{"name": "pricelist for contract test"} {"name": "pricelist for contract test"}
@@ -32,6 +34,13 @@ class TestContractBase(common.SavepointCase):
"email": "demo@demo.com", "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_1 = cls.env.ref("product.product_product_1")
cls.product_2 = cls.env.ref("product.product_product_2") cls.product_2 = cls.env.ref("product.product_product_2")
cls.product_1.taxes_id += cls.env["account.tax"].search( 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): class TestContract(TestContractBase):
def _add_template_line(self, overrides=None): def _add_template_line(self, overrides=None):
@@ -252,8 +274,7 @@ class TestContract(TestContractBase):
def test_contract(self): def test_contract(self):
self.assertEqual(self.contract.recurring_next_date, to_date("2018-01-15")) self.assertEqual(self.contract.recurring_next_date, to_date("2018-01-15"))
self.assertAlmostEqual(self.acct_line.price_subtotal, 50.0) self.assertAlmostEqual(self.acct_line.price_subtotal, 50.0)
res = self.acct_line._onchange_product_id() self.acct_line._onchange_product_id()
self.assertIn("uom_id", res["domain"])
self.acct_line.price_unit = 100.0 self.acct_line.price_unit = 100.0
self.contract.partner_id = self.partner.id self.contract.partner_id = self.partner.id
self.contract.recurring_create_invoice() self.contract.recurring_create_invoice()
@@ -483,22 +504,36 @@ class TestContract(TestContractBase):
self.contract.partner_id.property_product_pricelist, 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): def test_uom(self):
uom_litre = self.env.ref("uom.product_uom_litre") uom_litre = self.env.ref("uom.product_uom_litre")
self.acct_line.uom_id = uom_litre.id self.acct_line.uom_id = uom_litre.id
self.acct_line._onchange_product_id() self.acct_line._onchange_product_id()
self.assertEqual(self.acct_line.uom_id, self.acct_line.product_id.uom_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): def test_no_pricelist(self):
self.contract.pricelist_id = False self.contract.pricelist_id = False
self.acct_line.quantity = 2 self.acct_line.quantity = 2
self.assertAlmostEqual(self.acct_line.price_subtotal, 100.0) 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): def test_check_journal(self):
journal = self.env["account.journal"].search([("type", "=", "sale")]) journal = self.env["account.journal"].search([("type", "=", "sale")])
journal.write({"type": "general"}) journal.write({"type": "general"})
@@ -581,21 +616,6 @@ class TestContract(TestContractBase):
self.contract._onchange_contract_type() self.contract._onchange_contract_type()
self.assertFalse(any(self.contract.contract_line_ids.mapped("automatic_price"))) 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): def test_contract_onchange_product_id_uom(self):
"""It should update the UoM for the line.""" """It should update the UoM for the line."""
line = self._add_template_line( line = self._add_template_line(

View File

@@ -24,6 +24,7 @@
<label for="quantity" /> <label for="quantity" />
<div class="o_row"> <div class="o_row">
<field name="quantity" class="oe_inline" /> <field name="quantity" class="oe_inline" />
<field name="allowed_uom_categ_id" invisible="1" />
<field <field
name="uom_id" name="uom_id"
class="oe_inline" class="oe_inline"

View File

@@ -213,6 +213,7 @@
groups="analytic.group_analytic_tags" groups="analytic.group_analytic_tags"
/> />
<field name="quantity" /> <field name="quantity" />
<field name="allowed_uom_categ_id" invisible="1" />
<field name="uom_id" /> <field name="uom_id" />
<field <field
name="automatic_price" name="automatic_price"