diff --git a/CHANGELOG b/CHANGELOG index bf64b8c564..49585375f3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Prevent editing list and cost prices without a company * Make list price not required on product * Provide digits from unit of measure * Allow only fixed cost price as method for service diff --git a/product.py b/product.py index 78431572dc..8df2fe74b9 100644 --- a/product.py +++ b/product.py @@ -75,6 +75,9 @@ class Template( "regardless of stock level.") list_price = fields.MultiValue(fields.Numeric( "List Price", digits=price_digits, + states={ + 'readonly': ~Eval('context', {}).get('company'), + }, help="The standard price the product is sold at.")) list_prices = fields.One2Many( 'product.list_price', 'template', "List Prices") @@ -360,7 +363,10 @@ class Product( 'product.identifier', 'product', "Identifiers", help="Other identifiers associated with the variant.") cost_price = fields.MultiValue(fields.Numeric( - "Cost Price", required=True, digits=price_digits, + "Cost Price", digits=price_digits, + states={ + 'readonly': ~Eval('context', {}).get('company'), + }, help="The amount it costs to purchase or make the variant, " "or carry out the service.")) cost_prices = fields.One2Many( @@ -478,9 +484,18 @@ class Product( return pool.get('product.cost_price') return super(Product, cls).multivalue_model(field) + def set_multivalue(self, name, value, save=True, **pattern): + context = Transaction().context + if name in {'cost_price', 'list_price'} and not value: + if not pattern.get('company', context.get('company')): + return [] + return super().set_multivalue(name, value, save=save, **pattern) + @classmethod def default_cost_price(cls, **pattern): - return Decimal(0) + context = Transaction().context + if pattern.get('company', context.get('company')): + return Decimal(0) @classmethod def search_template(cls, name, clause): @@ -622,6 +637,11 @@ class ProductListPrice(ModelSQL, CompanyValueMixin): depends=['company']) list_price = fields.Numeric("List Price", digits=price_digits) + @classmethod + def __setup__(cls): + super().__setup__() + cls.company.required = True + @classmethod def __register__(cls, module_name): exist = backend.TableHandler.table_exist(cls._table) @@ -713,6 +733,11 @@ class ProductCostPrice(ModelSQL, CompanyValueMixin): cost_price = fields.Numeric( "Cost Price", required=True, digits=price_digits) + @classmethod + def __setup__(cls): + super().__setup__() + cls.company.required = True + @classmethod def __register__(cls, module_name): pool = Pool() diff --git a/tests/scenario_product_identifier.rst b/tests/scenario_product_identifier.rst index 72b74af571..53baf490da 100644 --- a/tests/scenario_product_identifier.rst +++ b/tests/scenario_product_identifier.rst @@ -21,7 +21,6 @@ Create product:: >>> template.name = 'product' >>> template.default_uom = unit >>> template.type = 'service' - >>> template.list_price = Decimal('40') >>> template.save() >>> product, = template.products diff --git a/tests/scenario_product_variant.rst b/tests/scenario_product_variant.rst index a2dc7ae3e6..91ea8cf72a 100644 --- a/tests/scenario_product_variant.rst +++ b/tests/scenario_product_variant.rst @@ -7,11 +7,16 @@ Imports:: >>> from decimal import Decimal >>> from proteus import Model >>> from trytond.tests.tools import activate_modules + >>> from trytond.modules.company.tests.tools import create_company Activate modules:: >>> config = activate_modules('product') +Create company:: + + >>> _ = create_company() + Create a template:: >>> ProductUom = Model.get('product.uom') diff --git a/tests/test_product.py b/tests/test_product.py index 1a163ba109..3ee024fe43 100644 --- a/tests/test_product.py +++ b/tests/test_product.py @@ -279,7 +279,6 @@ class ProductTestCase(CompanyTestMixin, ModuleTestCase): pt1, pt2 = Template.create([{ 'name': 'P1', 'type': 'goods', - 'list_price': Decimal(20), 'default_uom': kilogram.id, 'products': [('create', [{ 'code': '1', @@ -287,7 +286,6 @@ class ProductTestCase(CompanyTestMixin, ModuleTestCase): }, { 'name': 'P2', 'type': 'goods', - 'list_price': Decimal(20), 'default_uom': millimeter.id, 'products': [('create', [{ 'code': '2', @@ -318,7 +316,6 @@ class ProductTestCase(CompanyTestMixin, ModuleTestCase): 'name': 'Some product-1', 'categories': [('add', [category1.id])], 'type': 'goods', - 'list_price': Decimal('10'), 'default_uom': uom.id, 'products': [('create', [{}])], } @@ -326,7 +323,6 @@ class ProductTestCase(CompanyTestMixin, ModuleTestCase): 'name': 'Some product-2', 'categories': [('add', [category2.id])], 'type': 'goods', - 'list_price': Decimal('10'), 'default_uom': uom.id, 'products': [('create', [{}])], } @@ -397,14 +393,12 @@ class ProductTestCase(CompanyTestMixin, ModuleTestCase): values1 = { 'name': 'Product A', 'type': 'assets', - 'list_price': Decimal('10'), 'default_uom': uom.id, 'products': [('create', [{'suffix_code': 'AA'}])], } values2 = { 'name': 'Product B', 'type': 'goods', - 'list_price': Decimal('10'), 'default_uom': uom.id, 'products': [('create', [{'suffix_code': 'BB'}])], }