From 93440ccbce4004b5c7458be62772bd9aae2c9594 Mon Sep 17 00:00:00 2001 From: sbejaoui Date: Wed, 31 Oct 2018 16:23:34 +0100 Subject: [PATCH] [IMP] - Product with is_contract can be only of type service --- product_contract/models/product_template.py | 15 ++++++-- product_contract/tests/__init__.py | 2 +- product_contract/tests/test_product.py | 34 +++++++++++++++++++ .../tests/test_product_template.py | 31 ----------------- product_contract/tests/test_sale_order.py | 2 -- 5 files changed, 47 insertions(+), 37 deletions(-) create mode 100644 product_contract/tests/test_product.py delete mode 100644 product_contract/tests/test_product_template.py diff --git a/product_contract/models/product_template.py b/product_contract/models/product_template.py index fd7e00d4..1016c60c 100644 --- a/product_contract/models/product_template.py +++ b/product_contract/models/product_template.py @@ -1,8 +1,10 @@ # -*- coding: utf-8 -*- # Copyright 2017 LasLabs Inc. +# Copyright 2018 ACSONE SA/NV. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import api, fields, models +from odoo import api, fields, models, _ +from odoo.exceptions import ValidationError class ProductTemplate(models.Model): @@ -10,8 +12,7 @@ class ProductTemplate(models.Model): is_contract = fields.Boolean('Is a contract') contract_template_id = fields.Many2one( - comodel_name='account.analytic.contract', - string='Contract Template', + comodel_name='account.analytic.contract', string='Contract Template' ) @api.onchange('is_contract') @@ -21,3 +22,11 @@ class ProductTemplate(models.Model): """ if not self.is_contract: self.contract_template_id = False + + @api.constrains('is_contract', 'type') + def _check_contract_product_type(self): + """ + Contract product should be service type + """ + if self.is_contract and self.type != 'service': + raise ValidationError(_("Contract product should be service type")) diff --git a/product_contract/tests/__init__.py b/product_contract/tests/__init__.py index e5fbe249..b4af49c0 100644 --- a/product_contract/tests/__init__.py +++ b/product_contract/tests/__init__.py @@ -2,5 +2,5 @@ # Copyright 2017 LasLabs Inc. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from . import test_product_template +from . import test_product from . import test_sale_order diff --git a/product_contract/tests/test_product.py b/product_contract/tests/test_product.py new file mode 100644 index 00000000..3676dc05 --- /dev/null +++ b/product_contract/tests/test_product.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 LasLabs Inc. +# Copyright 2018 ACSONE SA/NV. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo.tests.common import TransactionCase +from odoo.exceptions import ValidationError + + +class TestProductTemplate(TransactionCase): + def setUp(self): + super(TestProductTemplate, self).setUp() + self.service_product = self.env.ref('product.product_product_1') + self.consu_product = self.env.ref('product.product_product_5') + self.contract = self.env['account.analytic.contract'].create( + {'name': 'Test'} + ) + + def test_change_is_contract(self): + """ It should verify that the contract_template_id is removed + when is_contract is False """ + self.service_product.is_contract = True + self.service_product.contract_template_id = self.contract.id + self.service_product.is_contract = False + self.service_product.product_tmpl_id._change_is_contract() + self.assertEquals(len(self.service_product.contract_template_id), 0) + + def test_check_contract_product_type(self): + """ + It should raise ValidationError on change of is_contract to True + for consu product + """ + with self.assertRaises(ValidationError): + self.consu_product.is_contract = True diff --git a/product_contract/tests/test_product_template.py b/product_contract/tests/test_product_template.py deleted file mode 100644 index 2938cc7f..00000000 --- a/product_contract/tests/test_product_template.py +++ /dev/null @@ -1,31 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2017 LasLabs Inc. -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - -from odoo.tests.common import TransactionCase - - -class TestProductTemplate(TransactionCase): - - def setUp(self): - super(TestProductTemplate, self).setUp() - self.product = self.env.ref( - 'product.product_product_4_product_template' - ) - self.contract = self.env['account.analytic.contract'].create({ - 'name': 'Test', - 'recurring_rule_type': 'yearly', - 'recurring_interval': 12345, - }) - - def test_change_is_contract(self): - """ It should verify that the contract_template_id is removed - when is_contract is False """ - self.product.is_contract = True - self.product.contract_template_id = self.contract.id - self.product.is_contract = False - self.product._change_is_contract() - self.assertEquals( - len(self.product.contract_template_id), - 0 - ) diff --git a/product_contract/tests/test_sale_order.py b/product_contract/tests/test_sale_order.py index 61f33858..f8a90e34 100644 --- a/product_contract/tests/test_sale_order.py +++ b/product_contract/tests/test_sale_order.py @@ -14,8 +14,6 @@ class TestSaleOrder(TransactionCase): self.sale = self.env.ref('sale.sale_order_2') self.contract = self.env['account.analytic.contract'].create({ 'name': 'Test', - 'recurring_rule_type': 'yearly', - 'recurring_interval': 12345, }) self.product.product_tmpl_id.is_contract = True self.product.product_tmpl_id.contract_template_id = self.contract.id