Browse Source

[IMP] - Product with is_contract can be only of type service

pull/208/head
sbejaoui 6 years ago
committed by Thomas Binsfeld
parent
commit
93440ccbce
  1. 15
      product_contract/models/product_template.py
  2. 2
      product_contract/tests/__init__.py
  3. 34
      product_contract/tests/test_product.py
  4. 31
      product_contract/tests/test_product_template.py
  5. 2
      product_contract/tests/test_sale_order.py

15
product_contract/models/product_template.py

@ -1,8 +1,10 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc. # Copyright 2017 LasLabs Inc.
# Copyright 2018 ACSONE SA/NV.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # 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): class ProductTemplate(models.Model):
@ -10,8 +12,7 @@ class ProductTemplate(models.Model):
is_contract = fields.Boolean('Is a contract') is_contract = fields.Boolean('Is a contract')
contract_template_id = fields.Many2one( 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') @api.onchange('is_contract')
@ -21,3 +22,11 @@ class ProductTemplate(models.Model):
""" """
if not self.is_contract: if not self.is_contract:
self.contract_template_id = False 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"))

2
product_contract/tests/__init__.py

@ -2,5 +2,5 @@
# Copyright 2017 LasLabs Inc. # Copyright 2017 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # 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 from . import test_sale_order

34
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

31
product_contract/tests/test_product_template.py

@ -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
)

2
product_contract/tests/test_sale_order.py

@ -14,8 +14,6 @@ class TestSaleOrder(TransactionCase):
self.sale = self.env.ref('sale.sale_order_2') self.sale = self.env.ref('sale.sale_order_2')
self.contract = self.env['account.analytic.contract'].create({ self.contract = self.env['account.analytic.contract'].create({
'name': 'Test', 'name': 'Test',
'recurring_rule_type': 'yearly',
'recurring_interval': 12345,
}) })
self.product.product_tmpl_id.is_contract = True self.product.product_tmpl_id.is_contract = True
self.product.product_tmpl_id.contract_template_id = self.contract.id self.product.product_tmpl_id.contract_template_id = self.contract.id

Loading…
Cancel
Save