diff --git a/product_contract/models/sale_order.py b/product_contract/models/sale_order.py index c5e9d416e..488fbefef 100644 --- a/product_contract/models/sale_order.py +++ b/product_contract/models/sale_order.py @@ -17,6 +17,18 @@ class SaleOrder(models.Model): compute='_compute_need_contract_creation' ) + @api.constrains('state') + def check_contact_is_not_resiliated(self): + for rec in self: + if rec.state not in ( + 'sale', + 'done', + 'cancel', + ) and rec.order_line.filtered('contract_id.is_resiliated'): + raise ValidationError( + _("You can't upsell or downsell a resiliated contract") + ) + @api.depends('order_line.contract_id', 'state') def _compute_need_contract_creation(self): for rec in self: diff --git a/product_contract/models/sale_order_line.py b/product_contract/models/sale_order_line.py index 26a30ed38..df4a5b5cb 100644 --- a/product_contract/models/sale_order_line.py +++ b/product_contract/models/sale_order_line.py @@ -50,6 +50,17 @@ class SaleOrderLine(models.Model): copy=False, ) + @api.constrains('contract_id') + def check_contact_is_not_resiliated(self): + for rec in self: + if ( + rec.order_id.state not in ('sale', 'done', 'cancel') + and rec.contract_id.is_resiliated + ): + raise ValidationError( + _("You can't upsell or downsell a resiliated contract") + ) + @api.multi @api.depends('product_id') def _compute_contract_template_id(self): diff --git a/product_contract/tests/test_sale_order.py b/product_contract/tests/test_sale_order.py index 0a938258b..d415d6838 100644 --- a/product_contract/tests/test_sale_order.py +++ b/product_contract/tests/test_sale_order.py @@ -342,3 +342,18 @@ class TestSaleOrder(TransactionCase): self.env['contract.contract'].search(action['domain']), self.sale.order_line.mapped('contract_id'), ) + + def test_check_contact_is_not_resiliated(self): + self.contract.is_resiliated = True + with self.assertRaises(ValidationError): + self.order_line1.contract_id = self.contract + + def test_check_contact_is_not_resiliated(self): + self.order_line1.contract_id = self.contract + self.sale.action_confirm() + self.contract.is_resiliated = True + self.sale.action_cancel() + with self.assertRaises(ValidationError): + self.sale.action_draft() + self.contract.is_resiliated = False + self.sale.action_draft() diff --git a/product_contract/views/sale_order.xml b/product_contract/views/sale_order.xml index 4fd1b90b9..2c462c993 100644 --- a/product_contract/views/sale_order.xml +++ b/product_contract/views/sale_order.xml @@ -41,6 +41,7 @@ domain="['|',('contract_template_id','=',contract_template_id), ('contract_template_id','=',False), ('partner_id','=',parent.partner_id), + ('is_resiliated','=',False), ]"/>