diff --git a/product_contract/models/sale_order.py b/product_contract/models/sale_order.py
index 70835795c..680505f0a 100644
--- a/product_contract/models/sale_order.py
+++ b/product_contract/models/sale_order.py
@@ -83,15 +83,8 @@ class SaleOrder(models.Model):
contract_model = self.env["contract.contract"]
contracts = []
for rec in self.filtered("is_contract"):
- line_to_create_contract = rec.order_line.filtered(
- lambda r: not r.contract_id and r.is_contract
- )
- line_to_update_contract = rec.order_line.filtered(
- lambda r: r.contract_id
- and r.is_contract
- and r
- not in r.contract_id.contract_line_ids.mapped("sale_order_line_id")
- )
+ line_to_create_contract = rec._get_line_to_create_contract()
+ line_to_update_contract = rec._get_line_to_update_contract()
contract_templates = self.env["contract.template"]
for order_line in line_to_create_contract:
contract_template = order_line.product_id.with_company(
diff --git a/product_contract/models/sale_order_line.py b/product_contract/models/sale_order_line.py
index 0a4a190d4..ed9e91347 100644
--- a/product_contract/models/sale_order_line.py
+++ b/product_contract/models/sale_order_line.py
@@ -141,8 +141,8 @@ class SaleOrderLine(models.Model):
rec.date_end = rec._get_date_end() if rec.date_start else False
@api.onchange("product_id")
- def onchange_product(self):
- super().onchange_product()
+ def product_id_change(self):
+ super().product_id_change()
for rec in self:
if rec.product_id.is_contract:
rec.is_contract = True
@@ -270,7 +270,7 @@ class SaleOrderLine(models.Model):
)
@api.constrains("product_id", "contract_id")
- def _check_contract_sale_contract_template(self):
+ def _check_contract_sale_line_is_contract(self):
for rec in self:
if rec.is_contract and not rec.product_id.is_contract:
raise ValidationError(
diff --git a/product_contract/tests/test_sale_order.py b/product_contract/tests/test_sale_order.py
index 804863f99..f7c31380c 100644
--- a/product_contract/tests/test_sale_order.py
+++ b/product_contract/tests/test_sale_order.py
@@ -65,8 +65,15 @@ class TestSaleOrder(SavepointCase):
cls.order_line1 = cls.sale.order_line.filtered(
lambda l: l.product_id == cls.product1
)
+
cls.order_line1.date_start = "2018-01-01"
cls.order_line1.product_uom_qty = 12
+ cls.order_line1.product_id_change()
+ cls.order_line2 = cls.sale.order_line.filtered(
+ lambda l: l.product_id == cls.product2
+ )
+ cls.order_line2.product_id_change()
+
pricelist = cls.sale.partner_id.property_product_pricelist.id
cls.contract = cls.env["contract.contract"].create(
{
@@ -194,7 +201,7 @@ class TestSaleOrder(SavepointCase):
{
"name": "Contract",
"contract_template_id": self.contract_template2.id,
- "partner_id": self.sale.partner_id.id,
+ "partner_id": self.env.user.partner_id.id,
"line_recurrence": True,
}
)
@@ -218,6 +225,7 @@ class TestSaleOrder(SavepointCase):
def test_no_contract_proudct(self):
"""it should create contract for only product contract"""
self.product1.is_contract = False
+ self.order_line1.product_id_change()
self.sale.action_confirm()
self.assertFalse(self.order_line1.contract_id)
@@ -241,6 +249,7 @@ class TestSaleOrder(SavepointCase):
def test_sale_order_create_invoice(self):
"""Should not invoice contract product on sale order create invoice"""
self.product2.is_contract = False
+ self.order_line2.product_id_change()
self.product2.invoice_policy = "order"
self.order_line1._compute_auto_renew()
self.sale.action_confirm()
diff --git a/product_contract/views/sale_order.xml b/product_contract/views/sale_order.xml
index a0ea683d0..2b08163fd 100644
--- a/product_contract/views/sale_order.xml
+++ b/product_contract/views/sale_order.xml
@@ -38,6 +38,7 @@
position="after"
>
+